passcontrol 0.9.3 → 0.9.4

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.
@@ -86,6 +86,7 @@ import { defaultAllowedModelForProvider } from "../cli/integration-defaults.mjs"
86
86
  import {
87
87
  PASSPORT_KEY_STORAGE_OS,
88
88
  createPassportCredentialStore,
89
+ importPassportKey,
89
90
  keyStorageDeclaration,
90
91
  migratePassportKey,
91
92
  } from "../cli/passport-key-store.mjs";
@@ -2771,34 +2772,30 @@ async function passportCommand(rest, opts = {}) {
2771
2772
  throw new Error("Passport secret does not match the supplied Passport ID. Nothing was stored.");
2772
2773
  }
2773
2774
 
2775
+ // The write/readback/commit sequence and its rollbacks live in
2776
+ // importPassportKey, which is where they can be tested: nothing in this file
2777
+ // is exported, and the store is built here with no seam, so a test of the
2778
+ // rollback would have to reach the operator's real Keychain.
2774
2779
  const store = createPassportCredentialStore();
2775
- const written = store.write(passportId, secret);
2776
- if (!written.ok) throw new Error(`${written.reason}. Nothing was written to the PassControl config.`);
2777
- const readback = store.read(passportId);
2778
- if (!readback.ok || readback.secret !== secret) {
2779
- store.delete(passportId);
2780
- throw new Error(`${store.name} did not return the key that was just stored. The new item was removed and config was not changed.`);
2781
- }
2782
-
2783
- try {
2784
- mergeConfigFileAtomic(target, {
2780
+ const result = importPassportKey({
2781
+ passportId,
2782
+ secret,
2783
+ store,
2784
+ oldId,
2785
+ oldStorage,
2786
+ commitConfig: () => mergeConfigFileAtomic(target, {
2785
2787
  PASSCONTROL_GATEWAY: gateway,
2786
2788
  PASSPORT_ID: passportId,
2787
2789
  PASSPORT_SECRET: "",
2788
2790
  PASSPORT_KEY_STORAGE: PASSPORT_KEY_STORAGE_OS,
2789
- });
2790
- } catch (error) {
2791
- store.delete(passportId);
2792
- throw error;
2793
- }
2791
+ }),
2792
+ });
2793
+ // The message already carries the filesystem detail: main()'s handler prints
2794
+ // `error.message` and nothing else, so a bare `cause` would never be seen.
2795
+ if (!result.ok) throw new Error(result.message, { cause: result.cause });
2796
+ if (result.warning) warn(result.warning);
2794
2797
 
2795
- if (oldStorage === PASSPORT_KEY_STORAGE_OS && oldId && oldId !== passportId) {
2796
- const removed = store.delete(oldId);
2797
- if (!removed.ok) {
2798
- warn(`The previous Passport key could not be confirmed removed from ${store.name}; the new import is active, but the old OS item needs manual cleanup.`);
2799
- }
2800
- }
2801
- ok(`Passport ${passportId} imported into ${store.name}.`);
2798
+ ok(result.message);
2802
2799
  step(`Global gateway: ${gateway}`);
2803
2800
  step("The private key was verified and never written to the config file or command line.");
2804
2801
  }
@@ -433,3 +433,109 @@ export function migratePassportKey({ passportId, secret, store, removeFileSecret
433
433
  message: `Migrated to tier 1 — ${store.name}; verified readback before removing the file key.`,
434
434
  };
435
435
  }
436
+
437
+ /**
438
+ * Move a verified passport secret into the OS store and commit the config that
439
+ * points at it, with one invariant holding the rollbacks together:
440
+ *
441
+ * **this function may only delete an item that it created.**
442
+ *
443
+ * Both failure paths below used to end in `store.delete(passportId)`, on the
444
+ * reasoning that the item under that id was the one just written. That is true
445
+ * exactly until `--replace` is used to re-import the SAME passport id, which it
446
+ * permits. OS storage is keyed by the id — macOS `add-generic-password -U`
447
+ * updates in place, and the DPAPI blob path is derived from the id — so in that
448
+ * one case the item removed is the operator's existing key, and the config left
449
+ * behind still points at a store that no longer has it.
450
+ *
451
+ * The pre-existence probe is UNCONDITIONAL, and not gated on the config saying
452
+ * `PASSPORT_KEY_STORAGE=os` for this id. The config is not authoritative about
453
+ * what the store contains: `cli/logout.mjs` blanks that marker in the same write
454
+ * that asks the store to delete, and the store is allowed to report that it
455
+ * could not confirm the removal — so an item can outlive the marker. The store
456
+ * is the only authority on its own contents. The cost is one extra read per
457
+ * import, which on a locked Keychain may mean one extra unlock prompt.
458
+ *
459
+ * The honest limit, and it is the same one `confirmGone` states above: a failed
460
+ * `read()` does not prove absence. A locked store reads as empty, so a rollback
461
+ * can still remove a real item. This narrows the window to that case; it does
462
+ * not close it, because the store cannot give positive evidence of absence.
463
+ *
464
+ * `commitConfig` is injected rather than called directly for the same reason
465
+ * `migratePassportKey` takes `removeFileSecret`: the rollback is the behaviour
466
+ * worth testing, and it is only reachable if the commit can be made to fail.
467
+ */
468
+ export function importPassportKey({
469
+ passportId,
470
+ secret,
471
+ store,
472
+ oldId = "",
473
+ oldStorage = "",
474
+ commitConfig,
475
+ }) {
476
+ if (!passportId || !secret) {
477
+ return { ok: false, message: "A Passport ID and secret are required." };
478
+ }
479
+
480
+ const existing = store.read(passportId);
481
+ const preExisting = existing.ok && typeof existing.secret === "string" && existing.secret.length > 0;
482
+ // The caller proves `secret` derives `passportId` before reaching here, so a
483
+ // same-id import is provably the same key the store already holds. Writing it
484
+ // changes nothing and only opens a window in which a rollback could take it.
485
+ const alreadyStored = preExisting && sameSecret(secret, existing.secret);
486
+
487
+ if (!alreadyStored) {
488
+ const written = store.write(passportId, secret);
489
+ if (!written.ok) {
490
+ // Nothing was created, so there is nothing to roll back.
491
+ return { ok: false, message: `${written.reason}. Nothing was written to the PassControl config.` };
492
+ }
493
+
494
+ const readback = store.read(passportId);
495
+ if (!readback.ok || !sameSecret(secret, readback.secret)) {
496
+ if (!preExisting) store.delete(passportId);
497
+ return {
498
+ ok: false,
499
+ message: preExisting
500
+ ? `${store.name} did not return the key that was just stored. The item already under this Passport ID was left in place, and config was not changed.`
501
+ : `${store.name} did not return the key that was just stored. The new item was removed and config was not changed.`,
502
+ };
503
+ }
504
+ }
505
+
506
+ try {
507
+ commitConfig();
508
+ } catch (error) {
509
+ if (!preExisting) store.delete(passportId);
510
+ // The CLI's top-level handler prints `error.message` and nothing else, so
511
+ // the filesystem detail has to travel inside the message or the operator
512
+ // loses the only line that says which path failed. An fs error names a
513
+ // path, not a key — the collapse-to-fixed-text rule above is about
514
+ // subprocess stderr, which can echo the secret back.
515
+ const detail = error instanceof Error ? error.message : String(error);
516
+ return {
517
+ ok: false,
518
+ cause: error,
519
+ message: preExisting
520
+ ? `The PassControl config could not be written; the key already in ${store.name} was left in place. ${detail}`
521
+ : `The PassControl config could not be written; the new item was removed from ${store.name}. ${detail}`,
522
+ };
523
+ }
524
+
525
+ let warning = null;
526
+ if (oldStorage === PASSPORT_KEY_STORAGE_OS && oldId && oldId !== passportId) {
527
+ const removed = store.delete(oldId);
528
+ if (!removed.ok) {
529
+ warning = `The previous Passport key could not be confirmed removed from ${store.name}; the new import is active, but the old OS item needs manual cleanup.`;
530
+ }
531
+ }
532
+
533
+ return {
534
+ ok: true,
535
+ alreadyStored,
536
+ warning,
537
+ message: alreadyStored
538
+ ? `${store.name} already held this Passport key; it was verified and left in place.`
539
+ : `Passport ${passportId} imported into ${store.name}.`,
540
+ };
541
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "passcontrol",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "Identity, scope and budget controls for AI agents while provider keys stay server-side.",
5
5
  "license": "BUSL-1.1",
6
6
  "homepage": "https://passcontrol.vertias.eu",