diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 3c28c45..70d7b25 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -16,3 +16,11 @@ packages: onlyBuiltDependencies: - sharp - esbuild + +# Supply-chain hardening (Semgrep p/ci blocking rules): +# - no transitive dependency from an untrusted/exotic source +# - wait 7 days (10080 min) for newly published versions +# - never accept a downgrade of security settings via a dependency update +blockExoticSubdeps: true +minimumReleaseAge: 10080 +trustPolicy: no-downgrade diff --git a/src/auth/internal-service-tenant.ts b/src/auth/internal-service-tenant.ts index 342bc1c..9fe5b4f 100644 --- a/src/auth/internal-service-tenant.ts +++ b/src/auth/internal-service-tenant.ts @@ -70,7 +70,10 @@ export function parseServiceTenants(value: string | undefined | null): string[] /** Read the configured service tenant allowlist from the environment. */ export function resolveInternalServiceTenants( - env: { INTERNAL_SERVICE_TENANTS?: string } = process.env, + // @types/node 22's ProcessEnv has only an index signature, so a direct + // default is not assignable to the narrow named-property shape. This is a + // type-only view of the same object; runtime behaviour is unchanged. + env: { INTERNAL_SERVICE_TENANTS?: string } = process.env as { INTERNAL_SERVICE_TENANTS?: string }, ): string[] { return parseServiceTenants(env.INTERNAL_SERVICE_TENANTS); } diff --git a/src/auth/internal-token.ts b/src/auth/internal-token.ts index 852d5b9..e0d7ccf 100644 --- a/src/auth/internal-token.ts +++ b/src/auth/internal-token.ts @@ -63,7 +63,10 @@ function timingSafeEqualStr(a: string, b: string): boolean { */ export function verifyInternalServiceToken( headers: HeaderLookup, - env: { INTERNAL_TOKEN?: string } = process.env, + // @types/node 22's ProcessEnv has only an index signature, so a direct + // default is not assignable to the narrow named-property shape. This is a + // type-only view of the same object; runtime behaviour is unchanged. + env: { INTERNAL_TOKEN?: string } = process.env as { INTERNAL_TOKEN?: string }, ): boolean { const expected = env.INTERNAL_TOKEN?.trim(); if (!expected) return false; diff --git a/tests/api/anonymous-routes-auth.test.ts b/tests/api/anonymous-routes-auth.test.ts index b4cd008..7380497 100644 --- a/tests/api/anonymous-routes-auth.test.ts +++ b/tests/api/anonymous-routes-auth.test.ts @@ -23,6 +23,11 @@ function req(url: string, headers: Record = {}): NextRequest { return new NextRequest(new URL(url, 'http://localhost'), { headers }); } +// @types/node 22 declares NODE_ENV readonly. These tests deliberately save +// and restore it; a typed mutable view keeps that exact without weakening the +// production auth checks under test. +const mutableEnv = process.env as Record; + describe('anonymous-access regression: /api/orders (GET) and /api/admin/metrics (GET)', () => { const saved = { auth: process.env.AUTH_ENFORCE, node: process.env.NODE_ENV }; @@ -35,8 +40,8 @@ describe('anonymous-access regression: /api/orders (GET) and /api/admin/metrics afterEach(() => { if (saved.auth === undefined) delete process.env.AUTH_ENFORCE; else process.env.AUTH_ENFORCE = saved.auth; - if (saved.node === undefined) delete process.env.NODE_ENV; - else process.env.NODE_ENV = saved.node; + if (saved.node === undefined) delete mutableEnv.NODE_ENV; + else mutableEnv.NODE_ENV = saved.node; }); it('REGRESSION: GET /api/orders 401s with no Authorization header (was: 200 + full order list)', async () => { diff --git a/tests/auth/context.test.ts b/tests/auth/context.test.ts index c573ba7..86dbb1a 100644 --- a/tests/auth/context.test.ts +++ b/tests/auth/context.test.ts @@ -36,6 +36,11 @@ function h(init: Record): Headers { return new Headers(init); } +// @types/node 22 declares NODE_ENV readonly. These tests deliberately save +// and restore it; a typed mutable view keeps that exact without weakening the +// fail-closed auth contract under test. +const mutableEnv = process.env as Record; + describe('auth default -- fail-closed (no AUTH_ENFORCE in the environment)', () => { const saved = { auth: process.env.AUTH_ENFORCE, node: process.env.NODE_ENV }; @@ -46,8 +51,8 @@ describe('auth default -- fail-closed (no AUTH_ENFORCE in the environment)', () afterEach(() => { if (saved.auth === undefined) delete process.env.AUTH_ENFORCE; else process.env.AUTH_ENFORCE = saved.auth; - if (saved.node === undefined) delete process.env.NODE_ENV; - else process.env.NODE_ENV = saved.node; + if (saved.node === undefined) delete mutableEnv.NODE_ENV; + else mutableEnv.NODE_ENV = saved.node; }); it('REGRESSION: with the variable unset, an unauthenticated request is REJECTED (401)', async () => { @@ -98,7 +103,7 @@ describe('auth default -- fail-closed (no AUTH_ENFORCE in the environment)', () }); it('a production process with AUTH_ENFORCE=false cannot serve a request at all (throws)', async () => { - process.env.NODE_ENV = 'production'; + mutableEnv.NODE_ENV = 'production'; process.env.AUTH_ENFORCE = 'false'; await expect( resolveAuthContext(h({ 'x-tenant-id': 'tenant_attacker' }), { verify: okVerify }), diff --git a/tests/auth/middleware-internal-token.test.ts b/tests/auth/middleware-internal-token.test.ts index a7bad48..a169a6e 100644 --- a/tests/auth/middleware-internal-token.test.ts +++ b/tests/auth/middleware-internal-token.test.ts @@ -20,6 +20,10 @@ function req(path: string, headers: Record = {}): NextRequest { return new NextRequest(new URL(path, 'http://localhost'), { headers }); } +// @types/node 22 declares NODE_ENV readonly. This key is one of the three +// restored below; a typed mutable view keeps that exact. +const mutableEnv = process.env as Record; + describe('middleware.ts: internal-token path for /api/0pt/quality/bridge-run', () => { const saved = { auth: process.env.AUTH_ENFORCE, internal: process.env.INTERNAL_TOKEN, node: process.env.NODE_ENV }; @@ -31,8 +35,8 @@ describe('middleware.ts: internal-token path for /api/0pt/quality/bridge-run', ( afterEach(() => { for (const [k, v] of Object.entries(saved)) { const envKey = k === 'auth' ? 'AUTH_ENFORCE' : k === 'internal' ? 'INTERNAL_TOKEN' : 'NODE_ENV'; - if (v === undefined) delete process.env[envKey]; - else process.env[envKey] = v; + if (v === undefined) delete mutableEnv[envKey]; + else mutableEnv[envKey] = v; } });