Skip to content

Commit 9b71e34

Browse files
tsouth89claude
andcommitted
fix(shared): bound the file-link label so bracket runs stop rescanning
FILE_LINK_TOKEN_REGEX let the label body repeat without a bound. Every whitespace in the composer is therefore a candidate start: the engine scans the rest of the text for a closing "]", fails, and rescans from the next whitespace. On a run of unterminated brackets that is quadratic, and collectComposerInlineTokens runs on raw composer text, so pasting one freezes the tab. Capping the body makes each attempt constant-bounded: 120k chars of " [[" 2838ms -> 33ms 60k chars 676ms -> 17ms 30k chars 167ms -> 8ms The cap cannot reject a link anyone could meaningfully write. Only a basename survives the `label !== basename` check below it, and the longest filename common filesystems allow is 255. Verified equivalent to the old pattern over 156,504 generated inputs built from [ ] ( ) \ @ " whitespace and path-ish fragments, comparing match count, offsets and every capture group: zero differences. The single intended difference is at the cap itself, which the two new boundary tests pin. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 05eb051 commit 9b71e34

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎packages/shared/src/composerInlineTokens.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,25 @@ describe("collectComposerInlineTokens", () => {
129129
},
130130
]);
131131
});
132+
133+
it("still collects a file link whose label is at the length cap", () => {
134+
const label = `${"a".repeat(508)}.tsx`;
135+
const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
136+
137+
expect(tokens).toHaveLength(1);
138+
expect(tokens[0]?.value).toBe(`src/${label}`);
139+
});
140+
141+
it("leaves a file link past the label cap as plain text", () => {
142+
const label = `${"a".repeat(509)}.tsx`;
143+
expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
144+
});
145+
146+
it("stays fast on unterminated bracket runs", () => {
147+
// Unbounded, the label body rescanned the rest of the text from every
148+
// whitespace: this input took seconds.
149+
const started = performance.now();
150+
expect(collectComposerInlineTokens(" [[".repeat(40_000))).toEqual([]);
151+
expect(performance.now() - started).toBeLessThan(1_000);
152+
});
132153
});

‎packages/shared/src/composerInlineTokens.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ export interface CollectComposerInlineTokensOptions {
2020

2121
const SKILL_TOKEN_REGEX = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s)/g;
2222
const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;
23-
const FILE_LINK_TOKEN_REGEX = /(^|\s)\[((?:\\.|[^\]\\])*)\]\(([^)\s]+)\)(?=\s)/g;
23+
/**
24+
* The label body is bounded rather than `*`. Unbounded, every whitespace in
25+
* the composer is a candidate start: the engine scans the rest of the text for
26+
* a closing `]`, fails, and rescans from the next whitespace — quadratic on
27+
* input like " [[[[[…". A cap makes each attempt constant-bounded.
28+
*
29+
* Only a basename ever survives the `label !== basename` check below, so this
30+
* cannot reject a link a user could meaningfully write; the longest filename
31+
* any common filesystem allows is 255.
32+
*/
33+
const MAX_FILE_LINK_LABEL_LENGTH = 512;
34+
const FILE_LINK_TOKEN_REGEX = new RegExp(
35+
`(^|\\s)\\[((?:\\\\.|[^\\]\\\\]){0,${MAX_FILE_LINK_LABEL_LENGTH}})\\]\\(([^)\\s]+)\\)(?=\\s)`,
36+
"g",
37+
);
2438
const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
2539
const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
2640
// Autocomplete emits canonical file links, so ambiguous bare @scope/package text stays a package.

0 commit comments

Comments
 (0)