Server: add baseline security response headers - #187
Merged
Conversation
Found during a repo-wide security audit: the server sends no hardening headers at all — no CORS config (fine, no browser client), but also nothing on X-Frame-Options, X-Content-Type-Options, Referrer-Policy, or HSTS. Adds SecurityHeadersMiddleware, applying X-Frame-Options: DENY, X-Content-Type-Options: nosniff, Referrer-Policy: no-referrer, and Strict-Transport-Security to every response. Registered at `.beginning` so it wraps Vapor's default ErrorMiddleware rather than being wrapped by it — at the default `.end` position an error thrown by routing or a handler unwinds past the middleware without running its header-setting code, so 404/401/429/500 responses would ship without these headers. Caught by a test that specifically checks a 404. Not auth/token/crypto/session logic, so not gated behind the security-review rule — pure response hardening with no behavior change for the iOS client (which ignores headers it doesn't recognize). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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
Found during a repo-wide security audit (auth surface, rate limiter, secrets, dependencies, transport/headers, authorization). The server currently sends no hardening headers on any response — no CORS config (correct: no browser client exists, so same-origin policy is the right default and adding CORS would be a regression), but also nothing on
X-Frame-Options,X-Content-Type-Options,Referrer-Policy, or HSTS.This is a JSON API consumed only by the iOS app, so these headers are second-order hardening (they matter if a response body is ever rendered by a browser — an error page, a misconfigured proxy, a future web client) rather than a direct fix for an exploitable path. Cheap, zero behavior change for the app, worth doing.
What it does
Adds
SecurityHeadersMiddleware(chess-server/Sources/App/Middleware/SecurityHeadersMiddleware.swift), applied to every response:X-Frame-Options: DENYX-Content-Type-Options: nosniffReferrer-Policy: no-referrerStrict-Transport-Security: max-age=31536000; includeSubDomainsRegistration order matters and is covered by a test. Registered at
.beginningso it wraps Vapor's defaultErrorMiddlewarerather than being wrapped by it. At the default.endposition, an error thrown by routing or a handler (a 404, a 401, a 429 from the rate limiter, a 500) unwinds straight past the middleware without running its header-setting code — I hit this myself first (testHeadersAreAlsoPresentOnErrorResponsesfailed against the naive.endregistration) and fixed it before opening this PR. Error responses are exactly the ones where these headers matter most, so this isn't a cosmetic detail.Test plan
SecurityHeadersMiddlewareTests: headers present on a normal 200 (/health) and on a 404 (/no-such-route).swift test --package-path chess-server— 91/91 pass locally (Xcode-beta toolchain).swiftformat --lintclean on the changed files.Security-sensitive?
No — this is response-header middleware, not auth/token/crypto/account-linking/session logic, so it's not gated behind CLAUDE.md rule 5's draft-until-APPROVE flow. Opened as a normal PR.