Skip to content

Commit c29724c

Browse files
committed
Add HTTPS enforcement middleware for production deployments (Issue #701)
Implement automatic HTTP to HTTPS redirection in production environments. This ensures that all credentials and sensitive data are transmitted only over encrypted HTTPS connections, preventing man-in-the-middle attacks and credential leaks. Changes: - Create httpsRedirect middleware that checks x-forwarded-proto header - Redirect HTTP requests to HTTPS in production - Add middleware to server.js before other routes - Handles reverse proxy scenarios (Netlify, Heroku, etc.) Fixes #701
1 parent 53f820b commit c29724c

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const httpsRedirect = (req, res, next) => {
2+
if (process.env.NODE_ENV === 'production') {
3+
if (req.header('x-forwarded-proto') !== 'https') {
4+
return res.redirect(301, `https://${req.header('host')}${req.url}`);
5+
}
6+
}
7+
next();
8+
};
9+
10+
module.exports = httpsRedirect;

backend/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ const cors = require('cors');
1010
require('./config/passportConfig');
1111

1212
const logger = require('./logger');
13+
const httpsRedirect = require('./middleware/httpsRedirect');
1314

1415
const app = express();
1516

17+
// HTTPS enforcement (must be before other middleware)
18+
app.use(httpsRedirect);
19+
1620
// CORS configuration
1721
const allowedOrigins = ['http://localhost:5173', 'https://github-spy.etlify.app'];
1822
app.use(cors({

0 commit comments

Comments
 (0)