Repository navigation
Keep input-parsing regexes linear on long values - #1811
Closed
helpcodeai wants to merge 1 commit into
Closed
helpcodeai wants to merge 1 commit into
helpcodeai wants to merge 1 commit into
Conversation
Three patterns flagged by code scanning backtrack quadratically on long crafted values: the e-mail check of a base-URL variable, the trailing dot/space trim of an upstream error message, and the path cut of a File Search host. Each is replaced by a linear form with the same results. The tool registry reload test no longer asserts a wall-clock bound, which flaked on CI; it checks that a reload iterates no registry-wide map.
7 of 14 tasks
Contributor
Author
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
Code scanning flagged three regular expressions that backtrack quadratically on long crafted values. Each one is replaced by a linear form that gives the same result for every input, so nothing changes for users. The same PR fixes a flaky timing assertion in the tool registry spec: it failed on CI with 426 ms against a 250 ms bound.
Cloud and self-hosted behave the same. The change touches only input parsing and a spec, with no deployment-mode branch.
Changes
common/base-url-variable.util.ts: the e-mail check (a@b.cgiven as a base URL) used^[^\s@/]+@[^\s@/]+\.[^\s@/]+$. On values likea@a.a.a.a…@, the engine tried every dot as the separator. The new pattern,^[^\s@/]+@[^\s@/][^\s@/.]*\.[^\s@/]+$, matches the first character of the domain on its own and then runs up to the first dot after it. It accepts exactly the same set: an exhaustive comparison of all strings up to 8 characters overa . @ / space \n b(6.7M values) found 0 differences.a@b..c,a@.b.canda@b.c.still get the e-mail reason, as before.adapters/adapters.service.ts:message.replace(/[.\s]+$/, '')(used twice when an upstream error excerpt is appended) is replaced bytrimTrailingDotsAndSpace(), a loop that walks back from the end. It gives the same output, including Unicode whitespace (0 differences over all strings up to 8 characters ofa . space \n NBSP U+2028 x).file-search/file-search-config.ts: the host's path cut.replace(/\/.*$/, '')becomessplit('/')[0]..does not match line terminators, so the old regex left a host with a line break whole, andHOST_REthen refused it. The new code keeps that rule explicitly: a host containing a line terminator is refused with the same "Enter the server name or IP address." error (0 differences in accept/reject or result over 6.7M values). The zod schema already caps this field at 253 characters, so this site was bounded in practice. It is changed for consistency and to clear the scanner.mcp-server/tool-registry.spec.ts: "reloads one connector without walking the rest of the registry" no longer assertselapsed < 250ms. It now instruments the iteration methods (keys,values,entries,forEach,Symbol.iterator) of the registry-wide maps (by id, by name, by organization, the order map). It also instruments the shared buckets the connector's tools live in (the 20k-entry name buckets and the organization's bucket), and asserts that 100 reloads never iterate any of them.REGISTRY_BENCH=1still prints the timing.Type
Testing
New spec cases sit beside each source file:
base-url-variable.util.spec.ts: these values keep the e-mail reason:a@b.c,a@b..c,a@.b.c,a@b.c.andme@acme.weclapp.com. These do not:a@b,a@b.,a@.banda@... A 50k-charactera@a.a.…@value must be answered in under 200 ms.adapters.service.spec.ts:trimTrailingDotsAndSpacemust equal the old regex on a table of messages. A message with a 50k-space run in the middle, plus 50k characters of trailing., must be handled in under 200 ms.file-search-config.spec.ts: the host is extracted from a scheme, a path or a trailing slash. Hosts with a line break, a CRLF or a leading/are refused. A host of slashes at the 253-character limit is checked too.tool-registry.spec.ts: the walk check described above.To check that the new tests catch the problems, I temporarily put back the old e-mail regex and added a
toolsByIdwalk tounregisterConnectorTools. Both tests then failed: the e-mail check took 564 ms against the 200 ms bound, and the walk check recorded 102 map iterations. I reverted both changes before committing.Commands, all run in
packages/backendafternpm ciat the root andDATABASE_URL=postgresql://x:x@localhost:5433/x npx prisma generate:I did not run the full backend suite or
npm run lintlocally; CI runs both.Checklist
npm test): only the four affected suites were run locally (all pass); full suite left to CIRelated Issues
None. Found by code scanning, plus the CI flake in the tool registry spec.