fix(shared): bound the file-link label so bracket runs stop rescanning (#5782)
Co-authored-by: tsouth89 <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
2 files changed
+36
-1
No files matched your search
@@ -129,4 +129,25 @@ describe("collectComposerInlineTokens", () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("still collects a file link whose label is at the length cap", () => {
|
||||
const label = `${"a".repeat(508)}.tsx`;
|
||||
const tokens = collectComposerInlineTokens(`see [${label}](src/${label}) ok`);
|
||||
|
||||
expect(tokens).toHaveLength(1);
|
||||
expect(tokens[0]?.value).toBe(`src/${label}`);
|
||||
});
|
||||
|
||||
it("leaves a file link past the label cap as plain text", () => {
|
||||
const label = `${"a".repeat(509)}.tsx`;
|
||||
expect(collectComposerInlineTokens(`see [${label}](src/${label}) ok`)).toEqual([]);
|
||||
});
|
||||
|
||||
it("stays fast on unterminated bracket runs", () => {
|
||||
// Unbounded, the label body rescanned the rest of the text from every
|
||||
// whitespace: this input took seconds.
|
||||
const started = performance.now();
|
||||
expect(collectComposerInlineTokens(" [[".repeat(40_000))).toEqual([]);
|
||||
expect(performance.now() - started).toBeLessThan(1_000);
|
||||
});
|
||||
});
|
||||
@@ -20,7 +20,21 @@ export interface CollectComposerInlineTokensOptions {
|
||||
|
||||
const SKILL_TOKEN_REGEX = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s)/g;
|
||||
const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;
|
||||
const FILE_LINK_TOKEN_REGEX = /(^|\s)\[((?:\\.|[^\]\\])*)\]\(([^)\s]+)\)(?=\s)/g;
|
||||
/**
|
||||
* The label body is bounded rather than `*`. Unbounded, every whitespace in
|
||||
* the composer is a candidate start: the engine scans the rest of the text for
|
||||
* a closing `]`, fails, and rescans from the next whitespace — quadratic on
|
||||
* input like " [[[[[…". A cap makes each attempt constant-bounded.
|
||||
*
|
||||
* Only a basename ever survives the `label !== basename` check below, so this
|
||||
* cannot reject a link a user could meaningfully write; the longest filename
|
||||
* any common filesystem allows is 255.
|
||||
*/
|
||||
const MAX_FILE_LINK_LABEL_LENGTH = 512;
|
||||
const FILE_LINK_TOKEN_REGEX = new RegExp(
|
||||
`(^|\\s)\\[((?:\\\\.|[^\\]\\\\]){0,${MAX_FILE_LINK_LABEL_LENGTH}})\\]\\(([^)\\s]+)\\)(?=\\s)`,
|
||||
"g",
|
||||
);
|
||||
const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
||||
const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
|
||||
// Autocomplete emits canonical file links, so ambiguous bare @scope/package text stays a package.
|
||||
|
||||
Reference in new issue
Block a user