Skip to content

Commit 0ff6d94

Browse files
committed
feat: add timezone validation to prevent SQL injection
Add isValidTimeZone() utility function to validate timezone strings before using them in SQL queries. The function ensures only safe characters are allowed in timezone names. Security improvements: - Validate timezone strings against safe character regex - Reject strings with SQL injection patterns - Support all valid IANA timezone formats - Length limits and empty string checks This prevents potential SQL injection in the SET TIME ZONE command while maintaining compatibility with all legitimate timezone values.
1 parent f991ef8 commit 0ff6d94

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/utils/time.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ export const calculateDelay = (attempts: number): number =>
77
Math.min(1000 * Math.pow(2, Math.max(1, attempts)) + Math.random() * 100, Math.pow(2, 31) - 1);
88

99
export const getCurrentTimeZone = (): string => Intl.DateTimeFormat().resolvedOptions().timeZone;
10+
11+
/**
12+
* Validates that a timezone string is safe to use in SQL.
13+
* PostgreSQL timezone names should only contain alphanumeric characters, underscores, slashes, plus, and minus.
14+
* @param timezone - The timezone string to validate
15+
* @returns true if the timezone is safe
16+
*/
17+
export const isValidTimeZone = (timezone: string): boolean => {
18+
// Allow only safe characters: alphanumeric, underscore, slash, plus, minus, and colon
19+
// This matches valid IANA timezone names like "America/New_York", "UTC", "GMT+8", etc.
20+
return /^[a-zA-Z0-9_/+:-]+$/.test(timezone) && timezone.length > 0 && timezone.length < 100;
21+
};

0 commit comments

Comments
 (0)