Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Address second-pass PR feedback on parser hardening

Julius Marminge4e791759cfec
diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.ts
index 66d90a519..3bd533da9 100644
--- a/apps/server/src/keybindings.test.ts
+++ b/apps/server/src/keybindings.test.ts
@@ -91,6 +91,14 @@ describe("server keybindings", () => {
when: "terminalFocus && (",
}),
);
+
+ assert.isNull(
+ compileResolvedKeybindingRule({
+ key: "mod+d",
+ command: "terminal.split",
+ when: `${"!".repeat(300)}terminalFocus`,
+ }),
+ );
});
it("upserts custom keybindings to ~/.t3/keybindings.json", () => {
diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.ts
index 5b1a1b473..edbb08794 100644
--- a/apps/server/src/keybindings.ts
+++ b/apps/server/src/keybindings.ts
@@ -24,6 +24,8 @@ type WhenToken =
| { type: "lparen" }
| { type: "rparen" };
+const MAX_WHEN_EXPRESSION_DEPTH = 256;
+
export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
{ key: "mod+j", command: "terminal.toggle" },
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
@@ -159,7 +161,10 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
if (!tokens || tokens.length === 0) return null;
let index = 0;
- const parsePrimary = (): KeybindingWhenNode | null => {
+ const parsePrimary = (depth: number): KeybindingWhenNode | null => {
+ if (depth > MAX_WHEN_EXPRESSION_DEPTH) {
+ return null;
+ }
const token = tokens[index];
if (!token) return null;
@@ -170,7 +175,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
if (token.type === "lparen") {
index += 1;
- const expressionNode = parseOr();
+ const expressionNode = parseOr(depth + 1);
const closeToken = tokens[index];
if (!expressionNode || !closeToken || closeToken.type !== "rparen") {
return null;
@@ -182,24 +187,34 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
return null;
};
- const parseUnary = (): KeybindingWhenNode | null => {
- const token = tokens[index];
- if (token?.type === "not") {
+ const parseUnary = (depth: number): KeybindingWhenNode | null => {
+ let notCount = 0;
+ while (tokens[index]?.type === "not") {
index += 1;
- const node = parseUnary();
- if (!node) return null;
- return { type: "not", node };
+ notCount += 1;
+ if (notCount > MAX_WHEN_EXPRESSION_DEPTH) {
+ return null;
+ }
}
- return parsePrimary();
+
+ let node = parsePrimary(depth);
+ if (!node) return null;
+
+ while (notCount > 0) {
+ node = { type: "not", node };
+ notCount -= 1;
+ }
+
+ return node;
};
- const parseAnd = (): KeybindingWhenNode | null => {
- let left = parseUnary();
+ const parseAnd = (depth: number): KeybindingWhenNode | null => {
+ let left = parseUnary(depth);
if (!left) return null;
while (tokens[index]?.type === "and") {
index += 1;
- const right = parseUnary();
+ const right = parseUnary(depth);
if (!right) return null;
left = { type: "and", left, right };
}
@@ -207,13 +222,13 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
return left;
};
- const parseOr = (): KeybindingWhenNode | null => {
- let left = parseAnd();
+ const parseOr = (depth: number): KeybindingWhenNode | null => {
+ let left = parseAnd(depth);
if (!left) return null;
while (tokens[index]?.type === "or") {
index += 1;
- const right = parseAnd();
+ const right = parseAnd(depth);
if (!right) return null;
left = { type: "or", left, right };
}
@@ -221,7 +236,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
return left;
};
- const ast = parseOr();
+ const ast = parseOr(0);
if (!ast || index !== tokens.length) return null;
return ast;
}
diff --git a/apps/web/src/projectScripts.ts b/apps/web/src/projectScripts.ts
index a71bd3e3b..d093138a6 100644
--- a/apps/web/src/projectScripts.ts
+++ b/apps/web/src/projectScripts.ts
@@ -48,6 +48,7 @@ export function nextProjectScriptId(name: string, existingIds: Iterable<string>)
suffix += 1;
}
+ // This last-resort fallback only triggers after exhausting thousands of suffixes.
return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
}