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 https://github.com/microsoft/vscode/issues/154048 #168597

Merged
merged 1 commit into from Dec 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/vs/base/common/uri.ts
Expand Up @@ -497,7 +497,7 @@ const encodeTable: { [ch: number]: string } = {
[CharCode.Space]: '%20',
};

function encodeURIComponentFast(uriComponent: string, allowSlash: boolean): string {
function encodeURIComponentFast(uriComponent: string, isPath: boolean, isAuthority: boolean): string {
let res: string | undefined = undefined;
let nativeEncodePos = -1;

Expand All @@ -513,7 +513,10 @@ function encodeURIComponentFast(uriComponent: string, allowSlash: boolean): stri
|| code === CharCode.Period
|| code === CharCode.Underline
|| code === CharCode.Tilde
|| (allowSlash && code === CharCode.Slash)
|| (isPath && code === CharCode.Slash)
|| (isAuthority && code === CharCode.OpenSquareBracket)
|| (isAuthority && code === CharCode.CloseSquareBracket)
|| (isAuthority && code === CharCode.Colon)
) {
// check if we are delaying native encode
if (nativeEncodePos !== -1) {
Expand Down Expand Up @@ -631,24 +634,24 @@ function _asFormatted(uri: URI, skipEncoding: boolean): string {
// <user>@<auth>
const userinfo = authority.substr(0, idx);
authority = authority.substr(idx + 1);
idx = userinfo.indexOf(':');
idx = userinfo.lastIndexOf(':');
if (idx === -1) {
res += encoder(userinfo, false);
res += encoder(userinfo, false, false);
} else {
// <user>:<pass>@<auth>
res += encoder(userinfo.substr(0, idx), false);
res += encoder(userinfo.substr(0, idx), false, false);
res += ':';
res += encoder(userinfo.substr(idx + 1), false);
res += encoder(userinfo.substr(idx + 1), false, true);
}
res += '@';
}
authority = authority.toLowerCase();
idx = authority.indexOf(':');
idx = authority.lastIndexOf(':');
if (idx === -1) {
res += encoder(authority, false);
res += encoder(authority, false, true);
} else {
// <auth>:<port>
res += encoder(authority.substr(0, idx), false);
res += encoder(authority.substr(0, idx), false, true);
res += authority.substr(idx);
}
}
Expand All @@ -666,15 +669,15 @@ function _asFormatted(uri: URI, skipEncoding: boolean): string {
}
}
// encode the rest of the path
res += encoder(path, true);
res += encoder(path, true, false);
}
if (query) {
res += '?';
res += encoder(query, false);
res += encoder(query, false, false);
}
if (fragment) {
res += '#';
res += !skipEncoding ? encodeURIComponentFast(fragment, false) : fragment;
res += !skipEncoding ? encodeURIComponentFast(fragment, false, false) : fragment;
}
return res;
}
Expand Down
9 changes: 8 additions & 1 deletion src/vs/base/test/common/uri.test.ts
Expand Up @@ -469,7 +469,7 @@ suite('URI', () => {
}), true);
});

test('Unable to open \'%A0.txt\': URI malformed #76506', function () {
test('Unable to open \'%A0.txt\': URI malformed #76506, part 2', function () {
assert.strictEqual(URI.parse('file://some/%.txt').toString(), 'file://some/%25.txt');
assert.strictEqual(URI.parse('file://some/%A0.txt').toString(), 'file://some/%25A0.txt');
});
Expand Down Expand Up @@ -595,4 +595,11 @@ suite('URI', () => {
//https://github.com/microsoft/vscode/issues/93831
assertJoined('file:///c:/foo/bar', './other/foo.img', 'file:///c:/foo/bar/other/foo.img', false);
});

test('vscode-uri: URI.toString() wrongly encode IPv6 literals #154048', function () {
assert.strictEqual(URI.parse('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html').toString(), 'http://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80/index.html');

assert.strictEqual(URI.parse('http://user@[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html').toString(), 'http://user@[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80/index.html');
assert.strictEqual(URI.parse('http://us[er@[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html').toString(), 'http://us%5Ber@[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80/index.html');
});
});