Repository navigation
fix(devtools): split the open-in-editor line suffix in linear time - #1178
ofri-peretz wants to merge 1 commit into
Conversation
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.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Title: fix(devtools): split the open-in-editor line suffix in linear time
Base: main
I ran a ReDoS check over
packages/*/srcand it flaggedFILE_LINE_COL_REinserver-rpc/general.ts. Your ownregexp/no-super-linear-backtrackingrule flags it too, and there was a disable comment above it, so I looked at what it costs.openInEditorrunsinput.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":"a" + ":1" * (n/2) + "x"behaves the same way (503 ms vs 0.09 ms at 50k). This is not a security report:openInEditoris 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: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:
:,1,a,/,\n,\r, U+2028 (137,257 strings): 0 differences in[input, suffix].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:5and::12:3suffixes are kept after the file name.":" * 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.tsgives 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.eslinton the two changed files is clean without theregexp/no-super-linear-backtrackingdisable comment, so I removed it. I did not run the fullpnpm 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.