Skip to content

Commit 65c42e8

Browse files
committed
fix: validate JWT_SECRET env var before use in IP hashing
Both check-duplicate-login.js and track-session.js use process.env.JWT_SECRET directly in hashIP() without validation. If JWT_SECRET is unset, crypto.createHash silently hashes with the literal string "undefined", producing deterministic but incorrect hashes that degrade IP-based security checks. Extract JWT_SECRET to a module-level constant with a validation guard, consistent with all other API files in the project (login.js, verify.js, reset-password.js, etc.).
1 parent 707ae28 commit 65c42e8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

api/check-duplicate-login.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Check for duplicate/multiple logins from different IPs
22
import crypto from 'crypto';
33

4+
const JWT_SECRET = process.env.JWT_SECRET;
5+
if (!JWT_SECRET) {
6+
console.error('Missing required environment variable: JWT_SECRET');
7+
}
8+
49
// Simple in-memory store for active sessions (in production, use Redis or Firebase)
510
const activeSessions = new Map();
611

@@ -15,7 +20,7 @@ setInterval(() => {
1520
}, 5 * 60 * 1000);
1621

1722
function hashIP(ip) {
18-
return crypto.createHash('sha256').update(ip + process.env.JWT_SECRET).digest('hex').substring(0, 16);
23+
return crypto.createHash('sha256').update(ip + JWT_SECRET).digest('hex').substring(0, 16);
1924
}
2025

2126
export default async function handler(req, res) {

api/track-session.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import geoip from 'geoip-lite';
55
import UAParser from 'ua-parser-js';
66
import crypto from 'crypto';
77

8+
const JWT_SECRET = process.env.JWT_SECRET;
9+
if (!JWT_SECRET) {
10+
console.error('Missing required environment variable: JWT_SECRET');
11+
}
12+
813
// VPN/Proxy detection based on common patterns
914
const VPN_INDICATORS = {
1015
// Known VPN/Proxy ASN ranges
@@ -38,7 +43,7 @@ const VPN_INDICATORS = {
3843

3944
// Hash IP for privacy
4045
function hashIP(ip) {
41-
return crypto.createHash('sha256').update(ip + process.env.JWT_SECRET).digest('hex').substring(0, 16);
46+
return crypto.createHash('sha256').update(ip + JWT_SECRET).digest('hex').substring(0, 16);
4247
}
4348

4449
// Detect if IP is likely a VPN/Proxy

0 commit comments

Comments
 (0)