@devries-cameron20 / t3code-1 / commits / 4e791759cf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Address second-pass PR feedback on parser hardening

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Feb 17, 2026 at 21:32 UTC 4e791759cfecaa74319959fe800e0a34c680c442
3 files changed +40 -16
apps/server/src/keybindings.test.ts
+8
@@ -91,6 +91,14 @@ describe("server keybindings", () => {
91 when: "terminalFocus && (",
92 }),
93 );
94 +
95 + assert.isNull(
96 + compileResolvedKeybindingRule({
97 + key: "mod+d",
98 + command: "terminal.split",
99 + when: `${"!".repeat(300)}terminalFocus`,
100 + }),
101 + );
102 });
103
104 it("upserts custom keybindings to ~/.t3/keybindings.json", () => {
apps/server/src/keybindings.ts
+31 -16
@@ -24,6 +24,8 @@ type WhenToken =
24 | { type: "lparen" }
25 | { type: "rparen" };
26
27 +const MAX_WHEN_EXPRESSION_DEPTH = 256;
28 +
29 export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
30 { key: "mod+j", command: "terminal.toggle" },
31 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
@@ -159,7 +161,10 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
161 if (!tokens || tokens.length === 0) return null;
162 let index = 0;
163
162 - const parsePrimary = (): KeybindingWhenNode | null => {
164 + const parsePrimary = (depth: number): KeybindingWhenNode | null => {
165 + if (depth > MAX_WHEN_EXPRESSION_DEPTH) {
166 + return null;
167 + }
168 const token = tokens[index];
169 if (!token) return null;
170
@@ -170,7 +175,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
175
176 if (token.type === "lparen") {
177 index += 1;
173 - const expressionNode = parseOr();
178 + const expressionNode = parseOr(depth + 1);
179 const closeToken = tokens[index];
180 if (!expressionNode || !closeToken || closeToken.type !== "rparen") {
181 return null;
@@ -182,24 +187,34 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
187 return null;
188 };
189
185 - const parseUnary = (): KeybindingWhenNode | null => {
186 - const token = tokens[index];
187 - if (token?.type === "not") {
190 + const parseUnary = (depth: number): KeybindingWhenNode | null => {
191 + let notCount = 0;
192 + while (tokens[index]?.type === "not") {
193 index += 1;
189 - const node = parseUnary();
190 - if (!node) return null;
191 - return { type: "not", node };
194 + notCount += 1;
195 + if (notCount > MAX_WHEN_EXPRESSION_DEPTH) {
196 + return null;
197 + }
198 }
193 - return parsePrimary();
199 +
200 + let node = parsePrimary(depth);
201 + if (!node) return null;
202 +
203 + while (notCount > 0) {
204 + node = { type: "not", node };
205 + notCount -= 1;
206 + }
207 +
208 + return node;
209 };
210
196 - const parseAnd = (): KeybindingWhenNode | null => {
197 - let left = parseUnary();
211 + const parseAnd = (depth: number): KeybindingWhenNode | null => {
212 + let left = parseUnary(depth);
213 if (!left) return null;
214
215 while (tokens[index]?.type === "and") {
216 index += 1;
202 - const right = parseUnary();
217 + const right = parseUnary(depth);
218 if (!right) return null;
219 left = { type: "and", left, right };
220 }
@@ -207,13 +222,13 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
222 return left;
223 };
224
210 - const parseOr = (): KeybindingWhenNode | null => {
211 - let left = parseAnd();
225 + const parseOr = (depth: number): KeybindingWhenNode | null => {
226 + let left = parseAnd(depth);
227 if (!left) return null;
228
229 while (tokens[index]?.type === "or") {
230 index += 1;
216 - const right = parseAnd();
231 + const right = parseAnd(depth);
232 if (!right) return null;
233 left = { type: "or", left, right };
234 }
@@ -221,7 +236,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
236 return left;
237 };
238
224 - const ast = parseOr();
239 + const ast = parseOr(0);
240 if (!ast || index !== tokens.length) return null;
241 return ast;
242 }
apps/web/src/projectScripts.ts
+1
@@ -48,6 +48,7 @@ export function nextProjectScriptId(name: string, existingIds: Iterable<string>)
48 suffix += 1;
49 }
50
51 + // This last-resort fallback only triggers after exhausting thousands of suffixes.
52 return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
53 }
54