fix(contracts): set zod jitless before any schema is built so the production csp sees no eval (F-7, 13-08)

zod probed Function("") on its first object schema, which the production CSP
(no unsafe-eval) reported on every boot. Schema code now imports z from
src/contracts/zod.ts, enforced by a dependency-cruiser rule, and the CSP smoke
drops its init-script workaround.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
donghyeon-ka
2026-09-27 12:58:57 +09:00
co-authored by Claude Opus 5.5
parent 606c191941
commit 9dc95ca066
16 changed files with 74 additions and 32 deletions
+12
View File
@@ -56,6 +56,18 @@
"path": "^lucide-react$" "path": "^lucide-react$"
} }
}, },
{
"name": "zod-is-reached-through-contracts-zod",
"comment": "F-7 (13-08). src/contracts/zod.ts sets zod jitless before any schema is built, so the production CSP never sees zod's Function probe. Schema code imports z from there.",
"severity": "error",
"from": {
"path": "^src",
"pathNot": "^src/contracts/zod\\.ts$"
},
"to": {
"path": "^zod$"
}
},
{ {
"name": "design-system-is-reached-through-its-index", "name": "design-system-is-reached-through-its-index",
"comment": "VD-05, 12-08. Code outside src/presentation/design-system imports its public index.ts only, so the primitives can change behind it. Named facades: src/presentation/components/ui/*.ts re-export the canonical primitives under their pre-VD-05 paths (VD-05 Rollback). Exception: src/presentation/templates/page-templates.tsx reads PaginationLandmarkContext, which index.ts does not export. Owner: frontend-platform. Revisit: export it from index.ts or move the page templates into the design system.", "comment": "VD-05, 12-08. Code outside src/presentation/design-system imports its public index.ts only, so the primitives can change behind it. Named facades: src/presentation/components/ui/*.ts re-export the canonical primitives under their pre-VD-05 paths (VD-05 Rollback). Exception: src/presentation/templates/page-templates.tsx reads PaginationLandmarkContext, which index.ts does not export. Owner: frontend-platform. Revisit: export it from index.ts or move the page templates into the design system.",
+7 -8
View File
@@ -9,14 +9,13 @@ verification compares that declaration with live responses. CSP deliberately
omits `unsafe-inline` and `unsafe-eval`; production code and built assets must omits `unsafe-inline` and `unsafe-eval`; production code and built assets must
remain compatible with that baseline. `tests/e2e/production-csp.spec.ts` (part of remain compatible with that baseline. `tests/e2e/production-csp.spec.ts` (part of
`test:e2e`, FE-GATE-008) serves the build with these headers and fails on a `test:e2e`, FE-GATE-008) serves the build with these headers and fails on a
`securitypolicyviolation` event or console error (review 13-08). One `securitypolicyviolation` event or console error (review 13-08). zod probes
violation is known: zod probes `Function("")` on its first object schema and `Function("")` on its first object schema, which the policy would report on
the policy refuses it. Parsing still works, but the default build reports the every boot. `src/contracts/zod.ts` sets `jitless` to skip that probe, and every
violation on every boot until zod is configured `jitless` before the first module that builds a schema imports `z` from there, so the setting is applied
schema is built. `z.config` in the entry module is too late, because the before the first schema whatever order the bundler evaluates chunks in.
bundler evaluates the shared chunks that build schemas first. The smoke sets `z.config` in the entry module would be too late. Import `z` from
zod's global config before page scripts run so it can catch every other `src/contracts/zod.ts`, not from `zod`, in new schema code. Features that are off by
violation; the spec comment names when to remove that. Features that are off by
default need CSP additions when a project selects them; see step 9 in default need CSP additions when a project selects them; see step 9 in
[optional adapter recipes](../architecture/optional-adapter-recipes.md). [optional adapter recipes](../architecture/optional-adapter-recipes.md).
`connect-src 'self' https:` lets script reach any HTTPS origin. Narrowing it to `connect-src 'self' https:` lets script reach any HTTPS origin. Narrowing it to
+16
View File
@@ -921,6 +921,22 @@ async function runGraphFixtureChecks(): Promise<GraphFixtureResult> {
target === "lucide-react", target === "lucide-react",
), ),
}, },
// F-7 (13-08): zod must be configured jitless before any schema is built.
{
name: "direct zod import outside src/contracts/zod.ts is rejected",
passed: blockingViolations(importBoundaryGraph).some(
({ rule, source, target }) =>
rule === "zod-is-reached-through-contracts-zod" &&
source === "src/presentation/pages/direct-zod.ts" &&
target === "zod",
),
},
{
name: "zod through src/contracts/zod.ts is accepted",
passed: !blockingViolations(importBoundaryGraph).some(({ source }) =>
["src/contracts/zod.ts", "src/presentation/pages/wrapped-zod.ts"].includes(source),
),
},
{ {
name: "unselected realtime runtime composition is rejected", name: "unselected realtime runtime composition is rejected",
passed: blockingViolations(importBoundaryGraph).some( passed: blockingViolations(importBoundaryGraph).some(
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "../../contracts/zod.ts";
import { projectValidationIssues } from "../../contracts/validation-issues.ts"; import { projectValidationIssues } from "../../contracts/validation-issues.ts";
const metaSchema = z const metaSchema = z
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "./zod.ts";
import { import {
CONTRACT_SET_ALGORITHM, CONTRACT_SET_ALGORITHM,
+1 -1
View File
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "./zod.ts";
import { contractSetSchema } from "./contract-set.ts"; import { contractSetSchema } from "./contract-set.ts";
+15
View File
@@ -0,0 +1,15 @@
import { z } from "zod";
/**
* F-7 (13-08). zod probes `new Function("")` when it builds its first object
* schema, to decide whether it may compile parsers. The production CSP has no
* 'unsafe-eval', so the browser reports that probe on every boot even though
* zod catches the refusal. `jitless` skips the probe; it only helps if it runs
* before any schema is built, and a bundler may evaluate a shared chunk before
* the entry module body. So every module that builds a schema imports `z`
* from here: the dependency, not import order in main.tsx, puts this first.
*/
z.config({ jitless: true });
export { z };
export type { ZodIssue, ZodType } from "zod";
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "../../../contracts/zod.ts";
import type { import type {
CommandEffectDescriptor, CommandEffectDescriptor,
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "../../../contracts/zod.ts";
import type { RuntimeValidator } from "../../../contracts/external-contract-runtime.ts"; import type { RuntimeValidator } from "../../../contracts/external-contract-runtime.ts";
import { runtimeSchemaCodecFromValidator } from "../../../contracts/schema-registry.ts"; import { runtimeSchemaCodecFromValidator } from "../../../contracts/schema-registry.ts";
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "../../../contracts/zod.ts";
export const referenceResourceFormSchema = z export const referenceResourceFormSchema = z
.object({ .object({
+1 -1
View File
@@ -8,7 +8,7 @@ import {
type FormEvent, type FormEvent,
} from "react"; } from "react";
import { flushSync } from "react-dom"; import { flushSync } from "react-dom";
import type { ZodType, ZodIssue } from "zod"; import type { ZodIssue, ZodType } from "../../contracts/zod.ts";
import type { AppFailure } from "../../contracts/errors.ts"; import type { AppFailure } from "../../contracts/errors.ts";
import { import {
useLocale, useLocale,
@@ -1,4 +1,4 @@
import { z } from "zod"; import { z } from "../../contracts/zod.ts";
export const PLATFORM_ROUTE_CODECS = { export const PLATFORM_ROUTE_CODECS = {
// D-16: a route without input ignores unknown keys (`?utm_source=`, OAuth // D-16: a route without input ignores unknown keys (`?utm_source=`, OAuth
+2 -16
View File
@@ -22,19 +22,8 @@ const securityHeaders = {
.join("; "), .join("; "),
}; };
/** // F-7. No zod setup here: the app sets `jitless` in src/contracts/zod.ts before
* Known violation, not accepted behavior. Zod probes `Function("")` when it // any schema is built, so zod's `Function("")` probe never reaches the policy.
* 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 }) => { test.beforeEach(async ({ page, baseURL }) => {
const origin = new URL(baseURL ?? "http://127.0.0.1:4173").origin; const origin = new URL(baseURL ?? "http://127.0.0.1:4173").origin;
await page.route( await page.route(
@@ -47,9 +36,6 @@ test.beforeEach(async ({ page, baseURL }) => {
}); });
}, },
); );
await page.addInitScript(() => {
(globalThis as { __zod_globalConfig?: object }).__zod_globalConfig = { jitless: true };
});
await page.addInitScript(() => { await page.addInitScript(() => {
const violations: string[] = []; const violations: string[] = [];
Object.defineProperty(window, "__cspViolations", { value: violations }); Object.defineProperty(window, "__cspViolations", { value: violations });
@@ -0,0 +1,6 @@
// F-7 (13-08): the one module that imports zod itself.
import { z } from "zod";
z.config({ jitless: true });
export { z };
@@ -0,0 +1,5 @@
// F-7 (13-08): schema code imports z from src/contracts/zod.ts, which sets
// jitless before any schema is built.
import { z } from "zod";
export const directSchema = z.string();
@@ -0,0 +1,3 @@
import { z } from "../../contracts/zod.ts";
export const wrappedSchema = z.string();