@tokenoftrust/storefront-runner 6.13.0 → 6.13.1

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.
@@ -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
- const OAUTH_TOKEN_PATH = "/api/apps/oauth/token";
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
- * `POST /api/apps/oauth/token` is not a browser form target either. It is the RFC 6749
106
- * token endpoint: an OAuth client posts `application/x-www-form-urlencoded` from a server,
107
- * authenticated by the client secret it carries (form fields or HTTP Basic), never by a
108
- * cookie, so a forged cross-site post gains nothing. Only that exact path and method are
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(request: Request, expectedOrigin: string): Response | null {
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" && new URL(request.url).pathname === OAUTH_TOKEN_PATH) return null;
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) {
@@ -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
- const crossSite = crossSiteFormRefusal(request, origin);
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 &&
@@ -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
- * NOTE (integration): The front door.s cross-site form check (lib/publication/shared/cdnForward.ts `crossSiteFormRefusal`,
18
- * the rule Astro.s `security.checkOrigin` applies) 403s a cross-origin form-encoded POST before it reaches this handler, which
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.0",
3
+ "version": "6.13.1",
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",