Skip to content

security: Apply Security Middleware Stack#3

Merged
nik-kale merged 1 commit into
mainfrom
security/apply-security-middleware
Dec 26, 2025
Merged

security: Apply Security Middleware Stack#3
nik-kale merged 1 commit into
mainfrom
security/apply-security-middleware

Conversation

@nik-kale
Copy link
Copy Markdown
Owner

Summary

This PR addresses a critical security vulnerability by applying the existing security middleware stack that was previously defined but never used. The backend now enforces CORS origin validation, security headers, rate limiting, input sanitization, and request validation.

Changes

  • Replaced default cors() with configureCors() for origin whitelist validation
  • Applied configureHelmet() for security headers (CSP, HSTS, X-Frame-Options)
  • Applied generalRateLimiter globally to prevent API abuse
  • Applied missionRateLimiter to POST /api/missions endpoint
  • Applied sanitizeInput middleware for XSS protection
  • Applied validateContentType and validateRequestSize middleware
  • Added notFoundHandler and errorHandler for proper error handling
  • Middleware applied in correct order for optimal security

Type of Change

  • Security patch
  • New feature
  • Bug fix
  • Breaking change
  • Documentation update

Testing

  • Verified no linting errors in modified files
  • Security middleware is now active and will:
    • Reject requests from non-whitelisted origins
    • Apply security headers to all responses
    • Rate limit requests after threshold exceeded
    • Sanitize XSS payloads in request bodies
    • Validate Content-Type and request sizes

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Comments added for complex logic
  • Documentation updated (inline comments)
  • No new warnings introduced
  • Middleware applied in correct order

Related Issues

Addresses Feature #1 from FEATURE_OPPORTUNITIES.md - Priority Score: 3.0 ⭐

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread backend/src/index.ts
Comment on lines +20 to +24
// Security middleware - Applied in order of execution
app.use(configureHelmet()); // Security headers (CSP, HSTS, X-Frame-Options)
app.use(configureCors()); // CORS with origin validation
app.use(generalRateLimiter); // Rate limiting for all endpoints
app.use(express.json()); // JSON body parser
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Trust proxy before applying rate limiters

The new global generalRateLimiter counts requests by req.ip, but the app still uses Express’s default trust proxy = false. Architecture.md documents the backend sitting behind an Nginx reverse proxy, so every request will appear to come from the proxy’s IP and all clients will share the same bucket, causing 429s even under moderate multi-user traffic. Set app.set('trust proxy', true) (or use the forwarded header) before installing the rate limiters so each client is limited independently.

Useful? React with 👍 / 👎.

@nik-kale nik-kale merged commit e41835f into main Dec 26, 2025
2 of 6 checks passed
@nik-kale nik-kale deleted the security/apply-security-middleware branch December 26, 2025 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants