From 6cdf7ef033bf9c0108842f01506a7d565c18e4e0 Mon Sep 17 00:00:00 2001 From: Brian DeHamer Date: Thu, 4 Jan 2024 09:51:00 -0800 Subject: [PATCH] fix ASN.1 date parsing w/ milliseconds (#923) Signed-off-by: Brian DeHamer --- .changeset/friendly-toes-end.md | 5 +++++ packages/core/src/__tests__/asn1/parse.test.ts | 10 +++++++++- packages/core/src/asn1/parse.ts | 6 ++++-- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .changeset/friendly-toes-end.md diff --git a/.changeset/friendly-toes-end.md b/.changeset/friendly-toes-end.md new file mode 100644 index 00000000..1ef4a0ff --- /dev/null +++ b/.changeset/friendly-toes-end.md @@ -0,0 +1,5 @@ +--- +"@sigstore/core": patch +--- + +Bug fix for parsing ASN.1 date/time values which include milliseconds diff --git a/packages/core/src/__tests__/asn1/parse.test.ts b/packages/core/src/__tests__/asn1/parse.test.ts index 65f70a34..f4ccc8db 100644 --- a/packages/core/src/__tests__/asn1/parse.test.ts +++ b/packages/core/src/__tests__/asn1/parse.test.ts @@ -56,7 +56,7 @@ describe('parseTime', () => { describe('when year is less than 50', () => { it('parses the date', () => { - expect(parseTime(Buffer.from('180212121110Z'), true)).toEqual( + expect(parseTime(Buffer.from('180212121110.099Z'), true)).toEqual( new Date('2018-02-12T12:11:10Z') ); }); @@ -71,6 +71,14 @@ describe('parseTime', () => { }); }); + describe('with long year and milliseconds', () => { + it('parses the date', () => { + expect(parseTime(Buffer.from('19180212121110.099Z'), false)).toEqual( + new Date('1918-02-12T12:11:10Z') + ); + }); + }); + describe('when the time is invalid', () => { it('throws an error', () => { expect(() => parseTime(Buffer.from('FOOBAR'), true)).toThrow( diff --git a/packages/core/src/asn1/parse.ts b/packages/core/src/asn1/parse.ts index 54d41e52..1a51c7c5 100644 --- a/packages/core/src/asn1/parse.ts +++ b/packages/core/src/asn1/parse.ts @@ -13,8 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const RE_TIME_SHORT_YEAR = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/; -const RE_TIME_LONG_YEAR = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/; +const RE_TIME_SHORT_YEAR = + /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\.\d{3})?Z$/; +const RE_TIME_LONG_YEAR = + /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\.\d{3})?Z$/; // Parse a BigInt from the DER-encoded buffer // https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-integer