Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize() double encode special characters #3271

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/apidom-reference/src/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,12 @@ export const sanitize = (uri: string) => {
return fromFileSystemPath(toFileSystemPath(uri));
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_ipv6
return encodeURI(decodeURI(uri)).replace(/%5B/g, '[').replace(/%5D/g, ']');
try {
return new URL(uri).toString();
} catch {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_ipv6
return encodeURI(decodeURI(uri)).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
};

/**
Expand Down
19 changes: 16 additions & 3 deletions packages/apidom-reference/test/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,28 @@ describe('util', function () {
});
});

context('given percent encoded URL', function () {
specify('should not double percent encode the URL', function () {
const url = 'https://example.com/path%20with%20spaces/';
context('given ipv6 URL', function () {
specify('should return valid ipV6 URL', function () {
const url = 'http://[2001:db8::1]:81/path/file.html?q=wery';
const sanitized = sanitize(url);

assert.strictEqual(sanitized, url);
});
});

context('given percent encoded URL', function () {
specify(
'should not double percent encode the URL including special characters ; / ? : @ & = + $ , #',
function () {
const url =
'https://example.com/path%20with%20spaces%2Fslashes%3Bsemicolons/?including=in%3Fparameters';
const sanitized = sanitize(url);

assert.strictEqual(sanitized, url);
},
);
});

context('given percent decoded file system path', function () {
specify('should sanitize the file system path', function () {
const url = '/home/User/path with spaces';
Expand Down
Loading