diff --git a/.dependency-cruiser.json b/.dependency-cruiser.json index 4f46c87a..7ab151ca 100644 --- a/.dependency-cruiser.json +++ b/.dependency-cruiser.json @@ -56,6 +56,18 @@ "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", "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.", diff --git a/docs/security/browser-boundary.md b/docs/security/browser-boundary.md index b8fa092d..807612a3 100644 --- a/docs/security/browser-boundary.md +++ b/docs/security/browser-boundary.md @@ -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 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 -`securitypolicyviolation` event or console error (review 13-08). One -violation is known: zod probes `Function("")` on its first object schema and -the policy refuses it. Parsing still works, but the default build reports the -violation on every boot until zod is configured `jitless` before the first -schema is built. `z.config` in the entry module is too late, because the -bundler evaluates the shared chunks that build schemas first. The smoke sets -zod's global config before page scripts run so it can catch every other -violation; the spec comment names when to remove that. Features that are off by +`securitypolicyviolation` event or console error (review 13-08). zod probes +`Function("")` on its first object schema, which the policy would report on +every boot. `src/contracts/zod.ts` sets `jitless` to skip that probe, and every +module that builds a schema imports `z` from there, so the setting is applied +before the first schema whatever order the bundler evaluates chunks in. +`z.config` in the entry module would be too late. Import `z` from +`src/contracts/zod.ts`, not from `zod`, in new schema code. Features that are off by default need CSP additions when a project selects them; see step 9 in [optional adapter recipes](../architecture/optional-adapter-recipes.md). `connect-src 'self' https:` lets script reach any HTTPS origin. Narrowing it to diff --git a/scripts/check-architecture.ts b/scripts/check-architecture.ts index bcaad88c..2413162c 100644 --- a/scripts/check-architecture.ts +++ b/scripts/check-architecture.ts @@ -921,6 +921,22 @@ async function runGraphFixtureChecks(): Promise { 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", passed: blockingViolations(importBoundaryGraph).some( diff --git a/src/adapters/http/schema-registry.ts b/src/adapters/http/schema-registry.ts index 1edb48c8..ffe12b80 100644 --- a/src/adapters/http/schema-registry.ts +++ b/src/adapters/http/schema-registry.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "../../contracts/zod.ts"; import { projectValidationIssues } from "../../contracts/validation-issues.ts"; const metaSchema = z diff --git a/src/contracts/contract-set.ts b/src/contracts/contract-set.ts index 5f36455c..1ffd8ca3 100644 --- a/src/contracts/contract-set.ts +++ b/src/contracts/contract-set.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "./zod.ts"; import { CONTRACT_SET_ALGORITHM, diff --git a/src/contracts/release-artifacts.ts b/src/contracts/release-artifacts.ts index e3449fd5..f662b422 100644 --- a/src/contracts/release-artifacts.ts +++ b/src/contracts/release-artifacts.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "./zod.ts"; import { contractSetSchema } from "./contract-set.ts"; diff --git a/src/contracts/zod.ts b/src/contracts/zod.ts new file mode 100644 index 00000000..d7c5060e --- /dev/null +++ b/src/contracts/zod.ts @@ -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"; diff --git a/src/features/reference-feature/contracts/reference-feature-contract-contribution.ts b/src/features/reference-feature/contracts/reference-feature-contract-contribution.ts index 0f38d803..589c20cc 100644 --- a/src/features/reference-feature/contracts/reference-feature-contract-contribution.ts +++ b/src/features/reference-feature/contracts/reference-feature-contract-contribution.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "../../../contracts/zod.ts"; import type { CommandEffectDescriptor, diff --git a/src/features/reference-feature/contracts/reference-schemas.ts b/src/features/reference-feature/contracts/reference-schemas.ts index 0d24d33d..276cee25 100644 --- a/src/features/reference-feature/contracts/reference-schemas.ts +++ b/src/features/reference-feature/contracts/reference-schemas.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "../../../contracts/zod.ts"; import type { RuntimeValidator } from "../../../contracts/external-contract-runtime.ts"; import { runtimeSchemaCodecFromValidator } from "../../../contracts/schema-registry.ts"; diff --git a/src/features/reference-feature/presentation/reference-resource-form.ts b/src/features/reference-feature/presentation/reference-resource-form.ts index 059c596b..27af8f7b 100644 --- a/src/features/reference-feature/presentation/reference-resource-form.ts +++ b/src/features/reference-feature/presentation/reference-resource-form.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "../../../contracts/zod.ts"; export const referenceResourceFormSchema = z .object({ diff --git a/src/presentation/forms/use-app-form.ts b/src/presentation/forms/use-app-form.ts index 5a4e00a8..80556329 100644 --- a/src/presentation/forms/use-app-form.ts +++ b/src/presentation/forms/use-app-form.ts @@ -8,7 +8,7 @@ import { type FormEvent, } from "react"; 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 { useLocale, diff --git a/src/presentation/routes/platform-route-codecs.ts b/src/presentation/routes/platform-route-codecs.ts index 103b3302..b20509a5 100644 --- a/src/presentation/routes/platform-route-codecs.ts +++ b/src/presentation/routes/platform-route-codecs.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z } from "../../contracts/zod.ts"; export const PLATFORM_ROUTE_CODECS = { // D-16: a route without input ignores unknown keys (`?utm_source=`, OAuth diff --git a/tests/e2e/production-csp.spec.ts b/tests/e2e/production-csp.spec.ts index 7ae22e49..954eafd4 100644 --- a/tests/e2e/production-csp.spec.ts +++ b/tests/e2e/production-csp.spec.ts @@ -22,19 +22,8 @@ const securityHeaders = { .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. - */ +// F-7. No zod setup here: the app sets `jitless` in src/contracts/zod.ts before +// any schema is built, so zod's `Function("")` probe never reaches the policy. test.beforeEach(async ({ page, baseURL }) => { const origin = new URL(baseURL ?? "http://127.0.0.1:4173").origin; 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(() => { const violations: string[] = []; Object.defineProperty(window, "__cspViolations", { value: violations }); diff --git a/tests/fixtures/architecture/dependency-graph/import-boundaries/contracts/zod.ts b/tests/fixtures/architecture/dependency-graph/import-boundaries/contracts/zod.ts new file mode 100644 index 00000000..4cc0a37c --- /dev/null +++ b/tests/fixtures/architecture/dependency-graph/import-boundaries/contracts/zod.ts @@ -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 }; diff --git a/tests/fixtures/architecture/dependency-graph/import-boundaries/presentation/pages/direct-zod.ts b/tests/fixtures/architecture/dependency-graph/import-boundaries/presentation/pages/direct-zod.ts new file mode 100644 index 00000000..245d03fb --- /dev/null +++ b/tests/fixtures/architecture/dependency-graph/import-boundaries/presentation/pages/direct-zod.ts @@ -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(); diff --git a/tests/fixtures/architecture/dependency-graph/import-boundaries/presentation/pages/wrapped-zod.ts b/tests/fixtures/architecture/dependency-graph/import-boundaries/presentation/pages/wrapped-zod.ts new file mode 100644 index 00000000..feb836ca --- /dev/null +++ b/tests/fixtures/architecture/dependency-graph/import-boundaries/presentation/pages/wrapped-zod.ts @@ -0,0 +1,3 @@ +import { z } from "../../contracts/zod.ts"; + +export const wrappedSchema = z.string();