Skip to content

Commit 64a891b

Browse files
committed
chore(cloudfront-signer): fix date parsing by changing to date constructor
1 parent 130a74a commit 64a891b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/cloudfront-signer/src/sign.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ import { getSignedCookies, getSignedUrl } from "./index";
77
const url = "https://d111111abcdef8.cloudfront.net/private-content/private.jpeg";
88
const keyPairId = "APKAEIBAERJR2EXAMPLE";
99
const dateLessThan = "2020-01-01";
10-
const epochDateLessThan = Math.round(new Date(dateLessThan).getTime() / 1000);
10+
const epochDateLessThan = Math.round(
11+
(typeof dateLessThan === "string" && /^\d+$/.test(dateLessThan)
12+
? Number(dateLessThan)
13+
: new Date(dateLessThan).getTime()) / 1000
14+
);
1115
const dateGreaterThan = "2019-12-01";
12-
const epochDateGreaterThan = Math.round(new Date(dateGreaterThan).getTime() / 1000);
16+
const epochDateGreaterThan = Math.round(
17+
(typeof dateGreaterThan === "string" && /^\d+$/.test(dateGreaterThan)
18+
? Number(dateGreaterThan)
19+
: new Date(dateGreaterThan).getTime()) / 1000
20+
);
21+
1322
const ipAddress = "10.0.0.0";
1423
const privateKey = Buffer.from(`
1524
-----BEGIN RSA PRIVATE KEY-----

packages/cloudfront-signer/src/sign.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,13 @@ class CloudfrontSignBuilder {
363363
if (!date) {
364364
return undefined;
365365
}
366-
const parsedDate = Date.parse(date);
367-
return isNaN(parsedDate) ? undefined : this.epochTime(new Date(parsedDate));
366+
let parsedDate: Date;
367+
if (/^\d+$/.test(date)) {
368+
parsedDate = new Date(Number(date));
369+
} else {
370+
parsedDate = new Date(date);
371+
}
372+
return isNaN(parsedDate.getTime()) ? undefined : this.epochTime(parsedDate);
368373
}
369374

370375
private parseDateWindow(expiration: string, start?: string): PolicyDates {

0 commit comments

Comments
 (0)