-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW: add further security: rate-limiter, cors, request size limit, he…
…lmet
- Loading branch information
Showing
4 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const helmet = require('helmet'); | ||
const cors = require('cors'); | ||
const rateLimit = require('express-rate-limit'); | ||
const express = require('express'); | ||
|
||
function setupSecurityMiddleware(app) { | ||
// Security headers | ||
app.use(helmet()); | ||
|
||
// Rate limiting | ||
const limiter = rateLimit({ | ||
windowMs: 15 * 60 * 1000, // 15 minutes | ||
max: 100, // limit each IP to 100 requests per windowMs | ||
message: 'Too many requests from this cosmic sector, please try again later', | ||
standardHeaders: true, | ||
legacyHeaders: false | ||
}); | ||
app.use(limiter); | ||
|
||
// CORS configuration | ||
app.use(cors({ | ||
origin: process.env.ALLOWED_ORIGINS?.split(',') || ['http://localhost:4004'], | ||
methods: ['GET', 'POST', 'PATCH', 'DELETE'], | ||
allowedHeaders: ['Content-Type', 'Authorization'], | ||
credentials: true | ||
})); | ||
|
||
// Request size limiting | ||
app.use(express.json({ limit: '10kb' })); | ||
app.use(express.urlencoded({ extended: true, limit: '10kb' })); | ||
} | ||
|
||
module.exports = { setupSecurityMiddleware }; |
This file contains 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