* Fix ReDoS in terminal file-path link detection that froze the app (#5970) A full-screen TUI such as ngrok, run through Windows ConPTY, emits its dashboard as a single newline-free logical line that is almost entirely alignment spaces. xterm accumulates it into one ~150k-char logical line, and on every hover/redraw Orca's file-path link providers scan that line. The spaced-path lookaheads (SPACED_PATH_WITH_SEPARATOR_REGEX and LINE_ENDING_SPACED_PATH_REGEX) began with `[^...]*\s+`, where the negated character class also matches whitespace. That overlap let the engine split a long run of spaces in exponentially many ways before failing, so a ~5k-char prefix took ~25s and pegged the renderer main thread, freezing the whole app. macOS uses a normal PTY and never builds that line, so it was unaffected; disabling terminal GPU acceleration did not help. Anchor the first lookahead segment at the first whitespace (add `\s` to the negated class) so it can no longer overlap the following `\s+`. The matches are identical; the scan drops from ~25,000ms to ~4ms. Adds a regression test asserting extraction stays roughly linear on long space-heavy lines. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Fix terminal link ReDoS guards * Trim terminal link padding --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
co-authored by
Claude Opus 4.8
Jinwoo Hong
Neil
parent
e21ddd378c
commit
73255f3c59
@@ -0,0 +1,29 @@
|
||||
import { performance } from 'node:perf_hooks'
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { extractTerminalFileLinks, extractTerminalFileLinkCandidates } from './terminal-links'
|
||||
|
||||
// Regression guard for #5970: a full-screen TUI (e.g. ngrok run through Windows
|
||||
// ConPTY) emits its dashboard as one newline-free line that is mostly alignment
|
||||
// spaces with a few separators. Keep these non-matching inputs large enough to
|
||||
// catch overlapping whitespace backtracking without making normal CI noisy.
|
||||
describe('terminal-links ReDoS guard (#5970)', () => {
|
||||
const cases: [string, string][] = [
|
||||
['separator + space padding', `a/${' '.repeat(30_000)}`],
|
||||
['advertised url + space padding', `Web Interface http://127.0.0.1:4040${' '.repeat(30_000)}`]
|
||||
]
|
||||
|
||||
for (const [name, line] of cases) {
|
||||
it(`scans "${name}" in roughly linear time`, () => {
|
||||
const start = performance.now()
|
||||
extractTerminalFileLinks(line)
|
||||
extractTerminalFileLinkCandidates(line)
|
||||
const elapsedMs = performance.now() - start
|
||||
expect(elapsedMs).toBeLessThan(500)
|
||||
})
|
||||
}
|
||||
|
||||
it('still detects separator paths that contain spaces', () => {
|
||||
const links = extractTerminalFileLinks('/Users/a/Foo Bar/file.ts')
|
||||
expect(links.some((link) => link.pathText === '/Users/a/Foo Bar/file.ts')).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -128,6 +128,20 @@ describe('terminal path helpers', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('trims terminal padding after line-ending spaced paths', () => {
|
||||
const links = extractTerminalFileLinks('/Users/alice/My Folder ')
|
||||
expect(links).toHaveLength(1)
|
||||
expect(links[0]).toMatchObject({
|
||||
pathText: '/Users/alice/My Folder',
|
||||
displayText: '/Users/alice/My Folder'
|
||||
})
|
||||
})
|
||||
|
||||
it('does not treat mid-line command arguments as line-ending spaced paths', () => {
|
||||
const links = extractTerminalFileLinks('run /usr/bin/env node, then continue')
|
||||
expect(links.map((link) => link.pathText)).not.toContain('/usr/bin/env node')
|
||||
})
|
||||
|
||||
it('keeps trailing separators on directory-like absolute paths', () => {
|
||||
const links = extractTerminalFileLinks('/Users/alice/worktree/')
|
||||
expect(links).toHaveLength(1)
|
||||
|
||||
@@ -33,12 +33,19 @@ const LOCAL_PATH_REGEX =
|
||||
// Matches separator paths whose file or folder names include spaces. This runs
|
||||
// before LOCAL_PATH_REGEX so `/Users/A/Foo Bar/file.ts` is claimed as one link
|
||||
// instead of split into `/Users/A/Foo` and `Bar/file.ts`.
|
||||
// Why this is intentionally broad: validating "space followed by a later
|
||||
// separator" inside the regex creates overlapping whitespace backtracking on
|
||||
// large ConPTY TUI lines. Keep the scan linear and filter candidates in code.
|
||||
const SPACED_PATH_WITH_SEPARATOR_REGEX =
|
||||
/(?:~[\\/]|[\\/]|\.{1,2}[\\/]|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])(?=[^()[\]{}'",;<>|`\r\n]*\s+[^()[\]{}'",;<>|`\r\n]*[\\/])[^()[\]{}'",;<>|`\r\n]+(?::\d+)?(?::\d+)?/g
|
||||
/(?:~[\\/]|[\\/]|\.{1,2}[\\/]|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^()[\]{}'",;<>|`\r\n]+(?::\d+)?(?::\d+)?/g
|
||||
// Why this shares the broad candidate shape: extension paths with prose after
|
||||
// them still need trimming, but the whitespace/extension test stays in code.
|
||||
const SPACED_PATH_WITH_EXTENSION_REGEX =
|
||||
/(?:~[\\/]|[\\/]|\.{1,2}[\\/]|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^()[\]{}'",;<>|`\r\n]*?\s+[^()[\]{}'",;<>|`\\/ \r\n]*\.[A-Za-z0-9_+-]+(?::\d+)?(?::\d+)?/g
|
||||
/(?:~[\\/]|[\\/]|\.{1,2}[\\/]|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^()[\]{}'",;<>|`\r\n]+(?::\d+)?(?::\d+)?/g
|
||||
// Why this is also broad: the candidates path runs on hover, including huge
|
||||
// space-padded TUI lines, so reject line-ending spaced paths outside the regex.
|
||||
const LINE_ENDING_SPACED_PATH_REGEX =
|
||||
/(?:~[\\/]|[\\/]|\.{1,2}[\\/]|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])(?=[^()[\]{}'",;<>|`\r\n]*\s+)[^()[\]{}'",;<>|`\r\n]*\S(?=\s*$)/g
|
||||
/(?:~[\\/]|[\\/]|\.{1,2}[\\/]|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^()[\]{}'",;<>|`\r\n]+(?::\d+)?(?::\d+)?/g
|
||||
const SPACED_LOCAL_PATH_REGEXES = [
|
||||
SPACED_PATH_WITH_SEPARATOR_REGEX,
|
||||
SPACED_PATH_WITH_EXTENSION_REGEX,
|
||||
@@ -144,6 +151,39 @@ function hasPathSeparator(text: string): boolean {
|
||||
return text.includes('/') || text.includes('\\')
|
||||
}
|
||||
|
||||
function hasSeparatorAfterWhitespace(text: string): boolean {
|
||||
let sawWhitespace = false
|
||||
for (const char of text) {
|
||||
if (/\s/.test(char)) {
|
||||
sawWhitespace = true
|
||||
continue
|
||||
}
|
||||
if (sawWhitespace && (char === '/' || char === '\\')) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function hasInternalWhitespaceBeforeTrimmedEnd(text: string): boolean {
|
||||
const trimmed = text.trimEnd()
|
||||
return /\s/.test(trimmed)
|
||||
}
|
||||
|
||||
function isAtTrimmedLineEnd(lineText: string, endIndex: number): boolean {
|
||||
return lineText.slice(endIndex).trim().length === 0
|
||||
}
|
||||
|
||||
function hasSpacedPathExtension(text: string): boolean {
|
||||
const trimmedRange = trimSpacedPathTrailingProse({
|
||||
text,
|
||||
startIndex: 0,
|
||||
endIndex: text.length
|
||||
})
|
||||
const trimmedText = trimmedRange.text.trimEnd()
|
||||
return /\s/.test(trimmedText) && /\.[A-Za-z0-9_+-]+(?::\d+)?(?::\d+)?$/.test(trimmedText)
|
||||
}
|
||||
|
||||
// Bare words are validated against the filesystem by the provider, so this
|
||||
// filter's job is to reject tokens that are obviously not filenames before
|
||||
// we pay for a stat. Plain words like `src` or `my-cli` are usually
|
||||
@@ -267,6 +307,15 @@ function trimSpacedPathTrailingProse(range: DetectedRange): DetectedRange {
|
||||
}
|
||||
}
|
||||
|
||||
function trimTrailingWhitespace(range: DetectedRange): DetectedRange {
|
||||
const text = range.text.trimEnd()
|
||||
return {
|
||||
text,
|
||||
startIndex: range.startIndex,
|
||||
endIndex: range.startIndex + text.length
|
||||
}
|
||||
}
|
||||
|
||||
function buildLineEndingSpacedPathPrefixRanges(range: DetectedRange): DetectedRange[] {
|
||||
const ranges: DetectedRange[] = []
|
||||
for (const match of range.text.matchAll(/\s+/g)) {
|
||||
@@ -347,6 +396,19 @@ function detectSpacedLocalPathLinks(
|
||||
const claimedRanges: [number, number][] = []
|
||||
for (const regex of SPACED_LOCAL_PATH_REGEXES) {
|
||||
for (const range of detectRanges(lineText, regex)) {
|
||||
if (regex === SPACED_PATH_WITH_SEPARATOR_REGEX && !hasSeparatorAfterWhitespace(range.text)) {
|
||||
continue
|
||||
}
|
||||
if (regex === SPACED_PATH_WITH_EXTENSION_REGEX && !hasSpacedPathExtension(range.text)) {
|
||||
continue
|
||||
}
|
||||
if (
|
||||
regex === LINE_ENDING_SPACED_PATH_REGEX &&
|
||||
(!hasInternalWhitespaceBeforeTrimmedEnd(range.text) ||
|
||||
!isAtTrimmedLineEnd(lineText, range.endIndex))
|
||||
) {
|
||||
continue
|
||||
}
|
||||
if (rangesOverlap(range, claimedRanges) || isInsideUriScheme(lineText, range)) {
|
||||
continue
|
||||
}
|
||||
@@ -355,7 +417,9 @@ function detectSpacedLocalPathLinks(
|
||||
? [range, ...buildLineEndingSpacedPathPrefixRanges(range)]
|
||||
: [range]
|
||||
const candidateLinks = candidateRanges
|
||||
.map((candidateRange) => toParsedLink(trimSpacedPathTrailingProse(candidateRange)))
|
||||
.map((candidateRange) =>
|
||||
toParsedLink(trimSpacedPathTrailingProse(trimTrailingWhitespace(candidateRange)))
|
||||
)
|
||||
.filter((link): link is ParsedTerminalFileLink => link !== null)
|
||||
const link = candidateLinks[0]
|
||||
if (link) {
|
||||
|
||||
Reference in New Issue
Block a user