Configure CORS and Helmet.js for secure HTTP headers#61
Open
arandomogg wants to merge 1 commit into
Open
Conversation
Add a dedicated security configuration module that restricts cross-origin requests to an allow-list of frontend origins and applies production-grade HTTP security headers via Helmet. - Parse allowed origins from the comma-separated CORS_ORIGIN env variable - Reject disallowed origins with a 403 via a typed CorsNotAllowedError - Permit credentialed and preflight (OPTIONS) requests for allowed origins - Configure Helmet with a strict CSP, HSTS, and referrer policy - Apply both middlewares globally across all API routes - Document multi-origin CORS configuration in .env.example - Add unit tests covering header hardening and CORS allow/deny behaviour Closes SwiftChainn#7
|
@arandomogg Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
Hardens the backend's HTTP security posture by introducing a dedicated, strongly-typed security configuration module. Cross-origin requests are now restricted to an explicit allow-list of frontend origins, and Helmet is configured with production-grade security headers and applied globally across all API routes.
Previously, CORS fell back to a permissive
*wildcard and Helmet ran with bare defaults. This change makes the policy explicit, configurable through the environment, and verifiable.Changes
src/config/security.ts(new): Centralized security configuration.getAllowedOrigins()parses the comma-separatedCORS_ORIGINenvironment variable into a normalized allow-list.isOriginAllowed()encapsulates the allow/deny decision (permits server-to-server requests without anOriginheader, supports an explicit*wildcard).corsOptionsDelegateresolves the policy per request, enables credentials and preflight handling, and exposes rate-limit headers.CorsNotAllowedErrorcarries a403status code so the global error handler returnsForbiddenrather than a generic500.helmetOptionsenforces a strict Content-Security-Policy, one-year HSTS withincludeSubDomains/preload, and ano-referrerpolicy.src/app.ts: Wires the new Helmet and CORS configuration, applied globally to every route. Addstrust proxyso headers and rate limiting use the correct client IP behind a proxy..env.example: Documents multi-originCORS_ORIGINconfiguration.tests/security.test.ts(new): Unit tests covering header hardening and CORS allow/deny/preflight behaviour.Acceptance criteria
/api/v1); security middleware is applied before all routes.CORS_ORIGIN.Proof of work
All unit tests pass:
npx tsc(build) andeslinton the changed files both pass cleanly.Closes #7