Skip to content

fix(resolution): remove catastrophic backtracking in the Vapor route regex - #1564

Open
pucedoteth wants to merge 1 commit into
colbymchenry:mainfrom
pucedoteth:fix/vapor-route-regex-backtracking
Open

fix(resolution): remove catastrophic backtracking in the Vapor route regex#1564
pucedoteth wants to merge 1 commit into
colbymchenry:mainfrom
pucedoteth:fix/vapor-route-regex-backtracking

Conversation

@pucedoteth

Copy link
Copy Markdown

Fixes #1544.

The bug

src/resolution/frameworks/swift.ts matched Vapor routes with:

/\b(\w+)\.(get|post|...)\s*\(\s*((?:[^,()]+,\s*)*)use:\s*([A-Za-z_][\w.]*)/g

[^,()]+ also matches whitespace, so each space after a comma can be consumed either by \s* on the current iteration or by [^,()]+ on the next one. When the call never reaches use: the match fails, and the engine explores every possible split.

Measured on app.get(arg0, arg1, … x) with no use: label:

arguments ms
16 1.7
20 23.8
24 379.7
28 6100.1

~4× per additional argument. At 40 arguments it does not finish — I aborted a run after 10 minutes. Any generated Swift file with a long argument list on a .get/.post/… call stalls index/sync/MCP indefinitely.

The fix

Keep the repeated group to <run>, and match the gap before use: once, outside the loop:

((?:[^,()]+,)*)\s*use:

, is excluded from the class, so each iteration is forced to span exactly one comma-delimited run — there is only one way to split the input, and the failure is linear. 2000 arguments now match in 0.2 ms; the 40-argument case is 0.5 ms.

Behaviour is unchanged

The capture group is only ever consumed by segJoin, which pulls "quoted" literals out of it via /"([^"]*)"/g. Moving whitespace outside the group therefore cannot change a route path. I diffed old vs new across the real call shapes and the receiver/method/path/handler are identical on every one:

same  app.get("users", use: list)                          -> GET /users -> list
same  app.get(use: index)                                  -> GET / -> index
same  app.get("users", ":id", use: UserController.show)    -> GET /users/:id -> UserController.show
same  routes.post("a","b", use: h)                         -> POST /a/b -> h
same  app.get( "x" ,  "y" ,  use:  self.handler )          -> GET /x/y -> self.handler
same  app.get("users", BlogUser.parameter, use: show)      -> GET /users -> show
same  req.parameters.get("id")                             -> no match
same  Environment.get("X")                                 -> no match

The last two matter: use: is what discriminates a route from Environment.get(…) / req.parameters.get(…), and both still correctly fail to match.

Tests

Added does not backtrack exponentially on a call that never reaches use: (#1544) to __tests__/frameworks.test.ts — 40 arguments, no use:, asserts it completes under 1s and extracts no routes. It passes in 0.5 ms with the fix; with the old regex restored it does not terminate, so the assertion fails.

npm run build && npx vitest run is green: 2902 passed, 178 skipped, up from 2901 by exactly the new test.

Note for anyone reproducing locally: the suite needs npm run build first — 54 tests in 15 files fail without dist/ because the CLI tests spawn dist/bin/codegraph.js.

The three sibling regexes in this file ((?:\w+\s*,\s*)* in the SwiftUI/UIKit patterns) are not affected — \w and \s are disjoint, so there is no ambiguity to backtrack on. I left them alone.

…regex

The repeated group was `(?:[^,()]+,\s*)*`. `[^,()]+` also matches
whitespace, so every space after a comma could be consumed either by
`[^,()]+` on the next iteration or by `\s*` on the current one. On a
`.get(...)`/`.post(...)` call whose argument list never reaches `use:`,
the match fails and the engine explores every split:

    args   ms
      16    1.7
      20   23.8
      24  379.7
      28 6100.1

That is ~4x per additional argument, so a generated Swift file with a
few dozen arguments stalls index/sync/MCP indefinitely.

Keeping the loop to `<run>,` and matching the gap before `use:` once,
outside the loop, leaves exactly one way to split the input: 2000
arguments now match in 0.2ms. Behaviour is unchanged — `segJoin` only
pulls `"quoted"` literals out of the captured group, so moving the
whitespace out of it cannot alter a route path, and the extracted
receiver/method/path/handler are identical across the real Vapor call
shapes (bare `use:`, string segments, non-string segments like
`BlogUser.parameter`, spaced arguments, and the `Environment.get("X")` /
`req.parameters.get("id")` non-matches).

Fixes colbymchenry#1544
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Vapor route detector regex has catastrophic backtracking, hangs index/sync/MCP on generated Swift files

1 participant