-
Notifications
You must be signed in to change notification settings - Fork 38
fixed security in /backend/index.js #25
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,70 +3,106 @@ require('dotenv').config(); | |||||||||||||
| const express = require('express'); | ||||||||||||||
| const cors = require('cors'); | ||||||||||||||
| const mongoose = require('mongoose'); | ||||||||||||||
| const helmet = require('helmet'); | ||||||||||||||
| const rateLimit = require('express-rate-limit'); | ||||||||||||||
| const mongoSanitize = require('express-mongo-sanitize'); | ||||||||||||||
| const xss = require('xss-clean'); | ||||||||||||||
|
|
||||||||||||||
| const seedDB = require('./seed/productSeeds'); | ||||||||||||||
| const syncWeaviate = require('./sync/syncWeaviate'); | ||||||||||||||
| const productRoutes = require('./routes/products'); | ||||||||||||||
| const checkoutRoutes = require('./routes/checkout'); | ||||||||||||||
| const authRoutes = require('./routes/auth'); | ||||||||||||||
| const { swaggerUi, swaggerSpec, setupSwaggerUi, setupSwaggerJson } = require('./docs/swagger'); | ||||||||||||||
| const searchRoutes = require('./routes/search'); | ||||||||||||||
| const { setupSwaggerUi, setupSwaggerJson } = require('./docs/swagger'); | ||||||||||||||
|
|
||||||||||||||
| // Validate required environment variables | ||||||||||||||
| const requiredEnv = ['MONGO_URI', 'JWT_SECRET']; | ||||||||||||||
| requiredEnv.forEach(key => { | ||||||||||||||
| if (!process.env[key]) { | ||||||||||||||
| console.error(`❌ Missing required env var: ${key}`); | ||||||||||||||
| process.exit(1); | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| // Create Express App | ||||||||||||||
| const app = express(); | ||||||||||||||
| const PORT = process.env.PORT || 8000; | ||||||||||||||
|
|
||||||||||||||
| // Database Connection + Seed + Weaviate Sync + Server Start | ||||||||||||||
| // Security middleware | ||||||||||||||
| app.disable('x-powered-by'); | ||||||||||||||
| app.use(helmet()); | ||||||||||||||
|
|
||||||||||||||
| const corsOptions = { | ||||||||||||||
| origin: process.env.CORS_ORIGIN?.split(',') || [], | ||||||||||||||
| methods: ['GET', 'POST', 'PUT', 'DELETE'], | ||||||||||||||
| credentials: true, | ||||||||||||||
| }; | ||||||||||||||
| app.use(cors(corsOptions)); | ||||||||||||||
|
|
||||||||||||||
| app.use(express.json()); | ||||||||||||||
| app.use(express.urlencoded({ extended: true })); | ||||||||||||||
| app.use(mongoSanitize()); | ||||||||||||||
| app.use(xss()); | ||||||||||||||
|
|
||||||||||||||
| // Rate limiter for auth routes | ||||||||||||||
| const authLimiter = rateLimit({ | ||||||||||||||
| windowMs: 15 * 60 * 1000, | ||||||||||||||
| max: 10, | ||||||||||||||
|
Comment on lines
+50
to
+51
|
||||||||||||||
| windowMs: 15 * 60 * 1000, | |
| max: 10, | |
| windowMs: parseInt(process.env.AUTH_RATE_LIMIT_WINDOW_MS, 10) || 15 * 60 * 1000, | |
| max: parseInt(process.env.AUTH_RATE_LIMIT_MAX, 10) || 10, |
Copilot
AI
Sep 20, 2025
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.
The error handler logs the full error stack to console, which could expose sensitive information in production logs. Consider logging less detailed information in production environments by checking NODE_ENV.
| console.error(err.stack); | |
| if (process.env.NODE_ENV !== 'production') { | |
| console.error(err.stack); | |
| } else { | |
| console.error('Error:', err.message); | |
| } |
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.
The CORS origin configuration allows an empty array as fallback, which would block all origins. Consider using a secure default like
['http://localhost:3000']for development or require CORS_ORIGIN to be explicitly set in production.