@tokenoftrust/storefront-runner 4.2.3 → 4.3.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.
@@ -90,12 +90,17 @@ export function lazyCheckoutLoaderJs(options: LazyCheckoutLoaderOptions): string
90
90
  try { return new URL(url, document.baseURI).origin === cartOrigin; } catch (e) { return false; }
91
91
  }
92
92
  // Foxy copies the Referer onto cart_cancel_and_continue_link and ignores a
93
- // continue parameter. The document policy stays strict-origin. A cart script
94
- // snapshots the Referer while its src is assigned, so that assignment uses the
95
- // store-home URL and restores the page before the turn ends. A cart navigation
96
- // sends the full document URL through the element's referrerPolicy. That URL
97
- // already includes the store origin and tenant path, and the history entry
98
- // stays the page that opened the cart.
93
+ // continue parameter. The document policy is strict-origin-when-cross-origin,
94
+ // so a cross-origin request drops the tenant path. A cart script snapshots the
95
+ // Referer while its src is assigned: that assignment uses the store-home URL
96
+ // (origin plus cookie path) and restores the page before the turn ends. A cart
97
+ // navigation uses that same URL, then puts the shopper's URL back when the page
98
+ // hides or is restored from the back-forward cache, so Back returns to the page
99
+ // that opened the cart. A form has no referrerPolicy, so a cart submit also sets
100
+ // a referrer meta for that navigation and puts the document policy back after it.
101
+ var pendingReturn = null;
102
+ var referrerMeta = null;
103
+ var documentReferrerPolicy = "strict-origin-when-cross-origin";
99
104
  function storeHome() {
100
105
  return location.origin + cookiePath;
101
106
  }
@@ -107,6 +112,41 @@ export function lazyCheckoutLoaderJs(options: LazyCheckoutLoaderOptions): string
107
112
  function restoreHref(current) {
108
113
  if (location.href !== current) window.history.replaceState(window.history.state, "", current);
109
114
  }
115
+ function revealForNavigation() {
116
+ var current = location.href;
117
+ if (current === storeHome()) return;
118
+ pendingReturn = current;
119
+ revealStoreHome();
120
+ }
121
+ function restorePendingReturn() {
122
+ if (!pendingReturn) return;
123
+ var back = pendingReturn;
124
+ restoreHref(back);
125
+ if (location.href === back) pendingReturn = null;
126
+ }
127
+ function loosenReferrer() {
128
+ if (!referrerMeta) {
129
+ referrerMeta = document.createElement("meta");
130
+ referrerMeta.setAttribute("name", "referrer");
131
+ (document.head || document.documentElement).appendChild(referrerMeta);
132
+ }
133
+ referrerMeta.setAttribute("content", referrerPolicy);
134
+ }
135
+ function tightenReferrer() {
136
+ if (!referrerMeta) return;
137
+ referrerMeta.setAttribute("content", documentReferrerPolicy);
138
+ }
139
+ function endCartNavigation() {
140
+ restorePendingReturn();
141
+ tightenReferrer();
142
+ }
143
+ document.addEventListener("visibilitychange", function () {
144
+ if (document.visibilityState === "hidden") endCartNavigation();
145
+ });
146
+ window.addEventListener("pagehide", endCartNavigation);
147
+ window.addEventListener("pageshow", function (event) {
148
+ if (event.persisted) endCartNavigation();
149
+ });
110
150
  var origCreate = document.createElement;
