Visitar URL original
fix(common): tolerate invalid percent-encoding in HttpUrlEncodingCodec by rootvector2 · Pull Request #69042 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20261006101747/https://github.com/angular/angular/pull/69042
Skip to content

fix(common): tolerate invalid percent-encoding in HttpUrlEncodingCodec - #69042

Open
rootvector2 wants to merge 2 commits into
angular:mainfrom
rootvector2:httpparams-decode-uri-fallback
Open

rootvector2 wants to merge 2 commits into
angular:mainfrom
rootvector2:httpparams-decode-uri-fallback

Conversation

@rootvector2

Copy link
Copy Markdown
Contributor

Noticed HttpUrlEncodingCodec.decodeKey/decodeValue call decodeURIComponent directly, and HttpParams feeds them straight from its fromString option (the class docs suggest passing window.location.search). A stray % in the query, e.g. ?a=100%done, is a valid query character but makes decodeURIComponent throw a URIError that escapes the HttpParams constructor. Decode in a try/catch and fall back to the raw value, leaving valid escapes decoded as before.

@pullapprove
pullapprove Bot requested a review from kirjs June 1, 2026 07:32
@angular-robot angular-robot Bot added the area: common Issues related to APIs in the @angular/common package label Jun 1, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jun 1, 2026
Comment thread packages/common/http/src/params.ts Outdated
Comment on lines 82 to 87
function tryDecodeURIComponent(value: string): string {
try {
return decodeURIComponent(value);
} catch {
return value;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As mentionned in #69061, can we have a private export of tryDecodeURIComponent in core to private duplicate implementations of this behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done — added ɵtryDecodeURIComponent as a private export in core and routed HttpUrlEncodingCodec through it. i'll rebase the upgrade/router/cookie PRs onto it so they drop their own copies.

Comment thread packages/core/src/util/decode_uri.ts Outdated
* @param value potential URI component to decode.
* @returns the decoded URI if it can be decoded, otherwise `undefined`.
*/
export function tryDecodeURIComponent(value: string): string | undefined {

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.

NIT: maybe instead of tryDecodeURIComponent and returning undefined, we can go with decodeURIComponentSafe and instead of returning undefined return the original value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done — renamed to decodeURIComponentSafe and it returns the original value on failure, so the callers drop the ?? value.

@alan-agius4

Copy link
Copy Markdown
Contributor

That being said, I wonder if the risk is too big for this change to land.

#69049 (comment)

Hoist the decode fallback into a private `ɵdecodeURIComponentSafe` export
in core and route `HttpUrlEncodingCodec` through it, so the codec packages
share a single implementation instead of duplicating it.
@rootvector2
rootvector2 force-pushed the httpparams-decode-uri-fallback branch from 55d8c70 to 8d06231 Compare June 18, 2026 16:03
@rootvector2

Copy link
Copy Markdown
Contributor Author

the mixed-encoding limitation from #69049 applies here too: a value like a=1%202%done has a valid and a stray escape in one segment, so the fallback hands back the whole thing raw and the %20 stays encoded. thats a real limit of the try/catch and i wont pretend otherwise.

the difference from the router is theres no navigation/validation gate here. HttpParams just parses a query string, and the class docs say you can pass window.location.search, which can legally hold a bare %. today that throws straight out of the constructor, so this swaps a hard throw for a raw-but-usable value rather than bypassing a security boundary the way the router parse did.

if you still feel the risk outweighs that, im fine closing it.

@rootvector2

Copy link
Copy Markdown
Contributor Author

any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: common Issues related to APIs in the @angular/common package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants