@xiaolikt/ai-vault 0.0.0-stage → 0.5.0
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.
- package/Cargo.lock +784 -0
- package/Cargo.toml +8 -0
- package/LICENSE +21 -0
- package/README.md +244 -2
- package/bin/ai-vault.cjs +71 -0
- package/crates/ai-vault/Cargo.toml +26 -0
- package/crates/ai-vault/src/audit.rs +77 -0
- package/crates/ai-vault/src/cli.rs +1129 -0
- package/crates/ai-vault/src/config.rs +77 -0
- package/crates/ai-vault/src/crypto.rs +265 -0
- package/crates/ai-vault/src/dialog.rs +222 -0
- package/crates/ai-vault/src/import.rs +175 -0
- package/crates/ai-vault/src/main.rs +22 -0
- package/crates/ai-vault/src/paths.rs +68 -0
- package/crates/ai-vault/src/session.rs +280 -0
- package/crates/ai-vault/src/store.rs +219 -0
- package/install.sh +21 -0
- package/package.json +76 -5
- package/packages/pi-ai-vault/README.md +62 -0
- package/packages/pi-ai-vault/package.json +47 -0
- package/packages/pi-ai-vault/src/bitwarden.ts +1482 -0
- package/packages/pi-ai-vault/src/cli.ts +171 -0
- package/packages/pi-ai-vault/src/host.ts +76 -0
- package/packages/pi-ai-vault/src/import-map.ts +91 -0
- package/packages/pi-ai-vault/src/index.ts +976 -0
- package/packages/pi-ai-vault/src/prompt.ts +195 -0
- package/packages/pi-ai-vault/src/scrub.ts +157 -0
- package/test/cli.sh +135 -0
- package/test/npm.sh +70 -0
- package/test/smoke.sh +77 -0
- package/tests/bitwarden-tools.test.mjs +330 -0
- package/tests/helpers.mjs +131 -0
- package/tests/import-map.test.mjs +74 -0
- package/tests/mocks/bin/bw +41 -0
- package/tests/mocks/bin/wl-copy +15 -0
- package/tests/mocks/bin/wl-paste +9 -0
- package/tests/mocks/bin/zenity +51 -0
- package/tests/prompt.test.mjs +200 -0
- package/tests/register.test.mjs +48 -0
- package/tests/scrub.test.mjs +98 -0
- package/tests/vault-cli.test.mjs +217 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, writeFileSync, readFileSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { confirmWindow, promptSecret, promptText } from "../packages/pi-ai-vault/src/prompt.ts";
|
|
7
|
+
|
|
8
|
+
const saved = {
|
|
9
|
+
PATH: process.env.PATH,
|
|
10
|
+
DISPLAY: process.env.DISPLAY,
|
|
11
|
+
WAYLAND_DISPLAY: process.env.WAYLAND_DISPLAY,
|
|
12
|
+
ZENITY_CODE: process.env.ZENITY_CODE,
|
|
13
|
+
ZENITY_VALUE: process.env.ZENITY_VALUE,
|
|
14
|
+
ZENITY_STDERR: process.env.ZENITY_STDERR,
|
|
15
|
+
XDPYINFO_FAIL: process.env.XDPYINFO_FAIL,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function restore() {
|
|
19
|
+
const env = process.env;
|
|
20
|
+
env.PATH = saved.PATH;
|
|
21
|
+
for (const key of ["DISPLAY", "WAYLAND_DISPLAY", "ZENITY_CODE", "ZENITY_VALUE", "ZENITY_STDERR", "XDPYINFO_FAIL"]) {
|
|
22
|
+
if (saved[key] === undefined) delete env[key];
|
|
23
|
+
else env[key] = saved[key];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function mockZenity() {
|
|
28
|
+
const dir = mkdtempSync(join(tmpdir(), "ai-vault-zenity-"));
|
|
29
|
+
const log = join(dir, "log");
|
|
30
|
+
writeFileSync(
|
|
31
|
+
join(dir, "zenity"),
|
|
32
|
+
`#!/bin/sh
|
|
33
|
+
printf '%s\\n' "$*" >> "${log}"
|
|
34
|
+
if [ -n "$ZENITY_STDERR" ]; then printf '%s\\n' "$ZENITY_STDERR" >&2; fi
|
|
35
|
+
if [ -n "$ZENITY_CODE" ]; then exit "$ZENITY_CODE"; fi
|
|
36
|
+
printf '%s\\n' "$ZENITY_VALUE"
|
|
37
|
+
`,
|
|
38
|
+
{ mode: 0o755 },
|
|
39
|
+
);
|
|
40
|
+
// A deterministic X display probe: the tests run without a real X server.
|
|
41
|
+
writeFileSync(
|
|
42
|
+
join(dir, "xdpyinfo"),
|
|
43
|
+
`#!/bin/sh
|
|
44
|
+
if [ -n "$XDPYINFO_FAIL" ]; then exit 1; fi
|
|
45
|
+
exit 0
|
|
46
|
+
`,
|
|
47
|
+
{ mode: 0o755 },
|
|
48
|
+
);
|
|
49
|
+
process.env.PATH = `${dir}:${saved.PATH}`;
|
|
50
|
+
process.env.DISPLAY = ":99";
|
|
51
|
+
delete process.env.WAYLAND_DISPLAY;
|
|
52
|
+
return { dir, log };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function tui(calls) {
|
|
56
|
+
return {
|
|
57
|
+
hasUI: true,
|
|
58
|
+
ui: {
|
|
59
|
+
async input(title, placeholder) {
|
|
60
|
+
calls.push(["input", title, placeholder]);
|
|
61
|
+
return "from-tui";
|
|
62
|
+
},
|
|
63
|
+
async confirm(title) {
|
|
64
|
+
calls.push(["confirm", title]);
|
|
65
|
+
return true;
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
test("promptSecret reads the zenity window and does not ask the TUI", async () => {
|
|
72
|
+
const { dir, log } = mockZenity();
|
|
73
|
+
process.env.ZENITY_VALUE = "window-secret";
|
|
74
|
+
delete process.env.ZENITY_CODE;
|
|
75
|
+
const calls = [];
|
|
76
|
+
try {
|
|
77
|
+
const value = await promptSecret(tui(calls), "ai-vault", "主密码");
|
|
78
|
+
assert.equal(value, "window-secret");
|
|
79
|
+
assert.equal(calls.length, 0);
|
|
80
|
+
assert.match(readFileSync(log, "utf8"), /--password/);
|
|
81
|
+
} finally {
|
|
82
|
+
restore();
|
|
83
|
+
rmSync(dir, { recursive: true, force: true });
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("cancelling the window does not fall through to the TUI", async () => {
|
|
88
|
+
const { dir } = mockZenity();
|
|
89
|
+
process.env.ZENITY_CODE = "1";
|
|
90
|
+
const calls = [];
|
|
91
|
+
try {
|
|
92
|
+
assert.equal(await promptSecret(tui(calls), "ai-vault", "主密码"), undefined);
|
|
93
|
+
assert.equal(await confirmWindow(tui(calls), "导入", "允许?"), false);
|
|
94
|
+
assert.equal(calls.length, 0);
|
|
95
|
+
} finally {
|
|
96
|
+
restore();
|
|
97
|
+
rmSync(dir, { recursive: true, force: true });
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("a silent zenity failure on an unreachable display falls back to the TUI", async () => {
|
|
102
|
+
const { dir } = mockZenity();
|
|
103
|
+
process.env.ZENITY_CODE = "1";
|
|
104
|
+
process.env.XDPYINFO_FAIL = "1";
|
|
105
|
+
delete process.env.ZENITY_STDERR;
|
|
106
|
+
const calls = [];
|
|
107
|
+
try {
|
|
108
|
+
assert.equal(await promptSecret(tui(calls), "ai-vault", "主密码"), "from-tui");
|
|
109
|
+
assert.equal(calls[0][0], "input");
|
|
110
|
+
} finally {
|
|
111
|
+
restore();
|
|
112
|
+
rmSync(dir, { recursive: true, force: true });
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("a zenity crash falls back to the TUI, a deny does not", async () => {
|
|
117
|
+
const { dir } = mockZenity();
|
|
118
|
+
process.env.ZENITY_CODE = "255";
|
|
119
|
+
process.env.ZENITY_STDERR = "This option is not available. Please see --help for all possible usages.";
|
|
120
|
+
const calls = [];
|
|
121
|
+
try {
|
|
122
|
+
assert.equal(await confirmWindow(tui(calls), "确认", "允许?"), true);
|
|
123
|
+
assert.equal(calls.length, 1);
|
|
124
|
+
} finally {
|
|
125
|
+
restore();
|
|
126
|
+
rmSync(dir, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("exit 1 with only GTK diagnostics is a cancel, not a failed window", async () => {
|
|
131
|
+
const { dir } = mockZenity();
|
|
132
|
+
process.env.ZENITY_CODE = "1";
|
|
133
|
+
process.env.ZENITY_STDERR =
|
|
134
|
+
"(zenity:4242): Gtk-WARNING **: 10:00:00.000: Trying to measure GtkLabel 0x1 for height of 9";
|
|
135
|
+
const calls = [];
|
|
136
|
+
try {
|
|
137
|
+
assert.equal(await promptSecret(tui(calls), "ai-vault", "主密码"), undefined);
|
|
138
|
+
assert.equal(await confirmWindow(tui(calls), "确认", "允许?"), false);
|
|
139
|
+
assert.equal(calls.length, 0, "a cancel was re-asked in the terminal");
|
|
140
|
+
} finally {
|
|
141
|
+
restore();
|
|
142
|
+
rmSync(dir, { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("exit 1 with a startup error means no window opened: fall back to the TUI", async () => {
|
|
147
|
+
const { dir } = mockZenity();
|
|
148
|
+
process.env.ZENITY_CODE = "1";
|
|
149
|
+
process.env.ZENITY_STDERR = "This option is not available. Please see --help for all possible usages.";
|
|
150
|
+
const calls = [];
|
|
151
|
+
try {
|
|
152
|
+
assert.equal(await promptSecret(tui(calls), "ai-vault", "主密码"), "from-tui");
|
|
153
|
+
assert.equal(calls[0][0], "input");
|
|
154
|
+
} finally {
|
|
155
|
+
restore();
|
|
156
|
+
rmSync(dir, { recursive: true, force: true });
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("exit 1 with 'cannot open display' falls back even behind a GTK prefix", async () => {
|
|
161
|
+
const { dir } = mockZenity();
|
|
162
|
+
process.env.ZENITY_CODE = "1";
|
|
163
|
+
process.env.ZENITY_STDERR = "** (zenity:4242): WARNING **: cannot open display: :99";
|
|
164
|
+
const calls = [];
|
|
165
|
+
try {
|
|
166
|
+
assert.equal(await confirmWindow(tui(calls), "确认", "允许?"), true);
|
|
167
|
+
assert.equal(calls.length, 1);
|
|
168
|
+
} finally {
|
|
169
|
+
restore();
|
|
170
|
+
rmSync(dir, { recursive: true, force: true });
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("missing zenity falls back to the TUI", async () => {
|
|
175
|
+
const dir = mkdtempSync(join(tmpdir(), "ai-vault-empty-"));
|
|
176
|
+
process.env.PATH = dir;
|
|
177
|
+
process.env.DISPLAY = ":99";
|
|
178
|
+
delete process.env.WAYLAND_DISPLAY;
|
|
179
|
+
const calls = [];
|
|
180
|
+
try {
|
|
181
|
+
assert.equal(await promptText(tui(calls), "名称", "条目名"), "from-tui");
|
|
182
|
+
assert.equal(calls[0][0], "input");
|
|
183
|
+
assert.equal(await confirmWindow(tui(calls), "确认", "允许?"), true);
|
|
184
|
+
assert.equal(calls[1][0], "confirm");
|
|
185
|
+
} finally {
|
|
186
|
+
restore();
|
|
187
|
+
rmSync(dir, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("no display and no UI fails closed", async () => {
|
|
192
|
+
delete process.env.DISPLAY;
|
|
193
|
+
delete process.env.WAYLAND_DISPLAY;
|
|
194
|
+
try {
|
|
195
|
+
assert.equal(await promptSecret(undefined, "t", "l"), undefined);
|
|
196
|
+
assert.equal(await confirmWindow({ hasUI: false, ui: tui([]).ui }, "t", "m"), false);
|
|
197
|
+
} finally {
|
|
198
|
+
restore();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { bind, cleanUpOnExit, useMocks } from "./helpers.mjs";
|
|
4
|
+
|
|
5
|
+
const dir = useMocks();
|
|
6
|
+
const { default: ompVault } = await import("../packages/pi-ai-vault/src/index.ts");
|
|
7
|
+
cleanUpOnExit(dir);
|
|
8
|
+
|
|
9
|
+
test("omp: one extension registers the local vault and live Bitwarden, without bw_inject", () => {
|
|
10
|
+
const b = bind(ompVault, true);
|
|
11
|
+
assert.deepEqual([...b.tools.keys()], [
|
|
12
|
+
"vault_list",
|
|
13
|
+
"vault_get",
|
|
14
|
+
"vault_add",
|
|
15
|
+
"vault_import",
|
|
16
|
+
"bw_list",
|
|
17
|
+
"bw_run",
|
|
18
|
+
"bw_clipboard",
|
|
19
|
+
"bw_reveal",
|
|
20
|
+
]);
|
|
21
|
+
assert.deepEqual([...b.commands.keys()].sort(), ["bw", "vault"]);
|
|
22
|
+
// omp reads the reason from session_switch; its session_start has none.
|
|
23
|
+
assert.equal(b.has("session_switch"), true);
|
|
24
|
+
assert.equal(b.has("session_start"), false);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("pi: bash and bw_inject are registered, and the lifecycle comes from session_start", () => {
|
|
28
|
+
const b = bind(ompVault, false);
|
|
29
|
+
const names = [...b.tools.keys()];
|
|
30
|
+
assert.equal(names.includes("bash"), true);
|
|
31
|
+
assert.equal(names.includes("bw_inject"), true);
|
|
32
|
+
assert.equal(names.includes("bw_run"), true);
|
|
33
|
+
assert.equal(b.has("session_start"), true);
|
|
34
|
+
assert.equal(b.has("session_switch"), false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("tools declare the approval tier omp prompts by: reads read, writes write", () => {
|
|
38
|
+
const b = bind(ompVault, true);
|
|
39
|
+
assert.equal(b.tools.get("vault_list").approval, "read");
|
|
40
|
+
assert.equal(b.tools.get("bw_list").approval, "read");
|
|
41
|
+
assert.equal(b.tools.get("vault_add").approval, "write");
|
|
42
|
+
assert.equal(b.tools.get("vault_import").approval, "write");
|
|
43
|
+
assert.equal(b.tools.get("bw_clipboard").approval, "write");
|
|
44
|
+
assert.equal(b.tools.get("bw_run").approval, "exec");
|
|
45
|
+
// A secret handed to the model keeps the strictest tier.
|
|
46
|
+
assert.equal(b.tools.get("vault_get").approval, undefined);
|
|
47
|
+
assert.equal(b.tools.get("bw_reveal").approval, undefined);
|
|
48
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { scrubDeep, scrubText } from "../packages/pi-ai-vault/src/scrub.ts";
|
|
4
|
+
|
|
5
|
+
const SECRET = "supersecret-value-123";
|
|
6
|
+
const values = [{ env: "T1", value: SECRET }];
|
|
7
|
+
|
|
8
|
+
test("redacts the raw value", () => {
|
|
9
|
+
const result = scrubText(`out: ${SECRET}\n`, values);
|
|
10
|
+
assert.equal(result.changed, true);
|
|
11
|
+
assert.equal(result.text, "out: [BW_SCRUBBED:T1]\n");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("redacts base64 and base64url", () => {
|
|
15
|
+
const raw = `${SECRET}x`;
|
|
16
|
+
const b64 = Buffer.from(raw).toString("base64");
|
|
17
|
+
const b64url = b64.replace(/=+$/, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
18
|
+
const result = scrubText(`${b64} ${b64url}`, [{ env: "T1", value: raw }]);
|
|
19
|
+
assert.equal(result.text, "[BW_SCRUBBED-B64:T1] [BW_SCRUBBED-B64URL:T1]");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("redacts base64 and hex that are wrapped or spaced", () => {
|
|
23
|
+
// GNU base64 wraps at 76 columns; xxd/od put spaces between hex bytes.
|
|
24
|
+
const raw = `${"correct-horse-battery-staple-".repeat(4)}end`;
|
|
25
|
+
const b64 = Buffer.from(raw).toString("base64");
|
|
26
|
+
assert.ok(b64.length > 76, "the fixture is long enough to wrap");
|
|
27
|
+
const wrapped = b64.match(/.{1,76}/g).join("\n");
|
|
28
|
+
const hex = Buffer.from(raw).toString("hex");
|
|
29
|
+
const spaced = hex.match(/.{1,2}/g).join(" ");
|
|
30
|
+
const result = scrubText(`b64:\n${wrapped}\nhex: ${spaced}\n`, [{ env: "W", value: raw }]);
|
|
31
|
+
assert.equal(result.changed, true);
|
|
32
|
+
assert.equal(result.text.includes("correct-horse"), false);
|
|
33
|
+
assert.match(result.text, /\[BW_SCRUBBED-B64:W\]/);
|
|
34
|
+
assert.match(result.text, /\[BW_SCRUBBED-HEX:W\]/);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("redacts base64 of a short password", () => {
|
|
38
|
+
// `echo -n "$VAULT_SECRET" | base64` must not leak an everyday password.
|
|
39
|
+
const raw = "hunter2abcd";
|
|
40
|
+
const result = scrubText(`| ${Buffer.from(raw).toString("base64")} |`, [{ env: "T1", value: raw }]);
|
|
41
|
+
assert.equal(result.text, "| [BW_SCRUBBED-B64:T1] |");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("redacts URL-encoded and hex forms", () => {
|
|
45
|
+
const raw = "a b&c=d/e";
|
|
46
|
+
const result = scrubText(`${encodeURIComponent(raw)} ${Buffer.from(raw).toString("hex")}`, [{ env: "U", value: raw }]);
|
|
47
|
+
assert.equal(result.text, "[BW_SCRUBBED-URL:U] [BW_SCRUBBED-HEX:U]");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("leaves short values and unrelated text alone", () => {
|
|
51
|
+
assert.equal(scrubText("abc abc", [{ env: "S", value: "abc" }]).changed, false);
|
|
52
|
+
assert.equal(scrubText("nothing here", values).changed, false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("scrubs nested details and preserves unchanged objects", () => {
|
|
56
|
+
const input = { trace: { units: [{ source: `export T1='${SECRET}'` }], history: [["argv", SECRET]] } };
|
|
57
|
+
const result = scrubDeep(input, values);
|
|
58
|
+
assert.equal(result.changed, true);
|
|
59
|
+
assert.equal(JSON.stringify(result.value).includes(SECRET), false);
|
|
60
|
+
const plain = { ok: true, count: 3, list: [null, "plain"] };
|
|
61
|
+
const same = scrubDeep(plain, values);
|
|
62
|
+
assert.equal(same.changed, false);
|
|
63
|
+
assert.equal(same.value, plain);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("leaves a Date, a Buffer, or a class instance as the object it was", () => {
|
|
67
|
+
class Handle {
|
|
68
|
+
constructor() {
|
|
69
|
+
this.fd = 3;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const date = new Date(0);
|
|
73
|
+
const blob = Buffer.from("abc");
|
|
74
|
+
const handle = new Handle();
|
|
75
|
+
const result = scrubDeep({ at: date, blob, handle, note: SECRET }, values);
|
|
76
|
+
assert.equal(result.changed, true);
|
|
77
|
+
assert.equal(result.value.at, date);
|
|
78
|
+
assert.equal(result.value.blob, blob);
|
|
79
|
+
assert.equal(result.value.handle, handle);
|
|
80
|
+
assert.equal(result.value.note, "[BW_SCRUBBED:T1]");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("scrubs a Buffer, Map, Set, and Error that carry a secret", () => {
|
|
84
|
+
const blob = Buffer.from(`raw ${SECRET}`);
|
|
85
|
+
const map = new Map([[SECRET, "value"], ["key", SECRET]]);
|
|
86
|
+
const set = new Set([SECRET, "plain"]);
|
|
87
|
+
const err = new Error(`failed with ${SECRET}`);
|
|
88
|
+
const result = scrubDeep({ blob, map, set, err }, values);
|
|
89
|
+
assert.equal(result.changed, true);
|
|
90
|
+
assert.equal(result.value.blob.toString("utf8").includes(SECRET), false);
|
|
91
|
+
assert.equal([...result.value.map.keys()].some((k) => String(k).includes(SECRET)), false);
|
|
92
|
+
assert.equal([...result.value.map.values()].some((v) => String(v).includes(SECRET)), false);
|
|
93
|
+
assert.equal([...result.value.set].some((v) => String(v).includes(SECRET)), false);
|
|
94
|
+
assert.equal(result.value.err.message.includes(SECRET), false);
|
|
95
|
+
assert.ok(result.value.err instanceof Error);
|
|
96
|
+
// The clean forms keep their identity, so the host's own objects are not replaced.
|
|
97
|
+
assert.equal(scrubDeep(blob, []).value, blob);
|
|
98
|
+
});
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { test, before, after } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { answers, bind, cleanUpOnExit, dialogTrace, makeCtx, subCtx, textOf, useMocks } from "./helpers.mjs";
|
|
7
|
+
|
|
8
|
+
// The extension is only a client: these drive the real ai-vault binary (built
|
|
9
|
+
// by `npm test`) plus the mock zenity, with a throwaway store.
|
|
10
|
+
const dir = useMocks();
|
|
11
|
+
const { default: aiVault } = await import("../packages/pi-ai-vault/src/index.ts");
|
|
12
|
+
cleanUpOnExit(dir);
|
|
13
|
+
|
|
14
|
+
const PW = "correct horse battery";
|
|
15
|
+
const STORE = join(process.env.AI_VAULT_DIR, "store.json");
|
|
16
|
+
const prompts = (prefix) => dialogTrace(dir).split("\n").filter((line) => line.startsWith(prefix)).length;
|
|
17
|
+
const masterPrompts = () => prompts("--password");
|
|
18
|
+
|
|
19
|
+
before(async () => {
|
|
20
|
+
const boot = bind(aiVault, true);
|
|
21
|
+
answers.master(dir, PW, PW);
|
|
22
|
+
await boot.cmd("init", makeCtx().ctx);
|
|
23
|
+
assert.ok(existsSync(STORE), "init created the shared store");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
after(() => answers.deny(dir, false));
|
|
27
|
+
|
|
28
|
+
/** Add GitHub through the human command: unlock, allow, type a username and password. */
|
|
29
|
+
async function addGitHub(b, ctx) {
|
|
30
|
+
answers.master(dir, PW);
|
|
31
|
+
answers.entries(dir, "octo");
|
|
32
|
+
answers.entryPassword(dir, "pw-one");
|
|
33
|
+
await b.cmd("add GitHub", ctx);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test("one machine-wide store, reported by /vault status", async () => {
|
|
37
|
+
const b = bind(aiVault, true);
|
|
38
|
+
const { ctx, notes } = makeCtx();
|
|
39
|
+
answers.master(dir, PW);
|
|
40
|
+
await b.cmd("unlock", ctx);
|
|
41
|
+
await b.cmd("status", ctx);
|
|
42
|
+
assert.match(notes.at(-1).message, /state: unlocked/);
|
|
43
|
+
assert.match(notes.at(-1).message, new RegExp(process.env.AI_VAULT_DIR.replace(/[.\\/]/g, "\\$&")));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("add stores an entry without a password in the tool call", async () => {
|
|
47
|
+
const b = bind(aiVault, true);
|
|
48
|
+
const ctx = makeCtx().ctx;
|
|
49
|
+
await addGitHub(b, ctx);
|
|
50
|
+
|
|
51
|
+
answers.master(dir, PW);
|
|
52
|
+
const listed = await b.tool("vault_list", {}, ctx);
|
|
53
|
+
assert.equal(listed.isError, undefined);
|
|
54
|
+
assert.deepEqual(
|
|
55
|
+
JSON.parse(textOf(listed)).map((i) => i.name),
|
|
56
|
+
["GitHub"],
|
|
57
|
+
);
|
|
58
|
+
// The entry password only ever existed inside a dialog answer.
|
|
59
|
+
assert.doesNotMatch(JSON.stringify(listed.details ?? {}), /pw-one/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("vault_get: a confirm window, then a grant reused without another window", async () => {
|
|
63
|
+
const b = bind(aiVault, true);
|
|
64
|
+
const ctx = makeCtx().ctx;
|
|
65
|
+
answers.master(dir, PW);
|
|
66
|
+
const before = prompts("--question");
|
|
67
|
+
const first = await b.tool("vault_get", { name: "GitHub", reason: "test" }, ctx);
|
|
68
|
+
assert.equal(first.details.via, "interactive-grant");
|
|
69
|
+
assert.match(textOf(first), /pw-one/);
|
|
70
|
+
|
|
71
|
+
const second = await b.tool("vault_get", { name: "GitHub" }, ctx);
|
|
72
|
+
assert.equal(second.details.via, "existing-grant");
|
|
73
|
+
assert.equal(prompts("--question"), before + 1, "a live grant must not open a second window");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("a denied window is reported and not retried", async () => {
|
|
77
|
+
const b = bind(aiVault, true);
|
|
78
|
+
const ctx = makeCtx().ctx;
|
|
79
|
+
answers.master(dir, PW);
|
|
80
|
+
answers.deny(dir);
|
|
81
|
+
const result = await b.tool("vault_get", { name: "GitHub" }, ctx);
|
|
82
|
+
assert.equal(result.isError, true);
|
|
83
|
+
assert.equal(result.details.denied, true);
|
|
84
|
+
assert.match(textOf(result), /do not retry/);
|
|
85
|
+
answers.deny(dir, false);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("a subagent has its own session: the parent's grant is not inherited", async () => {
|
|
89
|
+
const parent = bind(aiVault, true);
|
|
90
|
+
const sub = bind(aiVault, true);
|
|
91
|
+
const pc = makeCtx().ctx;
|
|
92
|
+
answers.master(dir, PW);
|
|
93
|
+
await parent.tool("vault_get", { name: "GitHub" }, pc);
|
|
94
|
+
|
|
95
|
+
const before = prompts("--question");
|
|
96
|
+
answers.master(dir, PW);
|
|
97
|
+
const result = await sub.tool("vault_get", { name: "GitHub" }, subCtx().ctx);
|
|
98
|
+
assert.match(textOf(result), /pw-one/);
|
|
99
|
+
assert.equal(prompts("--question"), before + 1, "the subagent needs its own approval");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("/new drops only the binding that switched", async () => {
|
|
103
|
+
const a = bind(aiVault, true);
|
|
104
|
+
const b = bind(aiVault, true);
|
|
105
|
+
const ac = makeCtx({ sessionId: "11111111-0000-4000-8000-000000000001" }).ctx;
|
|
106
|
+
const bc = makeCtx({ sessionId: "22222222-0000-4000-8000-000000000002" }).ctx;
|
|
107
|
+
answers.master(dir, PW);
|
|
108
|
+
await a.cmd("unlock", ac);
|
|
109
|
+
answers.master(dir, PW);
|
|
110
|
+
await b.cmd("unlock", bc);
|
|
111
|
+
|
|
112
|
+
await a.fire("session_switch", { reason: "new" }, ac);
|
|
113
|
+
|
|
114
|
+
answers.master(dir, PW);
|
|
115
|
+
const before = masterPrompts();
|
|
116
|
+
assert.equal((await a.tool("vault_list", {}, ac)).isError, undefined);
|
|
117
|
+
assert.equal(masterPrompts(), before + 1, "the switched binding unlocks again");
|
|
118
|
+
|
|
119
|
+
const untouched = await b.tool("vault_list", {}, bc);
|
|
120
|
+
assert.equal(untouched.isError, undefined);
|
|
121
|
+
assert.equal(masterPrompts(), before + 1, "the other binding keeps its token");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("a missing entry names what is available", async () => {
|
|
125
|
+
const b = bind(aiVault, true);
|
|
126
|
+
const ctx = makeCtx().ctx;
|
|
127
|
+
answers.master(dir, PW);
|
|
128
|
+
const result = await b.tool("vault_get", { name: "nope" }, ctx);
|
|
129
|
+
assert.equal(result.isError, true);
|
|
130
|
+
assert.match(textOf(result), /no such entry/);
|
|
131
|
+
assert.match(textOf(result), /GitHub/);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("a local miss falls back to Bitwarden: read once, no import", async () => {
|
|
135
|
+
const b = bind(aiVault, true);
|
|
136
|
+
const ctx = makeCtx().ctx;
|
|
137
|
+
answers.master(dir, PW);
|
|
138
|
+
await b.cmd("unlock", ctx);
|
|
139
|
+
// The Bitwarden mock unlocks with "testmaster"; the two windows are the
|
|
140
|
+
// read approval (yes) and the import offer (no).
|
|
141
|
+
answers.master(dir, "testmaster");
|
|
142
|
+
answers.questions(dir, "y", "n");
|
|
143
|
+
const result = await b.tool("vault_get", { name: "TestService" }, ctx);
|
|
144
|
+
assert.equal(result.details.via, "bitwarden-once");
|
|
145
|
+
assert.match(textOf(result), /supersecret-value-123/);
|
|
146
|
+
|
|
147
|
+
answers.master(dir, PW);
|
|
148
|
+
const listed = await b.tool("vault_list", {}, ctx);
|
|
149
|
+
assert.equal(
|
|
150
|
+
JSON.parse(textOf(listed)).some((i) => i.name === "TestService"),
|
|
151
|
+
false,
|
|
152
|
+
"a one-off read must not write the entry",
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("a local miss can be imported from Bitwarden", async () => {
|
|
157
|
+
const b = bind(aiVault, true);
|
|
158
|
+
const ctx = makeCtx().ctx;
|
|
159
|
+
answers.master(dir, PW);
|
|
160
|
+
await b.cmd("unlock", ctx);
|
|
161
|
+
answers.master(dir, "testmaster");
|
|
162
|
+
answers.questions(dir, "y", "y");
|
|
163
|
+
const result = await b.tool("vault_get", { name: "TestService" }, ctx);
|
|
164
|
+
assert.equal(result.details.via, "imported-from-bitwarden");
|
|
165
|
+
assert.match(textOf(result), /supersecret-value-123/);
|
|
166
|
+
|
|
167
|
+
answers.master(dir, PW);
|
|
168
|
+
const listed = await b.tool("vault_list", {}, ctx);
|
|
169
|
+
assert.equal(
|
|
170
|
+
JSON.parse(textOf(listed)).some((i) => i.name === "TestService"),
|
|
171
|
+
true,
|
|
172
|
+
"the import must land in the local vault",
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("a denied Bitwarden read is not retried", async () => {
|
|
177
|
+
const b = bind(aiVault, true);
|
|
178
|
+
const ctx = makeCtx().ctx;
|
|
179
|
+
answers.master(dir, PW);
|
|
180
|
+
await b.cmd("unlock", ctx);
|
|
181
|
+
answers.master(dir, "testmaster");
|
|
182
|
+
answers.questions(dir, "n");
|
|
183
|
+
const result = await b.tool("vault_get", { name: "OtherService" }, ctx);
|
|
184
|
+
assert.equal(result.isError, true);
|
|
185
|
+
assert.match(textOf(result), /do not retry/);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("never mode reads without any window, and the switch needs the password", async () => {
|
|
189
|
+
const bin = process.env.AI_VAULT_BIN;
|
|
190
|
+
const switchMode = (mode) =>
|
|
191
|
+
spawnSync(bin, ["mode", "set", mode, "--password-stdin"], {
|
|
192
|
+
input: `${PW}\n`,
|
|
193
|
+
env: process.env,
|
|
194
|
+
encoding: "utf8",
|
|
195
|
+
});
|
|
196
|
+
const toNever = switchMode("never");
|
|
197
|
+
assert.equal(toNever.status, 0, toNever.stderr);
|
|
198
|
+
|
|
199
|
+
const b = bind(aiVault, true);
|
|
200
|
+
const ctx = makeCtx().ctx;
|
|
201
|
+
const before = prompts("--question") + prompts("--password");
|
|
202
|
+
const result = await b.tool("vault_get", { name: "GitHub" }, ctx);
|
|
203
|
+
assert.equal(result.details.via, "no-password");
|
|
204
|
+
assert.match(textOf(result), /pw-one/);
|
|
205
|
+
assert.equal(prompts("--question") + prompts("--password"), before, "no window in never mode");
|
|
206
|
+
|
|
207
|
+
// A wrong password must not change the policy.
|
|
208
|
+
const wrong = spawnSync(bin, ["mode", "set", "session", "--password-stdin"], {
|
|
209
|
+
input: "definitely-wrong\n",
|
|
210
|
+
env: process.env,
|
|
211
|
+
encoding: "utf8",
|
|
212
|
+
});
|
|
213
|
+
assert.equal(wrong.status, 5);
|
|
214
|
+
|
|
215
|
+
const back = switchMode("session");
|
|
216
|
+
assert.equal(back.status, 0, back.stderr);
|
|
217
|
+
});
|