Skip to content

Commit

Permalink
fix: remove negative timestamps from uploads (#3897)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsenar authored Feb 5, 2025
1 parent 44e3fa9 commit 1712485
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/utils/__tests__/uploads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ describe('util/uploads', () => {
${{ lastModified: 'not a number' }} | ${null}
${{ lastModified: new Date('2017-01-02T03:04:05.678Z') }} | ${'2017-01-02T03:04:05Z'}
${{ lastModified: new Date('not valid') }} | ${null}
${{ lastModified: -11644473600 }} | ${null}
${{}} | ${null}
${{ lastModifiedDate: 1483326245678 }} | ${'2017-01-02T03:04:05Z'}
${{ lastModifiedDate: 'not a number' }} | ${null}
${{ lastModifiedDate: new Date('2017-01-02T03:04:05.678Z') }} | ${'2017-01-02T03:04:05Z'}
${{ lastModifiedDate: new Date('not valid') }} | ${null}
${{ lastModifiedDate: -11644473600 }} | ${null}
${{}} | ${null}
`(
'should return the properly formatted date when possible and return null otherwise',
Expand Down
24 changes: 17 additions & 7 deletions src/utils/uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,23 @@ function getFileLastModifiedAsISONoMSIfPossible(file: UploadFile): ?string {
// The compatibility chart at https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified#Browser_compatibility
// is not up to date as of 12-13-2018. Edge & ie11 do not support lastModified, but support lastModifiedDate.
const lastModified = file.lastModified || file.lastModifiedDate;
if (
lastModified &&
(typeof lastModified === 'string' || typeof lastModified === 'number' || lastModified instanceof Date)
) {
const lastModifiedDate = new Date(lastModified);
if (isValidDateObject(lastModifiedDate)) {
return toISOStringNoMS(lastModifiedDate);
if (lastModified) {
let lastModifiedDate: Date | null = null;

if (typeof lastModified === 'number') {
// Only non-negative timestamps are valid. In rare cases, the timestamp may be erroneously set to a negative value
// https://issues.chromium.org/issues/393149335
if (lastModified < 0) {
return null;
}
lastModifiedDate = new Date(lastModified); // Try number first
} else if (typeof lastModified === 'string' || lastModified instanceof Date) {
lastModifiedDate = new Date(lastModified);
}

if (lastModifiedDate && isValidDateObject(lastModifiedDate)) {
const isoString = toISOStringNoMS(lastModifiedDate);
return isoString;
}
}

Expand Down

0 comments on commit 1712485

Please sign in to comment.