UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

45 lines (44 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = isDateTimeString; const pattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/i; /** * Checks if the string is a date time according to RFC3339 * https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 */ function isDateTimeString(str) { if (str.length < 20 || str.length > 29) { return false; } if (!pattern.test(str)) { return false; } const [date, time] = str.split("T"); // Validate date value const [year, month, day] = date.split("-").map(Number); if (month < 1 || month > 12 || day < 1 || day > 31 || year < 0) { return false; } // Validate time value const [hour, minute, second] = time.split(":").map(Number); if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) { return false; } // Validate time offset value if (!str.endsWith("Z")) { const offset = str.slice(-5); const [offsetHour, offsetMinute] = offset.split(":").map(Number); if (offsetHour < 0 || offsetHour > 23 || offsetMinute < 0 || offsetMinute > 59) { return false; } } return true; }