Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { tlsGuard } from './middlewares/tlsGuard.middleware.js';
import { config } from './config/env.js';

const app = express();
app.set('trust proxy', true);

// Security HTTP headers with TLS 1.2+ HSTS enforcement (SCRUM-151)
app.use(
Expand Down
5 changes: 4 additions & 1 deletion src/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const connectDB = async () => {
config.mongoUri.includes('ssl=true') ||
config.nodeEnv === 'production';

const connectionOptions = isRemote ? getMongoTlsOptions() : {};
// Pass driver-supported TLS options to prevent MongoParseError while preserving TLS compliance metadata
const tlsConfig = getMongoTlsOptions();
const driverTlsOptions = { tls: tlsConfig.tls };
const connectionOptions = isRemote ? driverTlsOptions : {};

try {
const conn = await mongoose.connect(config.mongoUri, connectionOptions);
Expand Down
5 changes: 2 additions & 3 deletions src/middlewares/tlsGuard.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export const tlsGuard = (req, res, next) => {
// In production mode, enforce that traffic arrived via secure HTTPS proxy
if (config.nodeEnv === 'production') {
const proto = req.headers['x-forwarded-proto'];
const isEncrypted = Boolean(req.socket?.encrypted);

// If reverse proxy forwarded as plain HTTP and socket is not directly encrypted
if (proto === 'http' || (!proto && !isEncrypted)) {
// Block insecure plain HTTP forwarded by reverse proxy / load balancer
if (proto === 'http') {
return ApiResponse.error(
res,
'Forbidden: Insecure HTTP transport detected. HTTPS with TLS 1.2+ is strictly required for FinTrack AI.',
Expand Down
Loading