@blamejs/core 0.7.4 → 0.7.18

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.
Files changed (180) hide show
  1. package/CHANGELOG.md +423 -395
  2. package/README.md +150 -149
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +308 -284
  5. package/lib/api-key.js +660 -663
  6. package/lib/api-snapshot.js +338 -338
  7. package/lib/app-shutdown.js +385 -385
  8. package/lib/app.js +365 -365
  9. package/lib/archive.js +250 -250
  10. package/lib/atomic-file.js +544 -544
  11. package/lib/audit-chain.js +177 -177
  12. package/lib/audit-sign.js +344 -344
  13. package/lib/audit-tools.js +677 -677
  14. package/lib/audit.js +766 -766
  15. package/lib/auth/jwt.js +311 -311
  16. package/lib/auth/lockout.js +436 -436
  17. package/lib/auth/oauth.js +721 -721
  18. package/lib/auth/passkey.js +181 -181
  19. package/lib/auth/password.js +594 -594
  20. package/lib/backup/bundle.js +217 -217
  21. package/lib/backup/crypto.js +176 -176
  22. package/lib/backup/index.js +515 -515
  23. package/lib/backup/manifest.js +282 -282
  24. package/lib/break-glass.js +1338 -1338
  25. package/lib/bundler.js +441 -441
  26. package/lib/cache-redis.js +256 -256
  27. package/lib/cache.js +1206 -1206
  28. package/lib/canonical-json.js +115 -115
  29. package/lib/chain-writer.js +234 -234
  30. package/lib/cli-helpers.js +206 -206
  31. package/lib/cli.js +2334 -2334
  32. package/lib/cluster-provider-db.js +317 -317
  33. package/lib/cluster-storage.js +226 -226
  34. package/lib/cluster.js +703 -703
  35. package/lib/codepoint-class.js +196 -0
  36. package/lib/config-drift.js +301 -301
  37. package/lib/consent.js +222 -222
  38. package/lib/constants.js +191 -191
  39. package/lib/cookies.js +315 -315
  40. package/lib/credential-hash.js +322 -322
  41. package/lib/crypto.js +266 -266
  42. package/lib/csv.js +275 -286
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -421
  45. package/lib/db-query.js +406 -406
  46. package/lib/db-schema.js +319 -319
  47. package/lib/db.js +1288 -1288
  48. package/lib/deprecate.js +222 -222
  49. package/lib/dev.js +335 -335
  50. package/lib/dual-control.js +473 -473
  51. package/lib/error-page.js +420 -420
  52. package/lib/external-db-migrate.js +441 -441
  53. package/lib/external-db.js +1061 -1061
  54. package/lib/file-type.js +273 -273
  55. package/lib/file-upload.js +213 -10
  56. package/lib/forms.js +422 -422
  57. package/lib/framework-error.js +293 -215
  58. package/lib/framework-schema.js +717 -717
  59. package/lib/gate-contract.js +971 -0
  60. package/lib/guard-all.js +405 -0
  61. package/lib/guard-archive.js +739 -0
  62. package/lib/guard-csv.js +816 -0
  63. package/lib/guard-email.js +744 -0
  64. package/lib/guard-filename.js +724 -0
  65. package/lib/guard-html.js +976 -0
  66. package/lib/guard-json.js +729 -0
  67. package/lib/guard-markdown.js +586 -0
  68. package/lib/guard-svg.js +976 -0
  69. package/lib/guard-xml.js +405 -0
  70. package/lib/guard-yaml.js +529 -0
  71. package/lib/handlers.js +350 -350
  72. package/lib/http-client-cookie-jar.js +508 -508
  73. package/lib/http-client.js +1195 -1195
  74. package/lib/i18n.js +878 -878
  75. package/lib/jobs.js +185 -185
  76. package/lib/log-stream-cloudwatch.js +369 -369
  77. package/lib/log-stream-local.js +146 -146
  78. package/lib/log-stream-otlp-grpc.js +410 -410
  79. package/lib/log-stream-otlp.js +286 -286
  80. package/lib/log-stream-syslog.js +302 -302
  81. package/lib/log-stream-webhook.js +199 -199
  82. package/lib/log-stream.js +330 -330
  83. package/lib/log.js +500 -500
  84. package/lib/mail-bounce.js +528 -528
  85. package/lib/mail-dkim.js +369 -362
  86. package/lib/mail.js +981 -962
  87. package/lib/metrics.js +683 -683
  88. package/lib/middleware/api-encrypt.js +936 -936
  89. package/lib/middleware/attach-user.js +157 -157
  90. package/lib/middleware/body-parser.js +1170 -1091
  91. package/lib/middleware/bot-guard.js +178 -178
  92. package/lib/middleware/compression.js +452 -452
  93. package/lib/middleware/cors.js +314 -314
  94. package/lib/middleware/csp-nonce.js +348 -348
  95. package/lib/middleware/csrf-protect.js +316 -316
  96. package/lib/middleware/db-role-for.js +264 -264
  97. package/lib/middleware/health.js +392 -392
  98. package/lib/middleware/index.js +79 -79
  99. package/lib/middleware/rate-limit.js +358 -358
  100. package/lib/middleware/request-id.js +61 -61
  101. package/lib/middleware/request-log.js +168 -168
  102. package/lib/middleware/require-auth.js +104 -104
  103. package/lib/middleware/security-headers.js +116 -116
  104. package/lib/middleware/sse.js +166 -166
  105. package/lib/migrations.js +383 -383
  106. package/lib/mtls-ca.js +518 -518
  107. package/lib/mtls-engine-default.js +481 -481
  108. package/lib/network-dns.js +632 -632
  109. package/lib/network-heartbeat.js +290 -290
  110. package/lib/network-nts.js +574 -574
  111. package/lib/network-proxy.js +265 -265
  112. package/lib/network-tls.js +328 -328
  113. package/lib/network.js +233 -233
  114. package/lib/notify.js +612 -612
  115. package/lib/ntp-check.js +229 -229
  116. package/lib/numeric-bounds.js +111 -91
  117. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  118. package/lib/object-store/azure-blob.js +488 -488
  119. package/lib/object-store/gcs-bucket-ops.js +351 -351
  120. package/lib/object-store/gcs.js +519 -519
  121. package/lib/object-store/http-put.js +153 -153
  122. package/lib/object-store/index.js +197 -197
  123. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  124. package/lib/object-store/sigv4.js +903 -903
  125. package/lib/observability.js +151 -151
  126. package/lib/otel-export.js +269 -269
  127. package/lib/pagination.js +464 -464
  128. package/lib/parsers/index.js +80 -80
  129. package/lib/parsers/safe-env.js +642 -642
  130. package/lib/parsers/safe-ini.js +292 -292
  131. package/lib/parsers/safe-toml.js +784 -784
  132. package/lib/parsers/safe-xml.js +390 -390
  133. package/lib/parsers/safe-yaml.js +1015 -1015
  134. package/lib/permissions.js +708 -708
  135. package/lib/pqc-agent.js +87 -87
  136. package/lib/pqc-gate.js +279 -279
  137. package/lib/protobuf-encoder.js +190 -190
  138. package/lib/protocol-dispatcher.js +161 -161
  139. package/lib/pubsub-redis.js +167 -167
  140. package/lib/pubsub.js +429 -429
  141. package/lib/queue-local.js +476 -476
  142. package/lib/queue-redis.js +745 -745
  143. package/lib/queue-sqs.js +319 -319
  144. package/lib/queue.js +695 -695
  145. package/lib/redis-client.js +519 -519
  146. package/lib/request-helpers.js +340 -340
  147. package/lib/restore-bundle.js +237 -237
  148. package/lib/restore-rollback.js +259 -259
  149. package/lib/restore.js +409 -409
  150. package/lib/retry.js +376 -376
  151. package/lib/router.js +748 -748
  152. package/lib/safe-async.js +735 -735
  153. package/lib/safe-buffer.js +237 -237
  154. package/lib/safe-json.js +541 -541
  155. package/lib/safe-schema.js +1266 -1266
  156. package/lib/safe-url.js +159 -159
  157. package/lib/scheduler.js +706 -706
  158. package/lib/security-assert.js +373 -373
  159. package/lib/seeders.js +618 -618
  160. package/lib/session.js +478 -478
  161. package/lib/slug.js +269 -269
  162. package/lib/ssrf-guard.js +401 -401
  163. package/lib/static.js +184 -4
  164. package/lib/storage.js +471 -471
  165. package/lib/subject.js +281 -281
  166. package/lib/template.js +791 -791
  167. package/lib/testing.js +798 -798
  168. package/lib/time.js +310 -310
  169. package/lib/totp.js +302 -302
  170. package/lib/tracing.js +494 -494
  171. package/lib/uuid.js +132 -132
  172. package/lib/validate-opts.js +340 -319
  173. package/lib/vault/index.js +308 -308
  174. package/lib/vault/rotate.js +784 -784
  175. package/lib/vault/wrap.js +296 -296
  176. package/lib/vendor/noble-ciphers.cjs +9 -9
  177. package/lib/webhook.js +595 -595
  178. package/lib/websocket.js +1048 -1048
  179. package/package.json +77 -77
  180. package/sbom.cyclonedx.json +7 -7
