@xiaolikt/ai-vault 0.10.0 → 0.12.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.
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
3
3
  import { spawnSync } from "node:child_process";
4
4
  import { existsSync } from "node:fs";
5
5
  import { join } from "node:path";
6
- import { answers, bind, cleanUpOnExit, dialogTrace, makeCtx, subCtx, textOf, useMocks } from "./helpers.mjs";
6
+ import { answers, bind, cleanUpOnExit, dialogTrace, makeCtx, setSetting, subCtx, textOf, useMocks } from "./helpers.mjs";
7
7
 
8
8
  // The extension is only a client: these drive the real ai-vault binary (built
9
9
  // by `npm test`) plus the mock zenity, with a throwaway store.
@@ -188,29 +188,22 @@ test("a denied Bitwarden read is not retried", async () => {
188
188
 
189
189
  test("never mode reads without any window, and the switch needs the password", async () => {
190
190
  const bin = process.env.AI_VAULT_BIN;
191
- const switchMode = (mode) =>
192
- spawnSync(bin, ["mode", "set", mode, "--password-stdin"], {
193
- input: `${PW}\n`,
194
- env: process.env,
195
- encoding: "utf8",
196
- });
191
+ // `mode set` reads and writes the settings inside the store: it needs an
192
+ // unlocked session (the helper opens one) and the master password.
193
+ const switchMode = (mode) => setSetting("vault.unlock", mode);
197
194
  const toNever = switchMode("never");
198
- assert.equal(toNever.status, 0, toNever.stderr);
195
+ assert.equal(toNever.status, 0, "set never: " + toNever.stderr + " | env HOST=" + process.env.AI_VAULT_HOST + " SECRET=" + (process.env.AI_VAULT_HOST_SECRET ? "set" : "unset"));
199
196
 
200
197
  const b = bind(aiVault, true);
201
198
  const ctx = makeCtx().ctx;
202
199
  const before = prompts("--question") + prompts("--password");
203
200
  const result = await b.tool("vault_get", { name: "GitHub" }, ctx);
204
- assert.equal(result.details.via, "no-password");
201
+ assert.equal(result.details.via, "no-password", textOf(result));
205
202
  assert.match(textOf(result), /pw-one/);
206
203
  assert.equal(prompts("--question") + prompts("--password"), before, "no window in never mode");
207
204
 
208
205
  // A wrong password must not change the policy.
209
- const wrong = spawnSync(bin, ["mode", "set", "session", "--password-stdin"], {
210
- input: "definitely-wrong\n",
211
- env: process.env,
212
- encoding: "utf8",
213
- });
206
+ const wrong = setSetting("vault.unlock", "session", { password: "definitely-wrong" });
214
207
  assert.equal(wrong.status, 5);
215
208
 
216
209
  const back = switchMode("session");
@@ -219,7 +212,7 @@ test("never mode reads without any window, and the switch needs the password", a
219
212
 
220
213
  test("bitwarden.import=never reads once with a single window", async () => {
221
214
  const bin = process.env.AI_VAULT_BIN;
222
- spawnSync(bin, ["settings", "bitwarden.import", "never"], { env: process.env });
215
+ setSetting("bitwarden.import", "never");
223
216
  forgetSettings();
224
217
  const b = bind(aiVault, true);
225
218
  const ctx = makeCtx().ctx;
@@ -235,7 +228,7 @@ test("bitwarden.import=never reads once with a single window", async () => {
235
228
 
236
229
  test("bitwarden.import=always pulls the entry in without a window", async () => {
237
230
  const bin = process.env.AI_VAULT_BIN;
238
- spawnSync(bin, ["settings", "bitwarden.import", "always"], { env: process.env });
231
+ setSetting("bitwarden.import", "always");
239
232
  forgetSettings();
240
233
  const b = bind(aiVault, true);
241
234
  const ctx = makeCtx().ctx;
@@ -253,16 +246,24 @@ test("bitwarden.import=always pulls the entry in without a window", async () =>
253
246
  JSON.parse(textOf(listed)).some((i) => i.name === "OtherService"),
254
247
  true,
255
248
  );
256
- spawnSync(bin, ["settings", "bitwarden.import", "ask"], { env: process.env });
249
+ setSetting("bitwarden.import", "ask");
257
250
  forgetSettings();
258
251
  });
259
252
 
260
253
  test("a persisted Bitwarden session costs no password", async () => {
261
254
  const bin = process.env.AI_VAULT_BIN;
262
- const set = (key, value) => spawnSync(bin, ["settings", key, value], { env: process.env, encoding: "utf8" });
255
+ const set = (key, value) => setSetting(key, value);
263
256
  assert.equal(set("bitwarden.unlock", "never").status, 0);
264
257
  forgetSettings();
265
- const put = spawnSync(bin, ["bw-session", "put"], { input: "GOOD_SESSION", env: process.env, encoding: "utf8" });
258
+ answers.master(dir, PW);
259
+ await bind(aiVault, true).cmd("unlock", makeCtx().ctx);
260
+ const { settingsToken: st, settingsCli: sc } = await import("../packages/pi-ai-vault/src/settings.ts");
261
+ forgetSettings();
262
+ const put = spawnSync(bin, ["bw-session", "put", "--token", st() ?? ""], {
263
+ input: "GOOD_SESSION",
264
+ env: { ...process.env, AI_VAULT_HOST: "1", AI_VAULT_HOST_SECRET: sc().host.value },
265
+ encoding: "utf8",
266
+ });
266
267
  assert.equal(put.status, 0, put.stderr);
267
268
 
268
269
  const b = bind(aiVault, true);
@@ -313,3 +314,60 @@ test("a relayed read approval is signed by the host: the raw CLI refuses a bare
313
314
  assert.match(tampered.stderr, /modified outside ai-vault/);
314
315
  assert.equal(readdirSync(dir).includes(`${token}.json`), false, "the tampered file is deleted");
315
316
  });
317
+
318
+ test("vault_add stores a card: non-secret fields from the model, the CVV typed in a dialog", async () => {
319
+ const b = bind(aiVault, true);
320
+ const ctx = makeCtx().ctx;
321
+ answers.master(dir, PW);
322
+ answers.questions(dir, "y");
323
+ answers.entryPassword(dir, "000");
324
+ const result = await b.tool(
325
+ "vault_add",
326
+ {
327
+ name: "MyCard",
328
+ type: "card",
329
+ fields: { brand: "Mastercard", cardholderName: "Li Jun", expMonth: "01", expYear: "2030", code: undefined },
330
+ },
331
+ ctx,
332
+ );
333
+ assert.equal(result.isError, undefined, textOf(result));
334
+ assert.match(textOf(result), /as a card/);
335
+ // The list shows the type and the non-secret fields, never the code.
336
+ answers.master(dir, PW);
337
+ const listed = JSON.parse(textOf(await b.tool("vault_list", {}, ctx)));
338
+ const card = listed.find((i) => i.name === "MyCard");
339
+ assert.equal(card.type, "card");
340
+ assert.equal(card.fields.brand, "Mastercard");
341
+ assert.equal(JSON.stringify(listed).includes("000"), false);
342
+ });
343
+
344
+ test("vault_add refuses a secret the model tried to supply itself", async () => {
345
+ const b = bind(aiVault, true);
346
+ const ctx = makeCtx().ctx;
347
+ answers.master(dir, PW);
348
+ const result = await b.tool("vault_add", { name: "Leaky", type: "card", fields: { code: "123" } }, ctx);
349
+ assert.equal(result.isError, true);
350
+ assert.match(textOf(result), /secret field/);
351
+ });
352
+
353
+ test("vault_get falls back to Bitwarden for a non-login hit and imports it with its fields", async () => {
354
+ const b = bind(aiVault, true);
355
+ const ctx = makeCtx().ctx;
356
+ answers.master(dir, PW);
357
+ await b.cmd("unlock", ctx);
358
+ answers.master(dir, "testmaster");
359
+ answers.questions(dir, "y", "y");
360
+ const result = await b.tool("vault_get", { name: "CorpCard" }, ctx);
361
+ assert.equal(result.details.via, "imported-from-bitwarden");
362
+ const body = JSON.parse(textOf(result));
363
+ assert.equal(body.type, "card");
364
+ assert.equal(body.data.number, "4111111111111111");
365
+ assert.equal(body.data.code, "123");
366
+ answers.master(dir, PW);
367
+ const listed = JSON.parse(textOf(await b.tool("vault_list", {}, ctx)));
368
+ const card = listed.find((i) => i.name === "CorpCard");
369
+ assert.equal(card.type, "card");
370
+ assert.equal(card.fields.cardholderName, "Li Jun");
371
+ assert.equal(JSON.stringify(listed).includes("4111111111111111"), false, "list must mask the card number");
372
+ });
373
+