@tokenoftrust/storefront-runner 6.13.1 → 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.
@@ -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
  }
@@ -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
 
@@ -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 = await resolveCustomizationEnv({
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "6.13.1",
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",