@@ -1,903 +1,903 @@
1
- "use strict";
2
- /**
3
- * SigV4 protocol adapter — AWS Signature Version 4.
4
- *
5
- * One signing implementation covers the entire S3-API-compatible family:
6
- * AWS S3, Cloudflare R2, Backblaze B2 (S3 endpoint), MinIO, Wasabi,
7
- * Tigris, DigitalOcean Spaces, IDrive e2, Linode Object Storage, Storj
8
- * (S3 gateway). Service identifier is always "s3" for object storage.
9
- *
10
- * Config:
11
- * {
12
- * endpoint: 'https://s3.us-west-2.amazonaws.com' // or R2/MinIO/etc.
13
- * region: 'us-west-2' // required
14
- * bucket: 'my-bucket' // required
15
- * accessKeyId: '...' // required
16
- * secretAccessKey: '...' // required
17
- * sessionToken: '...' // optional (STS)
18
- * pathStyle: false // virtual-hosted
19
- * // by default
20
- * timeoutMs: C.TIME.seconds(30)
21
- * }
22
- *
23
- * Reference:
24
- * https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
25
- */
26
- var nodeCrypto = require("crypto");
27
- var { URL } = require("url");
28
- var { Readable } = require("stream");
29
- var safeXml = require("../parsers/safe-xml");
30
- var sharedRequest = require("./http-request");
31
- var C = require("../constants");
32
- var nb = require("../numeric-bounds");
33
- var requestHelpers = require("../request-helpers");
34
- var { ObjectStoreError } = require("../framework-error");
35
- var safeUrl = require("../safe-url");
36
-
37
- // Internal URL builder — endpoint + path string from validated config /
38
- // framework-encoded keys. Routes through safeUrl.parse so the protocol
39
- // allowlist + length cap apply uniformly. Presigned URLs with many
40
- // query params can approach the 8 KB default; raise the cap to 32 KB
41
- // so a worst-case multipart UploadPart URL still fits.
42
- function _internalUrl(input, allowedProtocols) {
43
- return safeUrl.parse(input, {
44
- allowedProtocols: allowedProtocols || safeUrl.ALLOW_HTTP_TLS,
45
- errorClass: ObjectStoreError,
46
- maxUrlLength: C.BYTES.kib(32),
47
- });
48
- }
49
-
50
- // S3 list responses are commonly 1000-key paginated payloads — well above
51
- // the parser's default 1 MiB / 10K element ceilings. These overrides
52
- // give headroom for normal responses without uncapping the parser.
53
- var LIST_PARSE_OPTS = {
54
- maxBytes: C.BYTES.mib(8),
55
- maxElements: C.BYTES.bytes(50000),
56
- };
57
-
58
- function _arrayify(value) {
59
- // xml-safe maps multiple same-tag children to an array; a single child
60
- // stays as the bare object; zero children means the property is absent.
61
- // List traversal needs a uniform array, so normalize.
62
- if (value == null) return [];
63
- return Array.isArray(value) ? value : [value];
64
- }
65
-
66
- var SERVICE = "s3";
67
- var ALGORITHM = "AWS4-HMAC-SHA256";
68
-
69
- // Presigned-URL expiry bounds. The SigV4 spec caps query-string-signed
70
- // URLs at 7 days; below 1 second is effectively unsignable.
71
- var PRESIGN_DEFAULT_EXPIRES_SECONDS = C.TIME.minutes(15) / C.TIME.seconds(1); // 15 minutes
72
- var PRESIGN_MAX_EXPIRES_SECONDS = C.TIME.days(7) / C.TIME.seconds(1); // 7 days (SigV4 hard cap)
73
- var PRESIGN_MIN_EXPIRES_SECONDS = 1;
74
-
75
- var _err = ObjectStoreError.factory;
76
-
77
- // ---- SigV4 primitives ----
78
-
79
- function sha256Hex(buf) {
80
- return nodeCrypto.createHash("sha256").update(buf).digest("hex");
81
- }
82
- function hmacSha256(key, data) {
83
- return nodeCrypto.createHmac("sha256", key).update(data).digest();
84
- }
85
-
86
- // AWS-style URI encoding: same as RFC 3986 except path '/' may be preserved.
87
- function awsUriEncode(str, encodeSlash) {
88
- var out = "";
89
- for (var i = 0; i < str.length; i++) {
90
- var c = str.charCodeAt(i);
91
- var ch = str.charAt(i);
92
- if ((c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A) ||
93
- (c >= 0x30 && c <= 0x39) ||
94
- ch === "-" || ch === "_" || ch === "." || ch === "~") {
95
- out += ch;
96
- } else if (ch === "/" && !encodeSlash) {
97
- out += "/";
98
- } else {
99
- var enc = encodeURIComponent(ch).replace(/!/g, "%21").replace(/\*/g, "%2A").replace(/'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
100
- out += enc;
101
- }
102
- }
103
- return out;
104
- }
105
-
106
- function canonicalQueryString(searchParams) {
107
- if (!searchParams || searchParams.toString() === "") return "";
108
- var pairs = [];
109
- searchParams.forEach(function (v, k) { pairs.push([k, v]); });
110
- pairs.sort(function (a, b) {
111
- if (a[0] < b[0]) return -1;
112
- if (a[0] > b[0]) return 1;
113
- if (a[1] < b[1]) return -1;
114
- if (a[1] > b[1]) return 1;
115
- return 0;
116
- });
117
- return pairs.map(function (p) {
118
- return awsUriEncode(p[0], true) + "=" + awsUriEncode(p[1], true);
119
- }).join("&");
120
- }
121
-
122
- function canonicalHeaders(headers) {
123
- var pairs = [];
124
- for (var k in headers) {
125
- if (headers[k] === undefined || headers[k] === null) continue;
126
- var lk = k.toLowerCase();
127
- var v = String(headers[k]).trim().replace(/\s+/g, " ");
128
- pairs.push([lk, v]);
129
- }
130
- pairs.sort(function (a, b) { return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; });
131
- var canon = "";
132
- var signed = [];
133
- for (var i = 0; i < pairs.length; i++) {
134
- canon += pairs[i][0] + ":" + pairs[i][1] + "\n";
135
- signed.push(pairs[i][0]);
136
- }
137
- return { canonical: canon, signed: signed.join(";") };
138
- }
139
-
140
- function canonicalRequest(method, urlObj, headers, payloadHash) {
141
- var canonHeaders = canonicalHeaders(headers);
142
- var path = urlObj.pathname;
143
- if (!path) path = "/";
144
- return [
145
- method.toUpperCase(),
146
- awsUriEncode(path, false),
147
- canonicalQueryString(urlObj.searchParams),
148
- canonHeaders.canonical,
149
- canonHeaders.signed,
150
- payloadHash,
151
- ].join("\n");
152
- }
153
-
154
- function stringToSign(amzDate, credentialScope, canonicalReq) {
155
- return [
156
- ALGORITHM,
157
- amzDate,
158
- credentialScope,
159
- sha256Hex(canonicalReq),
160
- ].join("\n");
161
- }
162
-
163
- function deriveSigningKey(secretAccessKey, dateStamp, region, service) {
164
- var kDate = hmacSha256("AWS4" + secretAccessKey, dateStamp);
165
- var kRegion = hmacSha256(kDate, region);
166
- var kService = hmacSha256(kRegion, service);
167
- return hmacSha256(kService, "aws4_request");
168
- }
169
-
170
- // SigV4 ISO compact format: YYYYMMDDTHHMMSSZ — slice indices fixed
171
- // by AWS spec.
172
- function _formatAmzDate(d) {
173
- var iso = d.toISOString().replace(/[-:]/g, "");
174
- return iso.slice(0, C.BYTES.bytes(8)) + "T" + iso.slice(9, 15) + "Z";
175
- }
176
- function _formatDateStamp(d) {
177
- return d.toISOString().slice(0, 10).replace(/-/g, "");
178
- }
179
-
180
- function signRequest(opts) {
181
- var date = opts.date || new Date();
182
- var amzDate = _formatAmzDate(date);
183
- var dateStamp = _formatDateStamp(date);
184
- // signRequest is called by callers signing requests to operator-
185
- // configured endpoints (cloudwatch, sqs, sns, custom S3-API stores).
186
- // Honor opts.allowedProtocols so a test-fixture http:// endpoint still
187
- // signs correctly while production https:// stays the default.
188
- var url = opts.url instanceof URL ? opts.url
189
- : _internalUrl(opts.url, opts.allowedProtocols);
190
- // service defaults to s3 for back-compat — every call site predating
191
- // v0.6.25 was object-store / S3. Other AWS services (logs, sqs, sns,
192
- // kinesis, etc.) pass opts.service explicitly. The credentialScope
193
- // and signing-key derivation both incorporate the service name, so
194
- // this MUST match what the target service expects.
195
- var service = opts.service || SERVICE;
196
-
197
- var headers = Object.assign({}, opts.headers || {});
198
- headers["host"] = url.host;
199
- headers["x-amz-date"] = amzDate;
200
- if (!headers["x-amz-content-sha256"]) {
201
- headers["x-amz-content-sha256"] = opts.payloadHash;
202
- }
203
- if (opts.sessionToken) {
204
- headers["x-amz-security-token"] = opts.sessionToken;
205
- }
206
-
207
- var canon = canonicalRequest(opts.method, url, headers, opts.payloadHash);
208
- var credentialScope = dateStamp + "/" + opts.region + "/" + service + "/aws4_request";
209
- var sts = stringToSign(amzDate, credentialScope, canon);
210
- var signingKey = deriveSigningKey(opts.secretAccessKey, dateStamp, opts.region, service);
211
- var signature = nodeCrypto.createHmac("sha256", signingKey).update(sts).digest("hex");
212
-
213
- var canonHeaders = canonicalHeaders(headers);
214
- var auth = ALGORITHM +
215
- " Credential=" + opts.accessKeyId + "/" + credentialScope +
216
- ", SignedHeaders=" + canonHeaders.signed +
217
- ", Signature=" + signature;
218
- headers["Authorization"] = auth;
219
-
220
- return { headers: headers, signature: signature, canonicalRequest: canon, stringToSign: sts };
221
- }
222
-
223
- // ---- HTTP request helper ----
224
-
225
- var _request = sharedRequest;
226
-
227
- // ---- Multipart-upload constants ----
228
-
229
- // S3 spec floor for non-final part size. Below this the API rejects
230
- // CompleteMultipartUpload with EntityTooSmall. The framework refuses
231
- // configurations below this floor at create() time so operators don't
232
- // see surprising failures only on large uploads.
233
- var MIN_PART_SIZE_BYTES = C.BYTES.mib(5);
234
- // S3 spec ceiling on part count. CompleteMultipartUpload rejects
235
- // uploads with more than 10000 parts.
236
- var MAX_PARTS = C.BYTES.bytes(10000);
237
- // Auto-multipart trigger: buffered bodies under this stay single-PUT.
238
- // Streams always go multipart since size isn't known up-front.
239
- var DEFAULT_MULTIPART_THRESHOLD_BYTES = C.BYTES.mib(64);
240
- // Conservative default part size — large enough to keep round-trip
241
- // overhead small relative to payload, small enough to fit comfortably
242
- // in a 4-way-concurrent upload's memory footprint.
243
- var DEFAULT_PART_SIZE_BYTES = C.BYTES.mib(16);
244
- var DEFAULT_PART_CONCURRENCY = 4;
245
-
246
- // ---- SSE option handling ----
247
-
248
- function _resolveSseHeaders(sse) {
249
- if (sse === undefined || sse === null) return null;
250
- var type;
251
- var keyId = null;
252
- if (typeof sse === "string") {
253
- type = sse;
254
- } else if (sse && typeof sse === "object") {
255
- type = sse.type;
256
- keyId = sse.keyId || null;
257
- } else {
258
- throw _err("INVALID_SSE",
259
- "opts.sse must be a string ('AES256' | 'aws:kms') or " +
260
- "{ type, keyId }, got " + typeof sse, true);
261
- }
262
- if (type !== "AES256" && type !== "aws:kms") {
263
- throw _err("INVALID_SSE",
264
- "opts.sse type must be 'AES256' or 'aws:kms', got '" + type + "'", true);
265
- }
266
- var h = { "x-amz-server-side-encryption": type };
267
- if (type === "aws:kms" && keyId) {
268
- h["x-amz-server-side-encryption-aws-kms-key-id"] = String(keyId);
269
- }
270
- return { type: type, keyId: keyId, headers: h };
271
- }
272
-
273
- function _verifySseResponse(sseRequested, resHeaders) {
274
- // Operators who specified an SSE policy expect the bucket / object to
275
- // honor it. If the server silently dropped the header (mis-configured
276
- // bucket policy, unsupported endpoint, etc.) the request looks like a
277
- // success but the at-rest data is unencrypted. Surface this as a
278
- // hard failure rather than a silent compliance hole.
279
- if (!sseRequested) return;
280
- var got = resHeaders["x-amz-server-side-encryption"];
281
- if (!got) {
282
- throw _err("SSE_NOT_APPLIED",
283
- "opts.sse was '" + sseRequested.type + "' but server did not " +
284
- "apply server-side encryption (no x-amz-server-side-encryption " +
285
- "response header)", true);
286
- }
287
- if (got !== sseRequested.type) {
288
- throw _err("SSE_MISMATCH",
289
- "opts.sse requested '" + sseRequested.type + "' but server " +
290
- "applied '" + got + "'", true);
291
- }
292
- }
293
-
294
- // ---- Multipart helpers ----
295
-
296
- // Build the CompleteMultipartUpload request body. Parts must be in
297
- // ascending PartNumber order. ETags from S3 include surrounding
298
- // quotes — preserve them exactly.
299
- function _buildCompleteMultipartXml(parts) {
300
- var body = "<CompleteMultipartUpload>";
301
- for (var i = 0; i < parts.length; i++) {
302
- body += "<Part>";
303
- body += "<PartNumber>" + parts[i].partNumber + "</PartNumber>";
304
- body += "<ETag>" + parts[i].etag + "</ETag>";
305
- body += "</Part>";
306
- }
307
- body += "</CompleteMultipartUpload>";
308
- return body;
309
- }
310
-
311
- // Read a Readable stream into fixed-size buffers. Yields one Buffer
312
- // per part (size <= partSize). The final part may be smaller. The
313
- // stream is consumed exactly once.
314
- async function _readStreamParts(readable, partSize) {
315
- var parts = [];
316
- var pending = [];
317
- var pendingBytes = 0;
318
- for await (var chunk of readable) {
319
- var buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
320
- pending.push(buf);
321
- pendingBytes += buf.length;
322
- while (pendingBytes >= partSize) {
323
- var combined = Buffer.concat(pending, pendingBytes);
324
- parts.push(combined.slice(0, partSize));
325
- var leftover = combined.slice(partSize);
326
- pending = leftover.length > 0 ? [leftover] : [];
327
- pendingBytes = leftover.length;
328
- }
329
- }
330
- if (pendingBytes > 0) {
331
- parts.push(Buffer.concat(pending, pendingBytes));
332
- }
333
- return parts;
334
- }
335
-
336
- // Run an array of async tasks with bounded parallelism. Preserves
337
- // result order by index.
338
- async function _bounded(items, concurrency, runner) {
339
- var results = new Array(items.length);
340
- var i = 0;
341
- async function worker() {
342
- while (true) {
343
- var idx = i++;
344
- if (idx >= items.length) return;
345
- results[idx] = await runner(items[idx], idx);
346
- }
347
- }
348
- var workers = [];
349
- for (var w = 0; w < Math.min(concurrency, items.length); w++) {
350
- workers.push(worker());
351
- }
352
- await Promise.all(workers);
353
- return results;
354
- }
355
-
356
- // ---- Public adapter factory ----
357
-
358
- function create(config) {
359
- if (!config) throw new Error("sigv4 protocol requires config");
360
- if (!config.region) throw new Error("sigv4: region is required");
361
- if (!config.bucket) throw new Error("sigv4: bucket is required");
362
- if (!config.accessKeyId) throw new Error("sigv4: accessKeyId is required");
363
- if (!config.secretAccessKey) throw new Error("sigv4: secretAccessKey is required");
364
-
365
- var endpoint = config.endpoint || ("https://s3." + config.region + ".amazonaws.com");
366
- if (endpoint.endsWith("/")) endpoint = endpoint.slice(0, -1);
367
- var pathStyle = !!(config.pathStyle || config.forcePathStyle);
368
-
369
- var partSize = config.partSizeBytes != null
370
- ? config.partSizeBytes
371
- : DEFAULT_PART_SIZE_BYTES;
372
- if (typeof partSize !== "number" || !isFinite(partSize) || partSize < MIN_PART_SIZE_BYTES) {
373
- throw _err("INVALID_CONFIG",
374
- "sigv4: partSizeBytes must be a number >= " + MIN_PART_SIZE_BYTES +
375
- " (S3 minimum part size), got " + partSize, true);
376
- }
377
- var multipartThreshold = config.multipartThresholdBytes != null
378
- ? config.multipartThresholdBytes
379
- : DEFAULT_MULTIPART_THRESHOLD_BYTES;
380
- if (typeof multipartThreshold !== "number" || !isFinite(multipartThreshold) || multipartThreshold < 0) {
381
- throw _err("INVALID_CONFIG",
382
- "sigv4: multipartThresholdBytes must be a non-negative finite number, got " +
383
- multipartThreshold, true);
384
- }
385
- var partConcurrency = config.partConcurrency != null
386
- ? config.partConcurrency
387
- : DEFAULT_PART_CONCURRENCY;
388
- if (typeof partConcurrency !== "number" || partConcurrency < 1 || !isFinite(partConcurrency)) {
389
- throw _err("INVALID_CONFIG",
390
- "sigv4: partConcurrency must be a positive finite number, got " + partConcurrency, true);
391
- }
392
- // HTTPS-only by default — AWS S3, R2, MinIO-over-https. Operators with
393
- // an internal cleartext S3-compatible endpoint (test fixtures, local
394
- // dev MinIO) opt in via config.allowedProtocols.
395
- var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
396
- var allowInternal = config.allowInternal != null ? config.allowInternal : null;
397
- safeUrl.parse(endpoint, {
398
- allowedProtocols: allowedProtocols,
399
- errorClass: ObjectStoreError,
400
- });
401
- var reqOpts = { timeoutMs: config.timeoutMs, allowedProtocols: allowedProtocols };
402
- if (allowInternal !== null) reqOpts.allowInternal = allowInternal;
403
-
404
- function _keyToUrl(key) {
405
- if (key.indexOf("\0") !== -1) throw _err("INVALID_KEY", "null byte in key", true);
406
- var encoded = key.split("/").map(function (s) { return awsUriEncode(s, true); }).join("/");
407
- if (pathStyle) {
408
- return _internalUrl(endpoint + "/" + config.bucket + "/" + encoded, allowedProtocols);
409
- }
410
- var u = _internalUrl(endpoint, allowedProtocols);
411
- u.hostname = config.bucket + "." + u.hostname;
412
- u.pathname = "/" + encoded;
413
- return u;
414
- }
415
-
416
- function _bucketUrl(searchParams) {
417
- var u;
418
- if (pathStyle) {
419
- u = _internalUrl(endpoint + "/" + config.bucket + "/", allowedProtocols);
420
- } else {
421
- u = _internalUrl(endpoint, allowedProtocols);
422
- u.hostname = config.bucket + "." + u.hostname;
423
- u.pathname = "/";
424
- }
425
- if (searchParams) {
426
- Object.keys(searchParams).forEach(function (k) { u.searchParams.set(k, searchParams[k]); });
427
- }
428
- return u;
429
- }
430
-
431
- function _makeSigned(method, url, payloadHash, extraHeaders) {
432
- var signed = signRequest({
433
- method: method,
434
- url: url,
435
- headers: extraHeaders || {},
436
- payloadHash: payloadHash,
437
- region: config.region,
438
- accessKeyId: config.accessKeyId,
439
- secretAccessKey: config.secretAccessKey,
440
- sessionToken: config.sessionToken,
441
- });
442
- return signed.headers;
443
- }
444
-
445
- function put(key, body, opts) {
446
- opts = opts || {};
447
- var sseRequested = _resolveSseHeaders(opts.sse);
448
- // Streams always go multipart — size isn't known up-front. Buffers
449
- // dispatch to multipart when they exceed the threshold; operators
450
- // can force the single-PUT path with `multipart: false` (small
451
- // bodies in unit tests, or to keep the request count to one).
452
- var isStream = body && typeof body === "object" && typeof body.pipe === "function";
453
- if (isStream) {
454
- if (opts.multipart === false) {
455
- return Promise.reject(_err("STREAM_REQUIRES_MULTIPART",
456
- "put(stream) requires multipart upload (set opts.multipart !== false)", true));
457
- }
458
- return _multipartPut(key, body, opts, sseRequested);
459
- }
460
- var buf = Buffer.isBuffer(body) ? body : Buffer.from(typeof body === "string" ? body : "", "utf8");
461
- if (opts.multipart !== false &&
462
- (opts.multipart === true || buf.length > multipartThreshold)) {
463
- return _multipartPut(key, buf, opts, sseRequested);
464
- }
465
- return _singlePut(key, buf, opts, sseRequested);
466
- }
467
-
468
- function _singlePut(key, buf, opts, sseRequested) {
469
- var url = _keyToUrl(key);
470
- var payloadHash = sha256Hex(buf);
471
- var contentType = opts.contentType || "application/octet-stream";
472
- var extra = {
473
- "Content-Type": contentType,
474
- "Content-Length": String(buf.length),
475
- };
476
- if (sseRequested) Object.assign(extra, sseRequested.headers);
477
- var headers = _makeSigned("PUT", url, payloadHash, extra);
478
- return _request("PUT", url, headers, buf, reqOpts).then(function (res) {
479
- _verifySseResponse(sseRequested, res.headers);
480
- return { size: buf.length, etag: res.headers.etag };
481
- });
482
- }
483
-
484
- async function _multipartPut(key, body, opts, sseRequested) {
485
- var contentType = opts.contentType || "application/octet-stream";
486
-
487
- // Slice into parts. For Buffers we slice up-front; for streams we
488
- // read sequentially into part-sized buffers (memory bounded).
489
- var parts;
490
- if (Buffer.isBuffer(body)) {
491
- parts = [];
492
- for (var off = 0; off < body.length; off += partSize) {
493
- parts.push(body.slice(off, Math.min(off + partSize, body.length)));
494
- }
495
- // Edge case: empty buffer → one zero-length part. S3 rejects
496
- // multipart with zero parts; rather than handling this corner
497
- // we route empty buffers through single-PUT instead.
498
- if (parts.length === 0) parts = [Buffer.alloc(0)];
499
- } else {
500
- parts = await _readStreamParts(body, partSize);
501
- if (parts.length === 0) parts = [Buffer.alloc(0)];
502
- }
503
- if (parts.length > MAX_PARTS) {
504
- throw _err("TOO_MANY_PARTS",
505
- "multipart upload would require " + parts.length + " parts " +
506
- "(S3 max " + MAX_PARTS + "); increase partSizeBytes", true);
507
- }
508
-
509
- // 1. Initiate — `?uploads` is the InitiateMultipartUpload subresource.
510
- // Build the URL with the bare token (no trailing `=`) for consistency
511
- // with sigv4-bucket-ops.js — strict S3 implementations route on the
512
- // bare form; the `URLSearchParams.set("uploads", "")` idiom produces
513
- // `?uploads=` which is accepted by AWS + MinIO for this specific
514
- // subresource, but the framework's convention is the bare form
515
- // everywhere. SigV4 canonicalization reads `url.searchParams` (which
516
- // still presents `uploads=` per AWS spec) so the signature path is
517
- // unchanged.
518
- var url = _keyToUrl(key);
519
- var initiateUrl = _internalUrl(url.href + (url.search ? "&" : "?") + "uploads", allowedProtocols);
520
- var initiateExtra = {
521
- "Content-Type": contentType,
522
- "Content-Length": "0",
523
- };
524
- if (sseRequested) Object.assign(initiateExtra, sseRequested.headers);
525
- var initiateHeaders = _makeSigned("POST", initiateUrl, sha256Hex(Buffer.alloc(0)), initiateExtra);
526
- var initRes = await _request("POST", initiateUrl, initiateHeaders, Buffer.alloc(0), reqOpts);
527
- _verifySseResponse(sseRequested, initRes.headers);
528
- var initDoc = safeXml.parse(initRes.body, LIST_PARSE_OPTS);
529
- var uploadId = initDoc.InitiateMultipartUploadResult &&
530
- initDoc.InitiateMultipartUploadResult.UploadId;
531
- if (!uploadId) {
532
- throw _err("MULTIPART_INIT_FAILED",
533
- "S3 InitiateMultipartUpload response missing UploadId", false);
534
- }
535
-
536
- var totalSize = 0;
537
- var uploadedEtags;
538
-
539
- try {
540
- // 2. Upload parts (concurrency-bounded)
541
- uploadedEtags = await _bounded(parts, partConcurrency, async function (partBuf, idx) {
542
- var partNumber = idx + 1;
543
- var partUrl = _internalUrl(url.href, allowedProtocols);
544
- partUrl.searchParams.set("partNumber", String(partNumber));
545
- partUrl.searchParams.set("uploadId", uploadId);
546
- var partHeaders = _makeSigned("PUT", partUrl, sha256Hex(partBuf), {
547
- "Content-Length": String(partBuf.length),
548
- });
549
- var partRes = await _request("PUT", partUrl, partHeaders, partBuf, reqOpts);
550
- if (!partRes.headers.etag) {
551
- throw _err("MULTIPART_PART_FAILED",
552
- "UploadPart response missing ETag for part " + partNumber, false);
553
- }
554
- totalSize += partBuf.length;
555
- return { partNumber: partNumber, etag: partRes.headers.etag };
556
- });
557
-
558
- // 3. Complete
559
- var completeUrl = _internalUrl(url.href, allowedProtocols);
560
- completeUrl.searchParams.set("uploadId", uploadId);
561
- var completeBody = Buffer.from(_buildCompleteMultipartXml(uploadedEtags), "utf8");
562
- var completeHeaders = _makeSigned("POST", completeUrl, sha256Hex(completeBody), {
563
- "Content-Type": "application/xml",
564
- "Content-Length": String(completeBody.length),
565
- });
566
- var completeRes = await _request("POST", completeUrl, completeHeaders, completeBody, reqOpts);
567
- // S3 may return 200 OK with an error body on CompleteMultipartUpload —
568
- // surface that as a hard error rather than a silent success.
569
- var completeDoc = safeXml.parse(completeRes.body, LIST_PARSE_OPTS);
570
- if (completeDoc.Error) {
571
- throw _err("MULTIPART_COMPLETE_FAILED",
572
- "CompleteMultipartUpload returned error: " +
573
- (completeDoc.Error.Code || "unknown") + " " +
574
- (completeDoc.Error.Message || ""), false);
575
- }
576
- // SSE was already verified on the InitiateMultipartUpload
577
- // response — that's the request that establishes the upload's
578
- // encryption policy server-side. The CompleteMultipartUpload
579
- // response may or may not echo the header depending on vendor;
580
- // re-verifying here would double-fault on otherwise-fine setups.
581
- var result = completeDoc.CompleteMultipartUploadResult || {};
582
- return { size: totalSize, etag: result.ETag || completeRes.headers.etag, multipart: true };
583
- } catch (e) {
584
- // Abort cleans up server-side storage for the partial upload.
585
- // Failures here are silently swallowed — the caller's original
586
- // error is what they need to see, not a secondary cleanup error.
587
- try {
588
- var abortUrl = _internalUrl(url.href, allowedProtocols);
589
- abortUrl.searchParams.set("uploadId", uploadId);
590
- var abortHeaders = _makeSigned("DELETE", abortUrl, sha256Hex(Buffer.alloc(0)));
591
- await _request("DELETE", abortUrl, abortHeaders, null, reqOpts);
592
- } catch (_e) { /* primary error wins */ }
593
- throw e;
594
- }
595
- }
596
-
597
- // get(key, opts?) — opts forwarded to the request as conditional /
598
- // range headers so operator HTTP routes can pass If-None-Match,
599
- // If-Match, If-Modified-Since, If-Unmodified-Since, and Range from the
600
- // client request straight through to S3. Returns just the body buffer
601
- // for backwards compatibility; operators wanting status + response
602
- // headers (304 vs 206 vs 200) call getResponse() instead.
603
- function get(key, opts) {
604
- return getResponse(key, opts).then(function (r) { return r.body; });
605
- }
606
-
607
- function getStream(key, opts) {
608
- return Readable.from(get(key, opts));
609
- }
610
-
611
- // getResponse(key, opts?) — full-fidelity GET. Returns
612
- // { body, statusCode, etag, lastModified, contentRange, size,
613
- // contentType }. Throws on non-2xx EXCEPT 304 (returned as
614
- // { statusCode: 304, etag, lastModified, body: null }) so operator
615
- // routes can short-circuit conditional GETs without losing the
616
- // response headers.
617
- function getResponse(key, opts) {
618
- opts = opts || {};
619
- var url = _keyToUrl(key);
620
- var headers = _makeSigned("GET", url, sha256Hex(Buffer.alloc(0)));
621
- if (opts.range) {
622
- headers["Range"] = "bytes=" + opts.range.start + "-" + opts.range.end;
623
- }
624
- if (opts.ifNoneMatch) headers["If-None-Match"] = opts.ifNoneMatch;
625
- if (opts.ifMatch) headers["If-Match"] = opts.ifMatch;
626
- if (opts.ifModifiedSince) headers["If-Modified-Since"] = opts.ifModifiedSince;
627
- if (opts.ifUnmodifiedSince) headers["If-Unmodified-Since"] = opts.ifUnmodifiedSince;
628
- var localReqOpts = Object.assign({}, reqOpts, { _resolveOnRedirect: false });
629
- return _request("GET", url, headers, null, localReqOpts).then(function (res) {
630
- return {
631
- statusCode: res.statusCode,
632
- body: res.body,
633
- etag: res.headers && res.headers.etag,
634
- lastModified: res.headers && res.headers["last-modified"]
635
- ? Date.parse(res.headers["last-modified"]) : null,
636
- contentRange: res.headers && res.headers["content-range"] || null,
637
- size: res.headers && res.headers["content-length"]
638
- ? parseInt(res.headers["content-length"], 10) : null,
639
- contentType: res.headers && res.headers["content-type"] || null,
640
- };
641
- }, function (err) {
642
- // 304 surfaces as a "non-2xx error" via httpClient; propagate it
643
- // as a structured 304 result instead so operator routes get
644
- // the conditional-GET short-circuit they expect.
645
- if (err && err.statusCode === requestHelpers.HTTP_STATUS.NOT_MODIFIED) {
646
- return {
647
- statusCode: requestHelpers.HTTP_STATUS.NOT_MODIFIED,
648
- body: null, etag: null, lastModified: null,
649
- };
650
- }
651
- throw err;
652
- });
653
- }
654
-
655
- function head(key) {
656
- var url = _keyToUrl(key);
657
- var headers = _makeSigned("HEAD", url, sha256Hex(Buffer.alloc(0)));
658
- return _request("HEAD", url, headers, null, reqOpts).then(function (res) {
659
- return {
660
- size: res.headers["content-length"] ? parseInt(res.headers["content-length"], 10) : null,
661
- etag: res.headers.etag,
662
- lastModified: res.headers["last-modified"] ? Date.parse(res.headers["last-modified"]) : null,
663
- };
664
- });
665
- }
666
-
667
- function deleteKey(key) {
668
- var url = _keyToUrl(key);
669
- var headers = _makeSigned("DELETE", url, sha256Hex(Buffer.alloc(0)));
670
- return _request("DELETE", url, headers, null, reqOpts).then(
671
- function () { return true; },
672
- function (e) { if (e.statusCode === 404) return false; throw e; }
673
- );
674
- }
675
-
676
- function _presign(method, opts) {
677
- opts = opts || {};
678
- if (!opts.key || typeof opts.key !== "string") {
679
- throw _err("INVALID_KEY", "presigned URL: key is required", true);
680
- }
681
- if (opts.key.indexOf("\0") !== -1) {
682
- throw _err("INVALID_KEY", "null byte in key", true);
683
- }
684
- var expiresIn = opts.expiresIn != null ? opts.expiresIn : PRESIGN_DEFAULT_EXPIRES_SECONDS;
685
- if (typeof expiresIn !== "number" ||
686
- expiresIn < PRESIGN_MIN_EXPIRES_SECONDS ||
687
- expiresIn > PRESIGN_MAX_EXPIRES_SECONDS) {
688
- throw _err("INVALID_EXPIRES",
689
- "presigned URL: expiresIn must be a number of seconds between " +
690
- PRESIGN_MIN_EXPIRES_SECONDS + " and " + PRESIGN_MAX_EXPIRES_SECONDS +
691
- " (7 days, SigV4 hard cap)", true);
692
- }
693
-
694
- var url = _keyToUrl(opts.key);
695
- var date = opts.date || new Date();
696
- var amzDate = _formatAmzDate(date);
697
- var dateStamp = _formatDateStamp(date);
698
- var credentialScope = dateStamp + "/" + config.region + "/" + SERVICE + "/aws4_request";
699
-
700
- // Build the headers we want to sign. host is always signed; if the
701
- // caller supplies opts.contentType the value is bound into the
702
- // signature so the client MUST send Content-Type matching exactly,
703
- // otherwise S3 rejects with SignatureDoesNotMatch — turning the
704
- // hint into real server-side enforcement.
705
- var headers = { host: url.host };
706
- if (opts.contentType) headers["content-type"] = opts.contentType;
707
- // SigV4 SignedHeaders: lowercase, semicolon-joined, AWS-spec order.
708
- var signedHeaderKeys = [];
709
- for (var hk in headers) signedHeaderKeys.push(hk);
710
- signedHeaderKeys.sort();
711
- var signedHeadersStr = signedHeaderKeys.join(";");
712
-
713
- // Query-string SigV4: presigning carries auth in the URL itself.
714
- // The set order doesn't matter — canonicalQueryString sorts the
715
- // params before hashing.
716
- url.searchParams.set("X-Amz-Algorithm", ALGORITHM);
717
- url.searchParams.set("X-Amz-Credential", config.accessKeyId + "/" + credentialScope);
718
- url.searchParams.set("X-Amz-Date", amzDate);
719
- url.searchParams.set("X-Amz-Expires", String(expiresIn));
720
- url.searchParams.set("X-Amz-SignedHeaders", signedHeadersStr);
721
- if (config.sessionToken) {
722
- url.searchParams.set("X-Amz-Security-Token", config.sessionToken);
723
- }
724
-
725
- // Payload hash for query-string presigning is the literal string
726
- // "UNSIGNED-PAYLOAD" — the body is not part of the signature.
727
- var canon = canonicalRequest(method, url, headers, "UNSIGNED-PAYLOAD");
728
- var sts = stringToSign(amzDate, credentialScope, canon);
729
- var signingKey = deriveSigningKey(config.secretAccessKey, dateStamp, config.region, SERVICE);
730
- var signature = nodeCrypto.createHmac("sha256", signingKey).update(sts).digest("hex");
731
-
732
- url.searchParams.set("X-Amz-Signature", signature);
733
-
734
- var clientHeaders = {};
735
- if (opts.contentType) clientHeaders["Content-Type"] = opts.contentType;
736
-
737
- return {
738
- url: url.toString(),
739
- method: method,
740
- headers: clientHeaders,
741
- expiresAt: date.getTime() + C.TIME.seconds(expiresIn),
742
- };
743
- }
744
-
745
- function presignedUploadUrl(opts) { return _presign("PUT", opts); }
746
- function presignedDownloadUrl(opts) { return _presign("GET", opts); }
747
-
748
- // POST-form policy presigning (S3-compatible). Unlike query-string
749
- // PUT presigning, this lets the operator enforce body-size at the
750
- // upload endpoint via the `content-length-range` policy condition.
751
- // Clients upload via multipart/form-data POST to the returned URL,
752
- // attaching every key in `fields` plus the file as the last field.
753
- //
754
- // Reference: AWS S3 "Browser-Based Uploads Using POST".
755
- function presignedUploadPolicy(opts) {
756
- opts = opts || {};
757
- if (!opts.key || typeof opts.key !== "string") {
758
- throw _err("INVALID_KEY", "presignedUploadPolicy: key is required", true);
759
- }
760
- if (opts.key.indexOf("\0") !== -1) {
761
- throw _err("INVALID_KEY", "null byte in key", true);
762
- }
763
- if (typeof opts.maxBytes !== "number" || !Number.isFinite(opts.maxBytes) ||
764
- opts.maxBytes <= 0) {
765
- throw _err("INVALID_MAX_BYTES",
766
- "presignedUploadPolicy: maxBytes (positive number of bytes) is required — " +
767
- "POST-form policy enforces body size via the content-length-range condition; " +
768
- "use presignedUploadUrl if size enforcement is not needed", true);
769
- }
770
- if (opts.minBytes !== undefined && !nb.isNonNegativeFiniteInt(opts.minBytes)) {
771
- throw _err("INVALID_MIN_BYTES",
772
- "presignedUploadPolicy: minBytes must be a non-negative finite integer; got " +
773
- nb.shape(opts.minBytes), true);
774
- }
775
- var minBytes = opts.minBytes !== undefined ? opts.minBytes : 0;
776
- var expiresIn = opts.expiresIn != null ? opts.expiresIn : PRESIGN_DEFAULT_EXPIRES_SECONDS;
777
- if (typeof expiresIn !== "number" ||
778
- expiresIn < PRESIGN_MIN_EXPIRES_SECONDS ||
779
- expiresIn > PRESIGN_MAX_EXPIRES_SECONDS) {
780
- throw _err("INVALID_EXPIRES",
781
- "presignedUploadPolicy: expiresIn must be a number of seconds between " +
782
- PRESIGN_MIN_EXPIRES_SECONDS + " and " + PRESIGN_MAX_EXPIRES_SECONDS +
783
- " (7 days, SigV4 hard cap)", true);
784
- }
785
-
786
- var date = opts.date || new Date();
787
- var amzDate = _formatAmzDate(date);
788
- var dateStamp = _formatDateStamp(date);
789
- var credentialScope = dateStamp + "/" + config.region + "/" + SERVICE + "/aws4_request";
790
- var credential = config.accessKeyId + "/" + credentialScope;
791
- var expirationIso = new Date(date.getTime() + C.TIME.seconds(expiresIn)).toISOString();
792
-
793
- // Policy conditions. Each entry must MATCH a corresponding form
794
- // field exactly OR be a structural constraint (content-length-range
795
- // / starts-with). Order doesn't matter — S3 evaluates the set.
796
- var conditions = [
797
- { "bucket": config.bucket },
798
- { "key": opts.key },
799
- { "x-amz-algorithm": ALGORITHM },
800
- { "x-amz-credential": credential },
801
- { "x-amz-date": amzDate },
802
- ["content-length-range", minBytes, opts.maxBytes],
803
- ];
804
- if (config.sessionToken) {
805
- conditions.push({ "x-amz-security-token": config.sessionToken });
806
- }
807
- if (opts.contentType) {
808
- conditions.push({ "content-type": opts.contentType });
809
- }
810
-
811
- var policy = { expiration: expirationIso, conditions: conditions };
812
- var policyJson = JSON.stringify(policy);
813
- var policyB64 = Buffer.from(policyJson, "utf8").toString("base64");
814
-
815
- var signingKey = deriveSigningKey(config.secretAccessKey, dateStamp, config.region, SERVICE);
816
- var signature = nodeCrypto.createHmac("sha256", signingKey).update(policyB64).digest("hex");
817
-
818
- var fields = {
819
- "key": opts.key,
820
- "x-amz-algorithm": ALGORITHM,
821
- "x-amz-credential": credential,
822
- "x-amz-date": amzDate,
823
- "policy": policyB64,
824
- "x-amz-signature": signature,
825
- };
826
- if (config.sessionToken) fields["x-amz-security-token"] = config.sessionToken;
827
- if (opts.contentType) fields["content-type"] = opts.contentType;
828
-
829
- // The bucket-root URL — no key in the path, key is a form field.
830
- var url = _bucketUrl();
831
-
832
- return {
833
- url: url.toString(),
834
- method: "POST",
835
- fields: fields,
836
- expiresAt: date.getTime() + C.TIME.seconds(expiresIn),
837
- maxBytes: opts.maxBytes,
838
- enforcement: "content-length-range", // S3-side enforced
839
- };
840
- }
841
-
842
- function list(prefix, opts) {
843
- opts = opts || {};
844
- var params = { "list-type": "2" };
845
- if (prefix) params["prefix"] = prefix;
846
- if (opts.maxResults) params["max-keys"] = String(opts.maxResults);
847
- if (opts.continuationToken) params["continuation-token"] = opts.continuationToken;
848
-
849
- var url = _bucketUrl(params);
850
- var headers = _makeSigned("GET", url, sha256Hex(Buffer.alloc(0)));
851
- return _request("GET", url, headers, null, reqOpts).then(function (res) {
852
- var doc = safeXml.parse(res.body, LIST_PARSE_OPTS);
853
- var result = doc.ListBucketResult || {};
854
- var contents = _arrayify(result.Contents);
855
- var items = contents.map(function (c) {
856
- return {
857
- key: c.Key,
858
- size: c.Size != null ? parseInt(c.Size, 10) : null,
859
- lastModified: c.LastModified ? Date.parse(c.LastModified) : null,
860
- };
861
- }).filter(function (it) { return it.key; });
862
- return {
863
- items: items,
864
- truncated: result.IsTruncated === "true",
865
- continuationToken: result.NextContinuationToken || null,
866
- };
867
- });
868
- }
869
-
870
- return {
871
- protocol: "sigv4",
872
- endpoint: endpoint,
873
- bucket: config.bucket,
874
- region: config.region,
875
- pathStyle: pathStyle,
876
- put: put,
877
- get: get,
878
- getStream: getStream,
879
- getResponse: getResponse,
880
- head: head,
881
- delete: deleteKey,
882
- list: list,
883
- presignedUploadUrl: presignedUploadUrl,
884
- presignedDownloadUrl: presignedDownloadUrl,
885
- presignedUploadPolicy: presignedUploadPolicy,
886
- };
887
- }
888
-
889
- module.exports = {
890
- create: create,
891
- signRequest: signRequest,
892
- canonicalRequest: canonicalRequest,
893
- stringToSign: stringToSign,
894
- deriveSigningKey: deriveSigningKey,
895
- canonicalQueryString: canonicalQueryString,
896
- canonicalHeaders: canonicalHeaders,
897
- awsUriEncode: awsUriEncode,
898
- sha256Hex: sha256Hex,
899
- formatAmzDate: _formatAmzDate,
900
- formatDateStamp: _formatDateStamp,
901
- SERVICE: SERVICE,
902
- ALGORITHM: ALGORITHM,
903
- };
1
+ "use strict";
2
+ /**
3
+ * SigV4 protocol adapter — AWS Signature Version 4.
4
+ *
5
+ * One signing implementation covers the entire S3-API-compatible family:
6
+ * AWS S3, Cloudflare R2, Backblaze B2 (S3 endpoint), MinIO, Wasabi,
7
+ * Tigris, DigitalOcean Spaces, IDrive e2, Linode Object Storage, Storj
8
+ * (S3 gateway). Service identifier is always "s3" for object storage.
9
+ *
10
+ * Config:
11
+ * {
12
+ * endpoint: 'https://s3.us-west-2.amazonaws.com' // or R2/MinIO/etc.
13
+ * region: 'us-west-2' // required
14
+ * bucket: 'my-bucket' // required
15
+ * accessKeyId: '...' // required
16
+ * secretAccessKey: '...' // required
17
+ * sessionToken: '...' // optional (STS)
18
+ * pathStyle: false // virtual-hosted
19
+ * // by default
20
+ * timeoutMs: C.TIME.seconds(30)
21
+ * }
22
+ *
23
+ * Reference:
24
+ * https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
25
+ */
26
+ var nodeCrypto = require("crypto");
27
+ var { URL } = require("url");
28
+ var { Readable } = require("stream");
29
+ var safeXml = require("../parsers/safe-xml");
30
+ var sharedRequest = require("./http-request");
31
+ var C = require("../constants");
32
+ var nb = require("../numeric-bounds");
33
+ var requestHelpers = require("../request-helpers");
34
+ var { ObjectStoreError } = require("../framework-error");
35
+ var safeUrl = require("../safe-url");
36
+
37
+ // Internal URL builder — endpoint + path string from validated config /
38
+ // framework-encoded keys. Routes through safeUrl.parse so the protocol
39
+ // allowlist + length cap apply uniformly. Presigned URLs with many
40
+ // query params can approach the 8 KB default; raise the cap to 32 KB
41
+ // so a worst-case multipart UploadPart URL still fits.
42
+ function _internalUrl(input, allowedProtocols) {
43
+ return safeUrl.parse(input, {
44
+ allowedProtocols: allowedProtocols || safeUrl.ALLOW_HTTP_TLS,
45
+ errorClass: ObjectStoreError,
46
+ maxUrlLength: C.BYTES.kib(32),
47
+ });
48
+ }
49
+
50
+ // S3 list responses are commonly 1000-key paginated payloads — well above
51
+ // the parser's default 1 MiB / 10K element ceilings. These overrides
52
+ // give headroom for normal responses without uncapping the parser.
53
+ var LIST_PARSE_OPTS = {
54
+ maxBytes: C.BYTES.mib(8),
55
+ maxElements: C.BYTES.bytes(50000),
56
+ };
57
+
58
+ function _arrayify(value) {
59
+ // xml-safe maps multiple same-tag children to an array; a single child
60
+ // stays as the bare object; zero children means the property is absent.
61
+ // List traversal needs a uniform array, so normalize.
62
+ if (value == null) return [];
63
+ return Array.isArray(value) ? value : [value];
64
+ }
65
+
66
+ var SERVICE = "s3";
67
+ var ALGORITHM = "AWS4-HMAC-SHA256";
68
+
69
+ // Presigned-URL expiry bounds. The SigV4 spec caps query-string-signed
70
+ // URLs at 7 days; below 1 second is effectively unsignable.
71
+ var PRESIGN_DEFAULT_EXPIRES_SECONDS = C.TIME.minutes(15) / C.TIME.seconds(1); // 15 minutes
72
+ var PRESIGN_MAX_EXPIRES_SECONDS = C.TIME.days(7) / C.TIME.seconds(1); // 7 days (SigV4 hard cap)
73
+ var PRESIGN_MIN_EXPIRES_SECONDS = 1;
74
+
75
+ var _err = ObjectStoreError.factory;
76
+
77
+ // ---- SigV4 primitives ----
78
+
79
+ function sha256Hex(buf) {
80
+ return nodeCrypto.createHash("sha256").update(buf).digest("hex");
81
+ }
82
+ function hmacSha256(key, data) {
83
+ return nodeCrypto.createHmac("sha256", key).update(data).digest();
84
+ }
85
+
86
+ // AWS-style URI encoding: same as RFC 3986 except path '/' may be preserved.
87
+ function awsUriEncode(str, encodeSlash) {
88
+ var out = "";
89
+ for (var i = 0; i < str.length; i++) {
90
+ var c = str.charCodeAt(i);
91
+ var ch = str.charAt(i);
92
+ if ((c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A) ||
93
+ (c >= 0x30 && c <= 0x39) ||
94
+ ch === "-" || ch === "_" || ch === "." || ch === "~") {
95
+ out += ch;
96
+ } else if (ch === "/" && !encodeSlash) {
97
+ out += "/";
98
+ } else {
99
+ var enc = encodeURIComponent(ch).replace(/!/g, "%21").replace(/\*/g, "%2A").replace(/'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
100
+ out += enc;
101
+ }
102
+ }
103
+ return out;
104
+ }
105
+
106
+ function canonicalQueryString(searchParams) {
107
+ if (!searchParams || searchParams.toString() === "") return "";
108
+ var pairs = [];
109
+ searchParams.forEach(function (v, k) { pairs.push([k, v]); });
110
+ pairs.sort(function (a, b) {
111
+ if (a[0] < b[0]) return -1;
112
+ if (a[0] > b[0]) return 1;
113
+ if (a[1] < b[1]) return -1;
114
+ if (a[1] > b[1]) return 1;
115
+ return 0;
116
+ });
117
+ return pairs.map(function (p) {
118
+ return awsUriEncode(p[0], true) + "=" + awsUriEncode(p[1], true);
119
+ }).join("&");
120
+ }
121
+
122
+ function canonicalHeaders(headers) {
123
+ var pairs = [];
124
+ for (var k in headers) {
125
+ if (headers[k] === undefined || headers[k] === null) continue;
126
+ var lk = k.toLowerCase();
127
+ var v = String(headers[k]).trim().replace(/\s+/g, " ");
128
+ pairs.push([lk, v]);
129
+ }
130
+ pairs.sort(function (a, b) { return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; });
131
+ var canon = "";
132
+ var signed = [];
133
+ for (var i = 0; i < pairs.length; i++) {
134
+ canon += pairs[i][0] + ":" + pairs[i][1] + "\n";
135
+ signed.push(pairs[i][0]);
136
+ }
137
+ return { canonical: canon, signed: signed.join(";") };
138
+ }
139
+
140
+ function canonicalRequest(method, urlObj, headers, payloadHash) {
141
+ var canonHeaders = canonicalHeaders(headers);
142
+ var path = urlObj.pathname;
143
+ if (!path) path = "/";
144
+ return [
145
+ method.toUpperCase(),
146
+ awsUriEncode(path, false),
147
+ canonicalQueryString(urlObj.searchParams),
148
+ canonHeaders.canonical,
149
+ canonHeaders.signed,
150
+ payloadHash,
151
+ ].join("\n");
152
+ }
153
+
154
+ function stringToSign(amzDate, credentialScope, canonicalReq) {
155
+ return [
156
+ ALGORITHM,
157
+ amzDate,
158
+ credentialScope,
159
+ sha256Hex(canonicalReq),
160
+ ].join("\n");
161
+ }
162
+
163
+ function deriveSigningKey(secretAccessKey, dateStamp, region, service) {
164
+ var kDate = hmacSha256("AWS4" + secretAccessKey, dateStamp);
165
+ var kRegion = hmacSha256(kDate, region);
166
+ var kService = hmacSha256(kRegion, service);
167
+ return hmacSha256(kService, "aws4_request");
168
+ }
169
+
170
+ // SigV4 ISO compact format: YYYYMMDDTHHMMSSZ — slice indices fixed
171
+ // by AWS spec.
172
+ function _formatAmzDate(d) {
173
+ var iso = d.toISOString().replace(/[-:]/g, "");
174
+ return iso.slice(0, C.BYTES.bytes(8)) + "T" + iso.slice(9, 15) + "Z";
175
+ }
176
+ function _formatDateStamp(d) {
177
+ return d.toISOString().slice(0, 10).replace(/-/g, "");
178
+ }
179
+
180
+ function signRequest(opts) {
181
+ var date = opts.date || new Date();
182
+ var amzDate = _formatAmzDate(date);
183
+ var dateStamp = _formatDateStamp(date);
184
+ // signRequest is called by callers signing requests to operator-
185
+ // configured endpoints (cloudwatch, sqs, sns, custom S3-API stores).
186
+ // Honor opts.allowedProtocols so a test-fixture http:// endpoint still
187
+ // signs correctly while production https:// stays the default.
188
+ var url = opts.url instanceof URL ? opts.url
189
+ : _internalUrl(opts.url, opts.allowedProtocols);
190
+ // service defaults to s3 for back-compat — every call site predating
191
+ // v0.6.25 was object-store / S3. Other AWS services (logs, sqs, sns,
192
+ // kinesis, etc.) pass opts.service explicitly. The credentialScope
193
+ // and signing-key derivation both incorporate the service name, so
194
+ // this MUST match what the target service expects.
195
+ var service = opts.service || SERVICE;
196
+
197
+ var headers = Object.assign({}, opts.headers || {});
198
+ headers["host"] = url.host;
199
+ headers["x-amz-date"] = amzDate;
200
+ if (!headers["x-amz-content-sha256"]) {
201
+ headers["x-amz-content-sha256"] = opts.payloadHash;
202
+ }
203
+ if (opts.sessionToken) {
204
+ headers["x-amz-security-token"] = opts.sessionToken;
205
+ }
206
+
207
+ var canon = canonicalRequest(opts.method, url, headers, opts.payloadHash);
208
+ var credentialScope = dateStamp + "/" + opts.region + "/" + service + "/aws4_request";
209
+ var sts = stringToSign(amzDate, credentialScope, canon);
210
+ var signingKey = deriveSigningKey(opts.secretAccessKey, dateStamp, opts.region, service);
211
+ var signature = nodeCrypto.createHmac("sha256", signingKey).update(sts).digest("hex");
212
+
213
+ var canonHeaders = canonicalHeaders(headers);
214
+ var auth = ALGORITHM +
215
+ " Credential=" + opts.accessKeyId + "/" + credentialScope +
216
+ ", SignedHeaders=" + canonHeaders.signed +
217
+ ", Signature=" + signature;
218
+ headers["Authorization"] = auth;
219
+
220
+ return { headers: headers, signature: signature, canonicalRequest: canon, stringToSign: sts };
221
+ }
222
+
223
+ // ---- HTTP request helper ----
224
+
225
+ var _request = sharedRequest;
226
+
227
+ // ---- Multipart-upload constants ----
228
+
229
+ // S3 spec floor for non-final part size. Below this the API rejects
230
+ // CompleteMultipartUpload with EntityTooSmall. The framework refuses
231
+ // configurations below this floor at create() time so operators don't
232
+ // see surprising failures only on large uploads.
233
+ var MIN_PART_SIZE_BYTES = C.BYTES.mib(5);
234
+ // S3 spec ceiling on part count. CompleteMultipartUpload rejects
235
+ // uploads with more than 10000 parts.
236
+ var MAX_PARTS = C.BYTES.bytes(10000);
237
+ // Auto-multipart trigger: buffered bodies under this stay single-PUT.
238
+ // Streams always go multipart since size isn't known up-front.
239
+ var DEFAULT_MULTIPART_THRESHOLD_BYTES = C.BYTES.mib(64);
240
+ // Conservative default part size — large enough to keep round-trip
241
+ // overhead small relative to payload, small enough to fit comfortably
242
+ // in a 4-way-concurrent upload's memory footprint.
243
+ var DEFAULT_PART_SIZE_BYTES = C.BYTES.mib(16);
244
+ var DEFAULT_PART_CONCURRENCY = 4;
245
+
246
+ // ---- SSE option handling ----
247
+
248
+ function _resolveSseHeaders(sse) {
249
+ if (sse === undefined || sse === null) return null;
250
+ var type;
251
+ var keyId = null;
252
+ if (typeof sse === "string") {
253
+ type = sse;
254
+ } else if (sse && typeof sse === "object") {
255
+ type = sse.type;
256
+ keyId = sse.keyId || null;
257
+ } else {
258
+ throw _err("INVALID_SSE",
259
+ "opts.sse must be a string ('AES256' | 'aws:kms') or " +
260
+ "{ type, keyId }, got " + typeof sse, true);
261
+ }
262
+ if (type !== "AES256" && type !== "aws:kms") {
263
+ throw _err("INVALID_SSE",
264
+ "opts.sse type must be 'AES256' or 'aws:kms', got '" + type + "'", true);
265
+ }
266
+ var h = { "x-amz-server-side-encryption": type };
267
+ if (type === "aws:kms" && keyId) {
268
+ h["x-amz-server-side-encryption-aws-kms-key-id"] = String(keyId);
269
+ }
270
+ return { type: type, keyId: keyId, headers: h };
271
+ }
272
+
273
+ function _verifySseResponse(sseRequested, resHeaders) {
274
+ // Operators who specified an SSE policy expect the bucket / object to
275
+ // honor it. If the server silently dropped the header (mis-configured
276
+ // bucket policy, unsupported endpoint, etc.) the request looks like a
277
+ // success but the at-rest data is unencrypted. Surface this as a
278
+ // hard failure rather than a silent compliance hole.
279
+ if (!sseRequested) return;
280
+ var got = resHeaders["x-amz-server-side-encryption"];
281
+ if (!got) {
282
+ throw _err("SSE_NOT_APPLIED",
283
+ "opts.sse was '" + sseRequested.type + "' but server did not " +
284
+ "apply server-side encryption (no x-amz-server-side-encryption " +
285
+ "response header)", true);
286
+ }
287
+ if (got !== sseRequested.type) {
288
+ throw _err("SSE_MISMATCH",
289
+ "opts.sse requested '" + sseRequested.type + "' but server " +
290
+ "applied '" + got + "'", true);
291
+ }
292
+ }
293
+
294
+ // ---- Multipart helpers ----
295
+
296
+ // Build the CompleteMultipartUpload request body. Parts must be in
297
+ // ascending PartNumber order. ETags from S3 include surrounding
298
+ // quotes — preserve them exactly.
299
+ function _buildCompleteMultipartXml(parts) {
300
+ var body = "<CompleteMultipartUpload>";
301
+ for (var i = 0; i < parts.length; i++) {
302
+ body += "<Part>";
303
+ body += "<PartNumber>" + parts[i].partNumber + "</PartNumber>";
304
+ body += "<ETag>" + parts[i].etag + "</ETag>";
305
+ body += "</Part>";
306
+ }
307
+ body += "</CompleteMultipartUpload>";
308
+ return body;
309
+ }
310
+
311
+ // Read a Readable stream into fixed-size buffers. Yields one Buffer
312
+ // per part (size <= partSize). The final part may be smaller. The
313
+ // stream is consumed exactly once.
314
+ async function _readStreamParts(readable, partSize) {
315
+ var parts = [];
316
+ var pending = [];
317
+ var pendingBytes = 0;
318
+ for await (var chunk of readable) {
319
+ var buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
320
+ pending.push(buf);
321
+ pendingBytes += buf.length;
322
+ while (pendingBytes >= partSize) {
323
+ var combined = Buffer.concat(pending, pendingBytes);
324
+ parts.push(combined.slice(0, partSize));
325
+ var leftover = combined.slice(partSize);
326
+ pending = leftover.length > 0 ? [leftover] : [];
327
+ pendingBytes = leftover.length;
328
+ }
329
+ }
330
+ if (pendingBytes > 0) {
331
+ parts.push(Buffer.concat(pending, pendingBytes));
332
+ }
333
+ return parts;
334
+ }
335
+
336
+ // Run an array of async tasks with bounded parallelism. Preserves
337
+ // result order by index.
338
+ async function _bounded(items, concurrency, runner) {
339
+ var results = new Array(items.length);
340
+ var i = 0;
341
+ async function worker() {
342
+ while (true) {
343
+ var idx = i++;
344
+ if (idx >= items.length) return;
345
+ results[idx] = await runner(items[idx], idx);
346
+ }
347
+ }
348
+ var workers = [];
349
+ for (var w = 0; w < Math.min(concurrency, items.length); w++) {
350
+ workers.push(worker());
351
+ }
352
+ await Promise.all(workers);
353
+ return results;
354
+ }
355
+
356
+ // ---- Public adapter factory ----
357
+
358
+ function create(config) {
359
+ if (!config) throw new Error("sigv4 protocol requires config");
360
+ if (!config.region) throw new Error("sigv4: region is required");
361
+ if (!config.bucket) throw new Error("sigv4: bucket is required");
362
+ if (!config.accessKeyId) throw new Error("sigv4: accessKeyId is required");
363
+ if (!config.secretAccessKey) throw new Error("sigv4: secretAccessKey is required");
364
+
365
+ var endpoint = config.endpoint || ("https://s3." + config.region + ".amazonaws.com");
366
+ if (endpoint.endsWith("/")) endpoint = endpoint.slice(0, -1);
367
+ var pathStyle = !!(config.pathStyle || config.forcePathStyle);
368
+
369
+ var partSize = config.partSizeBytes != null
370
+ ? config.partSizeBytes
371
+ : DEFAULT_PART_SIZE_BYTES;
372
+ if (typeof partSize !== "number" || !isFinite(partSize) || partSize < MIN_PART_SIZE_BYTES) {
373
+ throw _err("INVALID_CONFIG",
374
+ "sigv4: partSizeBytes must be a number >= " + MIN_PART_SIZE_BYTES +
375
+ " (S3 minimum part size), got " + partSize, true);
376
+ }
377
+ var multipartThreshold = config.multipartThresholdBytes != null
378
+ ? config.multipartThresholdBytes
379
+ : DEFAULT_MULTIPART_THRESHOLD_BYTES;
380
+ if (typeof multipartThreshold !== "number" || !isFinite(multipartThreshold) || multipartThreshold < 0) {
381
+ throw _err("INVALID_CONFIG",
382
+ "sigv4: multipartThresholdBytes must be a non-negative finite number, got " +
383
+ multipartThreshold, true);
384
+ }
385
+ var partConcurrency = config.partConcurrency != null
386
+ ? config.partConcurrency
387
+ : DEFAULT_PART_CONCURRENCY;
388
+ if (typeof partConcurrency !== "number" || partConcurrency < 1 || !isFinite(partConcurrency)) {
389
+ throw _err("INVALID_CONFIG",
390
+ "sigv4: partConcurrency must be a positive finite number, got " + partConcurrency, true);
391
+ }
392
+ // HTTPS-only by default — AWS S3, R2, MinIO-over-https. Operators with
393
+ // an internal cleartext S3-compatible endpoint (test fixtures, local
394
+ // dev MinIO) opt in via config.allowedProtocols.
395
+ var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
396
+ var allowInternal = config.allowInternal != null ? config.allowInternal : null;
397
+ safeUrl.parse(endpoint, {
398
+ allowedProtocols: allowedProtocols,
399
+ errorClass: ObjectStoreError,
400
+ });
401
+ var reqOpts = { timeoutMs: config.timeoutMs, allowedProtocols: allowedProtocols };
402
+ if (allowInternal !== null) reqOpts.allowInternal = allowInternal;
403
+
404
+ function _keyToUrl(key) {
405
+ if (key.indexOf("\0") !== -1) throw _err("INVALID_KEY", "null byte in key", true);
406
+ var encoded = key.split("/").map(function (s) { return awsUriEncode(s, true); }).join("/");
407
+ if (pathStyle) {
408
+ return _internalUrl(endpoint + "/" + config.bucket + "/" + encoded, allowedProtocols);
409
+ }
410
+ var u = _internalUrl(endpoint, allowedProtocols);
411
+ u.hostname = config.bucket + "." + u.hostname;
412
+ u.pathname = "/" + encoded;
413
+ return u;
414
+ }
415
+
416
+ function _bucketUrl(searchParams) {
417
+ var u;
418
+ if (pathStyle) {
419
+ u = _internalUrl(endpoint + "/" + config.bucket + "/", allowedProtocols);
420
+ } else {
421
+ u = _internalUrl(endpoint, allowedProtocols);
422
+ u.hostname = config.bucket + "." + u.hostname;
423
+ u.pathname = "/";
424
+ }
425
+ if (searchParams) {
426
+ Object.keys(searchParams).forEach(function (k) { u.searchParams.set(k, searchParams[k]); });
427
+ }
428
+ return u;
429
+ }
430
+
431
+ function _makeSigned(method, url, payloadHash, extraHeaders) {
432
+ var signed = signRequest({
433
+ method: method,
434
+ url: url,
435
+ headers: extraHeaders || {},
436
+ payloadHash: payloadHash,
437
+ region: config.region,
438
+ accessKeyId: config.accessKeyId,
439
+ secretAccessKey: config.secretAccessKey,
440
+ sessionToken: config.sessionToken,
441
+ });
442
+ return signed.headers;
443
+ }
444
+
445
+ function put(key, body, opts) {
446
+ opts = opts || {};
447
+ var sseRequested = _resolveSseHeaders(opts.sse);
448
+ // Streams always go multipart — size isn't known up-front. Buffers
449
+ // dispatch to multipart when they exceed the threshold; operators
450
+ // can force the single-PUT path with `multipart: false` (small
451
+ // bodies in unit tests, or to keep the request count to one).
452
+ var isStream = body && typeof body === "object" && typeof body.pipe === "function";
453
+ if (isStream) {
454
+ if (opts.multipart === false) {
455
+ return Promise.reject(_err("STREAM_REQUIRES_MULTIPART",
456
+ "put(stream) requires multipart upload (set opts.multipart !== false)", true));
457
+ }
458
+ return _multipartPut(key, body, opts, sseRequested);
459
+ }
460
+ var buf = Buffer.isBuffer(body) ? body : Buffer.from(typeof body === "string" ? body : "", "utf8");
461
+ if (opts.multipart !== false &&
462
+ (opts.multipart === true || buf.length > multipartThreshold)) {
463
+ return _multipartPut(key, buf, opts, sseRequested);
464
+ }
465
+ return _singlePut(key, buf, opts, sseRequested);
466
+ }
467
+
468
+ function _singlePut(key, buf, opts, sseRequested) {
469
+ var url = _keyToUrl(key);
470
+ var payloadHash = sha256Hex(buf);
471
+ var contentType = opts.contentType || "application/octet-stream";
472
+ var extra = {
473
+ "Content-Type": contentType,
474
+ "Content-Length": String(buf.length),
475
+ };
476
+ if (sseRequested) Object.assign(extra, sseRequested.headers);
477
+ var headers = _makeSigned("PUT", url, payloadHash, extra);
478
+ return _request("PUT", url, headers, buf, reqOpts).then(function (res) {
479
+ _verifySseResponse(sseRequested, res.headers);
480
+ return { size: buf.length, etag: res.headers.etag };
481
+ });
482
+ }
483
+
484
+ async function _multipartPut(key, body, opts, sseRequested) {
485
+ var contentType = opts.contentType || "application/octet-stream";
486
+
487
+ // Slice into parts. For Buffers we slice up-front; for streams we
488
+ // read sequentially into part-sized buffers (memory bounded).
489
+ var parts;
490
+ if (Buffer.isBuffer(body)) {
491
+ parts = [];
492
+ for (var off = 0; off < body.length; off += partSize) {
493
+ parts.push(body.slice(off, Math.min(off + partSize, body.length)));
494
+ }
495
+ // Edge case: empty buffer → one zero-length part. S3 rejects
496
+ // multipart with zero parts; rather than handling this corner
497
+ // we route empty buffers through single-PUT instead.
498
+ if (parts.length === 0) parts = [Buffer.alloc(0)];
499
+ } else {
500
+ parts = await _readStreamParts(body, partSize);
501
+ if (parts.length === 0) parts = [Buffer.alloc(0)];
502
+ }
503
+ if (parts.length > MAX_PARTS) {
504
+ throw _err("TOO_MANY_PARTS",
505
+ "multipart upload would require " + parts.length + " parts " +
506
+ "(S3 max " + MAX_PARTS + "); increase partSizeBytes", true);
507
+ }
508
+
509
+ // 1. Initiate — `?uploads` is the InitiateMultipartUpload subresource.
510
+ // Build the URL with the bare token (no trailing `=`) for consistency
511
+ // with sigv4-bucket-ops.js — strict S3 implementations route on the
512
+ // bare form; the `URLSearchParams.set("uploads", "")` idiom produces
513
+ // `?uploads=` which is accepted by AWS + MinIO for this specific
514
+ // subresource, but the framework's convention is the bare form
515
+ // everywhere. SigV4 canonicalization reads `url.searchParams` (which
516
+ // still presents `uploads=` per AWS spec) so the signature path is
517
+ // unchanged.
518
+ var url = _keyToUrl(key);
519
+ var initiateUrl = _internalUrl(url.href + (url.search ? "&" : "?") + "uploads", allowedProtocols);
520
+ var initiateExtra = {
521
+ "Content-Type": contentType,
522
+ "Content-Length": "0",
523
+ };
524
+ if (sseRequested) Object.assign(initiateExtra, sseRequested.headers);
525
+ var initiateHeaders = _makeSigned("POST", initiateUrl, sha256Hex(Buffer.alloc(0)), initiateExtra);
526
+ var initRes = await _request("POST", initiateUrl, initiateHeaders, Buffer.alloc(0), reqOpts);
527
+ _verifySseResponse(sseRequested, initRes.headers);
528
+ var initDoc = safeXml.parse(initRes.body, LIST_PARSE_OPTS);
529
+ var uploadId = initDoc.InitiateMultipartUploadResult &&
530
+ initDoc.InitiateMultipartUploadResult.UploadId;
531
+ if (!uploadId) {
532
+ throw _err("MULTIPART_INIT_FAILED",
533
+ "S3 InitiateMultipartUpload response missing UploadId", false);
534
+ }
535
+
536
+ var totalSize = 0;
537
+ var uploadedEtags;
538
+
539
+ try {
540
+ // 2. Upload parts (concurrency-bounded)
541
+ uploadedEtags = await _bounded(parts, partConcurrency, async function (partBuf, idx) {
542
+ var partNumber = idx + 1;
543
+ var partUrl = _internalUrl(url.href, allowedProtocols);
544
+ partUrl.searchParams.set("partNumber", String(partNumber));
545
+ partUrl.searchParams.set("uploadId", uploadId);
546
+ var partHeaders = _makeSigned("PUT", partUrl, sha256Hex(partBuf), {
547
+ "Content-Length": String(partBuf.length),
548
+ });
549
+ var partRes = await _request("PUT", partUrl, partHeaders, partBuf, reqOpts);
550
+ if (!partRes.headers.etag) {
551
+ throw _err("MULTIPART_PART_FAILED",
552
+ "UploadPart response missing ETag for part " + partNumber, false);
553
+ }
554
+ totalSize += partBuf.length;
555
+ return { partNumber: partNumber, etag: partRes.headers.etag };
556
+ });
557
+
558
+ // 3. Complete
559
+ var completeUrl = _internalUrl(url.href, allowedProtocols);
560
+ completeUrl.searchParams.set("uploadId", uploadId);
561
+ var completeBody = Buffer.from(_buildCompleteMultipartXml(uploadedEtags), "utf8");
562
+ var completeHeaders = _makeSigned("POST", completeUrl, sha256Hex(completeBody), {
563
+ "Content-Type": "application/xml",
564
+ "Content-Length": String(completeBody.length),
565
+ });
566
+ var completeRes = await _request("POST", completeUrl, completeHeaders, completeBody, reqOpts);
567
+ // S3 may return 200 OK with an error body on CompleteMultipartUpload —
568
+ // surface that as a hard error rather than a silent success.
569
+ var completeDoc = safeXml.parse(completeRes.body, LIST_PARSE_OPTS);
570
+ if (completeDoc.Error) {
571
+ throw _err("MULTIPART_COMPLETE_FAILED",
572
+ "CompleteMultipartUpload returned error: " +
573
+ (completeDoc.Error.Code || "unknown") + " " +
574
+ (completeDoc.Error.Message || ""), false);
575
+ }
576
+ // SSE was already verified on the InitiateMultipartUpload
577
+ // response — that's the request that establishes the upload's
578
+ // encryption policy server-side. The CompleteMultipartUpload
579
+ // response may or may not echo the header depending on vendor;
580
+ // re-verifying here would double-fault on otherwise-fine setups.
581
+ var result = completeDoc.CompleteMultipartUploadResult || {};
582
+ return { size: totalSize, etag: result.ETag || completeRes.headers.etag, multipart: true };
583
+ } catch (e) {
584
+ // Abort cleans up server-side storage for the partial upload.
585
+ // Failures here are silently swallowed — the caller's original
586
+ // error is what they need to see, not a secondary cleanup error.
587
+ try {
588
+ var abortUrl = _internalUrl(url.href, allowedProtocols);
589
+ abortUrl.searchParams.set("uploadId", uploadId);
590
+ var abortHeaders = _makeSigned("DELETE", abortUrl, sha256Hex(Buffer.alloc(0)));
591
+ await _request("DELETE", abortUrl, abortHeaders, null, reqOpts);
592
+ } catch (_e) { /* primary error wins */ }
593
+ throw e;
594
+ }
595
+ }
596
+
597
+ // get(key, opts?) — opts forwarded to the request as conditional /
598
+ // range headers so operator HTTP routes can pass If-None-Match,
599
+ // If-Match, If-Modified-Since, If-Unmodified-Since, and Range from the
600
+ // client request straight through to S3. Returns just the body buffer
601
+ // for backwards compatibility; operators wanting status + response
602
+ // headers (304 vs 206 vs 200) call getResponse() instead.
603
+ function get(key, opts) {
604
+ return getResponse(key, opts).then(function (r) { return r.body; });
605
+ }
606
+
607
+ function getStream(key, opts) {
608
+ return Readable.from(get(key, opts));
609
+ }
610
+
611
+ // getResponse(key, opts?) — full-fidelity GET. Returns
612
+ // { body, statusCode, etag, lastModified, contentRange, size,
613
+ // contentType }. Throws on non-2xx EXCEPT 304 (returned as
614
+ // { statusCode: 304, etag, lastModified, body: null }) so operator
615
+ // routes can short-circuit conditional GETs without losing the
616
+ // response headers.
617
+ function getResponse(key, opts) {
618
+ opts = opts || {};
619
+ var url = _keyToUrl(key);
620
+ var headers = _makeSigned("GET", url, sha256Hex(Buffer.alloc(0)));
621
+ if (opts.range) {
622
+ headers["Range"] = "bytes=" + opts.range.start + "-" + opts.range.end;
623
+ }
624
+ if (opts.ifNoneMatch) headers["If-None-Match"] = opts.ifNoneMatch;
625
+ if (opts.ifMatch) headers["If-Match"] = opts.ifMatch;
626
+ if (opts.ifModifiedSince) headers["If-Modified-Since"] = opts.ifModifiedSince;
627
+ if (opts.ifUnmodifiedSince) headers["If-Unmodified-Since"] = opts.ifUnmodifiedSince;
628
+ var localReqOpts = Object.assign({}, reqOpts, { _resolveOnRedirect: false });
629
+ return _request("GET", url, headers, null, localReqOpts).then(function (res) {
630
+ return {
631
+ statusCode: res.statusCode,
632
+ body: res.body,
633
+ etag: res.headers && res.headers.etag,
634
+ lastModified: res.headers && res.headers["last-modified"]
635
+ ? Date.parse(res.headers["last-modified"]) : null,
636
+ contentRange: res.headers && res.headers["content-range"] || null,
637
+ size: res.headers && res.headers["content-length"]
638
+ ? parseInt(res.headers["content-length"], 10) : null,
639
+ contentType: res.headers && res.headers["content-type"] || null,
640
+ };
641
+ }, function (err) {
642
+ // 304 surfaces as a "non-2xx error" via httpClient; propagate it
643
+ // as a structured 304 result instead so operator routes get
644
+ // the conditional-GET short-circuit they expect.
645
+ if (err && err.statusCode === requestHelpers.HTTP_STATUS.NOT_MODIFIED) {
646
+ return {
647
+ statusCode: requestHelpers.HTTP_STATUS.NOT_MODIFIED,
648
+ body: null, etag: null, lastModified: null,
649
+ };
650
+ }
651
+ throw err;
652
+ });
653
+ }
654
+
655
+ function head(key) {
656
+ var url = _keyToUrl(key);
657
+ var headers = _makeSigned("HEAD", url, sha256Hex(Buffer.alloc(0)));
658
+ return _request("HEAD", url, headers, null, reqOpts).then(function (res) {
659
+ return {
660
+ size: res.headers["content-length"] ? parseInt(res.headers["content-length"], 10) : null,
661
+ etag: res.headers.etag,
662
+ lastModified: res.headers["last-modified"] ? Date.parse(res.headers["last-modified"]) : null,
663
+ };
664
+ });
665
+ }
666
+
667
+ function deleteKey(key) {
668
+ var url = _keyToUrl(key);
669
+ var headers = _makeSigned("DELETE", url, sha256Hex(Buffer.alloc(0)));
670
+ return _request("DELETE", url, headers, null, reqOpts).then(
671
+ function () { return true; },
672
+ function (e) { if (e.statusCode === 404) return false; throw e; }
673
+ );
674
+ }
675
+
676
+ function _presign(method, opts) {
677
+ opts = opts || {};
678
+ if (!opts.key || typeof opts.key !== "string") {
679
+ throw _err("INVALID_KEY", "presigned URL: key is required", true);
680
+ }
681
+ if (opts.key.indexOf("\0") !== -1) {
682
+ throw _err("INVALID_KEY", "null byte in key", true);
683
+ }
684
+ var expiresIn = opts.expiresIn != null ? opts.expiresIn : PRESIGN_DEFAULT_EXPIRES_SECONDS;
685
+ if (typeof expiresIn !== "number" ||
686
+ expiresIn < PRESIGN_MIN_EXPIRES_SECONDS ||
687
+ expiresIn > PRESIGN_MAX_EXPIRES_SECONDS) {
688
+ throw _err("INVALID_EXPIRES",
689
+ "presigned URL: expiresIn must be a number of seconds between " +
690
+ PRESIGN_MIN_EXPIRES_SECONDS + " and " + PRESIGN_MAX_EXPIRES_SECONDS +
691
+ " (7 days, SigV4 hard cap)", true);
692
+ }
693
+
694
+ var url = _keyToUrl(opts.key);
695
+ var date = opts.date || new Date();
696
+ var amzDate = _formatAmzDate(date);
697
+ var dateStamp = _formatDateStamp(date);
698
+ var credentialScope = dateStamp + "/" + config.region + "/" + SERVICE + "/aws4_request";
699
+
700
+ // Build the headers we want to sign. host is always signed; if the
701
+ // caller supplies opts.contentType the value is bound into the
702
+ // signature so the client MUST send Content-Type matching exactly,
703
+ // otherwise S3 rejects with SignatureDoesNotMatch — turning the
704
+ // hint into real server-side enforcement.
705
+ var headers = { host: url.host };
706
+ if (opts.contentType) headers["content-type"] = opts.contentType;
707
+ // SigV4 SignedHeaders: lowercase, semicolon-joined, AWS-spec order.
708
+ var signedHeaderKeys = [];
709
+ for (var hk in headers) signedHeaderKeys.push(hk);
710
+ signedHeaderKeys.sort();
711
+ var signedHeadersStr = signedHeaderKeys.join(";");
712
+
713
+ // Query-string SigV4: presigning carries auth in the URL itself.
714
+ // The set order doesn't matter — canonicalQueryString sorts the
715
+ // params before hashing.
716
+ url.searchParams.set("X-Amz-Algorithm", ALGORITHM);
717
+ url.searchParams.set("X-Amz-Credential", config.accessKeyId + "/" + credentialScope);
718
+ url.searchParams.set("X-Amz-Date", amzDate);
719
+ url.searchParams.set("X-Amz-Expires", String(expiresIn));
720
+ url.searchParams.set("X-Amz-SignedHeaders", signedHeadersStr);
721
+ if (config.sessionToken) {
722
+ url.searchParams.set("X-Amz-Security-Token", config.sessionToken);
723
+ }
724
+
725
+ // Payload hash for query-string presigning is the literal string
726
+ // "UNSIGNED-PAYLOAD" — the body is not part of the signature.
727
+ var canon = canonicalRequest(method, url, headers, "UNSIGNED-PAYLOAD");
728
+ var sts = stringToSign(amzDate, credentialScope, canon);
729
+ var signingKey = deriveSigningKey(config.secretAccessKey, dateStamp, config.region, SERVICE);
730
+ var signature = nodeCrypto.createHmac("sha256", signingKey).update(sts).digest("hex");
731
+
732
+ url.searchParams.set("X-Amz-Signature", signature);
733
+
734
+ var clientHeaders = {};
735
+ if (opts.contentType) clientHeaders["Content-Type"] = opts.contentType;
736
+
737
+ return {
738
+ url: url.toString(),
739
+ method: method,
740
+ headers: clientHeaders,
741
+ expiresAt: date.getTime() + C.TIME.seconds(expiresIn),
742
+ };
743
+ }
744
+
745
+ function presignedUploadUrl(opts) { return _presign("PUT", opts); }
746
+ function presignedDownloadUrl(opts) { return _presign("GET", opts); }
747
+
748
+ // POST-form policy presigning (S3-compatible). Unlike query-string
749
+ // PUT presigning, this lets the operator enforce body-size at the
750
+ // upload endpoint via the `content-length-range` policy condition.
751
+ // Clients upload via multipart/form-data POST to the returned URL,
752
+ // attaching every key in `fields` plus the file as the last field.
753
+ //
754
+ // Reference: AWS S3 "Browser-Based Uploads Using POST".
755
+ function presignedUploadPolicy(opts) {
756
+ opts = opts || {};
757
+ if (!opts.key || typeof opts.key !== "string") {
758
+ throw _err("INVALID_KEY", "presignedUploadPolicy: key is required", true);
759
+ }
760
+ if (opts.key.indexOf("\0") !== -1) {
761
+ throw _err("INVALID_KEY", "null byte in key", true);
762
+ }
763
+ if (typeof opts.maxBytes !== "number" || !Number.isFinite(opts.maxBytes) ||
764
+ opts.maxBytes <= 0) {
765
+ throw _err("INVALID_MAX_BYTES",
766
+ "presignedUploadPolicy: maxBytes (positive number of bytes) is required — " +
767
+ "POST-form policy enforces body size via the content-length-range condition; " +
768
+ "use presignedUploadUrl if size enforcement is not needed", true);
769
+ }
770
+ if (opts.minBytes !== undefined && !nb.isNonNegativeFiniteInt(opts.minBytes)) {
771
+ throw _err("INVALID_MIN_BYTES",
772
+ "presignedUploadPolicy: minBytes must be a non-negative finite integer; got " +
773
+ nb.shape(opts.minBytes), true);
774
+ }
775
+ var minBytes = opts.minBytes !== undefined ? opts.minBytes : 0;
776
+ var expiresIn = opts.expiresIn != null ? opts.expiresIn : PRESIGN_DEFAULT_EXPIRES_SECONDS;
777
+ if (typeof expiresIn !== "number" ||
778
+ expiresIn < PRESIGN_MIN_EXPIRES_SECONDS ||
779
+ expiresIn > PRESIGN_MAX_EXPIRES_SECONDS) {
780
+ throw _err("INVALID_EXPIRES",
781
+ "presignedUploadPolicy: expiresIn must be a number of seconds between " +
782
+ PRESIGN_MIN_EXPIRES_SECONDS + " and " + PRESIGN_MAX_EXPIRES_SECONDS +
783
+ " (7 days, SigV4 hard cap)", true);
784
+ }
785
+
786
+ var date = opts.date || new Date();
787
+ var amzDate = _formatAmzDate(date);
788
+ var dateStamp = _formatDateStamp(date);
789
+ var credentialScope = dateStamp + "/" + config.region + "/" + SERVICE + "/aws4_request";
790
+ var credential = config.accessKeyId + "/" + credentialScope;
791
+ var expirationIso = new Date(date.getTime() + C.TIME.seconds(expiresIn)).toISOString();
792
+
793
+ // Policy conditions. Each entry must MATCH a corresponding form
794
+ // field exactly OR be a structural constraint (content-length-range
795
+ // / starts-with). Order doesn't matter — S3 evaluates the set.
796
+ var conditions = [
797
+ { "bucket": config.bucket },
798
+ { "key": opts.key },
799
+ { "x-amz-algorithm": ALGORITHM },
800
+ { "x-amz-credential": credential },
801
+ { "x-amz-date": amzDate },
802
+ ["content-length-range", minBytes, opts.maxBytes],
803
+ ];
804
+ if (config.sessionToken) {
805
+ conditions.push({ "x-amz-security-token": config.sessionToken });
806
+ }
807
+ if (opts.contentType) {
808
+ conditions.push({ "content-type": opts.contentType });
809
+ }
810
+
811
+ var policy = { expiration: expirationIso, conditions: conditions };
812
+ var policyJson = JSON.stringify(policy);
813
+ var policyB64 = Buffer.from(policyJson, "utf8").toString("base64");
814
+
815
+ var signingKey = deriveSigningKey(config.secretAccessKey, dateStamp, config.region, SERVICE);
816
+ var signature = nodeCrypto.createHmac("sha256", signingKey).update(policyB64).digest("hex");
817
+
818
+ var fields = {
819
+ "key": opts.key,
820
+ "x-amz-algorithm": ALGORITHM,
821
+ "x-amz-credential": credential,
822
+ "x-amz-date": amzDate,
823
+ "policy": policyB64,
824
+ "x-amz-signature": signature,
825
+ };
826
+ if (config.sessionToken) fields["x-amz-security-token"] = config.sessionToken;
827
+ if (opts.contentType) fields["content-type"] = opts.contentType;
828
+
829
+ // The bucket-root URL — no key in the path, key is a form field.
830
+ var url = _bucketUrl();
831
+
832
+ return {
833
+ url: url.toString(),
834
+ method: "POST",
835
+ fields: fields,
836
+ expiresAt: date.getTime() + C.TIME.seconds(expiresIn),
837
+ maxBytes: opts.maxBytes,
838
+ enforcement: "content-length-range", // S3-side enforced
839
+ };
840
+ }
841
+
842
+ function list(prefix, opts) {
843
+ opts = opts || {};
844
+ var params = { "list-type": "2" };
845
+ if (prefix) params["prefix"] = prefix;
846
+ if (opts.maxResults) params["max-keys"] = String(opts.maxResults);
847
+ if (opts.continuationToken) params["continuation-token"] = opts.continuationToken;
848
+
849
+ var url = _bucketUrl(params);
850
+ var headers = _makeSigned("GET", url, sha256Hex(Buffer.alloc(0)));
851
+ return _request("GET", url, headers, null, reqOpts).then(function (res) {
852
+ var doc = safeXml.parse(res.body, LIST_PARSE_OPTS);
853
+ var result = doc.ListBucketResult || {};
854
+ var contents = _arrayify(result.Contents);
855
+ var items = contents.map(function (c) {
856
+ return {
857
+ key: c.Key,
858
+ size: c.Size != null ? parseInt(c.Size, 10) : null,
859
+ lastModified: c.LastModified ? Date.parse(c.LastModified) : null,
860
+ };
861
+ }).filter(function (it) { return it.key; });
862
+ return {
863
+ items: items,
864
+ truncated: result.IsTruncated === "true",
865
+ continuationToken: result.NextContinuationToken || null,
866
+ };
867
+ });
868
+ }
869
+
870
+ return {
871
+ protocol: "sigv4",
872
+ endpoint: endpoint,
873
+ bucket: config.bucket,
874
+ region: config.region,
875
+ pathStyle: pathStyle,
876
+ put: put,
877
+ get: get,
878
+ getStream: getStream,
879
+ getResponse: getResponse,
880
+ head: head,
881
+ delete: deleteKey,
882
+ list: list,
883
+ presignedUploadUrl: presignedUploadUrl,
884
+ presignedDownloadUrl: presignedDownloadUrl,
885
+ presignedUploadPolicy: presignedUploadPolicy,
886
+ };
887
+ }
888
+
889
+ module.exports = {
890
+ create: create,
891
+ signRequest: signRequest,
892
+ canonicalRequest: canonicalRequest,
893
+ stringToSign: stringToSign,
894
+ deriveSigningKey: deriveSigningKey,
895
+ canonicalQueryString: canonicalQueryString,
896
+ canonicalHeaders: canonicalHeaders,
897
+ awsUriEncode: awsUriEncode,
898
+ sha256Hex: sha256Hex,
899
+ formatAmzDate: _formatAmzDate,
900
+ formatDateStamp: _formatDateStamp,
901
+ SERVICE: SERVICE,
902
+ ALGORITHM: ALGORITHM,
903
+ };