feat(#1004): implement XSS prevention with Content Security Policy - #1051
Open
agenes01 wants to merge 2 commits into
Open
feat(#1004): implement XSS prevention with Content Security Policy#1051agenes01 wants to merge 2 commits into
agenes01 wants to merge 2 commits into
Conversation
…y Policy
- Add cspMiddleware.ts to backend/services/shared/ with:
- CspPolicy typed interface for structured CSP directives
- DEFAULT_CSP_POLICY (strict: all-none for JSON API endpoints)
- HTML_CSP_POLICY (relaxed: for developer portal / HTML pages)
- buildCspHeader(policy) – assembles CSP header string from typed policy
- buildSecurityHeaders(policy) – full suite of 10 HTTP security headers
including X-Content-Type-Options, X-Frame-Options, HSTS, COOP, CORP,
COEP, Referrer-Policy and Permissions-Policy
- createCspMiddleware(policy?) – Express/Fastify-compatible middleware
- sanitizeHtml(input, opts?) – XSS vector stripping + HTML entity encoding
(removes script tags, event handlers, javascript:/vbscript: URIs,
iframe/svg/object/embed injections, CSS expression())
- sanitizeObject(obj, opts?) – recursive deep sanitizer for plain objects
- createXssSanitizerMiddleware(opts?) – middleware that sanitizes req.body,
req.query and req.params before route handlers run
- generateCspNonce() – cryptographic nonce (16 bytes base64url) for
inline-script whitelisting in HTML responses
- buildNoncePolicy(nonce, base?) – per-request nonce injection into CSP
- Export all new symbols and types from backend/services/shared/index.ts
- Update app.json web section with Content-Security-Policy and security
headers metadata for the Expo web bundle
- Add 93-test suite in __tests__/cspMiddleware.test.ts (all passing)
covering buildCspHeader, security headers, CSP middleware, sanitizeHtml,
sanitizeObject, XSS sanitizer middleware, nonce generation, and
integration of the two middleware layers
Closes Smartdevs17#1004
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
Closes #1004
This PR implements XSS prevention and Content Security Policy (CSP) support for the SubTrackr backend, covering the
backend/services/shared/andapp.jsonscopes defined in the issue.Changes
New:
backend/services/shared/cspMiddleware.tsA self-contained security module with zero new dependencies, providing:
DEFAULT_CSP_POLICY'none'exceptconnect-src 'self')HTML_CSP_POLICYbuildCspHeader(policy)Content-Security-Policyheader string from a typedCspPolicyobjectbuildSecurityHeaders(policy)createCspMiddleware(policy?)sanitizeHtml(input, opts?)javascript:/vbscript:URIs, iframe/SVG/object injection, CSSexpression()) and HTML-encodes remaining special characterssanitizeObject(obj, opts?)createXssSanitizerMiddleware(opts?)req.body,req.query, andreq.paramsbefore route handlers executegenerateCspNonce()buildNoncePolicy(nonce, base?)CspPolicywith the nonce injected intoscript-srcUpdated:
backend/services/shared/index.tsAll new symbols and TypeScript types re-exported from the shared package root.
Updated:
app.jsonAdded
web.meta.http-equivblock with a fully-specified CSP and supporting security headers for the Expo web bundle.Test coverage
New test file:
backend/services/shared/__tests__/cspMiddleware.test.tssanitizeHtml(XSS vectors + entity encoding + edge cases),sanitizeObject(nesting, arrays, primitives, DoS guard), XSS sanitizer middleware (field allowlist, query/params, graceful null handling), nonce generation, nonce policy injection, and an end-to-end integration test of both middleware layers chained together.Acceptance criteria checklist