]> git.kaiwu.me - quickjs.git/commitdiff
temporal: stricter timezone validation
authorFabrice Bellard <fabrice@bellard.org>
Tue, 10 Nov 2026 09:54:22 +0000 (10:54 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Tue, 10 Nov 2026 09:54:22 +0000 (10:54 +0100)
temporal/temporal.c

index e6bd521d3730e79c9045774d7a5279c896de5c3c..ccabbbef464b44bee86d042983f275a6b1bc126c 100644 (file)
@@ -196,7 +196,7 @@ int ti_parse_temporal_timezone_string(const char *str, int64_t *offset_ns)
     /* ISO date-time string with ±HHMM or ±HH:MM offset at end */
     if (len >= 5) {
         const char *offset_start = NULL;
-        int offset_len = 0;
+        size_t offset_len = 0;
         int i;
         for (i = (int)(len - 6); i >= 0; i--) {
             if ((str[i] == '+' || str[i] == '-') && str[i + 3] == ':') {
@@ -235,11 +235,7 @@ int ti_parse_temporal_timezone_string(const char *str, int64_t *offset_ns)
                 int hours = h1 * 10 + h2;
                 int mins = m1 * 10 + m2;
                 if (hours <= 23 && mins <= 59) {
-                    if ((int)len > (int)(offset_start - str) + offset_len && offset_start[offset_len] == ':')
-                        return -1;
-                    if ((int)len > (int)(offset_start - str) + offset_len && offset_start[offset_len] == '.')
-                        return -1;
-                    if (len >= 7 && !memcmp(str, "-000000", 7))
+                    if (len > (offset_start - str) + offset_len)
                         return -1;
                     *offset_ns = sign * ((int64_t)hours * 3600 + mins * 60) * 1000000000LL;
                     return 0;
@@ -3877,6 +3873,15 @@ int ti_validate_timezone_identifier(JSContext *ctx, JSValueConst v, const char *
             return JS_ThrowRangeError(ctx, "invalid time zone"), -1;
         }
     }
+    /* An offset time zone identifier or an available named time zone
+       identifier (GetAvailableNamedTimeZoneIdentifier is not empty) */
+    if (str[0] == '+' || str[0] == '-') {
+        int64_t offset_ns;
+        if (ti_parse_temporal_timezone_string(str, &offset_ns) < 0) {
+            JS_FreeCString(ctx, str);
+            return JS_ThrowRangeError(ctx, "invalid time zone"), -1;
+        }
+    }
     *out_str = str;
     return 1;
 }