fix: reset lastIndex before testing global/sticky content-type RegExp parsers - #6846
Merged
climba03003 merged 1 commit intoAug 8, 2026
Conversation
… parsers A content-type parser registered with a g- or y-flagged RegExp keeps a mutable lastIndex between calls, so getParser's .test() could skip part of the next content type and match inconsistently. Reset lastIndex before each test.
climba03003
reviewed
Jul 15, 2026
climba03003
left a comment
Member
There was a problem hiding this comment.
The fixes LGTM.
But I do not think both g and y flag should be used in Content-Type matching.
Contributor
Author
|
yeah fair, those flags come from user regexps tho, fastify never sets them itself. happy to just strip g/y at registration instead if you prefer that over resetting lastIndex |
Member
We may check the Independently by the fix, (maybe) we are changing the dev's intentions |
climba03003
approved these changes
Aug 8, 2026
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.
Checklist
npm run testandnpm run benchmarkWhat this does
ContentTypeParser.getParserwalksparserRegExpListand callsparserRegExp.test(ct)to find a matching custom parser. When a parser is registered with a RegExp that carries thegoryflag,RegExp.prototype.testis stateful: it advanceslastIndexon a match and resumes from there on the next call. So the same parser RegExp can match one request and then miss (or partially match) a later one for the same content type, depending on wherelastIndexwas left.The fix resets
parserRegExp.lastIndex = 0before eachtest(), which makes matching independent of previous calls. Parsers registered with non-global RegExps (the common case) are unaffected, sincelastIndexis not used there.Added a regression test that registers a global-flagged content-type RegExp and sends two matching requests; it fails before this change (the second request misses the parser) and passes after.