From 78e668d7b628f5816f18eefc889acb748e6cf24e Mon Sep 17 00:00:00 2001 From: MarkXian Date: Tue, 28 Jul 2026 14:24:49 +0800 Subject: [PATCH] fix(tui): reject malformed COLORFGBG indexes --- .changeset/strict-colorfgbg-background.md | 5 +++++ apps/kimi-code/src/tui/theme/detect.ts | 3 ++- apps/kimi-code/test/tui/terminal-theme.test.ts | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/strict-colorfgbg-background.md diff --git a/.changeset/strict-colorfgbg-background.md b/.changeset/strict-colorfgbg-background.md new file mode 100644 index 0000000000..f0efb75e96 --- /dev/null +++ b/.changeset/strict-colorfgbg-background.md @@ -0,0 +1,5 @@ +--- +"@moonshot-ai/kimi-code": patch +--- + +Ignore malformed COLORFGBG background color indexes during terminal theme detection. diff --git a/apps/kimi-code/src/tui/theme/detect.ts b/apps/kimi-code/src/tui/theme/detect.ts index 0f4d409532..63eb39300d 100644 --- a/apps/kimi-code/src/tui/theme/detect.ts +++ b/apps/kimi-code/src/tui/theme/detect.ts @@ -113,7 +113,8 @@ export function parseColorFgBg(value: string | undefined): ResolvedTheme | null const parts = value.split(";"); const bgRaw = parts.at(-1); if (bgRaw === undefined) return null; - const bg = parseInt(bgRaw, 10); + if (!/^\d+$/.test(bgRaw)) return null; + const bg = Number.parseInt(bgRaw, 10); if (!Number.isInteger(bg)) return null; // ANSI 0=black, 1=red, 2=green, 3=yellow, 4=blue, 5=magenta, 6=cyan, 8=bright black. const darkBgs = new Set([0, 1, 2, 3, 4, 5, 6, 8]); diff --git a/apps/kimi-code/test/tui/terminal-theme.test.ts b/apps/kimi-code/test/tui/terminal-theme.test.ts index 3159f80256..7b1ec997b3 100644 --- a/apps/kimi-code/test/tui/terminal-theme.test.ts +++ b/apps/kimi-code/test/tui/terminal-theme.test.ts @@ -15,6 +15,7 @@ import { handleTerminalThemeInput, installTerminalThemeTracking, } from "#/tui/utils/terminal-theme"; +import { parseColorFgBg } from "#/tui/theme/detect"; type InputListener = Parameters[0]; const DARK_OSC11_REPORT = "\u001B]11;rgb:2828/2c2c/3434\u0007"; @@ -173,3 +174,11 @@ describe('ColorPalette warning token', () => { expect(getBuiltInPalette('light')).toBe(lightColors); }); }); + +describe("parseColorFgBg", () => { + it("rejects malformed background color indexes", () => { + expect(parseColorFgBg("0;9")).toBe("light"); + expect(parseColorFgBg("0;9abc")).toBeNull(); + expect(parseColorFgBg("0;1.5")).toBeNull(); + }); +});