Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strict-vis-port-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Reject malformed port values for the session visualizer command.
19 changes: 15 additions & 4 deletions apps/kimi-code/src/cli/sub/vis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,28 @@ export function registerVisCommand(parent: Command, overrides?: Partial<VisDeps>
sessionId: string | undefined,
options: { port?: string; host?: string; open?: boolean },
) => {
const port = options.port === undefined ? undefined : Number.parseInt(options.port, 10);
await handleVis(createDefaultVisDeps(overrides), {
open: options.open !== false,
...(port === undefined || Number.isNaN(port) ? {} : { port }),
...(options.host === undefined ? {} : { host: options.host }),
...(sessionId === undefined ? {} : { sessionId }),
port: parseVisPort(options.port),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Route invalid ports through synchronous CLI parsing

When a user runs kimi vis --port 123abc, parseVisPort throws inside this async action, turning the error into a rejected promise. The real entrypoint uses program.parse(process.argv) at apps/kimi-code/src/main.ts:223, so Commander does not await that rejection; the installed global handler consequently records ordinary invalid input as a crash and Node emits an unhandled-rejection stack trace instead of a normal CLI validation error. Commander’s async-action guidance says to use parseAsync rather than parse for async handlers; alternatively, parse this option synchronously with an Option.argParser/InvalidArgumentError before entering the async action.

Useful? React with 👍 / 👎.

host: options.host,
sessionId,
});
},
);
}

export function parseVisPort(raw: string | undefined): number | undefined {
if (raw === undefined) return undefined;
if (!/^\d+$/.test(raw)) {
throw new Error(`error: invalid --port value: ${raw}`);
}
const port = Number.parseInt(raw, 10);
if (!Number.isFinite(port) || port < 0 || port > 65535) {
throw new Error(`error: invalid --port value: ${raw}`);
}
return port;
}

function createDefaultVisDeps(overrides: Partial<VisDeps> = {}): VisDeps {
return {
getHomeDir: overrides.getHomeDir ?? (() => createCliTelemetryBootstrap().homeDir),
Expand Down
12 changes: 11 additions & 1 deletion apps/kimi-code/test/cli/vis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { describe, it, expect, vi } from 'vitest';

import { handleVis, type VisDeps } from '#/cli/sub/vis';
import { handleVis, parseVisPort, type VisDeps } from '#/cli/sub/vis';

function makeDeps(over: Partial<VisDeps> = {}): {
deps: VisDeps;
Expand Down Expand Up @@ -112,3 +112,13 @@ describe('handleVis', () => {
expect(deps.waitForShutdown).not.toHaveBeenCalled();
});
});

describe('parseVisPort', () => {
it('rejects malformed port values', () => {
expect(parseVisPort(undefined)).toBeUndefined();
expect(parseVisPort('8080')).toBe(8080);
expect(() => parseVisPort('123abc')).toThrow(/invalid --port/);
expect(() => parseVisPort('1.5')).toThrow(/invalid --port/);
expect(() => parseVisPort('99999')).toThrow(/invalid --port/);
});
});