Skip to content

Commit 4c7b66a

Browse files
committed
fix: fix broken timezone mismatch detection in session tracking
geo.timezone from geoip-lite is an IANA string (e.g. "America/New_York"), not a numeric offset. parseInt() on it always returns NaN, making the comparison always false — the timezone mismatch security flag never fires. Convert the IANA timezone to a minute offset using toLocaleString() with the timeZone option, then compare against the browser-reported offset. Wrap in try/catch for invalid IANA timezone strings.
1 parent 707ae28 commit 4c7b66a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

api/track-session.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,21 @@ export default async function handler(req, res) {
249249

250250
// Geo anomaly (if timezone doesn't match)
251251
if (metadata.timezone && geo?.timezone) {
252-
const tzOffset = new Date().getTimezoneOffset();
253-
const geoOffset = parseInt(geo.timezone);
254-
if (Math.abs(tzOffset + geoOffset * 60) > 120) { // More than 2 hours difference
255-
securityFlags.push({
256-
type: 'timezone_mismatch',
257-
severity: 'low',
258-
detail: `Browser timezone doesn't match IP location`
259-
});
252+
try {
253+
// geo.timezone is an IANA string (e.g. "America/New_York"), not a numeric offset
254+
const now = new Date();
255+
const geoOffsetMs = now.getTime() - new Date(now.toLocaleString('en-US', { timeZone: geo.timezone })).getTime();
256+
const geoOffsetMin = Math.round(geoOffsetMs / 60000);
257+
const browserOffset = typeof metadata.timezone === 'number' ? metadata.timezone : now.getTimezoneOffset();
258+
if (Math.abs(geoOffsetMin - browserOffset) > 120) { // More than 2 hours difference
259+
securityFlags.push({
260+
type: 'timezone_mismatch',
261+
severity: 'low',
262+
detail: `Browser timezone doesn't match IP location`
263+
});
264+
}
265+
} catch {
266+
// Invalid IANA timezone — skip check
260267
}
261268
}
262269

0 commit comments

Comments
 (0)