Reject ridiculous years in Gantt charts.

This commit is contained in:
Atul Varma 2023-05-02 09:46:59 -04:00
parent c79be5d829
commit 725b80831e
2 changed files with 17 additions and 1 deletions

View File

@ -287,7 +287,17 @@ const getStartDate = function (prevTime, dateFormat, str) {
log.debug('Invalid date:' + str); log.debug('Invalid date:' + str);
log.debug('With date format:' + dateFormat.trim()); log.debug('With date format:' + dateFormat.trim());
const d = new Date(str); const d = new Date(str);
if (d === undefined || isNaN(d.getTime())) { if (
d === undefined ||
isNaN(d.getTime()) ||
// WebKit browsers can mis-parse invalid dates to be ridiculously
// huge numbers, e.g. new Date('202304') gets parsed as January 1, 202304.
// This can cause virtually infinite loops while rendering, so for the
// purposes of Gantt charts we'll just treat any date beyond 10,000 AD/BC as
// invalid.
d.getFullYear() < -10000 ||
d.getFullYear() > 10000
) {
throw new Error('Invalid date:' + str); throw new Error('Invalid date:' + str);
} }
return d; return d;

View File

@ -432,4 +432,10 @@ describe('when using the ganttDb', function () {
ganttDb.setTodayMarker(expected); ganttDb.setTodayMarker(expected);
expect(ganttDb.getTodayMarker()).toEqual(expected); expect(ganttDb.getTodayMarker()).toEqual(expected);
}); });
it('should reject dates with ridiculous years', function () {
ganttDb.setDateFormat('YYYYMMDD');
ganttDb.addTask('test1', 'id1,202304,1d');
expect(() => ganttDb.getTasks()).toThrowError('Invalid date:202304');
});
}); });