Skip to content

Commit 3bfae30

Browse files
committed
fix(cloudfront-signer): adding testcases to accept number in milliseconds
1 parent 64a891b commit 3bfae30

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

packages/cloudfront-signer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const s3ObjectKey = "private-content/private.jpeg";
1717
const url = `${cloudfrontDistributionDomain}/${s3ObjectKey}`;
1818
const privateKey = "CONTENTS-OF-PRIVATE-KEY";
1919
const keyPairId = "PUBLIC-KEY-ID-OF-CLOUDFRONT-KEY-PAIR";
20-
const dateLessThan = "2022-01-01"; // any Date constructor compatible
20+
const dateLessThan = "2022-01-01"; // accepts string (Date constructor compatible), number (milliseconds since epoch)
2121

2222
const signedUrl = getSignedUrl({
2323
url,

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ 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(
11-
(typeof dateLessThan === "string" && /^\d+$/.test(dateLessThan)
12-
? Number(dateLessThan)
13-
: new Date(dateLessThan).getTime()) / 1000
14-
);
10+
const epochDateLessThan = Math.round(new Date(dateLessThan).getTime() / 1000);
1511
const dateGreaterThan = "2019-12-01";
16-
const epochDateGreaterThan = Math.round(
17-
(typeof dateGreaterThan === "string" && /^\d+$/.test(dateGreaterThan)
18-
? Number(dateGreaterThan)
19-
: new Date(dateGreaterThan).getTime()) / 1000
20-
);
21-
12+
const epochDateGreaterThan = Math.round(new Date(dateGreaterThan).getTime() / 1000);
2213
const ipAddress = "10.0.0.0";
2314
const privateKey = Buffer.from(`
2415
-----BEGIN RSA PRIVATE KEY-----
@@ -613,3 +604,49 @@ describe("getSignedCookies", () => {
613604
expect(verifySignature(denormalizeBase64(result["CloudFront-Signature"]), policy)).toBeTruthy();
614605
});
615606
});
607+
608+
describe("getSignedUrl- when signing a URL with a date range", () => {
609+
const dateString = "2024-05-17";
610+
const dateNumber = 1125674245900;
611+
it("allows string input compatible with Date constructor", () => {
612+
const epochDateLessThan = Math.round(new Date(dateString).getTime() / 1000);
613+
const resultUrl = getSignedUrl({
614+
url,
615+
keyPairId,
616+
dateLessThan: dateString,
617+
privateKey,
618+
passphrase,
619+
});
620+
const resultCookies = getSignedCookies({
621+
url,
622+
keyPairId,
623+
dateLessThan: dateString,
624+
privateKey,
625+
passphrase,
626+
});
627+
628+
expect(resultUrl).toContain(`Expires=${epochDateLessThan}`);
629+
expect(resultCookies["CloudFront-Expires"]).toBe(epochDateLessThan);
630+
});
631+
632+
it("allows number input in milliseconds compatible with Date constructor", () => {
633+
const epochDateLessThan = Math.round(new Date(dateNumber).getTime() / 1000);
634+
const resultUrl = getSignedUrl({
635+
url,
636+
keyPairId,
637+
dateLessThan: dateNumber,
638+
privateKey,
639+
passphrase,
640+
});
641+
const resultCookies = getSignedCookies({
642+
url,
643+
keyPairId,
644+
dateLessThan: dateNumber,
645+
privateKey,
646+
passphrase,
647+
});
648+
649+
expect(resultUrl).toContain(`Expires=${epochDateLessThan}`);
650+
expect(resultCookies["CloudFront-Expires"]).toBe(epochDateLessThan);
651+
});
652+
});

packages/cloudfront-signer/src/sign.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,11 @@ class CloudfrontSignBuilder {
359359
return Math.round(date.getTime() / 1000);
360360
}
361361

362-
private parseDate(date?: string): number | undefined {
362+
private parseDate(date?: string | number | Date): number | undefined {
363363
if (!date) {
364364
return undefined;
365365
}
366-
let parsedDate: Date;
367-
if (/^\d+$/.test(date)) {
368-
parsedDate = new Date(Number(date));
369-
} else {
370-
parsedDate = new Date(date);
371-
}
366+
const parsedDate = new Date(date);
372367
return isNaN(parsedDate.getTime()) ? undefined : this.epochTime(parsedDate);
373368
}
374369

0 commit comments

Comments
 (0)