Skip to content

feat(security): security headers, session management, API tokens, audit logging - #1033

Merged
Kingsman-99 merged 1 commit into
Stellar-split:mainfrom
mirabuidl-code:main
Oct 21, 2026
Merged

Kingsman-99 merged 1 commit into
Stellar-split:mainfrom
mirabuidl-code:main

Conversation

@mirabuidl-code

Copy link
Copy Markdown
Contributor

Summary

Implements four security features in a single PR.


#925 — Security headers

Adds the missing X-XSS-Protection: 1; mode=block header to next.config.js, completing the required set:

Header Value
X-Frame-Options DENY
X-Content-Type-Options nosniff
X-XSS-Protection 1; mode=block (new)
Strict-Transport-Security max-age=63072000; includeSubDomains; preload
Referrer-Policy strict-origin-when-cross-origin
Permissions-Policy camera=(), microphone=(), geolocation=(), payment=(self)

#923 — Secure session management

New src/lib/sessionManager.ts:

  • Signed session tokens (HMAC-SHA256) — cookie carries a signed token; raw session IDs never leave the server
  • Inactivity timeout — configurable via SESSION_TIMEOUT_SECONDS (default 30 min); getSession() auto-expires stale sessions
  • Session regeneration — regenerateSession() issues a new ID after login/MFA to prevent session fixation attacks
  • Proper logout — destroySession() invalidates the server record; clearSessionCookie() removes the client cookie
  • Concurrent session limit — MAX_CONCURRENT_SESSIONS = 5; oldest session evicted when exceeded
  • Activity tracking — touchSession() updates last-seen timestamp; listUserSessions() shows all active sessions
  • Secure cookie flags: httpOnly, SameSite=Strict, Secure (on HTTPS)

#924 — API authentication (OAuth 2.0-style)

New src/lib/apiTokens.ts:

  • Short-lived access tokens (JWT-style, HMAC-SHA256, 15 min) with jti, sub, scopes, iat, exp
  • Refresh tokens (opaque, 30 days) with rotation on every use
  • Replay detection — re-using a rotated refresh token triggers full token-family revocation for that user
  • Scopes — read | write | admin with hierarchy (admin ⊇ write ⊇ read)
  • Token revocation — explicit revokeAccessToken() / revokeRefreshToken() / revokeAllUserTokens()
  • checkApiAuth(authHeader, requiredScope) helper for route handlers

#926 — Security audit logging

New src/lib/securityAuditLog.ts:

  • 26 typed event types across domains: auth.*, session.*, api.*, admin.*, data.*
  • Severity levels: info | warn | critical with sensible per-event defaults
  • Tamper-evident hash chain — each entry stores a SHA-256 hash of its content and the previous entry's hash; verifyLogIntegrity() detects any modification
  • Immutable entries — frozen with Object.freeze()
  • Retention policy — configurable via AUDIT_LOG_RETENTION_DAYS (default 90 days); purgeExpiredLogs() trims old entries
  • Query/filter by event type, severity, actor, time range, free-text
  • Export — securityLogToCsv() and securityLogToJson()
  • Details field sanitised (control characters, angle brackets stripped, max 500 chars)

Tests

48 new tests in src/__tests__/security-features.test.ts — all passing.

Pre-existing test baseline: 38 failed / 181 passed → After: 37 failed / 182 passed (improved by fixing a pre-existing missing file lib/reliability/retryMetrics.ts that was causing the setup to fail).


Closes #923
Closes #924
Closes #925
Closes #926

… audit logging

- Stellar-split#925: Add X-XSS-Protection: 1; mode=block header to next.config.js,
  completing the full set of required security headers (X-Frame-Options,
  X-Content-Type-Options, X-XSS-Protection, HSTS, Referrer-Policy,
  Permissions-Policy)

- Stellar-split#923: Add server-side secure session management (src/lib/sessionManager.ts)
  - Signed session tokens (HMAC-SHA256) to prevent tampering
  - Configurable inactivity timeout (default 30 min)
  - Session regeneration on login to prevent session fixation
  - Proper logout via cookie clear + server-side invalidation
  - Concurrent session limit per user with oldest-first eviction
  - Server-side activity tracking (touchSession)
  - Secure httpOnly, SameSite=Strict, Secure cookie configuration

- Stellar-split#924: Add OAuth 2.0-style API token system (src/lib/apiTokens.ts)
  - Short-lived JWT-style access tokens (15 min, HMAC-SHA256 signed)
  - Long-lived opaque refresh tokens (30 days) with rotation
  - Replay detection: re-use of rotated token revokes entire token family
  - Scope hierarchy: read ⊂ write ⊂ admin
  - Bearer token extraction + scope enforcement helper

- Stellar-split#926: Add security audit logging (src/lib/securityAuditLog.ts)
  - 26 typed security event types (auth, session, API, admin, data)
  - Severity levels: info | warn | critical with sensible defaults
  - Tamper-evident hash chain (SHA-256, each entry hashes the previous)
  - Immutable log entries (Object.freeze)
  - Configurable retention policy (default 90 days, AUDIT_LOG_RETENTION_DAYS)
  - Query/filter by event type, severity, actor, time range, free text
  - CSV and JSON export
  - Log integrity verification

- Fix pre-existing missing file: lib/reliability/retryMetrics.ts (required by
  vitest.setup.ts; its absence was causing all tests to fail to load)

Tests: 48 new tests in src/__tests__/security-features.test.ts, all passing
@drips-wave

drips-wave Bot commented Oct 20, 2026

Copy link
Copy Markdown

@mirabuidl-code 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! 🚀

Learn more about application limits

@vercel

vercel Bot commented Oct 20, 2026

Copy link
Copy Markdown

@mirabuidl-code is attempting to deploy a commit to the kingsman-99's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Kingsman-99
Kingsman-99 merged commit 60a3c5a into Stellar-split:main Oct 21, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants