Skip to content

fix(devtools): split the open-in-editor line suffix in linear time - #1178

Open
ofri-peretz wants to merge 1 commit into
nuxt:mainfrom
ofri-peretz:fix/open-in-editor-redos
Open

ofri-peretz wants to merge 1 commit into
nuxt:mainfrom
ofri-peretz:fix/open-in-editor-redos

Conversation

@ofri-peretz

Copy link
Copy Markdown

Title: fix(devtools): split the open-in-editor line suffix in linear time
Base: main

I ran a ReDoS check over packages/*/src and it flagged FILE_LINE_COL_RE in server-rpc/general.ts. Your own regexp/no-super-linear-backtracking rule flags it too, and there was a disable comment above it, so I looked at what it costs.

openInEditor runs input.match(/^(.*?)(:[:\d]*)$/) on the string it is given. With a long run of colons, the lazy .*? is retried at every colon, and each retry scans to the end before $ fails, so the cost is quadratic. I measured, in Node 24 on one machine, the input ":" * n + "x":

n old new
1,000 0.67 ms 0.05 ms
5,000 11.7 ms 0.01 ms
12,500 66 ms 0.03 ms
25,000 260 ms 0.04 ms
50,000 1,009 ms (2,088 ms in an earlier run) 0.17 ms
500,000 not run 0.86 ms

"a" + ":1" * (n/2) + "x" behaves the same way (503 ms vs 0.09 ms at 50k). This is not a security report: openInEditor is behind the Devframe client auth, and a trusted client can already open any file. It only stalls the single-threaded dev server for a while, but it is easy to avoid.

The fix keeps the same language and makes the prefix end on a character that cannot be part of the suffix (not :, a digit or a line terminator), so the suffix is found without rescanning:

const FILE_LINE_COL_RE = /^((?:.*?[^:\d\n\r\u2028\u2029])?)(\d*)(:[:\d]*)$/
// ...
input = match[1]! + match[2]!
suffix = match[3]!

The line terminators are listed on purpose: . does not match them, so [^...] must not either, otherwise "Foo.vue\n:3" would start matching.

How I checked it is the same:

  • Compared with the old regex on every string up to length 6 over :, 1, a, /, \n, \r, U+2028 (137,257 strings): 0 differences in [input, suffix].
  • A table of 28 realistic and awkward inputs (Foo.vue:3:5, Foo.vue::12:3, C:\proj\Foo.vue:3:5, /abs/file12:34, bare : and ::, line terminators): 0 differences.

Tests, in packages/devtools/test/open-in-editor.test.ts:

  • :3, :3:5 and ::12:3 suffixes are kept after the file name.
  • a line terminator before the colon is not read as a suffix.
  • a timing test: ":" * n + "x" at 100k and 400k, median of 7 runs each, asserting that 4x the input costs less than 8x the time (linear is about 4, quadratic about 16). I did not use an absolute budget so it does not depend on the machine.

Results: pnpm vitest run packages/devtools/test/open-in-editor.test.ts gives 12 passed, and it passed 8 times in a row. With the old regex and the same test at 12,500 and 50,000 (the 400k size would not finish in reasonable time on the old one), it fails: expected 15.03 to be less than 8, 1 failed, 11 passed. eslint on the two changed files is clean without the regexp/no-super-linear-backtracking disable comment, so I removed it. I did not run the full pnpm test, only lint on the two files and the unit test file.

This was found by eslint-plugin-secure-coding.

AI disclosure: drafted with Claude (AI-assisted); I reviewed and tested the change.

FILE_LINE_COL_RE was /^(.*?)(:[:\d]*)$/, which rescans to the end of the input
for every colon. A path that is a long run of colons took 2 s at 50k characters.
Anchor the prefix on a character that cannot belong to the suffix instead, which
also lets the regexp/no-super-linear-backtracking disable go.
@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

FILE_LINE_COL_RE now captures path digits separately from a trailing line-and-column suffix. openInEditor reconstructs the path and preserves the suffix when opening a file. Tests cover suffix variants, a suffix after a newline, and matching performance.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: Minimal · up to 475f7

This change makes splitting the line and column suffix from an editor file path run in linear time and keeps the resulting paths the same. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: splitting the open-in-editor line suffix in linear time in the devtools package. The changeset replaces FILE_LINE_COL_RE in server-rpc/general.ts with a regex whose prefix …
Description check Passed The description states the purpose: a ReDoS-style quadratic backtracking issue in FILE_LINE_COL_RE used by openInEditor. It gives the old and new regex, measured timings, equivalence checks, the added…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the change: splitting the open-in-editor line suffix in linear time in the devtools package. The changeset replaces FILE_LINE_COL_RE in server-rpc/general.ts with a regex whose prefix cannot consume suffix characters, which removes quadratic backtracking. The title matches the main change. It is short and uses the conventional fix(scope) form.

Full details: Description check

Explanation

The description states the purpose: a ReDoS-style quadratic backtracking issue in FILE_LINE_COL_RE used by openInEditor. It gives the old and new regex, measured timings, equivalence checks, the added tests in open-in-editor.test.ts, and lint results. It directly describes the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d1925c04-9cdd-4d7f-887d-4dd4d61e321a
Commits

Reviewing files that changed from the base of the PR and between 853f239 and 475f7d6.

Files selected for processing: 2

  • packages/devtools/src/server-rpc/general.ts
  • packages/devtools/test/open-in-editor.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant