@tokenoftrust/storefront-runner 6.15.4 → 6.17.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/apps/storefront/src/components/catalog/storefront/ProductCard.astro +2 -0
- package/apps/storefront/src/components/compliance/storefront/islands/CheckoutComplianceGate.tsx +1 -1
- package/apps/storefront/src/lib/access/auth/loginGate.ts +15 -0
- package/apps/storefront/src/lib/checkout/commerce/cardCartForm.ts +6 -1
- package/apps/storefront/src/lib/checkout/commerce/checkoutHandoffRoute.ts +11 -1
- package/apps/storefront/src/pages/api/commerce/final-cart-authorization.ts +5 -6
- package/apps/storefront/src/pages/api/compliance/preflight.ts +16 -1
- package/apps/storefront/src/pages/products/[handle].astro +4 -1
- package/package.json +1 -1
- package/packages/public-runtime/package.json +1 -0
- package/packages/public-runtime/src/catalog-scale.d.mts +12 -0
- package/packages/public-runtime/src/catalog-scale.mjs +70 -0
- package/packages/public-runtime/src/checkout.ts +75 -7
- package/packages/public-runtime/src/image-finding-dependencies.ts +34 -19
|
@@ -98,6 +98,8 @@ const cardCart = await buildCardCartForm({
|
|
|
98
98
|
apiBasePath,
|
|
99
99
|
environment: browserEnvironment.environment,
|
|
100
100
|
contentVersionId,
|
|
101
|
+
requestOrigin: Astro.locals.requestOrigin,
|
|
102
|
+
imageBasePath: Astro.locals.imageBasePath,
|
|
101
103
|
});
|
|
102
104
|
// A product with options is chosen on its product page, never from the card.
|
|
103
105
|
const productHref = withBase(basePath, pageUrlForm(`/products/${product.handle}`, tenant.seo));
|
package/apps/storefront/src/components/compliance/storefront/islands/CheckoutComplianceGate.tsx
CHANGED
|
@@ -646,7 +646,7 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
|
|
|
646
646
|
) : (
|
|
647
647
|
<button
|
|
648
648
|
type="button"
|
|
649
|
-
disabled={loading || waiting || (phase === "collect" && !email.trim())}
|
|
649
|
+
disabled={loading || waiting || decision?.status === "blocked" || (phase === "collect" && !email.trim())}
|
|
650
650
|
onClick={check}
|
|
651
651
|
class="rounded-full bg-[var(--color-primary)] px-6 py-3 font-display text-sm font-medium uppercase tracking-[0.08em] text-[var(--color-primary-contrast)] transition-opacity hover:opacity-90 disabled:opacity-60"
|
|
652
652
|
>
|
|
@@ -145,6 +145,20 @@ export function isAuthPath(pathname: string): boolean {
|
|
|
145
145
|
* catch that treats every failure as transient, so an HTML body there is swallowed
|
|
146
146
|
* silently and the reload can never fire while the tab polls once a second forever.
|
|
147
147
|
*/
|
|
148
|
+
const CAPTURED_ASSET_POOL_PATH = /^\/(?:[^/]+\/)?_a\/[0-9a-f]{64}(?:\.[A-Za-z0-9]+)?$/;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Content-addressed product/site images in `/_a/<sha256>.<ext>` (both host-routed and
|
|
152
|
+
* path-routed `/<tenant>/_a/<sha256>.<ext>`). Cross-site `<img>` loads from the managed
|
|
153
|
+
* checkout or receipt (`tot-preview.foxycart.com`) do not carry the `SameSite=Lax`
|
|
154
|
+
* `tot_session` cookie, and the path itself is an unguessable 256-bit SHA-256 content
|
|
155
|
+
* address. Only read methods (`GET` / `HEAD`, or when method is omitted) pass.
|
|
156
|
+
*/
|
|
157
|
+
export function isCapturedAssetPoolPath(pathname: string, method?: string): boolean {
|
|
158
|
+
if (method !== undefined && method !== "GET" && method !== "HEAD") return false;
|
|
159
|
+
return CAPTURED_ASSET_POOL_PATH.test(pathname);
|
|
160
|
+
}
|
|
161
|
+
|
|
148
162
|
export function isPublicPath(pathname: string): boolean {
|
|
149
163
|
if (pathname === "/api/cli/policy") return true;
|
|
150
164
|
if (pathname === "/api/preview/version") return true;
|
|
@@ -493,6 +507,7 @@ export function evaluateLoginGate(input: LoginGateInput): LoginGateDecision {
|
|
|
493
507
|
if (input.tenantExempt) return { action: "pass" };
|
|
494
508
|
if (isAuthPath(input.pathname)) return { action: "pass" };
|
|
495
509
|
if (isPublicPath(input.pathname)) return { action: "pass" };
|
|
510
|
+
if (isCapturedAssetPoolPath(input.pathname, input.method)) return { action: "pass" };
|
|
496
511
|
// Candidate-preview control-plane routes govern their OWN (stricter, honest) auth
|
|
497
512
|
// and must never fall to the "Coming soon" holding page for a real candidate.
|
|
498
513
|
if (isCandidatePreviewPath(input.pathname)) return { action: "pass" };
|
|
@@ -47,6 +47,8 @@ export async function buildCardCartForm(input: {
|
|
|
47
47
|
environment: string;
|
|
48
48
|
/** Candidate content pin this card rendered. Ignored on a live page. */
|
|
49
49
|
contentVersionId?: string;
|
|
50
|
+
requestOrigin?: string;
|
|
51
|
+
imageBasePath?: string;
|
|
50
52
|
}): Promise<CardCartForm | null> {
|
|
51
53
|
if (cardNeedsOptionChoice(input.product)) return null;
|
|
52
54
|
const variant = defaultCardVariant(input.product);
|
|
@@ -70,7 +72,10 @@ export async function buildCardCartForm(input: {
|
|
|
70
72
|
tenantId: commerceTenantId(input.tenant),
|
|
71
73
|
currency: input.tenant.currency,
|
|
72
74
|
contentVersionId: input.environment === "live" ? undefined : input.contentVersionId,
|
|
73
|
-
publicOrigin: tenantPublicOrigin(input.tenant
|
|
75
|
+
publicOrigin: tenantPublicOrigin(input.tenant, {
|
|
76
|
+
requestOrigin: input.requestOrigin,
|
|
77
|
+
basePath: input.imageBasePath || input.apiBasePath,
|
|
78
|
+
}),
|
|
74
79
|
});
|
|
75
80
|
const provider = input.tenant.serving?.provider === "s3-overwrite" ? "s3-overwrite" : "worker-r2-pointer";
|
|
76
81
|
return {
|
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
withShippingPolicy,
|
|
33
33
|
withReturnUrl,
|
|
34
34
|
withExcise,
|
|
35
|
+
withServedCartImage,
|
|
35
36
|
trustedCartReturnUrl,
|
|
36
37
|
tenantPublicOrigin,
|
|
37
38
|
CHECKOUT_NAVIGATION_REFERRER_POLICY,
|
|
@@ -240,7 +241,16 @@ async function cartFormPost(context: APIContext): Promise<Response> {
|
|
|
240
241
|
const returned = await withReturnUrl(excised, {
|
|
241
242
|
mode: commerce.mode, secret: signing.secret, returnUrl: submittedFromUrl(context.request, tenant),
|
|
242
243
|
});
|
|
243
|
-
const
|
|
244
|
+
const imaged = await withServedCartImage(returned, {
|
|
245
|
+
mode: commerce.mode,
|
|
246
|
+
secret: signing.secret,
|
|
247
|
+
canonicalDomain: tenant.canonicalDomain,
|
|
248
|
+
publicOrigin: tenantPublicOrigin(tenant, {
|
|
249
|
+
requestOrigin: context.locals.requestOrigin || new URL(context.request.url).origin,
|
|
250
|
+
basePath: context.locals.imageBasePath || context.locals.apiBasePath || context.locals.basePath,
|
|
251
|
+
}),
|
|
252
|
+
});
|
|
253
|
+
const location = await sessionedCartLocation(context, commerce, tenant, imaged);
|
|
244
254
|
if (!location) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503);
|
|
245
255
|
// The page adds the line itself (cartState.ts) and asks for the signed cart URL as data.
|
|
246
256
|
if ((context.request.headers.get("accept") ?? "").includes("application/json")) {
|
|
@@ -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, isRegulated,
|
|
5
|
+
commerceTenantId, isRegulated, totalsMatch, shippingMinorFor, isContentVersionId, catalogPinDigestFinding, cartLineKey,
|
|
6
6
|
EXCISE_REASONS, type CallerToken, type CatalogProduct, type CatalogVariant, type CommerceEvidence, type CommerceEvidenceProvenance,
|
|
7
7
|
type CommerceProviderCartSnapshot, type ExciseCarrier, type FinalCartBinding, type TenantConfig,
|
|
8
8
|
} from "@tot/public-runtime";
|
|
@@ -141,14 +141,13 @@ async function seam(context: APIContext, snapshot: CommerceProviderCartSnapshot,
|
|
|
141
141
|
/**
|
|
142
142
|
* The sandbox verification profile satisfies the final cart's verification only when ALL hold:
|
|
143
143
|
* the resolved provider is an explicit sandbox value, the checkout resolves to the shared test
|
|
144
|
-
* store on the test tier (already enforced by admission), the
|
|
145
|
-
*
|
|
146
|
-
*
|
|
144
|
+
* store on the test tier (already enforced by admission), and the cart carries the signed
|
|
145
|
+
* sandbox-cleared verification ref. Any other tier or provider keeps the real provider's outcome
|
|
146
|
+
* as the only evidence.
|
|
147
147
|
*/
|
|
148
|
-
async function sandboxVerificationApplies(
|
|
148
|
+
async function sandboxVerificationApplies(_tenant: TenantConfig, snapshot: CommerceProviderCartSnapshot): Promise<boolean> {
|
|
149
149
|
return isSandboxVerificationProvider((await readEnv("TOT_VERIFICATION_PROVIDER")) || undefined)
|
|
150
150
|
&& (await readEnv("COMMERCE_DEMO_CHECKOUT_TIER")) === "test"
|
|
151
|
-
&& isInternalTestTenant(tenant)
|
|
152
151
|
&& SANDBOX_CLEARED_REF.test(snapshot.verificationRef ?? "");
|
|
153
152
|
}
|
|
154
153
|
async function evaluateSeam(context: APIContext, snapshot: CommerceProviderCartSnapshot, selected: ReadonlyMap<string, CatalogProduct>, policy: SharedStoreEvidencePolicy, sandbox: boolean): Promise<SeamView & { verified: boolean }> {
|
|
@@ -34,7 +34,11 @@ import {
|
|
|
34
34
|
type TotVerificationClient,
|
|
35
35
|
type TotVerificationStatus,
|
|
36
36
|
} from "@/lib/compliance/verification/totVerificationClient";
|
|
37
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
SANDBOX_CLEARED_REF,
|
|
39
|
+
complianceGateRequiredFor,
|
|
40
|
+
sharedStorePolicy,
|
|
41
|
+
} from "@/lib/checkout/commerce/sharedStoreAdmission";
|
|
38
42
|
|
|
39
43
|
export const prerender = false;
|
|
40
44
|
|
|
@@ -403,6 +407,17 @@ export async function evaluatePreflight(
|
|
|
403
407
|
});
|
|
404
408
|
|
|
405
409
|
const applied = rulesOff ? verificationOnly(decision) : decision;
|
|
410
|
+
if (
|
|
411
|
+
!liveVerification &&
|
|
412
|
+
applied.status === "clear" &&
|
|
413
|
+
applied.requirements.ageVerification === "satisfied" &&
|
|
414
|
+
ruleset &&
|
|
415
|
+
isSandboxVerificationProvider(ruleset.verification.provider)
|
|
416
|
+
) {
|
|
417
|
+
const rawRef = typeof body.verificationRef === "string" ? body.verificationRef.trim() : "";
|
|
418
|
+
const ref = SANDBOX_CLEARED_REF.test(rawRef) ? rawRef : `sandbox-cleared:${crypto.randomUUID()}`;
|
|
419
|
+
liveVerification = { ref, status: "conditions_met" };
|
|
420
|
+
}
|
|
406
421
|
return liveVerification ? { decision: applied, verification: liveVerification } : { decision: applied };
|
|
407
422
|
}
|
|
408
423
|
|
|
@@ -270,7 +270,10 @@ if (commerce || sampleHandoff) {
|
|
|
270
270
|
tenantId: commerceTenantId(tenant),
|
|
271
271
|
currency: tenant.currency,
|
|
272
272
|
contentVersionId: Astro.locals.browserEnvironment.environment === "live" ? undefined : Astro.locals.contentVersionId,
|
|
273
|
-
publicOrigin: tenantPublicOrigin(tenant
|
|
273
|
+
publicOrigin: tenantPublicOrigin(tenant, {
|
|
274
|
+
requestOrigin: Astro.locals.requestOrigin,
|
|
275
|
+
basePath: Astro.locals.imageBasePath || apiBasePath,
|
|
276
|
+
}),
|
|
274
277
|
}),
|
|
275
278
|
] as const;
|
|
276
279
|
}),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.17.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",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"./responsive-renditions": "./src/responsive-renditions.ts",
|
|
33
33
|
"./svg-sanitize": "./src/svg-sanitize.ts",
|
|
34
34
|
"./incremental-sha256": "./src/incremental-sha256.ts",
|
|
35
|
+
"./catalog-scale": "./src/catalog-scale.mjs",
|
|
35
36
|
"./image-limits": "./src/image-limits.mjs",
|
|
36
37
|
"./tenant-record-merge": "./src/tenant-record-merge.mjs",
|
|
37
38
|
"./image-release-gate": "./src/image-release-gate.ts",
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface CatalogSize {
|
|
2
|
+
readonly products: number;
|
|
3
|
+
readonly bytes: number;
|
|
4
|
+
}
|
|
5
|
+
export type CatalogScaleLevel = "ok" | "warn" | "d1-required";
|
|
6
|
+
|
|
7
|
+
export declare const CATALOG_SCALE_WARN: Readonly<CatalogSize>;
|
|
8
|
+
export declare const CATALOG_SCALE_D1_REQUIRED: Readonly<CatalogSize>;
|
|
9
|
+
export declare function catalogScaleLevel(size: CatalogSize): CatalogScaleLevel;
|
|
10
|
+
export declare function catalogScaleFinding(
|
|
11
|
+
size: CatalogSize,
|
|
12
|
+
): { level: "warn" | "d1-required"; message: string } | null;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog scale thresholds: the one place the platform says how large a store's own
|
|
3
|
+
* `content/catalog/` may grow before each serving path stops being safe (ADR 0033).
|
|
4
|
+
*
|
|
5
|
+
* Two measures, because either can be the one that bites:
|
|
6
|
+
* - products: the count of `catalog/products/*.json` files;
|
|
7
|
+
* - bytes: the raw size of every file under `catalog/`.
|
|
8
|
+
*
|
|
9
|
+
* Evidence (synthetic Giant-shaped tenants, measured 2026-11-16 by
|
|
10
|
+
* `apps/storefront/src/lib/catalog/tot/catalogScale.test.ts`):
|
|
11
|
+
* - the listing projection parses in ~150 ms and ~47 MB of heap at 10,000 products;
|
|
12
|
+
* - reading the files one by one costs ~55 MB of heap at 5,000 products and ~111 MB at
|
|
13
|
+
* 10,000 against the Worker's 128 MB;
|
|
14
|
+
* - a store of 3,571 files / 42 MB failed to render in the materializer's wrangler-dev
|
|
15
|
+
* Worker (fb-1794371798042-4rqyh4), which is why bytes are a line of their own.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** A catalog at or past either line gets a non-blocking finding naming the next line. */
|
|
19
|
+
export const CATALOG_SCALE_WARN = Object.freeze({ products: 3000, bytes: 25_000_000 });
|
|
20
|
+
|
|
21
|
+
/** A catalog at or past either line must declare `catalog.serving: "d1"` to be materialized. */
|
|
22
|
+
export const CATALOG_SCALE_D1_REQUIRED = Object.freeze({ products: 5000, bytes: 40_000_000 });
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {{ products: number, bytes: number }} size
|
|
26
|
+
* @returns {"ok" | "warn" | "d1-required"}
|
|
27
|
+
*/
|
|
28
|
+
export function catalogScaleLevel({ products, bytes }) {
|
|
29
|
+
if (products >= CATALOG_SCALE_D1_REQUIRED.products || bytes >= CATALOG_SCALE_D1_REQUIRED.bytes) return "d1-required";
|
|
30
|
+
if (products >= CATALOG_SCALE_WARN.products || bytes >= CATALOG_SCALE_WARN.bytes) return "warn";
|
|
31
|
+
return "ok";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const mb = (bytes) => `${(bytes / 1_000_000).toFixed(1)} MB`;
|
|
35
|
+
|
|
36
|
+
const REMEDY =
|
|
37
|
+
"ask Token of Trust to serve this catalog from the D1 projection (`catalog.serving` is a platform " +
|
|
38
|
+
"setting, not a .tot/config.json key; public and static renders never read D1, ADR 0013), " +
|
|
39
|
+
"or split the catalog";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The finding text for a catalog at `warn` or `d1-required`, or null when it is `ok`. Names the
|
|
43
|
+
* count, the size, the next line and the remedy, and says what each CDN does at the line.
|
|
44
|
+
*
|
|
45
|
+
* @param {{ products: number, bytes: number }} size
|
|
46
|
+
* @returns {{ level: "warn" | "d1-required", message: string } | null}
|
|
47
|
+
*/
|
|
48
|
+
export function catalogScaleFinding(size) {
|
|
49
|
+
const level = catalogScaleLevel(size);
|
|
50
|
+
if (level === "ok") return null;
|
|
51
|
+
const have = `${size.products} products, ${mb(size.bytes)} of catalog files`;
|
|
52
|
+
const cdns =
|
|
53
|
+
"Cloudflare preview and publication read the catalog inside the Worker's 128 MB isolate; " +
|
|
54
|
+
"CloudFront static publication renders on a build machine, whose preview Worker bundles the catalog.";
|
|
55
|
+
if (level === "warn") {
|
|
56
|
+
return {
|
|
57
|
+
level,
|
|
58
|
+
message:
|
|
59
|
+
`catalog is ${have}; past ${CATALOG_SCALE_D1_REQUIRED.products} products or ` +
|
|
60
|
+
`${mb(CATALOG_SCALE_D1_REQUIRED.bytes)} a file-served catalog may not render. ` +
|
|
61
|
+
`${cdns} To stay ahead of it: ${REMEDY}.`,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
level,
|
|
66
|
+
message:
|
|
67
|
+
`catalog is ${have}, past the ${CATALOG_SCALE_D1_REQUIRED.products}-product / ` +
|
|
68
|
+
`${mb(CATALOG_SCALE_D1_REQUIRED.bytes)} line for file-served catalogs. ${cdns} Fix: ${REMEDY}.`,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -182,21 +182,41 @@ export interface BuildVariantCartPayloadOptions {
|
|
|
182
182
|
publicOrigin?: string;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
-
/**
|
|
186
|
-
|
|
185
|
+
/**
|
|
186
|
+
* The public origin (and optional base path) a cart image resolves against. When the page or
|
|
187
|
+
* handoff is served on a known `https://` request origin, that served base is used so a store
|
|
188
|
+
* whose canonical domain has not cut over still signs a reachable asset URL; otherwise it falls
|
|
189
|
+
* back to `https://<canonicalDomain>` from server-side tenant config.
|
|
190
|
+
*/
|
|
191
|
+
export function tenantPublicOrigin(
|
|
192
|
+
tenant: { canonicalDomain?: string | null },
|
|
193
|
+
served?: { requestOrigin?: string; basePath?: string },
|
|
194
|
+
): string | undefined {
|
|
195
|
+
if (served?.requestOrigin && URL.canParse(served.requestOrigin)) {
|
|
196
|
+
const url = new URL(served.requestOrigin);
|
|
197
|
+
if (url.protocol === "https:" && !url.username && !url.password && url.pathname === "/") {
|
|
198
|
+
const rawBase = served.basePath?.trim() ?? "";
|
|
199
|
+
const prefix = rawBase && rawBase !== "/" ? `/${rawBase.replace(/^\/+|\/+$/g, "")}` : "";
|
|
200
|
+
return `${url.origin}${prefix}`;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
187
203
|
const host = tenant.canonicalDomain?.trim().toLowerCase();
|
|
188
204
|
return host && /^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?(?::\d{1,5})?$/.test(host) ? `https://${host}` : undefined;
|
|
189
205
|
}
|
|
190
206
|
|
|
191
207
|
/**
|
|
192
|
-
* A root-relative image resolved against the public origin
|
|
193
|
-
* and any that fail to parse or would resolve onto another
|
|
208
|
+
* A root-relative image resolved against the public origin (preserving a path-routed base path).
|
|
209
|
+
* Absolute and protocol-relative values, and any that fail to parse or would resolve onto another
|
|
210
|
+
* origin, are signed as they are.
|
|
194
211
|
*/
|
|
195
212
|
function cartImageUrl(image: string, publicOrigin: string | undefined): string {
|
|
196
|
-
if (!publicOrigin || !image.startsWith("/") || image.startsWith("//")) return image;
|
|
213
|
+
if (!publicOrigin || !image.startsWith("/") || image.startsWith("//") || image.startsWith("/\\")) return image;
|
|
197
214
|
try {
|
|
198
|
-
const
|
|
199
|
-
|
|
215
|
+
const base = new URL(publicOrigin);
|
|
216
|
+
const basePath = base.pathname.replace(/\/+$/, "");
|
|
217
|
+
const path = basePath && !image.startsWith(`${basePath}/`) ? `${basePath}${image}` : image;
|
|
218
|
+
const resolved = new URL(path, base.origin);
|
|
219
|
+
return resolved.origin === base.origin ? resolved.href : image;
|
|
200
220
|
} catch {
|
|
201
221
|
return image;
|
|
202
222
|
}
|
|
@@ -1118,6 +1138,54 @@ export async function withReturnUrl(
|
|
|
1118
1138
|
return [...kept, await signField(opts.secret, code, field)];
|
|
1119
1139
|
}
|
|
1120
1140
|
|
|
1141
|
+
const CAPTURED_CART_ASSET_PATH = /^\/_a\/[0-9a-f]{64}(?:\.[A-Za-z0-9]+)?$/;
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* Rewrite an enforced cart's `image` field onto the served `publicOrigin` when it names a
|
|
1145
|
+
* captured asset-pool path (`/_a/<sha256>.<ext>`) either root-relatively or on the tenant's
|
|
1146
|
+
* canonical domain, and re-sign it against the cart's product code.
|
|
1147
|
+
*/
|
|
1148
|
+
export async function withServedCartImage(
|
|
1149
|
+
fields: readonly CartSubmissionField[],
|
|
1150
|
+
opts: {
|
|
1151
|
+
mode: ManagedCheckoutConfig["mode"];
|
|
1152
|
+
secret?: string;
|
|
1153
|
+
canonicalDomain?: string;
|
|
1154
|
+
publicOrigin?: string;
|
|
1155
|
+
},
|
|
1156
|
+
): Promise<CartSubmissionField[]> {
|
|
1157
|
+
if (!opts.publicOrigin) return [...fields];
|
|
1158
|
+
const code = fields.find((field) => field.name.split("||")[0] === "code")?.value;
|
|
1159
|
+
const idx = fields.findIndex((field) => field.name.split("||")[0] === "image");
|
|
1160
|
+
if (!code || idx === -1) return [...fields];
|
|
1161
|
+
const current = fields[idx]!.value;
|
|
1162
|
+
let assetPath: string | null = null;
|
|
1163
|
+
if (CAPTURED_CART_ASSET_PATH.test(current)) {
|
|
1164
|
+
assetPath = current;
|
|
1165
|
+
} else if (URL.canParse(current)) {
|
|
1166
|
+
const parsed = new URL(current);
|
|
1167
|
+
const canonical = tenantPublicOrigin({ canonicalDomain: opts.canonicalDomain });
|
|
1168
|
+
if (canonical && parsed.origin === canonical && CAPTURED_CART_ASSET_PATH.test(parsed.pathname)) {
|
|
1169
|
+
assetPath = `${parsed.pathname}${parsed.search}`;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (!assetPath) return [...fields];
|
|
1173
|
+
const rewritten = cartImageUrl(assetPath, opts.publicOrigin);
|
|
1174
|
+
if (rewritten === current) return [...fields];
|
|
1175
|
+
const field: CartField = { name: "image", value: rewritten };
|
|
1176
|
+
const signedField =
|
|
1177
|
+
opts.mode !== "signed"
|
|
1178
|
+
? unsignedField(field)
|
|
1179
|
+
: opts.secret
|
|
1180
|
+
? await signField(opts.secret, code, field)
|
|
1181
|
+
: (() => {
|
|
1182
|
+
throw new CartSigningUnavailableError();
|
|
1183
|
+
})();
|
|
1184
|
+
const next = [...fields];
|
|
1185
|
+
next[idx] = signedField;
|
|
1186
|
+
return next;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1121
1189
|
/** Cart URL the browser may navigate to after enforcement. Not a payment. */
|
|
1122
1190
|
export function foxyCartRedirectUrl(
|
|
1123
1191
|
cartUrl: string,
|
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
import type { ImageDependency, ImageDependencyFinding, ImageDependencyScan } from "./image-dependencies.js";
|
|
2
2
|
|
|
3
|
+
const findingDependenciesCache = new WeakMap<readonly ImageDependency[], Map<string, ImageDependency[]>>();
|
|
4
|
+
|
|
5
|
+
function indexDependenciesByFindingId(dependencies: readonly ImageDependency[]): Map<string, ImageDependency[]> {
|
|
6
|
+
const cached = findingDependenciesCache.get(dependencies);
|
|
7
|
+
if (cached) return cached;
|
|
8
|
+
const groupsByKey = new Map<string, ImageDependency[]>();
|
|
9
|
+
const byFindingId = new Map<string, ImageDependency[]>();
|
|
10
|
+
const counts = new Map<number, number>();
|
|
11
|
+
for (const dep of dependencies) {
|
|
12
|
+
if (
|
|
13
|
+
dep.disposition !== "capture-required" &&
|
|
14
|
+
dep.disposition !== "security-error" &&
|
|
15
|
+
dep.disposition !== "deferred"
|
|
16
|
+
) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const key = `${dep.artifactIndex}\0${dep.surface}\0${dep.disposition}\0${dep.reason}\0${dep.canonicalLocator}`;
|
|
20
|
+
let group = groupsByKey.get(key);
|
|
21
|
+
if (!group) {
|
|
22
|
+
group = [dep];
|
|
23
|
+
groupsByKey.set(key, group);
|
|
24
|
+
const ordinal = (counts.get(dep.artifactIndex) ?? 0) + 1;
|
|
25
|
+
counts.set(dep.artifactIndex, ordinal);
|
|
26
|
+
byFindingId.set(`${dep.artifactIndex}:${ordinal}`, group);
|
|
27
|
+
} else {
|
|
28
|
+
group.push(dep);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
findingDependenciesCache.set(dependencies, byFindingId);
|
|
32
|
+
return byFindingId;
|
|
33
|
+
}
|
|
34
|
+
|
|
3
35
|
/** Recover one unresolved finding's exact scanner group without a locator in its ID. */
|
|
4
36
|
export function dependenciesForScanFinding(
|
|
5
37
|
finding: ImageDependencyFinding,
|
|
@@ -9,23 +41,6 @@ export function dependenciesForScanFinding(
|
|
|
9
41
|
if (!match) return [];
|
|
10
42
|
const artifactIndex = Number(match[1]) - 1;
|
|
11
43
|
const groupOrdinal = Number(match[2]);
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
for (const dep of scan.dependencies) {
|
|
15
|
-
if (dep.disposition !== "capture-required" && dep.disposition !== "security-error" &&
|
|
16
|
-
dep.disposition !== "deferred") continue;
|
|
17
|
-
const key = `${dep.artifactIndex}\0${dep.surface}\0${dep.disposition}\0${dep.reason}\0${dep.canonicalLocator}`;
|
|
18
|
-
if (seen.has(key)) continue;
|
|
19
|
-
const ordinal = (counts.get(dep.artifactIndex) ?? 0) + 1;
|
|
20
|
-
counts.set(dep.artifactIndex, ordinal);
|
|
21
|
-
seen.set(key, { dep, ordinal });
|
|
22
|
-
}
|
|
23
|
-
const group = [...seen.values()].find(
|
|
24
|
-
(entry) => entry.dep.artifactIndex === artifactIndex && entry.ordinal === groupOrdinal,
|
|
25
|
-
);
|
|
26
|
-
if (!group) return [];
|
|
27
|
-
return scan.dependencies.filter((dep) =>
|
|
28
|
-
dep.artifactIndex === group.dep.artifactIndex && dep.surface === group.dep.surface &&
|
|
29
|
-
dep.disposition === group.dep.disposition && dep.reason === group.dep.reason &&
|
|
30
|
-
dep.canonicalLocator === group.dep.canonicalLocator);
|
|
44
|
+
const group = indexDependenciesByFindingId(scan.dependencies).get(`${artifactIndex}:${groupOrdinal}`);
|
|
45
|
+
return group ? [...group] : [];
|
|
31
46
|
}
|