refactor(api): standardize all API routes with withApi, apiFetch, Zod validation, and security hardening#4100
Open
owenwahlgren wants to merge 1 commit into
Open
refactor(api): standardize all API routes with withApi, apiFetch, Zod validation, and security hardening#4100owenwahlgren wants to merge 1 commit into
owenwahlgren wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
24e93d8 to
c20bce3
Compare
c20bce3 to
19c15de
Compare
…urity hardening
- lib/api/: withApi wrapper, successResponse/errorResponse envelope, Zod validation,
Prisma-backed rate limiting (serializable tx), assertOwnership, parsePagination
- apiFetch client: auto-unwraps envelope, handles FormData, throws ApiClientError
- 264 tests across 15 suites, api-ci.yml workflow, check-api-standards.sh
- Security: OTP rate limit, HMAC timing-safe OAuth, faucet key sanitization,
mass assignment guards, path traversal prevention, SSRF prevention, fetch timeouts
- All client code migrated from raw fetch/axios to apiFetch (zero remaining)
- ESLint enforces apiFetch usage, bans raw fetch('/api/') and axios
- zod-validation-error, @t3-oss/env-nextjs, security headers in proxy.ts
Closes #3969, #3971, #3974, #3908, #4013
19c15de to
7a56c32
Compare
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
Comprehensive API standardization across the entire Builders Hub backend. Migrates ~100 API routes from ad-hoc patterns to a shared infrastructure layer, fixes 12+ security vulnerabilities, adds 264 tests, eliminates all raw fetch/axios from client code, and enforces standards via ESLint + CI.
Closes #3969, closes #3971, closes #3974, closes #3908, closes #4013
Addresses #3973, #3970, #4096, #4082, #4079
Shared infrastructure (
lib/api/)withApi()— composable route wrapper: auth, Zod validation, Prisma-backed rate limiting, error envelopesuccessResponse()/errorResponse()— consistent{ success: true, data }/{ success: false, error: { code, message } }envelopeapiFetch()client — auto-unwraps envelope, throwsApiClientError, handles FormData nativelyzod-validation-errorfor human-readable validation messagesassertOwnership()— generic IDOR preventionparsePagination()— clamped pagination withMAX_PAGE_SIZE=100@t3-oss/env-nextjsfor type-safe env validation (80+ vars)X-Content-Type-Options,X-Frame-Options,X-Request-Id) inproxy.tsSecurity fixes
UPDATABLE_FIELDSwhitelist on projects + build-gamesraw/[...slug]rejects..,//, null bytescalendarIdZod regex validatedAbortControlleron all external API callsMAX_PAGE_SIZEenforced everywhere (including new events routes)Client migration — zero raw fetch/axios remaining
fetch/axiostoapiFetchapiFetchhandles FormData natively (auto-detects, skips JSON.stringify)eslint-disableexceptions neededno-restricted-syntaxbansfetch('/api/...')in client codeno-restricted-importsbansaxiosimportsscripts/check-api-standards.shvalidates at CI timeRoute consolidation
/project/*→/projects/*(deprecated wrappers for backwards compat)Tests: 264 passing across 15 suites (#3908)
CI/Precommit enforcement
.github/workflows/api-ci.yml— type-check, ESLint, standards script, vitestscripts/check-api-standards.sh— stack traces, pagination, auth, secrets, raw fetch, axios.lintstagedrc.js— API routes linted on commit with zero-warning toleranceBugs found during test audit
evaluate/route.tsZod schema unwiredoauth/tokenclient enumerationprojects/[id]null 200{ success: true, data: null }validator-alerts/[id]validation orderingnotifications/createno validationcalendar/googleenv before validationNew dependencies
zod-validation-error— human-readable Zod error messages@t3-oss/env-nextjs— type-safe env validation@vitejs/plugin-react(dev) — vitest React support@testing-library/jest-dom,@testing-library/react,@testing-library/dom,jsdom(dev) — test deps (were transitive, now explicit)Verification
All pass locally:
tsc --noEmit— 0 errorseslint app/api/ --max-warnings 0— 0 errorsvitest run tests/api/— 264/264 passscripts/check-api-standards.sh— 0 errors, 0 warningsTest plan
npx prisma migrate dev --name add_api_rate_limit_log