Skip to content

Commit 160487a

Browse files
author
daewoongoh
committed
fix(task): skip session title in Test mode and sanitize in linear time
The extra title request broke E2E suites that assert exact provider request sequences and changed the task name in the visual baseline. Replace the system-reminder regexes with an indexOf scan to fix the CodeQL ReDoS alert.
1 parent 9ca3fc5 commit 160487a

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ export class PendingActionSettlementError extends Error {
287287
}
288288
}
289289

290+
// Literal, not the vscode enum: specs that mock "vscode" without ExtensionMode would throw on the read.
291+
const EXTENSION_MODE_TEST = 3
292+
290293
export class Task extends EventEmitter<TaskEvents> implements TaskLike {
291294
readonly taskId: string
292295
readonly rootTaskId?: string
@@ -3118,9 +3121,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
31183121
// loop directly, and both must keep their existing names. `say`
31193122
// above persisted the history item, so the conditional write will
31203123
// find it.
3124+
// The extra provider request would break E2E and visual tests that assert
3125+
// exact request sequences and rendered task names, so Test mode skips it.
31213126
if (!this.parentTask && task) {
31223127
const provider = this.providerRef.deref()
3123-
if (provider) {
3128+
if (provider && provider.context.extensionMode !== EXTENSION_MODE_TEST) {
31243129
void generateSessionTitle({
31253130
provider,
31263131
taskId: this.taskId,

‎src/core/task/__tests__/Task.session-title.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ describe("Task#startTask session title trigger", () => {
173173
expect(resolved).toBe(true)
174174
})
175175

176+
it.each([
177+
["Production", 1, true],
178+
["Development", 2, true],
179+
["Test", 3, false],
180+
])("in %s extension mode fires title generation: %s", async (_name, extensionMode, shouldFire) => {
181+
const provider = createMockProvider()
182+
// The double only carries the context members the trigger reads.
183+
Object.assign(provider.context, { extensionMode })
184+
const task = createTestTask(provider)
185+
stubStartup(task)
186+
187+
await getStartTaskAccess(task).startTask("mode gated message")
188+
189+
expect(vi.mocked(generateSessionTitle).mock.calls.length > 0).toBe(shouldFire)
190+
})
191+
176192
it("does not fire for a subtask", async () => {
177193
const provider = createMockProvider()
178194
const parentTask = createTestTask(provider)

‎src/core/task/__tests__/session-title.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@ describe("sanitizeUserMessageForTitle", () => {
6565
["<system-reminder>a</system-reminder>\n<system-reminder>b</system-reminder>real", "real"],
6666
["start <system-reminder>unterminated", "start"],
6767
["<system-reminder>only system</system-reminder>", ""],
68+
["a<system-reminder>x</system-reminder>b<system-reminder>y</system-reminder>c", "abc"],
69+
["keep </system-reminder> stray close", "keep </system-reminder> stray close"],
6870
])("sanitizes %j", (input, expected) => {
6971
expect(sanitizeUserMessageForTitle(input)).toBe(expected)
7072
})
73+
74+
it("handles many unterminated openings in linear time", () => {
75+
const input = "<system-reminder>".repeat(50_000)
76+
const startedAt = performance.now()
77+
78+
expect(sanitizeUserMessageForTitle(input)).toBe("")
79+
expect(performance.now() - startedAt).toBeLessThan(500)
80+
})
7181
})
7282

7383
describe("extractLlmTitle", () => {

‎src/core/task/session-title.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@ export type SessionTitleApplyResult = "applied" | "conflict"
3232

3333
/** Removes system-injected `<system-reminder>` blocks and trims. */
3434
export function sanitizeUserMessageForTitle(raw: string): string {
35-
return (
36-
raw
37-
.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "")
38-
// An unterminated block (e.g. truncated input) would otherwise leak the
39-
// rest of the message as user text; drop everything after its opening.
40-
.replace(/<system-reminder>[\s\S]*$/g, "")
41-
.trim()
42-
)
35+
const open = "<system-reminder>"
36+
const close = "</system-reminder>"
37+
let result = ""
38+
let cursor = 0
39+
// Linear scan instead of a lazy regex: repeated unterminated openings made the regex quadratic.
40+
while (cursor < raw.length) {
41+
const start = raw.indexOf(open, cursor)
42+
if (start === -1) {
43+
result += raw.slice(cursor)
44+
break
45+
}
46+
result += raw.slice(cursor, start)
47+
const end = raw.indexOf(close, start + open.length)
48+
// An unterminated block would otherwise leak the rest of the message as user text; drop it.
49+
if (end === -1) {
50+
break
51+
}
52+
cursor = end + close.length
53+
}
54+
return result.trim()
4355
}
4456

4557
/** First line, `Title:` prefix removed, surrounding quotes removed. */

0 commit comments

Comments
 (0)