From fa8ea7bd892a1d72e31fc381f23be1b5ebf4a9c1 Mon Sep 17 00:00:00 2001 From: "Syed Mohammad Shah Mostafa (Tashfin)" Date: Sat, 21 Nov 2026 12:43:18 +0100 Subject: [PATCH] fix(core): percent-encode ! ' ( ) * in non-reserved URI template expansion RFC 6570 3.2.1 expands everything outside ALPHA / DIGIT / "-" / "." / "_" / "~", but encodeURIComponent leaves the sub-delimiters ! ' ( ) * unescaped, so values containing them expanded to URIs that differ from every other RFC 6570 implementation. Reserved expansion (+ and #) is unchanged: those characters are reserved and stay literal there. match() returns the raw matched span without decoding, so this does not affect expand/match symmetry. Refs #3219 --- packages/core-internal/src/shared/uriTemplate.ts | 11 ++++++++++- .../core-internal/test/shared/uriTemplate.test.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/core-internal/src/shared/uriTemplate.ts b/packages/core-internal/src/shared/uriTemplate.ts index 5ffe213acd..2b0270d3e8 100644 --- a/packages/core-internal/src/shared/uriTemplate.ts +++ b/packages/core-internal/src/shared/uriTemplate.ts @@ -104,9 +104,18 @@ export class UriTemplate { private encodeValue(value: string, operator: string): string { UriTemplate.validateLength(value, MAX_VARIABLE_LENGTH, 'Variable value'); if (operator === '+' || operator === '#') { + // Reserved expansion keeps RFC 3986 reserved characters — including + // the sub-delims "!" "'" "(" ")" "*" — and passes existing + // pct-triplets through, so encodeURI is the right encoder here. return encodeURI(value); } - return encodeURIComponent(value); + // encodeURIComponent leaves "!" "'" "(" ")" "*" unescaped, but RFC 6570 + // §3.2.1 expands everything outside ALPHA / DIGIT / "-" / "." / "_" / "~", + // so those five have to be percent-encoded explicitly. + return encodeURIComponent(value).replaceAll(/[!'()*]/g, char => { + const codePoint = char.codePointAt(0)!; + return `%${codePoint.toString(16).toUpperCase()}`; + }); } private expandPart( diff --git a/packages/core-internal/test/shared/uriTemplate.test.ts b/packages/core-internal/test/shared/uriTemplate.test.ts index bfc3237872..ddd4816344 100644 --- a/packages/core-internal/test/shared/uriTemplate.test.ts +++ b/packages/core-internal/test/shared/uriTemplate.test.ts @@ -35,6 +35,17 @@ describe('UriTemplate', () => { const template = new UriTemplate('{var}'); expect(template.expand({ var: 'value with spaces' })).toBe('value%20with%20spaces'); }); + + it('should encode the sub-delimiters encodeURIComponent leaves alone', () => { + const template = new UriTemplate('{var}'); + expect(template.expand({ var: "a!b'c(d)e*f" })).toBe('a%21b%27c%28d%29e%2Af'); + expect(template.expand({ var: 'Hello World!' })).toBe('Hello%20World%21'); + }); + + it('should keep sub-delimiters for reserved and fragment expansion', () => { + expect(new UriTemplate('{+var}').expand({ var: 'Hello World!' })).toBe('Hello%20World!'); + expect(new UriTemplate('{#var}').expand({ var: 'a(b)!' })).toBe('#a(b)!'); + }); }); describe('reserved expansion', () => {