From 9658447fb313cb8bc8bf91898e5dce0bea49ba11 Mon Sep 17 00:00:00 2001 From: Joe MacMahon Date: Wed, 21 Apr 2021 13:34:55 +0100 Subject: [PATCH] Disallow prepended and appended strings to RFC 3339 date-time Fixes validatorjs/validator.js#1653 Also adds corresponding test cases to ISO 8601 validator --- src/lib/isRFC3339.js | 2 +- test/validators.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/isRFC3339.js b/src/lib/isRFC3339.js index 5b3eaba85..48b025e0f 100644 --- a/src/lib/isRFC3339.js +++ b/src/lib/isRFC3339.js @@ -19,7 +19,7 @@ const partialTime = new RegExp(`${timeHour.source}:${timeMinute.source}:${timeSe const fullDate = new RegExp(`${dateFullYear.source}-${dateMonth.source}-${dateMDay.source}`); const fullTime = new RegExp(`${partialTime.source}${timeOffset.source}`); -const rfc3339 = new RegExp(`${fullDate.source}[ tT]${fullTime.source}`); +const rfc3339 = new RegExp(`^${fullDate.source}[ tT]${fullTime.source}$`); export default function isRFC3339(str) { assertString(str); diff --git a/test/validators.js b/test/validators.js index 31b7f960e..56e581d39 100644 --- a/test/validators.js +++ b/test/validators.js @@ -8925,6 +8925,8 @@ describe('Validators', () => { '2010-02-18T16:23.33.600', '2010-02-18T16,25:23:48,444', '2010-13-1', + 'nonsense2021-01-01T00:00:00Z', + '2021-01-01T00:00:00Znonsense', ]; it('should validate ISO 8601 dates', () => { @@ -9107,6 +9109,8 @@ describe('Validators', () => { '2009-05-00 14:39:22+0600', '2009-00-1 14:39:22Z', '2009-05-19T14:39:22', + 'nonsense2021-01-01T00:00:00Z', + '2021-01-01T00:00:00Znonsense', ], }); });