@tokenoftrust/storefront-runner 6.13.0 → 6.13.2
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/lib/access/auth/loginGate.ts +45 -0
- package/apps/storefront/src/lib/access/shared/tenantRouting.ts +19 -0
- package/apps/storefront/src/lib/publication/shared/cdnForward.ts +36 -8
- package/apps/storefront/src/middleware/controlPlane.ts +30 -0
- package/apps/storefront/src/middleware/index.ts +4 -3
- package/apps/storefront/src/middleware/renderChain.ts +9 -1
- package/apps/storefront/src/pages/api/newsletter/unsubscribe.ts +2 -3
- package/package.json +1 -1
|
@@ -391,6 +391,42 @@ export function isPreviewAppBootstrapRoute(method: string | undefined, renderPat
|
|
|
391
391
|
return PREVIEW_APP_ROUTES.some((route) => route.method === method && route.path.test(renderPath));
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
+
const APP_TOKEN_PATH = "/api/apps/oauth/token";
|
|
395
|
+
const APP_V1_PATH = /^\/api\/apps\/v1(?:\/(?!\.{1,2}(?:\/|$))[A-Za-z0-9._~-]+)+$/;
|
|
396
|
+
|
|
397
|
+
export interface AppCredentialRequest {
|
|
398
|
+
method: string | undefined;
|
|
399
|
+
/** The publication render path (tenant-relative), never the raw request path. */
|
|
400
|
+
renderPath: string | undefined;
|
|
401
|
+
/** The request's `Authorization` header. */
|
|
402
|
+
authorization: string | null | undefined;
|
|
403
|
+
/** The token request's form `grant_type`, when its body is form-encoded. */
|
|
404
|
+
formGrantType?: string | null;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* An app-gateway call carrying the app's own credential: a client-credentials
|
|
409
|
+
* `POST /api/apps/oauth/token` (Basic client authentication or a form-encoded
|
|
410
|
+
* `grant_type=client_credentials`), or an `/api/apps/v1/*` call with a Bearer.
|
|
411
|
+
* Presence only: the handlers authenticate the credential and enforce its tenant
|
|
412
|
+
* and install env. On a preview surface such a request reaches its handler and
|
|
413
|
+
* routes the preview stage, so only a `test` install of this tenant is served.
|
|
414
|
+
*/
|
|
415
|
+
export function isAppCredentialRequest(input: AppCredentialRequest): boolean {
|
|
416
|
+
const { method, renderPath } = input;
|
|
417
|
+
if (!method || renderPath === undefined) return false;
|
|
418
|
+
const authorization = input.authorization?.trim() ?? "";
|
|
419
|
+
if (method === "POST" && renderPath === APP_TOKEN_PATH) {
|
|
420
|
+
return /^basic\s+\S/i.test(authorization) || input.formGrantType === "client_credentials";
|
|
421
|
+
}
|
|
422
|
+
return APP_V1_PATH.test(renderPath) && /^Bearer\s+\S/.test(authorization);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** The render path is the app token endpoint, whose form body {@link isAppCredentialRequest} reads. */
|
|
426
|
+
export function isAppTokenPath(renderPath: string | undefined): boolean {
|
|
427
|
+
return renderPath === APP_TOKEN_PATH;
|
|
428
|
+
}
|
|
429
|
+
|
|
394
430
|
export type LoginGateDecision =
|
|
395
431
|
| { action: "pass" }
|
|
396
432
|
| { action: "redirect"; location: string };
|
|
@@ -438,6 +474,12 @@ export interface LoginGateInput {
|
|
|
438
474
|
method?: string;
|
|
439
475
|
/** The publication render path (tenant-relative), for {@link isPreviewAppBootstrapRoute}. */
|
|
440
476
|
renderPath?: string;
|
|
477
|
+
/**
|
|
478
|
+
* True when the request is an {@link isAppCredentialRequest} on a preview surface
|
|
479
|
+
* (a gate-list host, never a CDN-forwarded public host). It reaches its handler,
|
|
480
|
+
* which authenticates the credential for this tenant's preview stage.
|
|
481
|
+
*/
|
|
482
|
+
appCredential?: boolean;
|
|
441
483
|
}
|
|
442
484
|
|
|
443
485
|
/**
|
|
@@ -476,6 +518,9 @@ export function evaluateLoginGate(input: LoginGateInput): LoginGateDecision {
|
|
|
476
518
|
if (input.previewTokenValid && isPreviewAppBootstrapRoute(input.method, input.renderPath)) {
|
|
477
519
|
return { action: "pass" };
|
|
478
520
|
}
|
|
521
|
+
// An app's own credential on an app-gateway route reaches the handler, which is
|
|
522
|
+
// the authority for its tenant and install env (preview stage, `test` installs).
|
|
523
|
+
if (input.appCredential) return { action: "pass" };
|
|
479
524
|
if (input.hasValidSession) return { action: "pass" };
|
|
480
525
|
return { action: "redirect", location: loginRedirectLocation(input.pathname) };
|
|
481
526
|
}
|
|
@@ -78,6 +78,25 @@ function normalizePath(path: string): string {
|
|
|
78
78
|
return trimmed === "" ? "/" : trimmed;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The route path with a leading `/<domain>/` tenant segment removed (the shared Worker's
|
|
83
|
+
* path-selected tenant), without resolving it. Same segment test and normalization as
|
|
84
|
+
* {@link resolveTenantRouting}'s path step, so a host-shaped first segment is the only thing
|
|
85
|
+
* ever stripped; the URL parser has already collapsed dot segments, and `%2e` forms never
|
|
86
|
+
* decode here. A path with no such segment is returned unchanged; a trailing slash is kept, so
|
|
87
|
+
* an exact route match stays exact.
|
|
88
|
+
*/
|
|
89
|
+
export function tenantRelativePath(pathname: string): string {
|
|
90
|
+
const segments = pathname.replace(/^\/+/, "").split("/");
|
|
91
|
+
let first = segments[0] ?? "";
|
|
92
|
+
try {
|
|
93
|
+
first = decodeURIComponent(first);
|
|
94
|
+
} catch {
|
|
95
|
+
/* leave as-is if it isn't valid percent-encoding */
|
|
96
|
+
}
|
|
97
|
+
return looksLikeHost(first) ? `/${segments.slice(1).join("/")}` : pathname;
|
|
98
|
+
}
|
|
99
|
+
|
|
81
100
|
/**
|
|
82
101
|
* Routing for an authenticated CDN forward (lib/publication/shared/cdnForward.ts). The forward names
|
|
83
102
|
* its tenant outright and renders at the apex, as the tenant's own host would.
|
|
@@ -88,7 +88,32 @@ export async function requestClientIp(headers: Headers, cdnOriginSecret: string
|
|
|
88
88
|
|
|
89
89
|
const FORM_CONTENT_TYPES = ["application/x-www-form-urlencoded", "multipart/form-data", "text/plain"];
|
|
90
90
|
const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
|
|
91
|
-
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Machine-to-machine POST routes. Each is authenticated by a credential the caller puts in the
|
|
94
|
+
* request itself (client secret, HMAC signature, service secret, or a one-time token in the
|
|
95
|
+
* query), never by a cookie or cached browser credential, so a forged cross-site post gains
|
|
96
|
+
* nothing and a non-browser caller (no Origin, form or text body) must be admitted. Routes are
|
|
97
|
+
* listed under `/api/` and matched tenant-relative, so the bare form (custom domain, CloudFront
|
|
98
|
+
* forward) and the `/<tenant>/` shared-Worker form both hit them.
|
|
99
|
+
*/
|
|
100
|
+
const MACHINE_POST_API_ROUTES = [
|
|
101
|
+
"apps/oauth/token",
|
|
102
|
+
"newsletter/unsubscribe",
|
|
103
|
+
"pipeline/seed",
|
|
104
|
+
"pipeline/settings-live-backfill",
|
|
105
|
+
"internal/contact-vault/seal-legacy",
|
|
106
|
+
"apps/internal/order-forward",
|
|
107
|
+
"migration/c2c-verify",
|
|
108
|
+
"migration/source-validate",
|
|
109
|
+
"migration/catalog-subscription",
|
|
110
|
+
"changes/publish-callback",
|
|
111
|
+
"publication-spaces/projection-callback",
|
|
112
|
+
"publication-spaces/projection-authority",
|
|
113
|
+
];
|
|
114
|
+
export const MACHINE_POST_ROUTES: ReadonlySet<string> = new Set(
|
|
115
|
+
MACHINE_POST_API_ROUTES.map((route) => `/api/${route}`),
|
|
116
|
+
);
|
|
92
117
|
|
|
93
118
|
/**
|
|
94
119
|
* Astro's cross-site form check, against the origin the viewer actually used.
|
|
@@ -102,16 +127,19 @@ const OAUTH_TOKEN_PATH = "/api/apps/oauth/token";
|
|
|
102
127
|
* that sets it triggers a CORS preflight this Worker does not grant. Server-to-server
|
|
103
128
|
* writes — CI staging a bundle's `text/plain` objects into R2 — authenticate that way.
|
|
104
129
|
*
|
|
105
|
-
*
|
|
106
|
-
* token endpoint
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* exempt.
|
|
130
|
+
* The POST routes in {@link MACHINE_POST_ROUTES} are not browser form targets either (the RFC
|
|
131
|
+
* 6749 token endpoint, the RFC 8058 one-click unsubscribe, signed provider callbacks). Only an
|
|
132
|
+
* exact match of `routePath` (the request path with any tenant prefix already stripped,
|
|
133
|
+
* defaulting to the URL path) and POST is exempt.
|
|
110
134
|
*/
|
|
111
|
-
export function crossSiteFormRefusal(
|
|
135
|
+
export function crossSiteFormRefusal(
|
|
136
|
+
request: Request,
|
|
137
|
+
expectedOrigin: string,
|
|
138
|
+
routePath: string = new URL(request.url).pathname,
|
|
139
|
+
): Response | null {
|
|
112
140
|
if (SAFE_METHODS.has(request.method)) return null;
|
|
113
141
|
if (/^bearer\s+\S/i.test(request.headers.get("authorization") ?? "")) return null;
|
|
114
|
-
if (request.method === "POST" &&
|
|
142
|
+
if (request.method === "POST" && MACHINE_POST_ROUTES.has(routePath)) return null;
|
|
115
143
|
if (request.headers.get("origin") === expectedOrigin) return null;
|
|
116
144
|
const contentType = request.headers.get("content-type");
|
|
117
145
|
if (contentType !== null) {
|
|
@@ -26,6 +26,8 @@ import type {
|
|
|
26
26
|
import {
|
|
27
27
|
evaluateLoginGate,
|
|
28
28
|
isApiPath,
|
|
29
|
+
isAppCredentialRequest,
|
|
30
|
+
isAppTokenPath,
|
|
29
31
|
isAuthPath,
|
|
30
32
|
isAdminPath,
|
|
31
33
|
isSignInRedirectPath,
|
|
@@ -35,6 +37,7 @@ import {
|
|
|
35
37
|
sessionHostCapability,
|
|
36
38
|
type GateMode,
|
|
37
39
|
} from "@/lib/access/auth/loginGate";
|
|
40
|
+
import { hostInGateList } from "@/lib/access/shared/demoGate";
|
|
38
41
|
import { resolveAdminLanding, isAdminSelectPath } from "@/lib/access/auth/adminLanding";
|
|
39
42
|
import { resolveAdminTenantMode } from "@/lib/access/shared/adminTenantEnvelope";
|
|
40
43
|
import { gatedApiResponse, gatedHoldingResponse } from "@/lib/access/auth/gatePage";
|
|
@@ -99,6 +102,7 @@ export async function serveControlPlane(input: ControlPlaneInput): Promise<Respo
|
|
|
99
102
|
// otherwise an unauthenticated visitor gets a "Coming soon" holding page with
|
|
100
103
|
// a Sign-in button (auth paths are exempt so sign-in itself is reachable). The
|
|
101
104
|
// grant `resource` is the tenant's appDomain — never input.
|
|
105
|
+
let appTestStage = false;
|
|
102
106
|
if (input.hostLoginGated && !input.internalContentGrant) {
|
|
103
107
|
// From the platform registry by id, NOT off `tenant` — that object resolves
|
|
104
108
|
// from TENANT_CACHE KV first, so a record written before this field existed
|
|
@@ -140,6 +144,30 @@ export async function serveControlPlane(input: ControlPlaneInput): Promise<Respo
|
|
|
140
144
|
// admin/cockpit/api exclusion is enforced in evaluateLoginGate.
|
|
141
145
|
const previewTokenValid = preview.context.status === "valid";
|
|
142
146
|
|
|
147
|
+
// An app's own credential on an app-gateway route, on a preview surface: a
|
|
148
|
+
// login-gated gate-list host, never a CDN-forwarded public host, an unpaid
|
|
149
|
+
// custom domain or a publicly declared tenant. It reaches its handler and routes
|
|
150
|
+
// the preview stage, so the handler serves only a `test` install of this tenant.
|
|
151
|
+
if (
|
|
152
|
+
!tenantExempt &&
|
|
153
|
+
!locals.cdnForwarded &&
|
|
154
|
+
input.gateMode === "login" &&
|
|
155
|
+
hostInGateList(host, input.demoGateHosts)
|
|
156
|
+
) {
|
|
157
|
+
const formGrantType =
|
|
158
|
+
request.method === "POST" &&
|
|
159
|
+
isAppTokenPath(renderPath) &&
|
|
160
|
+
(request.headers.get("content-type") ?? "").includes("application/x-www-form-urlencoded")
|
|
161
|
+
? new URLSearchParams(await request.clone().text()).get("grant_type")
|
|
162
|
+
: null;
|
|
163
|
+
appTestStage = isAppCredentialRequest({
|
|
164
|
+
method: request.method,
|
|
165
|
+
renderPath,
|
|
166
|
+
authorization: request.headers.get("authorization"),
|
|
167
|
+
formGrantType,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
143
171
|
// Canonical admin tenant landing: an AUTHENTICATED viewer who is not a
|
|
144
172
|
// member/staff of THIS host's tenant must be routed to their OWN tenant
|
|
145
173
|
// (auto when sole) or the tenant picker (when several / none) — never bounced
|
|
@@ -184,6 +212,7 @@ export async function serveControlPlane(input: ControlPlaneInput): Promise<Respo
|
|
|
184
212
|
method: request.method,
|
|
185
213
|
// The publication render path, never the raw request path.
|
|
186
214
|
renderPath,
|
|
215
|
+
appCredential: appTestStage,
|
|
187
216
|
});
|
|
188
217
|
if (decision.action === "redirect") {
|
|
189
218
|
// Admin and the cockpit hard-redirect to sign-in rather than rendering the
|
|
@@ -298,6 +327,7 @@ export async function serveControlPlane(input: ControlPlaneInput): Promise<Respo
|
|
|
298
327
|
return readAvailabilityBalances(fromD1(appsD1), { tenantId: commerceTenantId(tenant), stage });
|
|
299
328
|
},
|
|
300
329
|
internalContentGrant: input.internalContentGrant,
|
|
330
|
+
appTestStage,
|
|
301
331
|
});
|
|
302
332
|
}
|
|
303
333
|
|
|
@@ -23,7 +23,7 @@ import { defineMiddleware } from "astro:middleware";
|
|
|
23
23
|
import type { APIContext, MiddlewareNext } from "astro";
|
|
24
24
|
import { getTenantResolver } from "@/config/resolver";
|
|
25
25
|
import { readEnv, readKv, readR2 } from "@/lib/publication/shared/runtimeEnv";
|
|
26
|
-
import { resolveCdnForwardRouting, resolveTenantRouting } from "@/lib/access/shared/tenantRouting";
|
|
26
|
+
import { resolveCdnForwardRouting, resolveTenantRouting, tenantRelativePath } from "@/lib/access/shared/tenantRouting";
|
|
27
27
|
import { crossSiteFormRefusal, verifyCdnForward } from "@/lib/publication/shared/cdnForward";
|
|
28
28
|
import { SAMPLE_CART_HANDOFF_URL, commerceTenantId, compileRouteTierIndexV1, resolvePublicationRoutePlanV1, type RouteTierIndexV1 , storefrontModeRequiresSignIn } from "@tot/public-runtime";
|
|
29
29
|
import type { PublicationArtifactBucketV1, TenantConfig } from "@tot/public-runtime";
|
|
@@ -158,9 +158,10 @@ async function serveRequest(
|
|
|
158
158
|
if (cdn) host = cdn.viewerHost;
|
|
159
159
|
const origin = cdn ? `https://${cdn.viewerHost}` : url.origin;
|
|
160
160
|
locals.requestOrigin = origin;
|
|
161
|
-
|
|
161
|
+
// Machine routes are matched on the tenant-relative path, so the shared Worker's
|
|
162
|
+
// `/<tenant>/api/...` form is judged exactly like a bare host's `/api/...`.
|
|
163
|
+
const crossSite = crossSiteFormRefusal(request, origin, cdn ? url.pathname : tenantRelativePath(url.pathname));
|
|
162
164
|
if (crossSite) return crossSite;
|
|
163
|
-
|
|
164
165
|
// The canonical TEST sample GET signs server catalog facts without shopper state.
|
|
165
166
|
const sampleHandoff = new URL(SAMPLE_CART_HANDOFF_URL);
|
|
166
167
|
if (request.method === "GET" && !cdn && url.origin === sampleHandoff.origin &&
|
|
@@ -186,6 +186,12 @@ export interface RenderChainInput {
|
|
|
186
186
|
readMirrorBalances?: ((stage: "preview" | "publication") => Promise<ReadonlyMap<string, { balance: number; asOf: string }> | null>) | null;
|
|
187
187
|
/** Exact per-request grant for an F2-selected internal publication space. */
|
|
188
188
|
internalContentGrant?: InternalContentGrant;
|
|
189
|
+
/**
|
|
190
|
+
* An app's own credential on an app-gateway route on a preview surface
|
|
191
|
+
* (`controlPlane.ts`): the request routes the preview stage (`test`), so the
|
|
192
|
+
* handler serves only a `test` install of this tenant.
|
|
193
|
+
*/
|
|
194
|
+
appTestStage?: boolean;
|
|
189
195
|
/** Pin already verified for this tenant by the control-plane login gate. */
|
|
190
196
|
verifiedCandidatePin?: CandidateContentPin;
|
|
191
197
|
}
|
|
@@ -256,7 +262,9 @@ export async function runRenderChain(input: RenderChainInput): Promise<Response>
|
|
|
256
262
|
// page-cache read. An exact publication selection is therefore authoritative
|
|
257
263
|
// for the whole request and can scope every downstream cache decision.
|
|
258
264
|
const previewToken = verifiedPreview?.token ?? null;
|
|
259
|
-
const customization =
|
|
265
|
+
const customization: Awaited<ReturnType<typeof resolveCustomizationEnv>> = input.appTestStage
|
|
266
|
+
? { env: "test" }
|
|
267
|
+
: await resolveCustomizationEnv({
|
|
260
268
|
isPreviewHost,
|
|
261
269
|
tenantId: tenantRecord.tenant_id,
|
|
262
270
|
token: previewToken,
|
|
@@ -14,9 +14,8 @@
|
|
|
14
14
|
* (re-unsubscribing is a no-op success) and NON-LEAKING: an unknown token gets the exact
|
|
15
15
|
* same response as a real one, so an observer can't probe which tokens exist.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* would break the RFC 8058 one-click POST from a mailbox provider. See the unit report.
|
|
17
|
+
* The front door's cross-site form check admits this route's POST (lib/publication/shared/cdnForward.ts
|
|
18
|
+
* `MACHINE_POST_ROUTES`), so the mailbox provider's origin-less form POST reaches this handler.
|
|
20
19
|
*/
|
|
21
20
|
import type { APIContext } from "astro";
|
|
22
21
|
import { readD1 } from "@/lib/publication/shared/env";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.13.
|
|
3
|
+
"version": "6.13.2",
|
|
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",
|