-
Notifications
You must be signed in to change notification settings - Fork 0
✨ Backend Auth, CSRF, and Security Headers #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…login and /logout; protect /trends and mutating APIs; add tests (auth_csrf) and update existing tests; switch CSRF token to URL-safe base64; clippy clean
…ppState, config, and pw-hash; align with existing project style
…satisfy type inference; all tests pass
…y warnings in tests (format args, remove needless borrows); keep unsafe env var blocks per project guidance. Refs #5
…itch RNG sources to password-hash rand_core OsRng to resolve rand_core version mismatch; run cargo fmt and clippy; address PR feedback. Refs #5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements comprehensive security features for the Axum backend including single-user session authentication, double-submit CSRF protection, and security headers. It protects the /trends UI route and all mutating API routes while keeping GET routes open for public access.
Key changes:
- Added session-based authentication with encrypted cookies and login/logout endpoints
- Implemented double-submit CSRF protection for all mutating requests (POST/PUT/DELETE)
- Added security headers including CSP, X-Frame-Options, and optional HSTS
Reviewed Changes
Copilot reviewed 16 out of 18 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| sleep-api/tests/trends_bars.rs | Updated test to include authentication flow and CSRF token handling |
| sleep-api/tests/auth_csrf.rs | New comprehensive test validating authentication and CSRF protection |
| sleep-api/tests/api_sleep.rs | Updated existing API tests to authenticate and send CSRF tokens |
| sleep-api/src/trends.rs | Minor string formatting improvement using string interpolation |
| sleep-api/src/security/mod.rs | New module documentation for security utilities |
| sleep-api/src/security/headers.rs | Implementation of security headers middleware |
| sleep-api/src/security/csrf.rs | CSRF protection implementation with double-submit cookie pattern |
| sleep-api/src/middleware/mod.rs | New middleware module documentation |
| sleep-api/src/middleware/auth_layer.rs | Authentication extractors for session validation |
| sleep-api/src/main.rs | Added new module declarations |
| sleep-api/src/lib.rs | Exposed new modules in public API |
| sleep-api/src/config.rs | Added configuration helpers for auth credentials and secrets |
| sleep-api/src/auth.rs | Core authentication utilities for session management |
| sleep-api/src/app.rs | Updated router with authentication routes and middleware |
| sleep-api/Cargo.toml | Added required dependencies for authentication and CSRF |
| .env.example | Configuration template with security settings |
…pendency; run fmt/clippy. Refs #5
…use config-based names for session/CSRF cookies; switch to axum_extra::either::Either for /login form+JSON
… CSRF; update README for COOKIE_SECURE and double-submit
…t(session): add SESSION_TTL_HOURS Max-Age; feat(security): COOKIE_SECURE dev-mode cookie names/flags; refactor(csrf): dynamic cookie names and header handling; docs(openapi,README): add /login.json, securitySchemes and CSRF requirements; chore(CSP): add connect-src 'self'
…-protect /logout; gate GET /sleep/date and trends APIs by session. headers: CSP allow cdn.jsdelivr.net for Chart.js.
… protected endpoints; gate GET endpoints in spec. tests: CSRF required on logout; add percent-encoded CSRF header test and dev cookie flags test.
…tests; add percent-encoded token and dev cookie flags coverage. build: add serial_test dev-dep.
…y warnings in tests (format args, remove needless borrows); keep unsafe env var blocks per project guidance. Refs #5
…itch RNG sources to password-hash rand_core OsRng to resolve rand_core version mismatch; run cargo fmt and clippy; address PR feedback. Refs #5
…pendency; run fmt/clippy. Refs #5
* PR-A: deps, .env.example, and pw-hash helper; add config for admin and session key * PR-A: add auth, CSRF, and security header modules (scaffold) * PR-A: wire AppState + session auth, CSRF, and security headers; add /login and /logout; protect /trends and mutating APIs; add tests (auth_csrf) and update existing tests; switch CSRF token to URL-safe base64; clippy clean * 🎨 Format * docs: add module and item docs for auth, CSRF, headers, middleware, AppState, config, and pw-hash; align with existing project style * fix(doctest): make axum Router state explicit in doctest examples to satisfy type inference; all tests pass * chore: replace eprintln! with tracing::debug in CSRF guard; fix clippy warnings in tests (format args, remove needless borrows); keep unsafe env var blocks per project guidance. Refs #5 * 🎨 Format * chore(csrf): use percent-encoding crate for header token decoding; switch RNG sources to password-hash rand_core OsRng to resolve rand_core version mismatch; run cargo fmt and clippy; address PR feedback. Refs #5 * ⬆️ Update lock file * 🎨 Format * chore(csrf): move X_CSRF_TOKEN to module scope; remove unused rand dependency; run fmt/clippy. Refs #5 * Lock file update * 🚨 Fix linter error * fix(login): accept HTML form and JSON payloads for /login * docs(csrf): remove unused CSRF_SECRET; clarify double-submit design * feat(cookie): support dev-mode cookie names/flags via COOKIE_SECURE; use config-based names for session/CSRF cookies; switch to axum_extra::either::Either for /login form+JSON * docs(openapi): add /login and /logout; document cookie-based auth and CSRF; update README for COOKIE_SECURE and double-submit * feat(auth): split /login handlers (form + JSON) and update tests; feat(session): add SESSION_TTL_HOURS Max-Age; feat(security): COOKIE_SECURE dev-mode cookie names/flags; refactor(csrf): dynamic cookie names and header handling; docs(openapi,README): add /login.json, securitySchemes and CSRF requirements; chore(CSP): add connect-src 'self' * auth: /login accepts form or JSON; form redirects 303. security: CSRF-protect /logout; gate GET /sleep/date and trends APIs by session. headers: CSP allow cdn.jsdelivr.net for Chart.js. * spec: /login 303 form redirect; deprecate /login.json; add 401/403 to protected endpoints; gate GET endpoints in spec. tests: CSRF required on logout; add percent-encoded CSRF header test and dev cookie flags test. * tests: isolate env with serial_test; enforce COOKIE_SECURE=1 in CSRF tests; add percent-encoded token and dev cookie flags coverage. build: add serial_test dev-dep. * 🩹 Small fixes * docs(app): add rustdoc for handlers and pages (auth, CRUD, trends) in app.rs
Summary
This PR implements PR-A: single-user session authentication, double-submit CSRF protection, and security headers for the Axum backend. It protects the /trends UI route and all mutating API routes, while keeping non‑mutating GET routes open.
Key changes
Tests
Quality gates
Security & operational notes
Follow-ups (optional)