check:test-evidence requires every e2e spec to use the strict fixture, which fails on the zod eval console error. The smoke now sets zod's global config jitless before page scripts run and requires zero violations; without that init script it fails on the known probe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
3.5 KiB
TypeScript
85 lines
3.5 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
|
|
import { expect, test, type Page } from "../support/browser/strict-browser-test.ts";
|
|
|
|
// 13-08. The Content-Security-Policy in config/hosting/security-headers.json was
|
|
// declared but never applied in a browser: `vite preview` sends no security
|
|
// headers. This smoke serves the build with the production headers attached
|
|
// and fails on any securitypolicyviolation event or console error.
|
|
const { headers: declaredHeaders } = JSON.parse(
|
|
readFileSync("config/hosting/security-headers.json", "utf8"),
|
|
) as { headers: Record<string, string> };
|
|
// The preview server is plain HTTP on loopback. `upgrade-insecure-requests` is a
|
|
// no-op for an HTTPS origin, but WebKit applies it to loopback and rewrites
|
|
// every same-origin request to https, which this server cannot answer. Every
|
|
// other directive is served exactly as declared.
|
|
const securityHeaders = {
|
|
...declaredHeaders,
|
|
"Content-Security-Policy": declaredHeaders["Content-Security-Policy"]!
|
|
.split(";")
|
|
.map((directive) => directive.trim())
|
|
.filter((directive) => directive !== "upgrade-insecure-requests")
|
|
.join("; "),
|
|
};
|
|
|
|
/**
|
|
* Known violation, not accepted behavior. Zod probes `Function("")` when it
|
|
* builds its first object schema, to decide whether it may compile parsers.
|
|
* The policy has no 'unsafe-eval', so the probe is refused (zod catches it and
|
|
* parses without compiling) and reported on every boot of the default build.
|
|
* `z.config({ jitless: true })` removes the probe, but it must run before the
|
|
* first schema is built, and the bundler evaluates the shared chunks that
|
|
* build schemas before the entry module body. Until the app does that, this
|
|
* smoke sets zod's shared global config (`globalThis.__zod_globalConfig`, read
|
|
* by zod/v4/core when it loads) before any page script runs, so
|
|
* every other violation is still caught. Owner: frontend-platform. Remove the
|
|
* init script below when the app configures zod itself.
|
|
*/
|
|
test.beforeEach(async ({ page, baseURL }) => {
|
|
const origin = new URL(baseURL ?? "http://127.0.0.1:4173").origin;
|
|
await page.route(
|
|
(url) => url.origin === origin,
|
|
async (route) => {
|
|
const response = await route.fetch();
|
|
await route.fulfill({
|
|
response,
|
|
headers: { ...response.headers(), ...securityHeaders },
|
|
});
|
|
},
|
|
);
|
|
await page.addInitScript(() => {
|
|
(globalThis as { __zod_globalConfig?: object }).__zod_globalConfig = { jitless: true };
|
|
});
|
|
await page.addInitScript(() => {
|
|
const violations: string[] = [];
|
|
Object.defineProperty(window, "__cspViolations", { value: violations });
|
|
document.addEventListener("securitypolicyviolation", (event) => {
|
|
violations.push(`${event.effectiveDirective} ${event.blockedURI}`);
|
|
});
|
|
});
|
|
});
|
|
|
|
async function cspViolations(page: Page): Promise<string[]> {
|
|
return page.evaluate(() => (window as unknown as { __cspViolations: string[] }).__cspViolations);
|
|
}
|
|
|
|
test("boots and changes route under the production security headers", async ({ page }) => {
|
|
const response = await page.goto("/");
|
|
expect(response?.headers()["content-security-policy"]).toBe(
|
|
securityHeaders["Content-Security-Policy"],
|
|
);
|
|
await expect(page.getByRole("heading", { level: 1 })).toHaveText(
|
|
"Clean Architecture Frontend",
|
|
);
|
|
|
|
await page
|
|
.getByRole("navigation", { name: "주요 탐색" })
|
|
.getByRole("link", { name: "화면 상태" })
|
|
.click();
|
|
await expect(
|
|
page.getByRole("heading", { level: 1, name: "화면 상태" }),
|
|
).toBeFocused();
|
|
|
|
expect(await cspViolations(page)).toEqual([]);
|
|
});
|