From 17124853da3ef5297e6f545d8776ff8e4093b782 Mon Sep 17 00:00:00 2001 From: John Senar Date: Wed, 5 Feb 2025 15:21:29 -0800 Subject: [PATCH] fix: remove negative timestamps from uploads (#3897) --- src/utils/__tests__/uploads.test.js | 2 ++ src/utils/uploads.js | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/utils/__tests__/uploads.test.js b/src/utils/__tests__/uploads.test.js index 3a6ae85ebf..74856320d3 100644 --- a/src/utils/__tests__/uploads.test.js +++ b/src/utils/__tests__/uploads.test.js @@ -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', diff --git a/src/utils/uploads.js b/src/utils/uploads.js index ed9524c89d..cf95b9f41c 100644 --- a/src/utils/uploads.js +++ b/src/utils/uploads.js @@ -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; } }