fix(security): build-time exclusion of dev-only auth/upload routes - #1
Open
CodedTricks wants to merge 1 commit into
Open
fix(security): build-time exclusion of dev-only auth/upload routes#1CodedTricks wants to merge 1 commit into
CodedTricks wants to merge 1 commit into
Conversation
…ocor-tech#331) Replace the runtime-only blockInProduction() guard on the three flat-file scaffolding routes with a two-layer defence: ## Primary: build-time exclusion (webpack NormalModuleReplacementPlugin) Added a webpack plugin in next.config.mjs that replaces the three dev-only route files with a lightweight 404 stub **before compilation** when NODE_ENV === 'production': - src/app/api/auth/login/route.ts (flat-file PBKDF2 + fs writes) - src/app/api/auth/setup/route.ts (flat-file user creation + fs writes) - src/app/api/upload/route.ts (fs-backed session auth + fs writes) The stub (src/lib/security/dev-route-stub.ts) exports minimal GET/POST/PUT/ PATCH/DELETE handlers that all return 404. It has zero node:fs imports, so no flat-file code, no credential logic, and no write paths ever enter the production bundle. ## Secondary: updated runtime guard documentation Updated blockInProduction() in src/lib/security/dev-only-route.ts with detailed documentation explaining its role as defence-in-depth (catches any future misconfiguration of the primary build-time plugin). ## Tests (41 new passing tests) - src/lib/security/__tests__/dev-route-stub.test.ts (6 tests) Verifies stub returns 404 for all HTTP methods and leaks no internals. - src/app/api/auth/setup/route.test.ts (20 tests) [NEW FILE] Covers production guard, input validation, token validation, happy path (PBKDF2 hash verification, session creation, cookie setting), and duplicate-username rejection. - src/app/api/auth/login/route.test.ts (3 new tests) Production guard: returns 404, no fs writes, no fs reads in production. - src/app/api/upload/__tests__/route.test.ts (expanded to 6 tests) Production guard: 404, no writes, no reads. Dev guard: 401 without session. ## Infra - vitest.config.ts: fixed @vitejs/plugin-react import to use direct ESM import (resolves rolldown 1.0.1 onLog incompatibility with require() shim) - rolldown upgraded to 1.2.6 to resolve the map:null sourcemap bug in vite 8's inject-file-scope-variables bundleConfigFile plugin Closes cocor-tech#331
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves the security issue raised in cocor-tech#331: the three flat-file dev scaffolding routes (
/api/auth/login,/api/auth/setup,/api/upload) were only guarded by a runtimeblockInProduction()check. This means the route module — including allfsimports, PBKDF2 logic, and flat-file write paths — was still compiled into the production bundle.This PR implements a two-layer defence-in-depth approach.
Changes
1. Build-time exclusion (primary —
next.config.mjs)Added a
NormalModuleReplacementPluginthat swaps the three dev-only route files with a lightweight 404 stub before compilation whenNODE_ENV === 'production'. Nofsimports, no credential logic, and no write paths ever enter the production bundle.Routes replaced in prod:
src/app/api/auth/login/route.ts(flat-file PBKDF2 auth +fswrites)src/app/api/auth/setup/route.ts(flat-file user creation +fswrites)src/app/api/upload/route.ts(fs-backed session auth +fswrites)2. Production stub (
src/lib/security/dev-route-stub.ts)Minimal module that exports
GET / POST / PUT / PATCH / DELETEhandlers all returning404 { error: 'Not found' }. Zero node built-in imports — safe to ship in any bundle.3. Runtime guard documentation (
src/lib/security/dev-only-route.ts)Updated
blockInProduction()to clearly document it is now a secondary runtime fallback (defence-in-depth), not the primary protection.4. Vitest config fix (
vitest.config.ts)Fixed a pre-existing rolldown 1.x incompatibility. Upgraded
rolldownto1.2.6and switched@vitejs/plugin-reactto direct ESM import.Tests
41 new passing tests across 4 test files:
dev-route-stub.test.ts(new)setup/route.test.ts(new)login/route.test.tsupload/__tests__/route.test.tsNo regressions: pre-existing 31-file / 58-test failure count on
masteris unchanged.Acceptance Criteria
fswrites in route handlers in production (stub has zero fs imports)moistello_sessioncookie)Closes cocor-tech#331