-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathsign.ts
449 lines (414 loc) · 11.8 KB
/
sign.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { createSign } from "crypto";
/**
* Input type to getSignedUrl and getSignedCookies.
* @public
*/
export type CloudfrontSignInput = CloudfrontSignInputWithParameters | CloudfrontSignInputWithPolicy;
/**
* @public
*/
export type CloudfrontSignerCredentials = {
/** The ID of the Cloudfront key pair. */
keyPairId: string;
/** The content of the Cloudfront private key. */
privateKey: string | Buffer;
/** The passphrase of RSA-SHA1 key*/
passphrase?: string;
};
/**
* @public
*/
export type CloudfrontSignInputWithParameters = CloudfrontSignerCredentials & {
/** The URL string to sign. */
url: string;
/** The date string for when the signed URL or cookie can no longer be accessed */
dateLessThan: string | number | Date;
/** The date string for when the signed URL or cookie can start to be accessed. */
dateGreaterThan?: string | number | Date;
/** The IP address string to restrict signed URL access to. */
ipAddress?: string;
/**
* [policy] should not be provided when using separate
* dateLessThan, dateGreaterThan, or ipAddress inputs.
*/
policy?: never;
};
/**
* @public
*/
export type CloudfrontSignInputWithPolicy = CloudfrontSignerCredentials & {
/**
* The URL string to sign. Optional when policy is provided.
*
* This will be used as the initial url if calling getSignedUrl
* with a policy.
*
* This will be ignored if calling getSignedCookies with a policy.
*/
url?: string;
/** The JSON-encoded policy string */
policy: string;
/** When using a policy, a separate dateLessThan should not be provided. */
dateLessThan?: never;
/** When using a policy, a separate dateGreaterThan should not be provided. */
dateGreaterThan?: never;
/** When using a policy, a separate ipAddress should not be provided. */
ipAddress?: never;
};
/**
* @public
*/
export interface CloudfrontSignedCookiesOutput {
/** ID of the Cloudfront key pair. */
"CloudFront-Key-Pair-Id": string;
/** Hashed, signed, and base64-encoded version of the JSON policy. */
"CloudFront-Signature": string;
/** The unix date time for when the signed URL or cookie can no longer be accessed. */
"CloudFront-Expires"?: number;
/** Base64-encoded version of the JSON policy. */
"CloudFront-Policy"?: string;
}
/**
* Creates a signed URL string using a canned or custom policy.
* @public
* @returns the input URL with signature attached as query parameters.
*/
export function getSignedUrl({
dateLessThan,
dateGreaterThan,
url,
keyPairId,
privateKey,
ipAddress,
policy,
passphrase,
}: CloudfrontSignInput): string {
const cloudfrontSignBuilder = new CloudfrontSignBuilder({
keyPairId,
privateKey,
passphrase,
});
if (!url && !policy) {
throw new Error("@aws-sdk/cloudfront-signer: Please provide 'url' or 'policy'.");
}
if (policy) {
cloudfrontSignBuilder.setCustomPolicy(policy);
} else {
cloudfrontSignBuilder.setPolicyParameters({
url,
dateLessThan,
dateGreaterThan,
ipAddress,
});
}
let baseUrl: string | undefined;
if (url) {
baseUrl = url;
} else if (policy) {
const resources = getPolicyResources(policy!);
if (!resources[0]) {
throw new Error(
"@aws-sdk/cloudfront-signer: No URL provided and unable to determine URL from first policy statement resource."
);
}
baseUrl = resources[0].replace("*://", "https://");
}
const newURL = new URL(baseUrl!);
newURL.search = Array.from(newURL.searchParams.entries())
.concat(Object.entries(cloudfrontSignBuilder.createCloudfrontAttribute()))
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join("&");
return getResource(newURL);
}
/**
* Creates signed cookies using a canned or custom policy.
* @public
* @returns an object with keys/values that can be added to cookies.
*/
export function getSignedCookies({
ipAddress,
url,
privateKey,
keyPairId,
dateLessThan,
dateGreaterThan,
policy,
passphrase,
}: CloudfrontSignInput): CloudfrontSignedCookiesOutput {
const cloudfrontSignBuilder = new CloudfrontSignBuilder({
keyPairId,
privateKey,
passphrase,
});
if (policy) {
cloudfrontSignBuilder.setCustomPolicy(policy);
} else {
cloudfrontSignBuilder.setPolicyParameters({
url,
dateLessThan,
dateGreaterThan,
ipAddress,
});
}
const cloudfrontCookieAttributes = cloudfrontSignBuilder.createCloudfrontAttribute();
const cookies: CloudfrontSignedCookiesOutput = {
"CloudFront-Key-Pair-Id": cloudfrontCookieAttributes["Key-Pair-Id"],
"CloudFront-Signature": cloudfrontCookieAttributes["Signature"],
};
if (cloudfrontCookieAttributes["Expires"]) {
cookies["CloudFront-Expires"] = cloudfrontCookieAttributes["Expires"];
}
if (cloudfrontCookieAttributes["Policy"]) {
cookies["CloudFront-Policy"] = cloudfrontCookieAttributes["Policy"];
}
return cookies;
}
/**
* @internal
*/
interface Policy {
Statement: Array<{
Resource: string;
Condition: {
DateLessThan: {
"AWS:EpochTime": number;
};
IpAddress?: {
"AWS:SourceIp": string;
};
DateGreaterThan?: {
"AWS:EpochTime": number;
};
};
}>;
}
/**
* @internal
*/
interface PolicyDates {
dateLessThan: number;
dateGreaterThan?: number;
}
/**
* @internal
*/
interface BuildPolicyInput extends PolicyDates, Pick<CloudfrontSignInput, "ipAddress"> {
resource: string;
}
/**
* @internal
*/
interface CloudfrontAttributes {
Expires?: number;
Policy?: string;
"Key-Pair-Id": string;
Signature: string;
}
/**
* Utility to get the allowed resources of a policy.
* @internal
*
* @param policy - The JSON/JSON-encoded policy
*/
function getPolicyResources(policy: string | Policy) {
const parsedPolicy: Policy = typeof policy === "string" ? JSON.parse(policy) : policy;
return (parsedPolicy?.Statement ?? []).map((s) => s.Resource);
}
/**
* @internal
*/
function getResource(url: URL): string {
switch (url.protocol) {
case "http:":
case "https:":
case "ws:":
case "wss:":
return url.toString();
case "rtmp:":
return url.pathname.replace(/^\//, "") + url.search + url.hash;
default:
throw new Error("Invalid URI scheme. Scheme must be one of http, https, or rtmp");
}
}
/**
* @internal
*/
class CloudfrontSignBuilder {
private keyPairId: string;
private privateKey: string | Buffer;
private passphrase?: string;
private policy: string;
private customPolicy = false;
private dateLessThan?: number | undefined;
constructor({ privateKey, keyPairId, passphrase }: CloudfrontSignerCredentials) {
this.keyPairId = keyPairId;
this.privateKey = privateKey;
this.policy = "";
this.passphrase = passphrase;
}
private buildPolicy(args: BuildPolicyInput): Policy {
const policy: Policy = {
Statement: [
{
Resource: args.resource,
Condition: {
DateLessThan: {
"AWS:EpochTime": args.dateLessThan,
},
},
},
],
};
if (args.dateGreaterThan) {
policy.Statement[0].Condition["DateGreaterThan"] = {
"AWS:EpochTime": args.dateGreaterThan,
};
}
if (args.ipAddress) {
const cidr = this.parseCIDR(args.ipAddress);
policy.Statement[0].Condition["IpAddress"] = {
"AWS:SourceIp": cidr,
};
}
return policy;
}
private normalizeBase64(str: string): string {
const replacements = {
"+": "-",
"=": "_",
"/": "~",
} as Record<string, string>;
return str.replace(/[+=/]/g, function (match) {
return replacements[match];
});
}
private encodeToBase64(str: string): string {
return this.normalizeBase64(Buffer.from(str).toString("base64"));
}
private validateIP(ipStr: string): void {
const octets = ipStr.split(".");
if (octets.length !== 4) {
throw new Error(`IP does not contain four octets.`);
}
const isValid = octets.every((octet: string) => {
const num = Number(octet);
return Number.isInteger(num) && num >= 0 && num <= 255;
});
if (!isValid) {
throw new Error("invalid IP octets");
}
}
private validateMask(maskStr: string): void {
const mask = Number(maskStr);
const isValid = Number.isInteger(mask) && mask >= 0 && mask <= 32;
if (!isValid) {
throw new Error("invalid mask");
}
}
private parseCIDR(cidrStr: string): string {
try {
const cidrParts = cidrStr.split("/");
if (cidrParts.some((part: string) => part.length === 0)) {
throw new Error("missing ip or mask part of CIDR");
}
this.validateIP(cidrParts[0]);
let mask = "32";
if (cidrParts.length === 2) {
this.validateMask(cidrParts[1]);
mask = cidrParts[1];
}
return `${cidrParts[0]}/${mask}`;
} catch (error) {
const errMessage = `IP address "${cidrStr}" is invalid`;
if (error instanceof Error) {
throw new Error(`${errMessage} due to ${error.message}.`);
} else {
throw new Error(`${errMessage}.`);
}
}
}
private epochTime(date: Date): number {
return Math.round(date.getTime() / 1000);
}
private parseDate(date?: string | number | Date): number | undefined {
if (!date) {
return undefined;
}
const parsedDate = new Date(date);
return isNaN(parsedDate.getTime()) ? undefined : this.epochTime(parsedDate);
}
private parseDateWindow(expiration: string | number | Date, start?: string | number | Date): PolicyDates {
const dateLessThan = this.parseDate(expiration);
if (!dateLessThan) {
throw new Error("dateLessThan is invalid. Ensure the date value is compatible with the Date constructor.");
}
return {
dateLessThan,
dateGreaterThan: this.parseDate(start),
};
}
private signData(data: string, privateKey: string | Buffer, passphrase?: string): string {
const sign = createSign("RSA-SHA1");
sign.update(data);
return sign.sign({ key: privateKey, passphrase }, "base64");
}
private signPolicy(policy: string, privateKey: string | Buffer, passphrase?: string): string {
return this.normalizeBase64(this.signData(policy, privateKey, passphrase));
}
setCustomPolicy(policy: string) {
this.customPolicy = true;
this.policy = policy;
}
setPolicyParameters({
url,
dateLessThan,
dateGreaterThan,
ipAddress,
}: {
url?: string;
dateLessThan?: string | number | Date;
dateGreaterThan?: string | number | Date;
ipAddress?: string;
}) {
if (!url || !dateLessThan) {
return false;
}
const resource = getResource(new URL(url));
const parsedDates = this.parseDateWindow(dateLessThan, dateGreaterThan);
this.dateLessThan = parsedDates.dateLessThan;
this.customPolicy = Boolean(parsedDates.dateGreaterThan) || Boolean(ipAddress);
this.policy = JSON.stringify(
this.buildPolicy({
resource,
ipAddress,
dateLessThan: parsedDates.dateLessThan,
dateGreaterThan: parsedDates.dateGreaterThan,
})
);
}
createCloudfrontAttribute(): CloudfrontAttributes {
if (!Boolean(this.policy)) {
throw new Error("Invalid policy");
}
const signature = this.signPolicy(this.policy, this.privateKey, this.passphrase);
return {
Expires: this.customPolicy ? undefined : this.dateLessThan,
Policy: this.customPolicy ? this.encodeToBase64(this.policy) : undefined,
"Key-Pair-Id": this.keyPairId,
Signature: signature,
};
}
}
/**
* @deprecated use CloudfrontSignInput, CloudfrontSignInputWithParameters, or CloudfrontSignInputWithPolicy.
*/
export type CloudfrontSignInputBase = {
url: string;
keyPairId: string;
privateKey: string | Buffer;
passphrase?: string;
dateLessThan?: string;
ipAddress?: string;
dateGreaterThan?: string;
};