fix: validate the received url before its re-encoded variants - #1194
Open
dirnalafeinte wants to merge 1 commit into
Open
dirnalafeinte wants to merge 1 commit into
dirnalafeinte wants to merge 1 commit into
Conversation
`validateRequest()` rebuilt every candidate url from `new URL(url)`, so the string it hashed was never the string Twilio signed. `new URL()` percent-encodes characters the back end leaves as-is (`'` becomes `%27`), and the legacy querystring round trip rewrites `+` as `%20`, so a url whose query contains both a space encoded as `+` and a single quote could not be reproduced by any of the four variants and validation failed on a validly signed request. Check the received url as-is first, plus a port-toggled form built by string surgery on the authority only, so the path and query keep their exact bytes. The four existing variants are left untouched and still run afterwards, so urls that only validate in a normalized form keep working. Fixes twilio#1183 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Fixes #1183
validateRequest()rebuilds every candidate URL fromnew URL(url), so the string it hashes is never the string Twilio actually signed. Two separate normalizations get in the way:new URL()percent-encodes characters the back end leaves as-is, so'becomes%27withLegacyQuerystring()round-trips throughquerystring.parse/stringify, which decodes+to a space and re-encodes it as%20Each of the four existing variants goes through at least one of those, so a URL whose query contains both a space encoded as
+and a single quote cannot be reproduced by any of them, and a validly signed request is rejected.The existing single-quote tests pass only by coincidence: Node's
querystring.escapedoes not escape', so the legacy-querystring variant happens to reproduce the raw string. Add a+to the same query and that coincidence disappears — which is exactly the?name=William+O'haracase in #1183.Change
Check the URL exactly as received first, since that is the string Twilio signed, followed by a port-toggled form of it. The port toggle is done with string surgery on the authority component only (handling
userinfo@and IPv6 literals), so the path and query keep their exact bytes rather than being rebuilt from a parsed object.The four existing variants are left untouched and still run afterwards, so URLs that only validate in a normalized form keep working. The change is purely additive — it accepts signatures that are already valid, and every candidate is still verified against the auth token through the same constant-time comparison.
Testing
Added three cases covering
?name=William+O'hara— identical URL, target with the port, and target without the port. All three fail onmainand pass with this change.npm run test:typescript(tsc --noEmit) andprettier --checkare both clean.Note:
PULL_REQUEST_TEMPLATE.mdasks to runnpm run webhook-testfor Request Validation changes, but no such script exists inpackage.json. The equivalentspec/cluster/webhook.spec.tsneeds live credentials (TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN,TWILIO_API_KEY,TWILIO_API_SECRET) plus localtunnel and makes real API calls, so I did not run it. Happy to if a maintainer can run it against CI credentials.Checklist