mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-21 02:54:25 +00:00
Compare commits
2 Commits
fix/codeql
...
fix/codeql
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b03f265540 | ||
|
|
909af20270 |
1
changelog.d/fixes/20823-codeql-redos-model-ids.md
Normal file
1
changelog.d/fixes/20823-codeql-redos-model-ids.md
Normal file
@@ -0,0 +1 @@
|
||||
- **fix(security):** GitHub Copilot Responses-only inference, model display names and the Context7 library id check use linear regexes, so a long crafted model or library id no longer blocks the event loop for seconds (CodeQL js/polynomial-redos #1325, #1324, #1296)
|
||||
@@ -1 +0,0 @@
|
||||
- **fix(security):** Compression engines bound their regex work on hostile input: the headroom GCF inline-array check and the RTK raw-output command slug run in linear time, and CCR `grep` rejects patterns with more than one unbounded quantifier and stops a scan that exceeds a 1 s time budget (CodeQL #1301, #1302, #1349)
|
||||
@@ -48,7 +48,9 @@ export function isValidContext7LibraryId(id: string): id is string {
|
||||
// Each segment: starts with alphanumeric (GitHub owner/repo convention —
|
||||
// no leading '-'), path-safe chars, no trailing dot, no dot-run.
|
||||
const seg = /^[A-Za-z0-9][\w-]*(?:\.[\w-]+)*$/; // starts alnum, dots only interior, no dot-run
|
||||
const m = /^\/(.+)\/(.+)$/.exec(id);
|
||||
// Segments split on '/' with [^/]+ (linear). `seg` rejects '/' and line terminators, so this
|
||||
// accepts the same ids as the former /^\/(.+)\/(.+)$/, which was O(n^2) on a run of slashes.
|
||||
const m = /^\/([^/]+)\/([^/]+)$/.exec(id);
|
||||
return m !== null && seg.test(m[1]) && seg.test(m[2]);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,9 @@
|
||||
*
|
||||
* `full`/absent → whole text (backward-compat). All modes are fail-safe:
|
||||
* incoherent params or an unsafe regex yield { error }, never throw. The grep
|
||||
* mode validates the pattern with `safe-regex` (no nested quantifiers), allows at
|
||||
* most one unbounded quantifier, bounds matches/pattern length, and runs the scan
|
||||
* under a wall-clock budget (CodeQL #1349): a caller-supplied pattern can still be
|
||||
* super-linear (`.*x` is quadratic per line), so the budget is the hard bound.
|
||||
* mode validates the pattern with `safe-regex` (no catastrophic backtracking)
|
||||
* before compiling, and bounds matches/pattern length.
|
||||
*/
|
||||
import vm from "node:vm";
|
||||
import safeRegex from "safe-regex";
|
||||
|
||||
export type CcrMode = "full" | "head" | "tail" | "lines" | "grep" | "stats";
|
||||
@@ -27,8 +24,6 @@ export type CcrQueryResult = { content: string } | { error: string };
|
||||
export const MAX_RANGE_LINES = 10_000;
|
||||
export const MAX_GREP_MATCHES = 1_000;
|
||||
export const MAX_PATTERN_LEN = 512;
|
||||
/** Wall-clock budget for one grep scan over a block (ms). */
|
||||
export const GREP_TIME_BUDGET_MS = 1_000;
|
||||
|
||||
const err = (m: string): CcrQueryResult => ({ error: m });
|
||||
const ok = (c: string): CcrQueryResult => ({ content: c });
|
||||
@@ -58,104 +53,30 @@ function sliceLines(lines: string[], start?: number, end?: number): CcrQueryResu
|
||||
return ok(lines.slice(start - 1, Math.min(end, lines.length)).join("\n"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of unbounded quantifiers (`*`, `+`, `{n,}`) outside character classes and
|
||||
* escapes. Two or more in sequence make the backtracking polynomial in the line length
|
||||
* (`.*.*.*.*x` is O(n^4)), which `safe-regex`'s star-height check does not catch.
|
||||
*/
|
||||
export function countUnboundedQuantifiers(pattern: string): number {
|
||||
let count = 0;
|
||||
let i = 0;
|
||||
while (i < pattern.length) {
|
||||
const c = pattern[i];
|
||||
if (c === "\\") {
|
||||
i += 2;
|
||||
} else if (c === "[") {
|
||||
i = classEnd(pattern, i);
|
||||
} else {
|
||||
if (c === "*" || c === "+" || (c === "{" && isOpenRange(pattern, i))) count++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/** Index just past the character class that opens at `start` (escapes honoured). */
|
||||
function classEnd(pattern: string, start: number): number {
|
||||
let i = start + 1;
|
||||
while (i < pattern.length && pattern[i] !== "]") i += pattern[i] === "\\" ? 2 : 1;
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
/** True when an open range quantifier `{n,}` starts at `start`. */
|
||||
function isOpenRange(pattern: string, start: number): boolean {
|
||||
let j = start + 1;
|
||||
while (j < pattern.length && pattern[j] >= "0" && pattern[j] <= "9") j++;
|
||||
return j > start + 1 && pattern[j] === "," && pattern[j + 1] === "}";
|
||||
}
|
||||
|
||||
// The scan runs in a dedicated context only so `timeout` can stop a runaway regex.
|
||||
// No host object enters it: inputs are primitives, the result leaves as a JSON string.
|
||||
const GREP_SCRIPT = new vm.Script(`(() => {
|
||||
"use strict";
|
||||
let re;
|
||||
try { re = new RegExp(grepPattern); } catch { return JSON.stringify({ invalid: true }); }
|
||||
const matched = [];
|
||||
let truncated = false;
|
||||
for (const line of grepText.split("\\n")) {
|
||||
if (re.test(line)) {
|
||||
matched.push(line);
|
||||
if (matched.length >= grepMaxMatches) { truncated = true; break; }
|
||||
}
|
||||
}
|
||||
return JSON.stringify({ matched, truncated });
|
||||
})()`);
|
||||
let grepContext: vm.Context | null = null;
|
||||
|
||||
function runGrepScan(
|
||||
text: string,
|
||||
pattern: string
|
||||
): { matched: string[]; truncated: boolean } | "invalid" | "timeout" {
|
||||
grepContext ??= vm.createContext(Object.create(null), {
|
||||
codeGeneration: { strings: false, wasm: false },
|
||||
});
|
||||
grepContext.grepPattern = pattern;
|
||||
grepContext.grepText = text;
|
||||
grepContext.grepMaxMatches = MAX_GREP_MATCHES;
|
||||
let raw: unknown;
|
||||
try {
|
||||
raw = GREP_SCRIPT.runInContext(grepContext, { timeout: GREP_TIME_BUDGET_MS });
|
||||
} catch (e) {
|
||||
if ((e as { code?: string })?.code === "ERR_SCRIPT_EXECUTION_TIMEOUT") return "timeout";
|
||||
throw e;
|
||||
} finally {
|
||||
grepContext.grepText = "";
|
||||
grepContext.grepPattern = "";
|
||||
}
|
||||
const parsed = JSON.parse(String(raw)) as {
|
||||
invalid?: boolean;
|
||||
matched?: string[];
|
||||
truncated?: boolean;
|
||||
};
|
||||
if (parsed.invalid === true) return "invalid";
|
||||
return { matched: parsed.matched ?? [], truncated: parsed.truncated === true };
|
||||
}
|
||||
|
||||
function grepLines(text: string, pattern?: string, unique?: boolean): CcrQueryResult {
|
||||
function grepLines(lines: string[], pattern?: string, unique?: boolean): CcrQueryResult {
|
||||
if (!pattern) return err("grep requires a 'pattern'");
|
||||
if (pattern.length > MAX_PATTERN_LEN) return err(`pattern exceeds ${MAX_PATTERN_LEN} chars`);
|
||||
if (!safeRegex(pattern)) return err("pattern rejected: potentially catastrophic backtracking");
|
||||
if (countUnboundedQuantifiers(pattern) > 1) {
|
||||
return err("pattern rejected: at most one unbounded quantifier (*, +, {n,}) is allowed");
|
||||
let re: RegExp;
|
||||
try {
|
||||
re = new RegExp(pattern);
|
||||
} catch {
|
||||
return err("invalid regex pattern");
|
||||
}
|
||||
const scan = runGrepScan(text, pattern);
|
||||
if (scan === "invalid") return err("invalid regex pattern");
|
||||
if (scan === "timeout") {
|
||||
return err(`grep exceeded its ${GREP_TIME_BUDGET_MS} ms time budget; use a narrower pattern`);
|
||||
const matched: string[] = [];
|
||||
let truncated = false;
|
||||
for (const line of lines) {
|
||||
if (re.test(line)) {
|
||||
matched.push(line);
|
||||
if (matched.length >= MAX_GREP_MATCHES) {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
const out = unique ? [...new Set(scan.matched)] : scan.matched;
|
||||
const out = unique ? [...new Set(matched)] : matched;
|
||||
const body = out.join("\n");
|
||||
return ok(scan.truncated ? `${body}\n…[truncated at ${MAX_GREP_MATCHES} matches]` : body);
|
||||
return ok(truncated ? `${body}\n…[truncated at ${MAX_GREP_MATCHES} matches]` : body);
|
||||
}
|
||||
|
||||
function blockStats(text: string, lines: string[]): CcrQueryResult {
|
||||
@@ -180,7 +101,7 @@ export function queryBlock(text: string, q: CcrQuery): CcrQueryResult {
|
||||
case "lines":
|
||||
return sliceLines(lines, q.start, q.end);
|
||||
case "grep":
|
||||
return grepLines(text, q.pattern, q.unique);
|
||||
return grepLines(lines, q.pattern, q.unique);
|
||||
case "stats":
|
||||
return blockStats(text, lines);
|
||||
default:
|
||||
|
||||
@@ -10,9 +10,7 @@
|
||||
|
||||
const JSON_NUMBER_RE = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
||||
const NUMERIC_LIKE_RE = /^[+-]\.?\d|^\.\d|^0\d/;
|
||||
// `[` excluded from the bracket body keeps the scan linear on runs of `[` (CodeQL #1301).
|
||||
// Same verdict for .test(): any match can restart at the last `[` before its `]`.
|
||||
const INLINE_ARRAY_RE = /\[[^[\]]*\]\s*:/;
|
||||
const INLINE_ARRAY_RE = /\[[^\]]*\]\s*:/;
|
||||
|
||||
/** Check if a string value must be quoted per Section 2.4. */
|
||||
export function needsQuote(s: string): boolean {
|
||||
|
||||
@@ -106,10 +106,7 @@ export function maybePersistRtkRawOutput(
|
||||
const maxBytes = Math.max(1024, Math.floor(options.maxBytes ?? 1_048_576));
|
||||
const redaction = redactRtkRawOutput(safeUtf8Slice(raw, maxBytes));
|
||||
const now = Date.now();
|
||||
// Bound the client-supplied command before the regex passes (CodeQL #1302): the slug
|
||||
// keeps at most 48 chars, so 256 raw chars leave the result unchanged for real commands.
|
||||
const commandSlug = (options.command || "tool-output")
|
||||
.slice(0, 256)
|
||||
.replace(/[^A-Za-z0-9_-]+/g, "_")
|
||||
.replace(/^_+|_+$/g, "")
|
||||
.slice(0, 48);
|
||||
|
||||
@@ -46,8 +46,11 @@ const DASH_VERSION_BRANDS = new Set(["gpt"]);
|
||||
/** Release-date stamps at the end of ids ("claude-3-5-sonnet-20241022"). */
|
||||
const DATE_TOKEN = /^(19|20)\d{6}$/;
|
||||
|
||||
/** "v4.1" / "V2.6" — an explicit version marker. */
|
||||
const VERSION_MARKER = /^v\d+([.\d]*)$/i;
|
||||
/**
|
||||
* "v4.1" / "V2.6" — an explicit version marker. One leading digit, then digits and dots: the
|
||||
* same language as /^v\d+[.\d]*$/ without the overlapping quantifiers that made it O(n^2).
|
||||
*/
|
||||
const VERSION_MARKER = /^v\d[.\d]*$/i;
|
||||
|
||||
/** Digits left over after the brand prefix ("qwen3.8" -> "qwen" + "3.8"). */
|
||||
const BRANDED_VERSION = /^([A-Za-z]+)(\d[.\d]*)$/;
|
||||
|
||||
@@ -19,9 +19,19 @@
|
||||
* them.
|
||||
*/
|
||||
|
||||
const GITHUB_RESPONSES_ONLY_GPT = /^(?:.*codex.*|gpt-(?:[6-9]|5\.[5-9]).*)$/i;
|
||||
// Split from one /^(?:.*codex.*|gpt-(?:[6-9]|5\.[5-9]).*)$/i, which backtracked in O(n^2) on a
|
||||
// long client model id (CodeQL js/polynomial-redos). Same accept set: that pattern's `.` never
|
||||
// matched a line terminator, so an id holding one stays rejected.
|
||||
const CODEX_FAMILY = /codex/i;
|
||||
const RESPONSES_ONLY_GPT_PREFIX = /^gpt-(?:[6-9]|5\.[5-9])/i;
|
||||
const LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
|
||||
const NON_RESPONSES_FAMILY = /claude|gemini/i;
|
||||
|
||||
function isResponsesOnlyModelId(modelId: string): boolean {
|
||||
if (LINE_TERMINATOR.test(modelId)) return false;
|
||||
return CODEX_FAMILY.test(modelId) || RESPONSES_ONLY_GPT_PREFIX.test(modelId);
|
||||
}
|
||||
|
||||
export function inferGithubUnsyncedTargetFormat(
|
||||
providerId: string,
|
||||
modelId: string
|
||||
@@ -30,7 +40,7 @@ export function inferGithubUnsyncedTargetFormat(
|
||||
return undefined;
|
||||
}
|
||||
const bareModelId = modelId.replace(/^(?:github|gh|ghe-copilot)\//i, "");
|
||||
if (!GITHUB_RESPONSES_ONLY_GPT.test(bareModelId) || NON_RESPONSES_FAMILY.test(bareModelId)) {
|
||||
if (!isResponsesOnlyModelId(bareModelId) || NON_RESPONSES_FAMILY.test(bareModelId)) {
|
||||
return undefined;
|
||||
}
|
||||
return "openai-responses";
|
||||
|
||||
@@ -1,219 +0,0 @@
|
||||
/**
|
||||
* Regex work in the compression engines stays bounded on hostile input
|
||||
* (CodeQL #1301, #1302, #1349).
|
||||
*
|
||||
* - #1301 headroom/gcf needsQuote(): the inline-array check ran in O(n^2) on runs of `[`.
|
||||
* - #1302 rtk maybePersistRtkRawOutput(): the command slug trim ran in O(n^2) on runs of `_`
|
||||
* from a client-supplied tool_use command of unbounded length.
|
||||
* - #1349 ccr grep: a caller-supplied pattern such as `.*.*.*.*x` passed the star-height
|
||||
* gate and blocked the event loop for minutes on a single long line.
|
||||
*
|
||||
* Each block pairs a timing assertion (the pathological input finishes fast or is refused)
|
||||
* with a behaviour assertion (ordinary input gives the same result as before).
|
||||
*/
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { needsQuote } from "../../../open-sse/services/compression/engines/headroom/gcf/scalar.ts";
|
||||
import { maybePersistRtkRawOutput } from "../../../open-sse/services/compression/engines/rtk/rawOutput.ts";
|
||||
import { queryBlock } from "../../../open-sse/services/compression/engines/ccr/ccrQuery.ts";
|
||||
|
||||
/** Ceiling for the fixed paths. They run in a few ms when idle; before the fix the same
|
||||
* inputs took 10 s (#1301), 21 s (#1302) and minutes (#1349). The headroom absorbs a loaded
|
||||
* CI runner (one fixed case reached 806 ms at load average 44) without hiding a regression. */
|
||||
const FAST_MS = 1_000;
|
||||
/** Upper bound for a grep stopped by its 1 s time budget (budget plus scheduling slack). */
|
||||
const BUDGET_CEILING_MS = 5_000;
|
||||
|
||||
function timed<T>(fn: () => T): { value: T; ms: number } {
|
||||
const t0 = performance.now();
|
||||
const value = fn();
|
||||
return { value, ms: performance.now() - t0 };
|
||||
}
|
||||
|
||||
/** Reference semantics of the SPEC 2.4 inline-array rule: some `[` whose first following
|
||||
* `]` is followed by optional whitespace and a `:`. Written as a plain linear scan. */
|
||||
function hasInlineArrayHeader(s: string): boolean {
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (s[i] !== "[") continue;
|
||||
const close = s.indexOf("]", i + 1);
|
||||
if (close === -1) return false;
|
||||
let k = close + 1;
|
||||
while (k < s.length && /\s/.test(s[k])) k++;
|
||||
if (s[k] === ":") return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Deterministic PRNG (mulberry32) so the equivalence sample is reproducible. */
|
||||
function prng(seed: number): () => number {
|
||||
let a = seed >>> 0;
|
||||
return () => {
|
||||
a = (a + 0x6d2b79f5) >>> 0;
|
||||
let t = a;
|
||||
t = Math.imul(t ^ (t >>> 15), t | 1);
|
||||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
describe("#1301 headroom/gcf needsQuote inline-array check", () => {
|
||||
it("finishes fast on 50k '[' characters", () => {
|
||||
for (const s of [
|
||||
"[".repeat(50_000),
|
||||
"a" + "[".repeat(50_000) + "]",
|
||||
"[".repeat(50_000) + "]x",
|
||||
]) {
|
||||
const { value, ms } = timed(() => needsQuote(s));
|
||||
assert.equal(value, false);
|
||||
assert.ok(ms < FAST_MS, `needsQuote took ${ms.toFixed(1)}ms on ${s.length} chars`);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the same verdict on ordinary values", () => {
|
||||
assert.equal(needsQuote("ERR[404]: Not Found"), true);
|
||||
assert.equal(needsQuote("[Speaker 1]: Hello"), true);
|
||||
assert.equal(needsQuote("a[[b]: c"), true);
|
||||
assert.equal(needsQuote("x[a[b] :y"), true);
|
||||
assert.equal(needsQuote("arr[0] index"), false);
|
||||
assert.equal(needsQuote("a]: [b"), false);
|
||||
assert.equal(needsQuote("a[b] c: d"), false);
|
||||
});
|
||||
|
||||
it("matches the reference rule on a random sample", () => {
|
||||
const rand = prng(1301);
|
||||
const alphabet = "[]: ab";
|
||||
for (let n = 0; n < 20_000; n++) {
|
||||
const len = Math.floor(rand() * 12);
|
||||
let body = "";
|
||||
for (let i = 0; i < len; i++) body += alphabet[Math.floor(rand() * alphabet.length)];
|
||||
// Leading/trailing 'a' keeps the other needsQuote rules (edge space, numeric, '#'…)
|
||||
// out of the way, so the verdict depends only on the inline-array rule.
|
||||
const s = `a${body}a`;
|
||||
assert.equal(needsQuote(s), hasInlineArrayHeader(s), JSON.stringify(s));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("#1302 rtk raw-output command slug", () => {
|
||||
function persist(command: string) {
|
||||
const base = process.env.DATA_DIR || os.tmpdir();
|
||||
const dataDir = fs.mkdtempSync(path.join(base, "rtk-slug-"));
|
||||
const previous = process.env.DATA_DIR;
|
||||
process.env.DATA_DIR = dataDir;
|
||||
try {
|
||||
return timed(() =>
|
||||
maybePersistRtkRawOutput("error: build failed\nline 2", { retention: "always", command })
|
||||
);
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.DATA_DIR;
|
||||
else process.env.DATA_DIR = previous;
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function slugOf(pointerPath: string): string {
|
||||
const match = /^\d+-(.*)-[0-9a-f]{24}\.log$/.exec(path.basename(pointerPath));
|
||||
assert.ok(match, pointerPath);
|
||||
return match[1];
|
||||
}
|
||||
|
||||
it("finishes fast on a 50k-underscore command", () => {
|
||||
for (const command of ["_".repeat(50_000) + "x", "x" + "_".repeat(50_000) + "x"]) {
|
||||
const { value, ms } = persist(command);
|
||||
assert.ok(value, "pointer expected");
|
||||
assert.ok(ms < FAST_MS, `slug took ${ms.toFixed(1)}ms on ${command.length} chars`);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the same slug for ordinary commands", () => {
|
||||
const cases: Array<[string, string]> = [
|
||||
["git status --short", "git_status_--short"],
|
||||
[" npm test -- --watch=false ", "npm_test_--_--watch_false"],
|
||||
["__init__", "init"],
|
||||
["cargo build --release && ./target/release/app", "cargo_build_--release_target_release_app"],
|
||||
["a".repeat(100), "a".repeat(48)],
|
||||
];
|
||||
for (const [command, expected] of cases) {
|
||||
const { value } = persist(command);
|
||||
assert.ok(value);
|
||||
assert.equal(slugOf(value.path), expected, command);
|
||||
}
|
||||
const { value } = persist("");
|
||||
assert.ok(value);
|
||||
assert.equal(slugOf(value.path), "tool-output");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#1349 ccr grep pattern", () => {
|
||||
it("refuses a multi-quantifier pattern on a 500+ char line quickly", () => {
|
||||
const text = "a".repeat(600);
|
||||
const { value, ms } = timed(() => queryBlock(text, { mode: "grep", pattern: ".*.*.*.*x" }));
|
||||
assert.ok("error" in value, JSON.stringify(value));
|
||||
assert.ok(ms < FAST_MS, `grep took ${ms.toFixed(1)}ms`);
|
||||
});
|
||||
|
||||
it("stops a single-quantifier pattern that exceeds the time budget", () => {
|
||||
// 2 MiB of 4096-char lines: `.*x` is quadratic per line (minutes before the fix).
|
||||
const text = Array.from({ length: 512 }, () => "a".repeat(4095)).join("\n");
|
||||
const { value, ms } = timed(() => queryBlock(text, { mode: "grep", pattern: ".*x" }));
|
||||
assert.ok("error" in value, JSON.stringify(value).slice(0, 200));
|
||||
assert.match((value as { error: string }).error, /time budget/);
|
||||
assert.ok(ms < BUDGET_CEILING_MS, `grep took ${ms.toFixed(1)}ms`);
|
||||
});
|
||||
|
||||
it("counts only unbounded quantifiers outside classes and escapes", () => {
|
||||
const tooMany = ["a.*b.*c", "\\d+\\.\\d+", "x{2,}y+", "a.*?b.+?c"];
|
||||
for (const pattern of tooMany) {
|
||||
const r = queryBlock("abc", { mode: "grep", pattern });
|
||||
assert.ok("error" in r, pattern);
|
||||
assert.match((r as { error: string }).error, /unbounded quantifier/, pattern);
|
||||
}
|
||||
const text = ["l1 ERROR upstream timeout", "l2 *+ literal", "l3 {2,} braces", "l4 ok"].join(
|
||||
"\n"
|
||||
);
|
||||
assert.deepEqual(queryBlock(text, { mode: "grep", pattern: "ERROR.*timeout" }), {
|
||||
content: "l1 ERROR upstream timeout",
|
||||
});
|
||||
assert.deepEqual(queryBlock(text, { mode: "grep", pattern: "[*+]+ literal" }), {
|
||||
content: "l2 *+ literal",
|
||||
});
|
||||
assert.deepEqual(queryBlock(text, { mode: "grep", pattern: "\\*\\+ lit.*" }), {
|
||||
content: "l2 *+ literal",
|
||||
});
|
||||
assert.deepEqual(queryBlock(text, { mode: "grep", pattern: "\\{2,\\} b.*s" }), {
|
||||
content: "l3 {2,} braces",
|
||||
});
|
||||
assert.deepEqual(queryBlock(text, { mode: "grep", pattern: "l[0-9]{1,2} (ok|ERROR)" }), {
|
||||
content: "l1 ERROR upstream timeout\nl4 ok",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps grep results unchanged on an ordinary log block", () => {
|
||||
const lines = Array.from(
|
||||
{ length: 5_000 },
|
||||
(_, i) => `2026-11-20T10:00:${String(i % 60).padStart(2, "0")}Z INFO req=${i} status=200`
|
||||
);
|
||||
lines[1234] = "2026-11-20T10:00:34Z ERROR req=1234 upstream timeout";
|
||||
lines[4321] = "2026-11-20T10:00:01Z WARN req=4321 slow upstream";
|
||||
const text = lines.join("\n");
|
||||
const expected = (re: RegExp) => lines.filter((l) => re.test(l)).join("\n");
|
||||
for (const pattern of ["ERROR", "(WARN|ERROR) req=", "req=12\\d\\d ", "^\\S+ WARN"]) {
|
||||
assert.deepEqual(
|
||||
queryBlock(text, { mode: "grep", pattern }),
|
||||
{ content: expected(new RegExp(pattern)) },
|
||||
pattern
|
||||
);
|
||||
}
|
||||
assert.deepEqual(queryBlock(text, { mode: "grep", pattern: "INFO", unique: true }), {
|
||||
content: lines
|
||||
.filter((l) => l.includes("INFO"))
|
||||
.slice(0, 1000)
|
||||
.join("\n")
|
||||
.concat("\n…[truncated at 1000 matches]"),
|
||||
});
|
||||
});
|
||||
});
|
||||
197
tests/unit/model-id-regex-redos.test.ts
Normal file
197
tests/unit/model-id-regex-redos.test.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
// CodeQL js/polynomial-redos #1325, #1324, #1296: three model/library id regexes backtracked in
|
||||
// O(n^2) on crafted ids. Each id reaches its regex without a length cap (a client model id
|
||||
// "gh/<id>", an upstream /v1/models id, a raw Context7 search item id).
|
||||
//
|
||||
// Two checks per function:
|
||||
// - a 50k-char pathological id returns fast (measured on the exported function);
|
||||
// - a truth table generated by running the pre-fix functions (git show of the base) stays the
|
||||
// same, so the linear rewrites keep the exact accept/reject behavior.
|
||||
|
||||
const { inferGithubUnsyncedTargetFormat } =
|
||||
await import("../../src/sse/services/githubResponsesFallback.ts");
|
||||
const { modelDisplayName } = await import("../../src/shared/utils/modelDisplayName.ts");
|
||||
const { isValidContext7LibraryId } = await import("../../open-sse/executors/context7-fetch.ts");
|
||||
|
||||
const PATHOLOGICAL_LENGTH = 50_000;
|
||||
const BUDGET_MS = 200;
|
||||
|
||||
function elapsedMs(fn: () => unknown): number {
|
||||
const start = performance.now();
|
||||
fn();
|
||||
return performance.now() - start;
|
||||
}
|
||||
|
||||
const GITHUB_TABLE: Array<[string, string, string | null]> = [
|
||||
["gh", "gpt-6", "openai-responses"],
|
||||
["gh", "gpt-6.1-sol", "openai-responses"],
|
||||
["gh", "GPT-7-mini", "openai-responses"],
|
||||
["gh", "gpt-9", "openai-responses"],
|
||||
["gh", "gpt-60", "openai-responses"],
|
||||
["gh", "gpt-5.5", "openai-responses"],
|
||||
["gh", "gpt-5.9-codex", "openai-responses"],
|
||||
["gh", "GPT-5.5", "openai-responses"],
|
||||
["gh", "gpt-5.4", null],
|
||||
["gh", "gpt-5", null],
|
||||
["gh", "gpt-5.10", null],
|
||||
["gh", "gpt-5-codex", "openai-responses"],
|
||||
["gh", "gpt-4o", null],
|
||||
["gh", "gpt-4.1", null],
|
||||
["gh", "gpt-10", null],
|
||||
["gh", "gpt-", null],
|
||||
["gh", "gpt_6", null],
|
||||
["gh", "agpt-6", null],
|
||||
["gh", " gpt-6", null],
|
||||
["gh", "gpt-6 ", "openai-responses"],
|
||||
["gh", "codex-mini", "openai-responses"],
|
||||
["gh", "my-CODEX", "openai-responses"],
|
||||
["gh", "codex", "openai-responses"],
|
||||
["gh", "Codex-Max", "openai-responses"],
|
||||
["gh", "code-x", null],
|
||||
["gh", "claude-codex", null],
|
||||
["gh", "gemini-codex", null],
|
||||
["gh", "gpt-6-claude", null],
|
||||
["gh", "o3", null],
|
||||
["gh", "", null],
|
||||
["gh", "gpt-6\n", null],
|
||||
["gh", "gpt-6\nx", null],
|
||||
["gh", "\ngpt-6", null],
|
||||
["gh", "codex\r", null],
|
||||
["gh", "gpt-6\u2028x", null],
|
||||
["gh", "x\u2029codex", null],
|
||||
["gh", "codex\t", "openai-responses"],
|
||||
["gh", "gpt-6\u0085", "openai-responses"],
|
||||
["gh", "gh/gpt-6", "openai-responses"],
|
||||
["gh", "github/gpt-6.1", "openai-responses"],
|
||||
["gh", "GH/codex", "openai-responses"],
|
||||
["gh", "ghe-copilot/gpt-5.5", "openai-responses"],
|
||||
["github", "gpt-6", "openai-responses"],
|
||||
["ghe-copilot", "gpt-5.6-codex", "openai-responses"],
|
||||
["github", "claude-opus-5-5", null],
|
||||
["openai", "gpt-6", null],
|
||||
["codex", "codex-mini", null],
|
||||
["gh", "openai/gpt-6", null],
|
||||
["gh", "x/codex", "openai-responses"],
|
||||
];
|
||||
|
||||
const DISPLAY_TABLE: Array<[string, string]> = [
|
||||
["deepseek-v4.1-flash", "DeepSeek V4.1 Flash"],
|
||||
["gpt-v2", "GPT-V2"],
|
||||
["gpt-V2.6", "GPT-V2.6"],
|
||||
["gpt-v", "GPT V"],
|
||||
["gpt-v.", "GPT V."],
|
||||
["gpt-v.1", "GPT V.1"],
|
||||
["gpt-v1.", "GPT-V1."],
|
||||
["gpt-v1..2", "GPT-V1..2"],
|
||||
["gpt-v1x", "GPT V1x"],
|
||||
["gpt-vx1", "GPT Vx1"],
|
||||
["gpt-v12.3.4", "GPT-V12.3.4"],
|
||||
["gpt-v1-turbo", "GPT-V1 Turbo"],
|
||||
["gpt-vv1", "GPT Vv1"],
|
||||
["gpt-4-v1", "GPT-4 V1"],
|
||||
["foo-v2.6-bar", "Foo V2.6 Bar"],
|
||||
["v1", "V1"],
|
||||
["v", "V"],
|
||||
["mistral-v0.1", "Mistral V0.1"],
|
||||
["model-V2.6", "Model V2.6"],
|
||||
["gpt-6.1-sol", "GPT-6.1 Sol"],
|
||||
["claude-opus-5-5", "Claude Opus 5.5"],
|
||||
["glm-5.3-flash", "GLM 5.3 Flash"],
|
||||
["claude-3-5-sonnet-20241022", "Claude 3.5 Sonnet"],
|
||||
["qwen3.8", "Qwen 3.8"],
|
||||
["cmc/deepseek/deepseek-v4.1-flash", "DeepSeek V4.1 Flash"],
|
||||
["gpt-v1 x", "GPT-V1 X"],
|
||||
["", ""],
|
||||
];
|
||||
|
||||
const CONTEXT7_TABLE: Array<[string, boolean]> = [
|
||||
["/vercel/next.js", true],
|
||||
["/upstash/context7", true],
|
||||
["/a/b", true],
|
||||
["/a.b/c-d_e", true],
|
||||
["/a/b/c", false],
|
||||
["//a", false],
|
||||
["/a//b", false],
|
||||
["/a/", false],
|
||||
["a/b", false],
|
||||
["/a/b/", false],
|
||||
["/a/b\n", false],
|
||||
["/a\n/b", false],
|
||||
["/\n/b", false],
|
||||
["/a/\n", false],
|
||||
["/a/b\r", false],
|
||||
["/a/b\u2028", false],
|
||||
["/a\u2029/b", false],
|
||||
["/./b", false],
|
||||
["/a/..", false],
|
||||
["/a/b..c", false],
|
||||
["/-a/b", false],
|
||||
["/a/b.", false],
|
||||
["/a/.b", false],
|
||||
["", false],
|
||||
["/", false],
|
||||
["//", false],
|
||||
["///", false],
|
||||
["/a", false],
|
||||
["/a/b c", false],
|
||||
["/A1/B2", true],
|
||||
["/_a/b", false],
|
||||
];
|
||||
|
||||
test("#1325 GitHub Responses-only inference is linear on a 'codex' run ending in a line break", () => {
|
||||
const id = "codex".repeat(PATHOLOGICAL_LENGTH / 5) + "\n!";
|
||||
let result: unknown = "unset";
|
||||
const ms = elapsedMs(() => {
|
||||
result = inferGithubUnsyncedTargetFormat("gh", id);
|
||||
});
|
||||
assert.equal(result, undefined);
|
||||
assert.ok(ms < BUDGET_MS, `took ${ms.toFixed(1)}ms`);
|
||||
});
|
||||
|
||||
test("#1325 GitHub Responses-only inference keeps the pre-fix truth table", () => {
|
||||
for (const [providerId, modelId, expected] of GITHUB_TABLE) {
|
||||
assert.equal(
|
||||
inferGithubUnsyncedTargetFormat(providerId, modelId) ?? null,
|
||||
expected,
|
||||
JSON.stringify([providerId, modelId])
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("#1324 model display name is linear on a long version-marker token", () => {
|
||||
const id = "gpt-v" + "0".repeat(PATHOLOGICAL_LENGTH - 6) + "x";
|
||||
let result = "";
|
||||
const ms = elapsedMs(() => {
|
||||
result = modelDisplayName(id);
|
||||
});
|
||||
assert.ok(result.startsWith("GPT V0"));
|
||||
assert.ok(ms < BUDGET_MS, `took ${ms.toFixed(1)}ms`);
|
||||
});
|
||||
|
||||
test("#1324 model display name keeps the pre-fix truth table", () => {
|
||||
for (const [id, expected] of DISPLAY_TABLE) {
|
||||
assert.equal(modelDisplayName(id), expected, JSON.stringify(id));
|
||||
}
|
||||
});
|
||||
|
||||
test("#1296 Context7 library id check is linear on a run of slashes ending in a line break", () => {
|
||||
for (const id of [
|
||||
"/".repeat(PATHOLOGICAL_LENGTH - 1) + "\n",
|
||||
"/a".repeat(PATHOLOGICAL_LENGTH / 2 - 1) + "\n",
|
||||
]) {
|
||||
let result: unknown = "unset";
|
||||
const ms = elapsedMs(() => {
|
||||
result = isValidContext7LibraryId(id);
|
||||
});
|
||||
assert.equal(result, false);
|
||||
assert.ok(ms < BUDGET_MS, `took ${ms.toFixed(1)}ms`);
|
||||
}
|
||||
});
|
||||
|
||||
test("#1296 Context7 library id check keeps the pre-fix truth table", () => {
|
||||
for (const [id, expected] of CONTEXT7_TABLE) {
|
||||
assert.equal(isValidContext7LibraryId(id), expected, JSON.stringify(id));
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user