Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions apps/storefront/src/lib/protectedPageEligibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const NON_PAGE_ROUTE_ROOTS = new Set([
"product",
"product-category",
"kategoria-produktu",
"pro-cat",
"pro-category",
"product-tag",
"pro-tag",
"brand",
"product-brand",
"category",
"tag",
"author",
"cart",
"checkout",
"wishlist",
"reading-list",
"account",
"auth",
"login",
"register",
"forgot-password",
"order-success",
"unsubscribe",
"community",
]);

export function shouldProbeProtectedPage(uri: string): boolean {
let segments: string[];
try {
segments = new URL(uri, "https://storefront.invalid").pathname
.split("/")
.filter(Boolean)
.map((segment) => decodeURIComponent(segment).toLowerCase());
} catch {
return false;
}

if (/^[a-z]{2}(?:-[a-z0-9]+)?$/.test(segments[0] || "")) {
segments = segments.slice(1);
}

const [root, child] = segments;
if (!root || NON_PAGE_ROUTE_ROOTS.has(root)) return false;
if (root === "shop") return segments.length === 1;
if (root === "sklep") return segments.length === 1;
if (root === "blog") {
return segments.length === 1 && !["category", "tag", "author"].includes(child);
}

return true;
}
22 changes: 22 additions & 0 deletions apps/storefront/src/lib/protectedPages.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { shouldProbeProtectedPage } from "./protectedPageEligibility.ts";

const source = (relative: string) => readFileSync(new URL(relative, import.meta.url), "utf8");

Expand All @@ -14,13 +15,34 @@ test("protected pages use authenticated non-cacheable REST access and strict log
assert.match(client, /X-FunkyCommerce-Page-Proof/);
assert.match(client, /cachePrivate:\s*true/);
assert.match(client, /isRestRouteAvailable\(PROTECTED_PAGE_ROUTE/);
assert.match(client, /!shouldProbeProtectedPage\(normalizedUri\) \|\| unprotectedPageUris\.has\(normalizedUri\)/);
assert.match(client, /protectedPageRequests\.get\(normalizedUri\)/);
assert.match(client, /unprotectedPageUris\.add\(uri\)/);
assert.match(gate, /parseStorefrontAuthRef\(`\$\{pathname\}\$\{search\}\$\{hash\}`\)/);
assert.match(gate, /role="alert"/);
assert.match(gate, /aria-labelledby="protected-page-title"/);
assert.ok(gate.indexOf("function ProtectedPageGate") < gate.indexOf("export function CmsPageContent"));
assert.match(client, /response\.status === 429/);
});

test("protected-page fallback skips known storefront and taxonomy routes", () => {
for (const uri of [
"/kategoria-produktu/oliwa-i-przyprawy/",
"/pl/kategoria-produktu/oliwa-i-przyprawy/",
"/product-category/olive-oil/",
"/shop/category/olive-oil/",
"/product-tag/sale/",
"/blog/category/recipes/",
"/author/store-owner/",
"/product/sample-product/",
"/checkout/",
]) {
assert.equal(shouldProbeProtectedPage(uri), false, uri);
}

assert.equal(shouldProbeProtectedPage("/company/shipping-policy/"), true);
});

test("CMS behaviors follow the global code-controls preference after protected-page updates", () => {
const content = source("../components/CmsPageContent.tsx");

Expand Down
26 changes: 24 additions & 2 deletions apps/storefront/src/lib/protectedPages.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BACKEND_ORIGIN } from "@funky/sdk";
import { authStore } from "./auth.ts";
import type { CmsPage } from "./pages.ts";
import { shouldProbeProtectedPage } from "./protectedPageEligibility.ts";
import { isRestRouteAvailable } from "./restRouteAvailability.ts";

const PROOF_PREFIX = "funkycommerce-page-proof:";
const PROTECTED_PAGE_ROUTE = "/funkycommerce/v1/protected-page";
const unprotectedPageUris = new Set<string>();
const protectedPageRequests = new Map<string, Promise<CmsPage | null>>();

export class ProtectedPageError extends Error {
readonly kind: "auth-required" | "password-required" | "invalid-password";
Expand Down Expand Up @@ -55,9 +58,28 @@ async function protectedPageRequest(uri: string, password?: string): Promise<Res
});
}

export async function getProtectedPageByUri(uri: string): Promise<CmsPage | null> {
export function getProtectedPageByUri(uri: string): Promise<CmsPage | null> {
const normalizedUri = new URL(uri, "https://storefront.invalid").pathname;
if (!shouldProbeProtectedPage(normalizedUri) || unprotectedPageUris.has(normalizedUri)) {
return Promise.resolve(null);
}
const pendingRequest = protectedPageRequests.get(normalizedUri);
if (pendingRequest) return pendingRequest;

const request = loadProtectedPage(normalizedUri).finally(() => {
protectedPageRequests.delete(normalizedUri);
});
protectedPageRequests.set(normalizedUri, request);
return request;
}

async function loadProtectedPage(uri: string): Promise<CmsPage | null> {
const response = await protectedPageRequest(uri);
if (!response || response.status === 404) return null;
if (!response) return null;
if (response.status === 404) {
unprotectedPageUris.add(uri);
return null;
}
if (response.status === 401) throw new ProtectedPageError("auth-required", "Sign in with permission to read this private page.");
if (response.status === 403) throw new ProtectedPageError("password-required", "This page requires a password.");
if (!response.ok) throw new Error(`Protected page request failed with status ${response.status}`);
Expand Down
Loading