Fix extractQuerystring treating a missing '?' as index 0 - #10234
Open
Zuhef wants to merge 1 commit into
Open
Conversation
extractQuerystring guarded with `if (!queryStart)`, but indexOf returns -1
when the character is absent, and !(-1) is false. So the early return never
fired for a URL without a query string; execution fell through to
url.substring(-1, ...), which JavaScript clamps to substring(0, ...) and
returns the entire URL. Conversely !0 is true, so an input that begins with
'?' returned '' and lost its query string.
extractQuerystring('https://example.com/path') // 'https://example.com/path', want ''
extractQuerystring('https://example.com/path#frag') // 'https://example.com/path', want ''
extractQuerystring('?a=1&b=2') // '', want '?a=1&b=2'
Compare against `queryStart < 0` instead.
packages/util/src/query.ts had no test file, so add one covering
querystring, querystringDecode and extractQuerystring, including the three
cases above.
🦋 Changeset detectedLatest commit: 6eca4e3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
There was a problem hiding this comment.
Code Review
This pull request fixes a bug in extractQuerystring() where it incorrectly handled cases when the query string was not found (returning -1) or when the URL started with a question mark (returning 0). The check !queryStart was replaced with queryStart < 0. Additionally, a comprehensive set of unit tests was added to verify the behavior of query string utility functions. There are no review comments, and I have no feedback to provide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
extractQuerystring()inpackages/util/src/query.tsguards withif (!queryStart):indexOfreturns-1when the character is absent, and!(-1)isfalse, so the early return never fires for a URL without a query string. Execution falls through tourl.substring(-1, …), which JavaScript clamps tosubstring(0, …)— returning the entire URL. And!0istrue, so an input that legitimately starts with?returns''and loses its query string.Three of five cases violate the documented contract ("Extract the query string part of a URL, including the leading question mark (if present)"):
https://example.com/path'https://example.com/path'''https://example.com/path#frag'https://example.com/path'''?a=1&b=2'''?a=1&b=2'https://example.com/p?a=1'?a=1''?a=1'✅https://example.com/p?a=1#frag'?a=1''?a=1'✅Fix
Compare against
queryStart < 0.Blast radius
The only consumer in this repo is
packages/auth/src/core/action_code_url.ts, which uses it to parse email action links:I traced both changed paths there and neither alters current auth behaviour:
querystringDecode, which split it into a single junk key ({'https://x/y': 'undefined'}), so['link']wasundefined. After, it decodes''→{}, so['link']is stillundefined. Same result, less nonsense in between.?— before, the query was dropped and lookups failed; after, a nesteddeep_link_id/linkin such a value is found, which is what the function is documented to do.So the fix is strictly more correct and does not loosen anything. Flagging it explicitly because the caller sits in
auth.Tests
packages/util/src/query.tshad no test file, so this addspackages/util/test/query.test.tscoveringquerystring,querystringDecodeandextractQuerystring, including the three cases above.That is
58 passingon an unmodified checkout, so the 11 additions are all new coverage. Reverting onlysrc/query.tsfails exactly the three regression cases:prettier --check(3.9.4, the pinned version) passes on both files, andtsc --noEmitreports nothing for either — the only diagnostics in my environment come from a newer@types/nodethan TypeScript 5.5.4 expects, infs.d.ts/stream.d.ts. I could not runeslintlocally because the versionnpxresolved wants flat config while the repo uses.eslintrc; CI will cover it.Not included
querystringDecode('?a')returns{ a: 'undefined' }— the literal string, becausedecodeURIComponent(undefined)stringifies — whereURLSearchParamswould give''. That is a separate function and changing it could affect callers, so I left it alone and only noted it here. Happy to follow up if you'd like it fixed.