Harden keybinding parsing and config resolution
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
c70e2d5adc
commit
be32ce9792
4 files changed
+266
-6
No files matched your search
@@ -402,6 +402,92 @@ describe("WebSocket Server", () => {
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("warns and ignores keybindings with malformed when expressions", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const fakeHome = makeTempDir("t3code-home-invalid-when-");
|
||||
const configDir = path.join(fakeHome, ".t3");
|
||||
fs.mkdirSync(configDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(configDir, "keybindings.json"),
|
||||
JSON.stringify([{ key: "mod+j", command: "terminal.toggle", when: "terminalFocus && (" }]),
|
||||
"utf8",
|
||||
);
|
||||
vi.spyOn(os, "homedir").mockReturnValue(fakeHome);
|
||||
|
||||
server = createTestServer({ cwd: "/my/workspace" });
|
||||
await server.start();
|
||||
const addr = server.httpServer.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
connections.push(ws);
|
||||
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(response.error).toBeUndefined();
|
||||
expect(response.result).toEqual({
|
||||
cwd: "/my/workspace",
|
||||
keybindings: [
|
||||
{ key: "mod+j", command: "terminal.toggle" },
|
||||
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
|
||||
{ key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
|
||||
{ key: "mod+shift+o", command: "chat.new" },
|
||||
{ key: "mod+o", command: "editor.openFavorite" },
|
||||
],
|
||||
});
|
||||
expect(
|
||||
warnSpy.mock.calls.some(([message]) =>
|
||||
String(message).includes("ignoring invalid keybinding entries"),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("reads keybindings once at startup and caches the resolved config", async () => {
|
||||
const fakeHome = makeTempDir("t3code-home-cached-config-");
|
||||
const configDir = path.join(fakeHome, ".t3");
|
||||
fs.mkdirSync(configDir, { recursive: true });
|
||||
const keybindingsPath = path.join(configDir, "keybindings.json");
|
||||
fs.writeFileSync(
|
||||
keybindingsPath,
|
||||
JSON.stringify([{ key: "cmd+j", command: "terminal.toggle" }]),
|
||||
"utf8",
|
||||
);
|
||||
vi.spyOn(os, "homedir").mockReturnValue(fakeHome);
|
||||
|
||||
server = createTestServer({ cwd: "/my/workspace" });
|
||||
await server.start();
|
||||
const addr = server.httpServer.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const firstResponse = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(firstResponse.error).toBeUndefined();
|
||||
expect(firstResponse.result).toEqual({
|
||||
cwd: "/my/workspace",
|
||||
keybindings: [
|
||||
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
|
||||
{ key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
|
||||
{ key: "mod+shift+o", command: "chat.new" },
|
||||
{ key: "mod+o", command: "editor.openFavorite" },
|
||||
{ key: "cmd+j", command: "terminal.toggle" },
|
||||
],
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
keybindingsPath,
|
||||
JSON.stringify([{ key: "cmd+k", command: "terminal.toggle" }]),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const secondResponse = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(secondResponse.error).toBeUndefined();
|
||||
expect(secondResponse.result).toEqual(firstResponse.result);
|
||||
});
|
||||
|
||||
it("warns and ignores unsupported keybindings config format", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const fakeHome = makeTempDir("t3code-home-unsupported-format-");
|
||||
|
||||
+122
-1
@@ -78,6 +78,122 @@ const DEFAULT_KEYBINDINGS: KeybindingsConfig = [
|
||||
{ key: "mod+o", command: "editor.openFavorite" },
|
||||
];
|
||||
|
||||
type WhenTokenType = "identifier" | "not" | "and" | "or" | "lparen" | "rparen";
|
||||
|
||||
interface WhenToken {
|
||||
type: WhenTokenType;
|
||||
}
|
||||
|
||||
function tokenizeWhenExpression(expression: string): WhenToken[] | null {
|
||||
const tokens: WhenToken[] = [];
|
||||
let index = 0;
|
||||
|
||||
while (index < expression.length) {
|
||||
const current = expression[index];
|
||||
if (!current) break;
|
||||
|
||||
if (/\s/.test(current)) {
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (expression.startsWith("&&", index)) {
|
||||
tokens.push({ type: "and" });
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (expression.startsWith("||", index)) {
|
||||
tokens.push({ type: "or" });
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (current === "!") {
|
||||
tokens.push({ type: "not" });
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (current === "(") {
|
||||
tokens.push({ type: "lparen" });
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (current === ")") {
|
||||
tokens.push({ type: "rparen" });
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const identifier = /^[A-Za-z_][A-Za-z0-9_.-]*/.exec(expression.slice(index));
|
||||
if (!identifier) {
|
||||
return null;
|
||||
}
|
||||
tokens.push({ type: "identifier" });
|
||||
index += identifier[0].length;
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
function isValidWhenExpression(when: string): boolean {
|
||||
const tokens = tokenizeWhenExpression(when);
|
||||
if (!tokens || tokens.length === 0) return false;
|
||||
let index = 0;
|
||||
|
||||
const parsePrimary = (): boolean => {
|
||||
const token = tokens[index];
|
||||
if (!token) return false;
|
||||
|
||||
if (token.type === "identifier") {
|
||||
index += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (token.type === "lparen") {
|
||||
index += 1;
|
||||
const parsed = parseOr();
|
||||
const close = tokens[index];
|
||||
if (!parsed || !close || close.type !== "rparen") return false;
|
||||
index += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const parseUnary = (): boolean => {
|
||||
const token = tokens[index];
|
||||
if (token?.type === "not") {
|
||||
index += 1;
|
||||
return parseUnary();
|
||||
}
|
||||
return parsePrimary();
|
||||
};
|
||||
|
||||
const parseAnd = (): boolean => {
|
||||
if (!parseUnary()) return false;
|
||||
|
||||
while (tokens[index]?.type === "and") {
|
||||
index += 1;
|
||||
if (!parseUnary()) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const parseOr = (): boolean => {
|
||||
if (!parseAnd()) return false;
|
||||
|
||||
while (tokens[index]?.type === "or") {
|
||||
index += 1;
|
||||
if (!parseAnd()) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!parseOr()) return false;
|
||||
return index === tokens.length;
|
||||
}
|
||||
|
||||
function mergeWithDefaultKeybindings(custom: KeybindingsConfig): KeybindingsConfig {
|
||||
if (custom.length === 0) {
|
||||
return [...DEFAULT_KEYBINDINGS];
|
||||
@@ -114,6 +230,10 @@ function readKeybindingsConfig(logger: ReturnType<typeof createLogger>): Keybind
|
||||
for (const entry of parsed) {
|
||||
const result = keybindingRuleSchema.safeParse(entry);
|
||||
if (result.success) {
|
||||
if (result.data.when && !isValidWhenExpression(result.data.when)) {
|
||||
invalidEntries += 1;
|
||||
continue;
|
||||
}
|
||||
sanitized.push(result.data);
|
||||
continue;
|
||||
}
|
||||
@@ -174,6 +294,7 @@ export function createServer(options: ServerOptions) {
|
||||
const logger = createLogger("ws");
|
||||
const logWebSocketEvents =
|
||||
explicitLogWsEvents ?? parseBooleanEnv(process.env.T3CODE_LOG_WS_EVENTS) ?? Boolean(devUrl);
|
||||
const keybindingsConfig = readKeybindingsConfig(logger);
|
||||
|
||||
function logOutgoingPush(push: WsPush, recipients: number) {
|
||||
if (!logWebSocketEvents) return;
|
||||
@@ -496,7 +617,7 @@ export function createServer(options: ServerOptions) {
|
||||
case WS_METHODS.serverGetConfig:
|
||||
return {
|
||||
cwd,
|
||||
keybindings: readKeybindingsConfig(logger),
|
||||
keybindings: keybindingsConfig,
|
||||
};
|
||||
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user