111
151
  document.createElement = function (name) {
112
152
  var el = origCreate.apply(document, arguments);
@@ -254,6 +294,7 @@ export function lazyCheckoutLoaderJs(options: LazyCheckoutLoaderOptions): string
254
294
  anchor.referrerPolicy = referrerPolicy;
255
295
  if (state === 'ready') {
256
296
  stampAnchor(anchor);
297
+ if (!event.defaultPrevented) revealForNavigation();
257
298
  return;
258
299
  }
259
300
  if (event.defaultPrevented) return;
@@ -314,6 +355,11 @@ export function lazyCheckoutLoaderJs(options: LazyCheckoutLoaderOptions): string
314
355
  if (!cartForm || state === 'ready') {
315
356
  if (!event.defaultPrevented && state === 'ready') {
316
357
  form.referrerPolicy = referrerPolicy;
358
+ if (cartForm) {
359
+ form.setAttribute("referrerpolicy", referrerPolicy);
360
+ loosenReferrer();
361
+ revealForNavigation();
362
+ }
317
363
  }
318
364
  return;
319
365
  }
@@ -2,7 +2,7 @@
2
2
  import type { APIContext } from "astro";
3
3
  import { createLocalJWKSet, decodeProtectedHeader, jwtVerify, type JSONWebKeySet } from "jose";
4
4
  import {
5
- commerceTenantId, totalsMatch, canonicalJson, shippingMinorFor, isContentVersionId, providerCartDigestMaterial,
5
+ commerceTenantId, totalsMatch, shippingMinorFor, isContentVersionId, catalogPinDigestFinding,
6
6
  type CallerToken, type CatalogProduct, type CommerceEvidence, type CommerceEvidenceProvenance,
7
7
  type CommerceProviderCartSnapshot, type FinalCartBinding, type TenantConfig,
8
8
  } from "@tot/public-runtime";
@@ -183,7 +183,8 @@ async function authorize(context: APIContext): Promise<Response> {
183
183
  : createContentProvider(tenant, { get: kvGet, env: contentEnv });
184
184
  const tot = await createToTClient(tenant, await readD1("CATALOG_DB"), content);
185
185
  const authorityContext = { ...context, locals: { ...context.locals, tenant, tot, content } } as APIContext;
186
- if (snapshot.providerCartDigest !== await digest(canonicalJson(providerCartDigestMaterial(snapshot)))) return deny("provider-cart-digest");
186
+ const catalogPin = await catalogPinDigestFinding(snapshot);
187
+ if (catalogPin) return deny(catalogPin);
187
188
  let listing: CatalogProduct[];
188
189
  try { listing = await tot.getAllActiveProducts(); } catch { return deny("catalog-unavailable", 503); }
189
190
  if (!Array.isArray(listing)) return deny("catalog-unavailable", 503);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "4.2.3",
3
+ "version": "4.3.0",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",
@@ -401,6 +401,29 @@ export function providerCartDigestMaterial(snapshot: {
401
401
  return snapshot.contentVersionId === undefined ? material : { ...material, contentVersionId: snapshot.contentVersionId };
402
402
  }
403
403
 
404
+ /** Finding when the provider-cart digest was not computed over the rendered content pin. */
405
+ export const PROVIDER_CART_DIGEST_FINDING = "provider-cart-digest";
406
+
407
+ /**
408
+ * The provider-cart digest check the final-cart authority applies. Returns
409
+ * `provider-cart-digest` when `providerCartDigest` is not the digest of
410
+ * `providerCartDigestMaterial` (the content pin is inside that material when
411
+ * the snapshot names one). Returns null when the digest matches.
412
+ */
413
+ export async function catalogPinDigestFinding(snapshot: {
414
+ providerCartDigest: string;
415
+ tenantId: string;
416
+ stage: string;
417
+ currency: string;
418
+ lines: readonly FinalCartLine[];
419
+ totals: MinorTotals;
420
+ destinationDigest: string;
421
+ contentVersionId?: string;
422
+ }): Promise<typeof PROVIDER_CART_DIGEST_FINDING | null> {
423
+ const expected = await sha256Hex(canonicalJson(providerCartDigestMaterial(snapshot)));
424
+ return snapshot.providerCartDigest === expected ? null : PROVIDER_CART_DIGEST_FINDING;
425
+ }
426
+
404
427
  /** Safe source references explaining each server-owned authority decision. */
405
428
  export interface CommerceAuthoritySource {
406
429
  requirement: "required" | "not-required";