Skip to content

Commit fcfb59f

Browse files
authored
fix(cloudfront-signer): accept passphrase when signing (#4232)
1 parent 2fa1dd5 commit fcfb59f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fC3JsQKBgANzZbf9D0lgQE1wsb45fzrAPAqRQHeVY7V8sZPQoJFcZ2Ymp/3L/UHc
4141
NwfPmGXHQDQaK9I3XpHfbyOelD6ghHi/wZj0sKR3Uoo84n8sIpCdUvwitjlHlZBE
4242
aoCHJ9c5Pnu6FwMAjP8aaKLQDvoHZKVWL2Ml6A6V3Ed95Itp/g2J
4343
-----END RSA PRIVATE KEY-----`);
44+
const passphrase = "SAMPLE";
4445

4546
function createSignature(data: string): string {
4647
const signer = createSign("RSA-SHA1");
@@ -71,6 +72,7 @@ describe("getSignedUrl", () => {
7172
keyPairId,
7273
dateLessThan,
7374
privateKey,
75+
passphrase,
7476
})
7577
);
7678
if (!result.query) {
@@ -86,6 +88,7 @@ describe("getSignedUrl", () => {
8688
keyPairId,
8789
dateLessThan,
8890
privateKey,
91+
passphrase,
8992
})
9093
);
9194
if (!result.query) {
@@ -113,6 +116,7 @@ describe("getSignedUrl", () => {
113116
keyPairId,
114117
dateLessThan,
115118
privateKey,
119+
passphrase,
116120
});
117121
const policyStr = JSON.stringify({
118122
Statement: [
@@ -140,6 +144,7 @@ describe("getSignedUrl", () => {
140144
dateLessThan,
141145
dateGreaterThan,
142146
privateKey,
147+
passphrase,
143148
});
144149
const policyStr = JSON.stringify({
145150
Statement: [
@@ -170,6 +175,7 @@ describe("getSignedUrl", () => {
170175
dateLessThan,
171176
ipAddress,
172177
privateKey,
178+
passphrase,
173179
});
174180
const policyStr = JSON.stringify({
175181
Statement: [
@@ -201,6 +207,7 @@ describe("getSignedUrl", () => {
201207
dateGreaterThan,
202208
ipAddress,
203209
privateKey,
210+
passphrase,
204211
});
205212
const policyStr = JSON.stringify({
206213
Statement: [
@@ -233,6 +240,7 @@ describe("getSignedUrl", () => {
233240
keyPairId,
234241
dateLessThan,
235242
privateKey,
243+
passphrase,
236244
};
237245
expect(
238246
getSignedUrl({
@@ -253,6 +261,7 @@ describe("getSignedUrl", () => {
253261
keyPairId,
254262
dateLessThan,
255263
privateKey,
264+
passphrase,
256265
};
257266
expect(() =>
258267
getSignedUrl({
@@ -298,6 +307,7 @@ describe("getSignedUrl", () => {
298307
keyPairId,
299308
dateLessThan,
300309
privateKey,
310+
passphrase,
301311
});
302312
const policyStr = JSON.stringify({
303313
Statement: [
@@ -324,6 +334,7 @@ describe("getSignedUrl", () => {
324334
keyPairId,
325335
privateKey,
326336
policy,
337+
passphrase,
327338
});
328339
const signature = createSignature(policy);
329340
expect(result).toBe(`${url}?Policy=${encodeToBase64(policy)}&Key-Pair-Id=${keyPairId}&Signature=${signature}`);
@@ -339,6 +350,7 @@ describe("getSignedCookies", () => {
339350
keyPairId,
340351
dateLessThan,
341352
privateKey,
353+
passphrase,
342354
};
343355
expect(
344356
getSignedCookies({
@@ -359,6 +371,7 @@ describe("getSignedCookies", () => {
359371
keyPairId,
360372
dateLessThan,
361373
privateKey,
374+
passphrase,
362375
};
363376
expect(() =>
364377
getSignedCookies({
@@ -404,6 +417,7 @@ describe("getSignedCookies", () => {
404417
keyPairId,
405418
dateLessThan,
406419
privateKey,
420+
passphrase,
407421
});
408422
const policyStr = JSON.stringify({
409423
Statement: [
@@ -425,6 +439,7 @@ describe("getSignedCookies", () => {
425439
keyPairId,
426440
dateLessThan,
427441
privateKey,
442+
passphrase,
428443
});
429444
const policyStr = JSON.stringify({
430445
Statement: [
@@ -456,6 +471,7 @@ describe("getSignedCookies", () => {
456471
dateLessThan,
457472
dateGreaterThan,
458473
privateKey,
474+
passphrase,
459475
});
460476
const policyStr = JSON.stringify({
461477
Statement: [
@@ -490,6 +506,7 @@ describe("getSignedCookies", () => {
490506
dateLessThan,
491507
ipAddress,
492508
privateKey,
509+
passphrase,
493510
});
494511
const policyStr = JSON.stringify({
495512
Statement: [
@@ -525,6 +542,7 @@ describe("getSignedCookies", () => {
525542
dateGreaterThan,
526543
ipAddress,
527544
privateKey,
545+
passphrase,
528546
});
529547
const policyStr = JSON.stringify({
530548
Statement: [
@@ -562,6 +580,7 @@ describe("getSignedCookies", () => {
562580
keyPairId,
563581
privateKey,
564582
policy,
583+
passphrase,
565584
});
566585
const signature = createSignature(policy);
567586
const expected = {

packages/cloudfront-signer/src/sign.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export interface CloudfrontSignInputBase {
1111
keyPairId: string;
1212
/** The content of the Cloudfront private key. */
1313
privateKey: string | Buffer;
14+
/** The passphrase of RSA-SHA1 key*/
15+
passphrase?: string;
1416
/** The date string for when the signed URL or cookie can no longer be accessed. */
1517
dateLessThan?: string;
1618
/** The IP address string to restrict signed URL access to. */
@@ -66,6 +68,7 @@ export function getSignedUrl({
6668
privateKey,
6769
ipAddress,
6870
policy,
71+
passphrase,
6972
}: CloudfrontSignInput): string {
7073
const parsedUrl = parseUrl(url);
7174
const queryParams: string[] = [];
@@ -75,6 +78,7 @@ export function getSignedUrl({
7578
const cloudfrontSignBuilder = new CloudfrontSignBuilder({
7679
keyPairId,
7780
privateKey,
81+
passphrase,
7882
});
7983
if (policy) {
8084
cloudfrontSignBuilder.setCustomPolicy(policy);
@@ -115,10 +119,12 @@ export function getSignedCookies({
115119
dateLessThan,
116120
dateGreaterThan,
117121
policy,
122+
passphrase,
118123
}: CloudfrontSignInput): CloudfrontSignedCookiesOutput {
119124
const cloudfrontSignBuilder = new CloudfrontSignBuilder({
120125
keyPairId,
121126
privateKey,
127+
passphrase,
122128
});
123129
if (policy) {
124130
cloudfrontSignBuilder.setCustomPolicy(policy);
@@ -207,14 +213,24 @@ class CloudfrontURLParser {
207213
class CloudfrontSignBuilder {
208214
private keyPairId: string;
209215
private privateKey: string | Buffer;
216+
private passphrase?: string;
210217
private policy: string;
211218
private customPolicy = false;
212219
private dateLessThan?: number | undefined;
213220
private urlParser = new CloudfrontURLParser();
214-
constructor({ privateKey, keyPairId }: { keyPairId: string; privateKey: string | Buffer }) {
221+
constructor({
222+
privateKey,
223+
keyPairId,
224+
passphrase,
225+
}: {
226+
keyPairId: string;
227+
privateKey: string | Buffer;
228+
passphrase?: string;
229+
}) {
215230
this.keyPairId = keyPairId;
216231
this.privateKey = privateKey;
217232
this.policy = "";
233+
this.passphrase = passphrase;
218234
}
219235

220236
private buildPolicy(args: BuildPolicyInput): Policy {
@@ -327,14 +343,14 @@ class CloudfrontSignBuilder {
327343
};
328344
}
329345

330-
private signData(data: string, privateKey: string | Buffer): string {
346+
private signData(data: string, privateKey: string | Buffer, passphrase?: string): string {
331347
const sign = createSign("RSA-SHA1");
332348
sign.update(data);
333-
return sign.sign(privateKey, "base64");
349+
return sign.sign({ key: privateKey, passphrase }, "base64");
334350
}
335351

336-
private signPolicy(policy: string, privateKey: string | Buffer): string {
337-
return this.normalizeBase64(this.signData(policy, privateKey));
352+
private signPolicy(policy: string, privateKey: string | Buffer, passphrase?: string): string {
353+
return this.normalizeBase64(this.signData(policy, privateKey, passphrase));
338354
}
339355

340356
setCustomPolicy(policy: string) {
@@ -374,7 +390,7 @@ class CloudfrontSignBuilder {
374390
if (!Boolean(this.policy)) {
375391
throw new Error("Invalid policy");
376392
}
377-
const signature = this.signPolicy(this.policy, this.privateKey);
393+
const signature = this.signPolicy(this.policy, this.privateKey, this.passphrase);
378394
return {
379395
Expires: this.customPolicy ? undefined : this.dateLessThan,
380396
Policy: this.customPolicy ? this.encodeToBase64(this.policy) : undefined,

0 commit comments

Comments
 (0)