@sparkvault/sdk-js 8.11.3 → 8.11.5

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.
@@ -87,6 +87,21 @@ class PopupBlockedError extends SparkVaultError {
87
87
  Object.setPrototypeOf(this, PopupBlockedError.prototype);
88
88
  }
89
89
  }
90
+ /**
91
+ * The popup opened but its window handle went dead before the page ever
92
+ * pinged back — the signature of a Cross-Origin-Opener-Policy severance
93
+ * (a host page serving `COOP: same-origin` detaches any cross-origin popup:
94
+ * the popup loses `window.opener`, the opener's handle reports `closed`).
95
+ * postMessage can never work in this state, so retrying the popup is futile;
96
+ * callers should fall back to the redirect flow.
97
+ */
98
+ class PopupSeveredError extends SparkVaultError {
99
+ constructor() {
100
+ super('The sign-in window could not communicate with this page.', 'popup_severed');
101
+ this.name = 'PopupSeveredError';
102
+ Object.setPrototypeOf(this, PopupSeveredError.prototype);
103
+ }
104
+ }
90
105
 
91
106
  /**
92
107
  * SparkVault-owned domain registry.
@@ -714,6 +729,24 @@ function base64urlEncode(data) {
714
729
  function arrayBufferToBase64url(buffer) {
715
730
  return base64urlEncode(new Uint8Array(buffer));
716
731
  }
732
+ /**
733
+ * Encode a string as STANDARD base64 (with padding) of its UTF-8 bytes.
734
+ *
735
+ * Unlike base64urlEncode this keeps `+`/`/` and padding, so it round-trips
736
+ * through a server-side standard-base64 decoder. Used to carry arbitrary
737
+ * Unicode through an HTTP header value (which is limited to latin1 bytes).
738
+ *
739
+ * @param value - String to encode
740
+ * @returns Standard base64 string
741
+ */
742
+ function base64EncodeUtf8(value) {
743
+ const bytes = new TextEncoder().encode(value);
744
+ let binary = '';
745
+ for (let i = 0; i < bytes.length; i++) {
746
+ binary += String.fromCharCode(bytes[i]);
747
+ }
748
+ return btoa(binary);
749
+ }
717
750
  /**
718
751
  * Decode base64url string to Uint8Array
719
752
  *
@@ -1461,6 +1494,16 @@ const DEFAULT_HEIGHT = 640;
1461
1494
  const DEFAULT_TIMEOUT_MS = 30 * 60 * 1000;
1462
1495
  /** How often to check whether the user closed the popup manually. */
1463
1496
  const CLOSED_POLL_INTERVAL_MS = 500;
1497
+ /**
1498
+ * Window after opening within which `popup.closed === true` with NO ready ping
1499
+ * is read as a severed handle, not a manual close. A COOP-severed handle
1500
+ * (host page serves `Cross-Origin-Opener-Policy: same-origin`) flips to
1501
+ * `closed` on the first poll while the real window stays open in front of the
1502
+ * user; a human closing a window they asked for takes longer than this. A slow
1503
+ * network can only delay the ready ping — it never makes `closed` true — so a
1504
+ * legitimate popup on a slow connection cannot trip this.
1505
+ */
1506
+ const SEVERED_DETECT_WINDOW_MS = 2500;
1464
1507
  /**
1465
1508
  * Open a popup and resolve with the first validated result envelope it posts.
1466
1509
  *
@@ -1486,7 +1529,9 @@ class PopupLauncher {
1486
1529
  return;
1487
1530
  }
1488
1531
  options.onOpen?.(popup);
1532
+ const openedAt = Date.now();
1489
1533
  let settled = false;
1534
+ let readyReceived = false;
1490
1535
  const cleanup = () => {
1491
1536
  window.removeEventListener('message', handleMessage);
1492
1537
  clearTimeout(timeoutId);
@@ -1501,6 +1546,7 @@ class PopupLauncher {
1501
1546
  const data = event.data;
1502
1547
  // A "ready" ping confirms the window loaded; notify but don't settle.
1503
1548
  if (options.readyType && data?.type === options.readyType) {
1549
+ readyReceived = true;
1504
1550
  options.onReady?.();
1505
1551
  return;
1506
1552
  }
@@ -1524,6 +1570,14 @@ class PopupLauncher {
1524
1570
  if (popup.closed && !settled) {
1525
1571
  settled = true;
1526
1572
  cleanup();
1573
+ // `closed` going true right after open, before the page ever pinged,
1574
+ // is a dead handle (COOP severance — see SEVERED_DETECT_WINDOW_MS),
1575
+ // not a person closing the window: the real window is still open but
1576
+ // can never postMessage back, so retrying the popup can never work.
1577
+ if (!readyReceived && Date.now() - openedAt <= SEVERED_DETECT_WINDOW_MS) {
1578
+ reject(new PopupSeveredError());
1579
+ return;
1580
+ }
1527
1581
  reject(new UserCancelledError('Authentication was cancelled.'));
1528
1582
  }
1529
1583
  }, CLOSED_POLL_INTERVAL_MS);
@@ -8044,12 +8098,35 @@ class DialogLauncher {
8044
8098
  handleError(error) {
8045
8099
  if (this.settled)
8046
8100
  return;
8101
+ // Severed handle (COOP: same-origin on the host page): the popup is open in
8102
+ // front of the person and working, but postMessage can never deliver and a
8103
+ // retry popup severs identically — the ONLY flow that can complete is the
8104
+ // redirect flow, so switch to it. When the owner can't arm the redirect
8105
+ // return leg it resolves false and we degrade to the retry alert.
8106
+ if (error instanceof PopupSeveredError && this.callbacks.onPopupSevered) {
8107
+ this.callbacks.onPopupSevered().then((navigating) => {
8108
+ if (this.settled)
8109
+ return;
8110
+ if (!navigating) {
8111
+ this.callbacks.onPopupClosedUnresolved?.();
8112
+ this.showAlert();
8113
+ }
8114
+ // Navigating: this page is unloading into the redirect flow; keep the
8115
+ // bridge up (mirrors flow:'redirect', whose promise never settles here).
8116
+ }).catch(() => {
8117
+ if (this.settled)
8118
+ return;
8119
+ this.callbacks.onPopupClosedUnresolved?.();
8120
+ this.showAlert();
8121
+ });
8122
+ return;
8123
+ }
8047
8124
  // Blocked (never opened) or closed (gone) - the popup can't be confirmed,
8048
8125
  // so surface the warning so the user can reopen it. Either way the opener
8049
8126
  // observed the end with no result, so there is nothing for the next-load
8050
8127
  // self-heal to recover (a late success message still wins: this branch
8051
8128
  // leaves the flow unsettled, and onSuccess re-clears regardless).
8052
- if (error instanceof PopupBlockedError || error instanceof UserCancelledError) {
8129
+ if (error instanceof PopupBlockedError || error instanceof UserCancelledError || error instanceof PopupSeveredError) {
8053
8130
  this.callbacks.onPopupClosedUnresolved?.();
8054
8131
  this.showAlert();
8055
8132
  return;
@@ -8757,6 +8834,10 @@ class IdentityModule {
8757
8834
  // while the retry alert is up); "Try again" re-arms it.
8758
8835
  onPopupClosedUnresolved: () => this.clearSignInPending(),
8759
8836
  onPopupReopened: () => this.markSignInPending(),
8837
+ // COOP-severed popup: postMessage can never deliver, so switch this tab
8838
+ // to the redirect flow (returns via handleRedirectResult). The pending
8839
+ // marker stays armed — the returned #sv_code fragment retires it.
8840
+ onPopupSevered: () => this.startRedirectFlow(mergedOptions),
8760
8841
  });
8761
8842
  this.launcher.start();
8762
8843
  return settledPromise;
@@ -12308,6 +12389,14 @@ class VaultUploadModule {
12308
12389
  * operations live in their own modules; this module owns the vault record,
12309
12390
  * its sharing/upload configuration, and the browser upload widget.
12310
12391
  */
12392
+ /** A string every char of which fits in a single header byte (latin1). */
12393
+ function isLatin1(value) {
12394
+ for (let i = 0; i < value.length; i++) {
12395
+ if (value.charCodeAt(i) > 0xff)
12396
+ return false;
12397
+ }
12398
+ return true;
12399
+ }
12311
12400
  class VaultsModule {
12312
12401
  constructor(config, http) {
12313
12402
  this.http = http;
@@ -12449,9 +12538,12 @@ class VaultsModule {
12449
12538
  /**
12450
12539
  * Delete a vault and all its ingots. This action is irreversible!
12451
12540
  *
12452
- * Requires the vault's exact name as confirmation. It is sent as the
12453
- * `X-Confirm-Name` header and must match the vault name exactly
12454
- * (case-sensitive).
12541
+ * Requires the vault's exact name as confirmation, matched case-sensitively.
12542
+ * The name is sent base64(UTF-8) as the `X-Confirm-Name-B64` header so a vault
12543
+ * named with any Unicode (emoji, CJK, the U+202F in macOS-style names) is
12544
+ * deletable — an HTTP header value cannot carry non-latin1 characters. The
12545
+ * legacy raw `X-Confirm-Name` header is included only for latin1 names, so
12546
+ * older API servers keep working.
12455
12547
  *
12456
12548
  * @example
12457
12549
  * await sv.vaults.delete('vlt_abc', 'My Vault');
@@ -12461,9 +12553,13 @@ class VaultsModule {
12461
12553
  if (!name) {
12462
12554
  throw new ValidationError('Vault name is required to confirm deletion');
12463
12555
  }
12464
- await this.http.delete(`/v1/vaults/${cleanId}`, {
12465
- headers: { 'X-Confirm-Name': name },
12466
- });
12556
+ const headers = {
12557
+ 'X-Confirm-Name-B64': base64EncodeUtf8(name),
12558
+ };
12559
+ if (isLatin1(name)) {
12560
+ headers['X-Confirm-Name'] = name;
12561
+ }
12562
+ await this.http.delete(`/v1/vaults/${cleanId}`, { headers });
12467
12563
  }
12468
12564
  /**
12469
12565
  * Get a vault's public-sharing configuration.