fix(isISO8601): restrict UTC offset range - #2834
Open
deepakganesh78 wants to merge 1 commit into
Open
Conversation
Reject ISO 8601 timezone offsets outside the UTC range while preserving +14:00 and -12:00 boundary values. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2834 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2598 2598
Branches 658 658
=========================================
Hits 2598 2598 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the isISO8601 validator to reject ISO 8601 timezone offsets outside the valid UTC-offset range (−12:00 through +14:00), addressing #1883.
Changes:
- Tightens the ISO 8601 regex timezone-offset branch to enforce the
+14:00and-12:00boundaries (including compact forms like+1400,-12). - Applies the same timezone-offset constraint to both the regular and
strictSeparatorISO 8601 patterns. - Adds targeted test cases covering newly-accepted boundary offsets and newly-rejected out-of-range offsets.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/lib/isISO8601.js |
Updates the ISO 8601 regexes to restrict timezone offsets to the valid UTC range. |
test/validators.test.js |
Adds regression tests for boundary offsets and out-of-range offsets in both separator modes. |
Suppressed comments (1)
src/lib/isISO8601.js:7
- Same issue as the non-strict-separator regex: the timezone offset pattern nests the negative-offset branch under a leading
+, which breaks valid-HH[:MM]offsets and can permit+-...strings. Adjust the alternation so+and-are siblings and enforce+14:00/-12:00boundaries.
const iso8601StrictSeparator = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W(0[1-9]|[1-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[0-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|(?:\+(?:(?:0\d|1[0-3]):?[0-5]\d|14:?00|(?:0\d|1[0-4]))|-(?:(?:0\d|1[01]):?[0-5]\d|12:?00|(?:0\d|1[0-2]))))?)?)?$/;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /* eslint-disable max-len */ | ||
| // from http://goo.gl/0ejHHW | ||
| const iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W(0[1-9]|[1-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[0-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/; | ||
| const iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W(0[1-9]|[1-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[0-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|(?:\+(?:(?:0\d|1[0-3]):?[0-5]\d|14:?00|(?:0\d|1[0-4]))|-(?:(?:0\d|1[01]):?[0-5]\d|12:?00|(?:0\d|1[0-2]))))?)?)?$/; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1883
Reproduction
On current
master,isISO8601accepts UTC offsets outside the valid ISO 8601 UTC-offset range:Root cause
The ISO 8601 regex allowed any signed timezone hour from
00through23, regardless of sign. That accepts offsets beyond the UTC range of-12:00through+14:00.Fix
The timezone-offset branch now validates positive and negative offsets separately, preserving valid boundaries (
+14:00,+1400,+14,-12:00,-1200,-12) while rejecting values beyond them (+14:01,+15:00,-12:01,-13:00, etc.). The same change is applied to both regular andstrictSeparatorISO 8601 patterns.Compatibility notes
This only rejects timezone offsets outside the accepted UTC range; existing valid ISO 8601 date/time forms and in-range offsets continue to pass. README table update is not applicable because no validator/options were added or renamed.
Validation
node --require '@babel/register' -e ...:+15:00and-13:00returnedtrue.npx mocha --require @babel/register --reporter dot test\\validators.test.js --grep "ISO 8601"=> 2 passing, 3 failing.npm test(build + lint + tests) => 321 passing; coverage: Statements 100% (2743/2743), Branches 96.59% (1697/1757), Functions 100% (427/427), Lines 100% (2428/2428).Checklist