@aws-sdk/cloudfront-signer 3.901.0 → 3.914.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-cjs/index.js CHANGED
@@ -1,308 +1,263 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
20
2
 
21
- // src/index.ts
22
- var index_exports = {};
23
- __export(index_exports, {
24
- getSignedCookies: () => getSignedCookies,
25
- getSignedUrl: () => getSignedUrl
26
- });
27
- module.exports = __toCommonJS(index_exports);
3
+ var protocols = require('@smithy/core/protocols');
4
+ var crypto = require('crypto');
28
5
 
29
- // src/sign.ts
30
- var import_crypto = require("crypto");
31
- function getSignedUrl({
32
- dateLessThan,
33
- dateGreaterThan,
34
- url,
35
- keyPairId,
36
- privateKey,
37
- ipAddress,
38
- policy,
39
- passphrase
40
- }) {
41
- const cloudfrontSignBuilder = new CloudfrontSignBuilder({
42
- keyPairId,
43
- privateKey,
44
- passphrase
45
- });
46
- if (!url && !policy) {
47
- throw new Error("@aws-sdk/cloudfront-signer: Please provide 'url' or 'policy'.");
48
- }
49
- if (policy) {
50
- cloudfrontSignBuilder.setCustomPolicy(policy);
51
- } else {
52
- cloudfrontSignBuilder.setPolicyParameters({
53
- url,
54
- dateLessThan,
55
- dateGreaterThan,
56
- ipAddress
6
+ function getSignedUrl({ dateLessThan, dateGreaterThan, url, keyPairId, privateKey, ipAddress, policy, passphrase, }) {
7
+ const cloudfrontSignBuilder = new CloudfrontSignBuilder({
8
+ keyPairId,
9
+ privateKey,
10
+ passphrase,
57
11
  });
58
- }
59
- let baseUrl;
60
- if (url) {
61
- baseUrl = url;
62
- } else if (policy) {
63
- const resources = getPolicyResources(policy);
64
- if (!resources[0]) {
65
- throw new Error(
66
- "@aws-sdk/cloudfront-signer: No URL provided and unable to determine URL from first policy statement resource."
67
- );
12
+ if (!url && !policy) {
13
+ throw new Error("@aws-sdk/cloudfront-signer: Please provide 'url' or 'policy'.");
68
14
  }
69
- baseUrl = resources[0].replace("*://", "https://");
70
- }
71
- const startFlag = baseUrl.includes("?") ? "&" : "?";
72
- const params = Object.entries(cloudfrontSignBuilder.createCloudfrontAttribute()).filter(([, value]) => value !== void 0).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
73
- const urlString = baseUrl + startFlag + params;
74
- return getResource(urlString);
15
+ if (policy) {
16
+ cloudfrontSignBuilder.setCustomPolicy(policy);
17
+ }
18
+ else {
19
+ cloudfrontSignBuilder.setPolicyParameters({
20
+ url,
21
+ dateLessThan,
22
+ dateGreaterThan,
23
+ ipAddress,
24
+ });
25
+ }
26
+ let baseUrl;
27
+ if (url) {
28
+ baseUrl = url;
29
+ }
30
+ else if (policy) {
31
+ const resources = getPolicyResources(policy);
32
+ if (!resources[0]) {
33
+ throw new Error("@aws-sdk/cloudfront-signer: No URL provided and unable to determine URL from first policy statement resource.");
34
+ }
35
+ baseUrl = resources[0].replace("*://", "https://");
36
+ }
37
+ const startFlag = baseUrl.includes("?") ? "&" : "?";
38
+ const params = Object.entries(cloudfrontSignBuilder.createCloudfrontAttribute())
39
+ .filter(([, value]) => value !== undefined)
40
+ .map(([key, value]) => `${protocols.extendedEncodeURIComponent(key)}=${protocols.extendedEncodeURIComponent(value)}`)
41
+ .join("&");
42
+ function encodeBaseUrlQuery(url) {
43
+ if (url.includes("?")) {
44
+ const [hostAndPath, query] = url.split("?");
45
+ const params = [...new URLSearchParams(query).entries()]
46
+ .map(([key, value]) => `${protocols.extendedEncodeURIComponent(key)}=${protocols.extendedEncodeURIComponent(value)}`)
47
+ .join("&");
48
+ return `${hostAndPath}?${params}`;
49
+ }
50
+ return url;
51
+ }
52
+ const urlString = encodeBaseUrlQuery(baseUrl) + startFlag + params;
53
+ return getResource(urlString);
75
54
  }
76
- __name(getSignedUrl, "getSignedUrl");
77
- function getSignedCookies({
78
- ipAddress,
79
- url,
80
- privateKey,
81
- keyPairId,
82
- dateLessThan,
83
- dateGreaterThan,
84
- policy,
85
- passphrase
86
- }) {
87
- const cloudfrontSignBuilder = new CloudfrontSignBuilder({
88
- keyPairId,
89
- privateKey,
90
- passphrase
91
- });
92
- if (policy) {
93
- cloudfrontSignBuilder.setCustomPolicy(policy);
94
- } else {
95
- cloudfrontSignBuilder.setPolicyParameters({
96
- url,
97
- dateLessThan,
98
- dateGreaterThan,
99
- ipAddress
55
+ function getSignedCookies({ ipAddress, url, privateKey, keyPairId, dateLessThan, dateGreaterThan, policy, passphrase, }) {
56
+ const cloudfrontSignBuilder = new CloudfrontSignBuilder({
57
+ keyPairId,
58
+ privateKey,
59
+ passphrase,
100
60
  });
101
- }
102
- const cloudfrontCookieAttributes = cloudfrontSignBuilder.createCloudfrontAttribute();
103
- const cookies = {
104
- "CloudFront-Key-Pair-Id": cloudfrontCookieAttributes["Key-Pair-Id"],
105
- "CloudFront-Signature": cloudfrontCookieAttributes["Signature"]
106
- };
107
- if (cloudfrontCookieAttributes["Expires"]) {
108
- cookies["CloudFront-Expires"] = cloudfrontCookieAttributes["Expires"];
109
- }
110
- if (cloudfrontCookieAttributes["Policy"]) {
111
- cookies["CloudFront-Policy"] = cloudfrontCookieAttributes["Policy"];
112
- }
113
- return cookies;
61
+ if (policy) {
62
+ cloudfrontSignBuilder.setCustomPolicy(policy);
63
+ }
64
+ else {
65
+ cloudfrontSignBuilder.setPolicyParameters({
66
+ url,
67
+ dateLessThan,
68
+ dateGreaterThan,
69
+ ipAddress,
70
+ });
71
+ }
72
+ const cloudfrontCookieAttributes = cloudfrontSignBuilder.createCloudfrontAttribute();
73
+ const cookies = {
74
+ "CloudFront-Key-Pair-Id": cloudfrontCookieAttributes["Key-Pair-Id"],
75
+ "CloudFront-Signature": cloudfrontCookieAttributes["Signature"],
76
+ };
77
+ if (cloudfrontCookieAttributes["Expires"]) {
78
+ cookies["CloudFront-Expires"] = cloudfrontCookieAttributes["Expires"];
79
+ }
80
+ if (cloudfrontCookieAttributes["Policy"]) {
81
+ cookies["CloudFront-Policy"] = cloudfrontCookieAttributes["Policy"];
82
+ }
83
+ return cookies;
114
84
  }
115
- __name(getSignedCookies, "getSignedCookies");
116
85
  function getPolicyResources(policy) {
117
- const parsedPolicy = typeof policy === "string" ? JSON.parse(policy) : policy;
118
- return (parsedPolicy?.Statement ?? []).map((s) => s.Resource);
86
+ const parsedPolicy = typeof policy === "string" ? JSON.parse(policy) : policy;
87
+ return (parsedPolicy?.Statement ?? []).map((s) => s.Resource);
119
88
  }
120
- __name(getPolicyResources, "getPolicyResources");
121
89
  function getResource(urlString) {
122
- const protocol = urlString.slice(0, urlString.indexOf("//"));
123
- switch (protocol) {
124
- case "http:":
125
- case "https:":
126
- case "ws:":
127
- case "wss:":
128
- return urlString;
129
- case "rtmp:":
130
- const url = new URL(urlString);
131
- const origin = `${protocol}//${url.hostname}`;
132
- return urlString.substring(origin.length).replace(/(?::\d+)?\//, "");
133
- default:
134
- throw new Error("Invalid URI scheme. Scheme must be one of http, https, or rtmp");
135
- }
90
+ const protocol = urlString.slice(0, urlString.indexOf("//"));
91
+ switch (protocol) {
92
+ case "http:":
93
+ case "https:":
94
+ case "ws:":
95
+ case "wss:":
96
+ return urlString;
97
+ case "rtmp:":
98
+ const url = new URL(urlString);
99
+ const origin = `${protocol}//${url.hostname}`;
100
+ return urlString.substring(origin.length).replace(/(?::\d+)?\//, "");
101
+ default:
102
+ throw new Error("Invalid URI scheme. Scheme must be one of http, https, or rtmp");
103
+ }
136
104
  }
137
- __name(getResource, "getResource");
138
- var CloudfrontSignBuilder = class {
139
- static {
140
- __name(this, "CloudfrontSignBuilder");
141
- }
142
- keyPairId;
143
- privateKey;
144
- passphrase;
145
- policy;
146
- customPolicy = false;
147
- dateLessThan;
148
- constructor({ privateKey, keyPairId, passphrase }) {
149
- this.keyPairId = keyPairId;
150
- this.privateKey = privateKey;
151
- this.policy = "";
152
- this.passphrase = passphrase;
153
- }
154
- buildPolicy(args) {
155
- const policy = {
156
- Statement: [
157
- {
158
- Resource: args.resource,
159
- Condition: {
160
- DateLessThan: {
161
- "AWS:EpochTime": args.dateLessThan
162
- }
163
- }
105
+ class CloudfrontSignBuilder {
106
+ keyPairId;
107
+ privateKey;
108
+ passphrase;
109
+ policy;
110
+ customPolicy = false;
111
+ dateLessThan;
112
+ constructor({ privateKey, keyPairId, passphrase }) {
113
+ this.keyPairId = keyPairId;
114
+ this.privateKey = privateKey;
115
+ this.policy = "";
116
+ this.passphrase = passphrase;
117
+ }
118
+ buildPolicy(args) {
119
+ const policy = {
120
+ Statement: [
121
+ {
122
+ Resource: args.resource,
123
+ Condition: {
124
+ DateLessThan: {
125
+ "AWS:EpochTime": args.dateLessThan,
126
+ },
127
+ },
128
+ },
129
+ ],
130
+ };
131
+ if (args.dateGreaterThan) {
132
+ policy.Statement[0].Condition["DateGreaterThan"] = {
133
+ "AWS:EpochTime": args.dateGreaterThan,
134
+ };
164
135
  }
165
- ]
166
- };
167
- if (args.dateGreaterThan) {
168
- policy.Statement[0].Condition["DateGreaterThan"] = {
169
- "AWS:EpochTime": args.dateGreaterThan
170
- };
136
+ if (args.ipAddress) {
137
+ const cidr = this.parseCIDR(args.ipAddress);
138
+ policy.Statement[0].Condition["IpAddress"] = {
139
+ "AWS:SourceIp": cidr,
140
+ };
141
+ }
142
+ return policy;
171
143
  }
172
- if (args.ipAddress) {
173
- const cidr = this.parseCIDR(args.ipAddress);
174
- policy.Statement[0].Condition["IpAddress"] = {
175
- "AWS:SourceIp": cidr
176
- };
144
+ normalizeBase64(str) {
145
+ const replacements = {
146
+ "+": "-",
147
+ "=": "_",
148
+ "/": "~",
149
+ };
150
+ return str.replace(/[+=/]/g, function (match) {
151
+ return replacements[match];
152
+ });
177
153
  }
178
- return policy;
179
- }
180
- normalizeBase64(str) {
181
- const replacements = {
182
- "+": "-",
183
- "=": "_",
184
- "/": "~"
185
- };
186
- return str.replace(/[+=/]/g, function(match) {
187
- return replacements[match];
188
- });
189
- }
190
- encodeToBase64(str) {
191
- return this.normalizeBase64(Buffer.from(str).toString("base64"));
192
- }
193
- validateIP(ipStr) {
194
- const octets = ipStr.split(".");
195
- if (octets.length !== 4) {
196
- throw new Error(`IP does not contain four octets.`);
154
+ encodeToBase64(str) {
155
+ return this.normalizeBase64(Buffer.from(str).toString("base64"));
197
156
  }
198
- const isValid = octets.every((octet) => {
199
- const num = Number(octet);
200
- return Number.isInteger(num) && num >= 0 && num <= 255;
201
- });
202
- if (!isValid) {
203
- throw new Error("invalid IP octets");
157
+ validateIP(ipStr) {
158
+ const octets = ipStr.split(".");
159
+ if (octets.length !== 4) {
160
+ throw new Error(`IP does not contain four octets.`);
161
+ }
162
+ const isValid = octets.every((octet) => {
163
+ const num = Number(octet);
164
+ return Number.isInteger(num) && num >= 0 && num <= 255;
165
+ });
166
+ if (!isValid) {
167
+ throw new Error("invalid IP octets");
168
+ }
204
169
  }
205
- }
206
- validateMask(maskStr) {
207
- const mask = Number(maskStr);
208
- const isValid = Number.isInteger(mask) && mask >= 0 && mask <= 32;
209
- if (!isValid) {
210
- throw new Error("invalid mask");
170
+ validateMask(maskStr) {
171
+ const mask = Number(maskStr);
172
+ const isValid = Number.isInteger(mask) && mask >= 0 && mask <= 32;
173
+ if (!isValid) {
174
+ throw new Error("invalid mask");
175
+ }
211
176
  }
212
- }
213
- parseCIDR(cidrStr) {
214
- try {
215
- const cidrParts = cidrStr.split("/");
216
- if (cidrParts.some((part) => part.length === 0)) {
217
- throw new Error("missing ip or mask part of CIDR");
218
- }
219
- this.validateIP(cidrParts[0]);
220
- let mask = "32";
221
- if (cidrParts.length === 2) {
222
- this.validateMask(cidrParts[1]);
223
- mask = cidrParts[1];
224
- }
225
- return `${cidrParts[0]}/${mask}`;
226
- } catch (error) {
227
- const errMessage = `IP address "${cidrStr}" is invalid`;
228
- if (error instanceof Error) {
229
- throw new Error(`${errMessage} due to ${error.message}.`);
230
- } else {
231
- throw new Error(`${errMessage}.`);
232
- }
177
+ parseCIDR(cidrStr) {
178
+ try {
179
+ const cidrParts = cidrStr.split("/");
180
+ if (cidrParts.some((part) => part.length === 0)) {
181
+ throw new Error("missing ip or mask part of CIDR");
182
+ }
183
+ this.validateIP(cidrParts[0]);
184
+ let mask = "32";
185
+ if (cidrParts.length === 2) {
186
+ this.validateMask(cidrParts[1]);
187
+ mask = cidrParts[1];
188
+ }
189
+ return `${cidrParts[0]}/${mask}`;
190
+ }
191
+ catch (error) {
192
+ const errMessage = `IP address "${cidrStr}" is invalid`;
193
+ if (error instanceof Error) {
194
+ throw new Error(`${errMessage} due to ${error.message}.`);
195
+ }
196
+ else {
197
+ throw new Error(`${errMessage}.`);
198
+ }
199
+ }
233
200
  }
234
- }
235
- epochTime(date) {
236
- return Math.round(date.getTime() / 1e3);
237
- }
238
- parseDate(date) {
239
- if (!date) {
240
- return void 0;
201
+ epochTime(date) {
202
+ return Math.round(date.getTime() / 1000);
241
203
  }
242
- const parsedDate = new Date(date);
243
- return isNaN(parsedDate.getTime()) ? void 0 : this.epochTime(parsedDate);
244
- }
245
- parseDateWindow(expiration, start) {
246
- const dateLessThan = this.parseDate(expiration);
247
- if (!dateLessThan) {
248
- throw new Error("dateLessThan is invalid. Ensure the date value is compatible with the Date constructor.");
204
+ parseDate(date) {
205
+ if (!date) {
206
+ return undefined;
207
+ }
208
+ const parsedDate = new Date(date);
209
+ return isNaN(parsedDate.getTime()) ? undefined : this.epochTime(parsedDate);
249
210
  }
250
- return {
251
- dateLessThan,
252
- dateGreaterThan: this.parseDate(start)
253
- };
254
- }
255
- signData(data, privateKey, passphrase) {
256
- const sign = (0, import_crypto.createSign)("RSA-SHA1");
257
- sign.update(data);
258
- return sign.sign({ key: privateKey, passphrase }, "base64");
259
- }
260
- signPolicy(policy, privateKey, passphrase) {
261
- return this.normalizeBase64(this.signData(policy, privateKey, passphrase));
262
- }
263
- setCustomPolicy(policy) {
264
- this.customPolicy = true;
265
- this.policy = policy;
266
- }
267
- setPolicyParameters({
268
- url,
269
- dateLessThan,
270
- dateGreaterThan,
271
- ipAddress
272
- }) {
273
- if (!url || !dateLessThan) {
274
- return false;
211
+ parseDateWindow(expiration, start) {
212
+ const dateLessThan = this.parseDate(expiration);
213
+ if (!dateLessThan) {
214
+ throw new Error("dateLessThan is invalid. Ensure the date value is compatible with the Date constructor.");
215
+ }
216
+ return {
217
+ dateLessThan,
218
+ dateGreaterThan: this.parseDate(start),
219
+ };
275
220
  }
276
- const resource = getResource(url);
277
- const parsedDates = this.parseDateWindow(dateLessThan, dateGreaterThan);
278
- this.dateLessThan = parsedDates.dateLessThan;
279
- this.customPolicy = Boolean(parsedDates.dateGreaterThan) || Boolean(ipAddress);
280
- this.policy = JSON.stringify(
281
- this.buildPolicy({
282
- resource,
283
- ipAddress,
284
- dateLessThan: parsedDates.dateLessThan,
285
- dateGreaterThan: parsedDates.dateGreaterThan
286
- })
287
- );
288
- }
289
- createCloudfrontAttribute() {
290
- if (!Boolean(this.policy)) {
291
- throw new Error("Invalid policy");
221
+ signData(data, privateKey, passphrase) {
222
+ const sign = crypto.createSign("RSA-SHA1");
223
+ sign.update(data);
224
+ return sign.sign({ key: privateKey, passphrase }, "base64");
292
225
  }
293
- const signature = this.signPolicy(this.policy, this.privateKey, this.passphrase);
294
- return {
295
- Expires: this.customPolicy ? void 0 : this.dateLessThan,
296
- Policy: this.customPolicy ? this.encodeToBase64(this.policy) : void 0,
297
- "Key-Pair-Id": this.keyPairId,
298
- Signature: signature
299
- };
300
- }
301
- };
302
- // Annotate the CommonJS export names for ESM import in node:
303
-
304
- 0 && (module.exports = {
305
- getSignedUrl,
306
- getSignedCookies
307
- });
226
+ signPolicy(policy, privateKey, passphrase) {
227
+ return this.normalizeBase64(this.signData(policy, privateKey, passphrase));
228
+ }
229
+ setCustomPolicy(policy) {
230
+ this.customPolicy = true;
231
+ this.policy = policy;
232
+ }
233
+ setPolicyParameters({ url, dateLessThan, dateGreaterThan, ipAddress, }) {
234
+ if (!url || !dateLessThan) {
235
+ return false;
236
+ }
237
+ const resource = getResource(url);
238
+ const parsedDates = this.parseDateWindow(dateLessThan, dateGreaterThan);
239
+ this.dateLessThan = parsedDates.dateLessThan;
240
+ this.customPolicy = Boolean(parsedDates.dateGreaterThan) || Boolean(ipAddress);
241
+ this.policy = JSON.stringify(this.buildPolicy({
242
+ resource,
243
+ ipAddress,
244
+ dateLessThan: parsedDates.dateLessThan,
245
+ dateGreaterThan: parsedDates.dateGreaterThan,
246
+ }));
247
+ }
248
+ createCloudfrontAttribute() {
249
+ if (!Boolean(this.policy)) {
250
+ throw new Error("Invalid policy");
251
+ }
252
+ const signature = this.signPolicy(this.policy, this.privateKey, this.passphrase);
253
+ return {
254
+ Expires: this.customPolicy ? undefined : this.dateLessThan,
255
+ Policy: this.customPolicy ? this.encodeToBase64(this.policy) : undefined,
256
+ "Key-Pair-Id": this.keyPairId,
257
+ Signature: signature,
258
+ };
259
+ }
260
+ }
308
261
 
262
+ exports.getSignedCookies = getSignedCookies;
263
+ exports.getSignedUrl = getSignedUrl;
package/dist-es/sign.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { extendedEncodeURIComponent } from "@smithy/core/protocols";
1
2
  import { createSign } from "crypto";
2
3
  export function getSignedUrl({ dateLessThan, dateGreaterThan, url, keyPairId, privateKey, ipAddress, policy, passphrase, }) {
3
4
  const cloudfrontSignBuilder = new CloudfrontSignBuilder({
@@ -33,9 +34,19 @@ export function getSignedUrl({ dateLessThan, dateGreaterThan, url, keyPairId, pr
33
34
  const startFlag = baseUrl.includes("?") ? "&" : "?";
34
35
  const params = Object.entries(cloudfrontSignBuilder.createCloudfrontAttribute())
35
36
  .filter(([, value]) => value !== undefined)
36
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
37
+ .map(([key, value]) => `${extendedEncodeURIComponent(key)}=${extendedEncodeURIComponent(value)}`)
37
38
  .join("&");
38
- const urlString = baseUrl + startFlag + params;
39
+ function encodeBaseUrlQuery(url) {
40
+ if (url.includes("?")) {
41
+ const [hostAndPath, query] = url.split("?");
42
+ const params = [...new URLSearchParams(query).entries()]
43
+ .map(([key, value]) => `${extendedEncodeURIComponent(key)}=${extendedEncodeURIComponent(value)}`)
44
+ .join("&");
45
+ return `${hostAndPath}?${params}`;
46
+ }
47
+ return url;
48
+ }
49
+ const urlString = encodeBaseUrlQuery(baseUrl) + startFlag + params;
39
50
  return getResource(urlString);
40
51
  }
41
52
  export function getSignedCookies({ ipAddress, url, privateKey, keyPairId, dateLessThan, dateGreaterThan, policy, passphrase, }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/cloudfront-signer",
3
- "version": "3.901.0",
3
+ "version": "3.914.0",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
6
6
  "build:cjs": "node ../../scripts/compilation/inline cloudfront-signer",
@@ -23,7 +23,8 @@
23
23
  },
24
24
  "license": "Apache-2.0",
25
25
  "dependencies": {
26
- "@smithy/url-parser": "^4.2.0",
26
+ "@smithy/core": "^3.17.0",
27
+ "@smithy/url-parser": "^4.2.3",
27
28
  "tslib": "^2.6.2"
28
29
  },
29
30
  "files": [