Skip to content

Zero-pad percent-encoding for code points below 0x10 in UriTemplate - #323012

Merged
Martin Aeschlimann (aeschli) merged 2 commits into
microsoft:mainfrom
Osamaali313:fix/uritemplate-pctencode-padding
Jul 3, 2026
Merged

Martin Aeschlimann (aeschli) merged 2 commits into
microsoft:mainfrom
Osamaali313:fix/uritemplate-pctencode-padding

Conversation

@Osamaali313

Copy link
Copy Markdown
Contributor

Problem

pctEncode in src/vs/base/common/uriTemplate.ts builds escapes without zero-padding:

out += '%' + chr.toString(16).toUpperCase();

For any code point < 0x10, toString(16) yields a single hex digit, so the function emits a one-digit escape instead of the RFC 3986 / RFC 6570 mandated two-digit form:

input char code before expected
tab 0x09 %9 %09
newline 0x0A %A %0A
carriage return 0x0D %D %0D
bell 0x07 %7 %07

The result is a malformed URI; decodeURIComponent rejects it with URI malformed. UriTemplate resolves MCP and agent resource URIs (e.g. src/vs/workbench/contrib/mcp/, src/vs/platform/agentHost/), so a variable value containing a control character produces a broken URL.

Reproduction

Running the real UriTemplate (transpiled):

UriTemplate.parse('{x}').resolve({ x: 'a\tb' })  ->  'a%9b'   (expected 'a%09b')
decodeURIComponent('a%9b')                        ->  throws URI malformed

Fix

Zero-pad to two hex digits:

out += '%' + chr.toString(16).toUpperCase().padStart(2, '0');

padStart is a no-op for code points >= 0x10, so existing escapes (%20, %21, %3B, …) are unchanged — verified against the existing test expectations.

Test

Adds a regression test in src/vs/base/test/common/uriTemplate.test.ts asserting {x} with 'a\tb', '\n', '\r' resolve to a%09b, %0A, %0D.

pctEncode emitted '%' + chr.toString(16) without padding, so a code point < 0x10 produced a single-digit escape (e.g. tab -> %9, newline -> %0A becomes %A, CR -> %D) instead of the RFC 3986/6570 two-digit form. The result is a malformed URI that decodeURIComponent rejects with "URI malformed". UriTemplate is used to resolve MCP and agent resource URIs, where a variable value containing a control character would yield a broken URL.

Zero-pad to two hex digits with padStart(2, '0'); this is a no-op for code points >= 0x10, so existing escapes (%20, %21, ...) are unchanged. Adds a regression test.
Copilot AI review requested due to automatic review settings June 25, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@aeschli
Martin Aeschlimann (aeschli) enabled auto-merge (squash) July 2, 2026 08:41
@aeschli
Martin Aeschlimann (aeschli) merged commit 2ba2224 into microsoft:main Jul 3, 2026
29 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.128.0 milestone Jul 3, 2026
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Aug 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants