kodorun-cli 0.0.0-stage → 0.1.122

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
1
+ import { describe, it, after } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import * as fs from "node:fs";
4
+ import * as os from "node:os";
5
+ import * as path from "node:path";
6
+ import { WebSocketServer } from "ws";
7
+ process.env.KODORUN_CLI_NO_AUTOSTART = "1";
8
+ import { normalizeGatewayUrl, redactSensitive, resolveSafePathInRoot } from "./index.js";
9
+ import { V2SessionTransport } from "./transport/v2.js";
10
+ describe("CLI security primitives", () => {
11
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "kodorun-cli-test-"));
12
+ const outsideDir = fs.mkdtempSync(path.join(os.tmpdir(), "kodorun-cli-outside-"));
13
+ after(() => {
14
+ fs.rmSync(tempRoot, { recursive: true, force: true });
15
+ fs.rmSync(outsideDir, { recursive: true, force: true });
16
+ });
17
+ it("resolveSafePathInRoot allows nested paths inside root and blocks escapes", () => {
18
+ const nestedDir = path.join(tempRoot, "src");
19
+ fs.mkdirSync(nestedDir, { recursive: true });
20
+ const filePath = path.join(nestedDir, "app.ts");
21
+ fs.writeFileSync(filePath, "export {};\n", "utf8");
22
+ assert.equal(resolveSafePathInRoot(tempRoot, "src/app.ts"), fs.realpathSync(filePath));
23
+ assert.equal(resolveSafePathInRoot(tempRoot, "src/new-subdir/new-file.ts")?.endsWith(path.join("src", "new-subdir", "new-file.ts")), true);
24
+ assert.equal(resolveSafePathInRoot(tempRoot, "../outside.txt"), null);
25
+ assert.equal(resolveSafePathInRoot(tempRoot, "src/../../outside.txt"), null);
26
+ assert.equal(resolveSafePathInRoot(tempRoot, "src/app.ts\0.txt"), null);
27
+ assert.equal(resolveSafePathInRoot(tempRoot, outsideDir), null);
28
+ });
29
+ it("resolveSafePathInRoot blocks non-existent leaf under outside symlink/junction", () => {
30
+ const linkPath = path.join(tempRoot, "escape-link");
31
+ try {
32
+ fs.symlinkSync(outsideDir, linkPath, process.platform === "win32" ? "junction" : "dir");
33
+ }
34
+ catch {
35
+ return;
36
+ }
37
+ assert.equal(resolveSafePathInRoot(tempRoot, "escape-link/new-secret.txt"), null);
38
+ });
39
+ it("redactSensitive masks JSON secrets, query tokens, and JWTs", () => {
40
+ const fakeJwt = `${"a".repeat(24)}.${"b".repeat(24)}.${"c".repeat(24)}`;
41
+ const sample = JSON.stringify({
42
+ password: "my-secret-password",
43
+ sessionSecret: "another-secret-value",
44
+ jwt: fakeJwt,
45
+ longKey: "X".repeat(48),
46
+ ok: "visible",
47
+ });
48
+ const out = redactSensitive(sample);
49
+ assert.equal(out.includes("my-secret-password"), false);
50
+ assert.equal(out.includes("another-secret-value"), false);
51
+ assert.equal(out.includes(fakeJwt), false);
52
+ assert.equal(out.includes("X".repeat(48)), false);
53
+ assert.equal(out.includes("[redacted_jwt]"), true);
54
+ assert.equal(out.includes("[redacted_secret]"), true);
55
+ assert.equal(out.includes("visible"), true);
56
+ });
57
+ it("normalizeGatewayUrl enforces https except loopback", () => {
58
+ assert.equal(normalizeGatewayUrl("gateway.example.com/"), "https://gateway.example.com");
59
+ assert.equal(normalizeGatewayUrl("http://127.0.0.1:3001/"), "http://127.0.0.1:3001");
60
+ assert.equal(normalizeGatewayUrl("ws://localhost:3001/"), "http://localhost:3001");
61
+ assert.throws(() => normalizeGatewayUrl("http://example.com"));
62
+ assert.throws(() => normalizeGatewayUrl("ftp://example.com"));
63
+ });
64
+ });
65
+ describe("V2SessionTransport E2EE handshake & message round-trip", () => {
66
+ it("pairs CLI and app over mock relay, rejects plaintext injection, and exchanges encrypted RPC", async () => {
67
+ const wss = new WebSocketServer({ host: "127.0.0.1", port: 0 });
68
+ await new Promise((resolve, reject) => {
69
+ wss.once("listening", resolve);
70
+ wss.once("error", reject);
71
+ });
72
+ const address = wss.address();
73
+ assert.ok(address && typeof address !== "string");
74
+ const gatewayUrl = `http://127.0.0.1:${address.port}`;
75
+ let cliSock = null;
76
+ let appSock = null;
77
+ wss.on("connection", (ws, req) => {
78
+ const url = new URL(req.url || "/", "http://127.0.0.1");
79
+ if (url.pathname.endsWith("/cli")) {
80
+ cliSock = ws;
81
+ ws.send(JSON.stringify({ type: "connected", role: "cli" }));
82
+ }
83
+ else {
84
+ appSock = ws;
85
+ ws.send(JSON.stringify({ type: "connected", role: "app" }));
86
+ }
87
+ if (cliSock && appSock) {
88
+ cliSock.send(JSON.stringify({ type: "peer_connected", peer: "app" }));
89
+ appSock.send(JSON.stringify({ type: "peer_connected", peer: "cli" }));
90
+ }
91
+ ws.on("message", (data, isBinary) => {
92
+ const peer = ws === cliSock ? appSock : cliSock;
93
+ if (peer && peer.readyState === peer.OPEN) {
94
+ peer.send(data, { binary: isBinary });
95
+ }
96
+ });
97
+ });
98
+ const sharedSecret = "shared-v2-session-secret-0123456789abcdef";
99
+ let closedReasons = [];
100
+ const cliTransport = new V2SessionTransport({
101
+ gatewayUrl,
102
+ password: sharedSecret,
103
+ sessionSecret: sharedSecret,
104
+ role: "cli",
105
+ handlers: {
106
+ onSystemMessage: () => { },
107
+ onProtocolRequest: async (msg) => ({
108
+ v: 1,
109
+ id: msg.id,
110
+ ns: msg.ns,
111
+ action: msg.action,
112
+ ok: true,
113
+ payload: { echo: msg.payload.ping },
114
+ }),
115
+ onClose: (reason) => closedReasons.push(reason),
116
+ },
117
+ });
118
+ const responses = [];
119
+ const appTransport = new V2SessionTransport({
120
+ gatewayUrl,
121
+ password: sharedSecret,
122
+ sessionSecret: sharedSecret,
123
+ role: "app",
124
+ handlers: {
125
+ onSystemMessage: () => { },
126
+ onProtocolRequest: async (msg) => ({
127
+ v: 1,
128
+ id: msg.id,
129
+ ns: msg.ns,
130
+ action: msg.action,
131
+ ok: true,
132
+ payload: {},
133
+ }),
134
+ onProtocolResponse: (resp) => {
135
+ responses.push(resp);
136
+ },
137
+ onClose: (reason) => closedReasons.push(reason),
138
+ },
139
+ });
140
+ try {
141
+ await Promise.all([cliTransport.connect(), appTransport.connect()]);
142
+ assert.equal(cliTransport.isSecure(), true);
143
+ assert.equal(appTransport.isSecure(), true);
144
+ await appTransport.sendMessage({
145
+ v: 1,
146
+ id: "req-1",
147
+ ns: "system",
148
+ action: "ping",
149
+ payload: { ping: "pong" },
150
+ });
151
+ for (let i = 0; i < 40 && responses.length === 0; i++) {
152
+ await new Promise((r) => setTimeout(r, 25));
153
+ }
154
+ assert.equal(responses.length, 1);
155
+ assert.equal(responses[0].payload.echo, "pong");
156
+ }
157
+ finally {
158
+ cliTransport.close();
159
+ appTransport.close();
160
+ for (const client of wss.clients) {
161
+ client.terminate();
162
+ }
163
+ await new Promise((resolve) => wss.close(() => resolve()));
164
+ }
165
+ assert.equal(closedReasons.length, 0);
166
+ });
167
+ });
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ declare function redactSensitive(input: unknown): string;
3
+ export declare function resolveSafePathInRoot(rootDir: string, requestedPath: string): string | null;
4
+ declare function normalizeGatewayUrl(input: string): string;
5
+ export { normalizeGatewayUrl, redactSensitive };