-
Notifications
You must be signed in to change notification settings - Fork 21
ARSN-552: don't throw in case of bad Date inputs #2591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leif-scality
wants to merge
2
commits into
development/8.2
Choose a base branch
from
bugfix/ARSN-552-auth-v4-bad-date-throw
base: development/8.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,15 +15,33 @@ export function convertAmzTimeToMs(timestamp: string) { | |
| } | ||
|
|
||
| /** | ||
| * Convert UTC timestamp to ISO 8601 timestamp | ||
| * @param timestamp of UTC form: Fri, 10 Feb 2012 21:34:55 GMT | ||
| * @return ISO8601 timestamp of form: YYYYMMDDTHHMMSSZ | ||
| */ | ||
| export function convertUTCtoISO8601(timestamp: string | number) { | ||
| // convert to ISO string: YYYY-MM-DDTHH:mm:ss.sssZ. | ||
| const converted = new Date(timestamp).toISOString(); | ||
| // Remove "-"s and "."s and milliseconds | ||
| return converted.split('.')[0].replace(/-|:/g, '').concat('Z'); | ||
| * Convert UTC timestamp to ISO 8601 compact format | ||
| * @param timestamp - UTC timestamp (e.g., 'Fri, 10 Feb 2012 21:34:55 GMT') or Unix timestamp | ||
| * @return ISO8601 timestamp of form YYYYMMDDTHHMMSSZ, or undefined if invalid | ||
| * | ||
| * @example | ||
| * convertUTCtoISO8601('Fri, 10 Feb 2012 21:34:55 GMT'); // '20120210T213455Z' | ||
| * convertUTCtoISO8601(1328910895000); // '20120210T213455Z' | ||
| * convertUTCtoISO8601('invalid'); // undefined | ||
| */ | ||
| export function convertUTCtoISO8601(timestamp: string | number): string | undefined { | ||
| if (timestamp == null) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const date = new Date(timestamp); | ||
|
|
||
| if (isNaN(date.getTime())) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| // Can throw RangeError. | ||
| const converted = date.toISOString(); | ||
| return converted.split('.')[0].replace(/-|:/g, '').concat('Z'); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -41,16 +59,65 @@ export function checkTimeSkew(timestamp: string, expiry: number, log: RequestLog | |
| if ((currentTime + fifteenMinutes) < parsedTimestamp) { | ||
| log.debug('current time pre-dates timestamp', { | ||
| parsedTimestamp, | ||
| currentTimeInMilliseconds: currentTime }); | ||
| currentTimeInMilliseconds: currentTime | ||
| }); | ||
| return true; | ||
| } | ||
| const expiryInMilliseconds = expiry * 1000; | ||
| if (currentTime > parsedTimestamp + expiryInMilliseconds) { | ||
| log.debug('signature has expired', { | ||
| parsedTimestamp, | ||
| expiry, | ||
| currentTimeInMilliseconds: currentTime }); | ||
| currentTimeInMilliseconds: currentTime | ||
| }); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Validates if a string is in ISO 8601 compact format: YYYYMMDDTHHMMSSZ | ||
| * | ||
| * Checks that: | ||
| * - String is exactly 16 characters long | ||
| * - Format matches YYYYMMDDTHHMMSSZ (8 digits, 'T', 6 digits, 'Z') | ||
| * - All date/time components are valid (no Feb 30th, no 25:00:00, etc.) | ||
| * - No silent date corrections occur (prevents rollover) | ||
| * | ||
| * @param str - The string to validate | ||
| * @returns true if the string is a valid ISO 8601 compact format, false otherwise | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * isValidISO8601Compact('20160208T201405Z'); // true | ||
| * isValidISO8601Compact('20160230T201405Z'); // false (Feb 30 invalid) | ||
| * isValidISO8601Compact('20160208T251405Z'); // false (25 hours invalid) | ||
| * isValidISO8601Compact('2016-02-08T20:14:05Z'); // false (wrong format) | ||
| * isValidISO8601Compact('abcd0208T201405Z'); // false (contains letters) | ||
| * ``` | ||
| */ | ||
| export function isValidISO8601Compact(str: string): boolean { | ||
| if (str == null || typeof str !== 'string') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: |
||
| return false; | ||
| } | ||
|
|
||
| // Match format: YYYYMMDDTHHMMSSZ | ||
| const match = str.match(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/); | ||
| if (!match) { | ||
| return false; | ||
| } | ||
|
|
||
| const [, year, month, day, hour, minute, second] = match; | ||
|
|
||
| // Construct standard ISO format and validate | ||
| const isoString = `${year}-${month}-${day}T${hour}:${minute}:${second}.000Z`; | ||
| const date = new Date(isoString); | ||
|
|
||
| try { | ||
| // date.toISOString() can throw. | ||
| // date.toISOString() === isoString check prevents silent date corrections (30 February to 1 March) | ||
| return !Number.isNaN(date.getTime()) && date.toISOString() === isoString; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.