Skip to content

Keep input-parsing regexes linear on long values - #1811

Closed
helpcodeai wants to merge 1 commit into
mainfrom
fix/bounded-regex-inputs
Closed

helpcodeai wants to merge 1 commit into
mainfrom
fix/bounded-regex-inputs

Conversation

@helpcodeai

Copy link
Copy Markdown
Contributor

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.c given as a base URL) used ^[^\s@/]+@[^\s@/]+\.[^\s@/]+$. On values like a@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 over a . @ / space \n b (6.7M values) found 0 differences. a@b..c, a@.b.c and a@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 by trimTrailingDotsAndSpace(), 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 of a . space \n NBSP U+2028 x).
  • file-search/file-search-config.ts: the host's path cut .replace(/\/.*$/, '') becomes split('/')[0]. . does not match line terminators, so the old regex left a host with a line break whole, and HOST_RE then 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 asserts elapsed < 250 ms. 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=1 still prints the timing.

Type

  • Bug fix
  • New feature
  • Documentation
  • Refactoring
  • Performance improvement
  • Other (describe below): flaky test made deterministic

Testing

  • Added/updated unit tests
  • Tested manually (describe steps below)
  • No tests needed (explain why)

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. and me@acme.weclapp.com. These do not: a@b, a@b., a@.b and a@... A 50k-character a@a.a.…@ value must be answered in under 200 ms.
  • adapters.service.spec.ts: trimTrailingDotsAndSpace must 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 toolsById walk to unregisterConnectorTools. 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/backend after npm ci at the root and DATABASE_URL=postgresql://x:x@localhost:5433/x npx prisma generate:

npx jest --maxWorkers=4 src/common/base-url-variable.util.spec.ts src/adapters/adapters.service.spec.ts src/file-search/file-search-config.spec.ts src/mcp-server/tool-registry.spec.ts
# Test Suites: 4 passed, 4 total; Tests: 427 passed, 427 total

npx tsc --noEmit -p tsconfig.json
# exit 0

npx eslint <the 7 changed files>
# exit 0 (no --fix)

I did not run the full backend suite or npm run lint locally; CI runs both.

Checklist

  • My code follows the existing code style
  • I have tested my changes locally
  • I have updated documentation (if applicable): not applicable, no user-facing change
  • All existing tests pass (npm test): only the four affected suites were run locally (all pass); full suite left to CI
  • This PR has a descriptive title

Related Issues

None. Found by code scanning, plus the CI flake in the tool registry spec.

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.
@helpcodeai

Copy link
Copy Markdown
Contributor Author

Folded into the v0.55.8 release PR #1813 (merged as 5777a92).

@helpcodeai helpcodeai closed this Nov 21, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant