@needmoretruth/nmts-cli 0.45.0 → 0.47.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/AGENTS.md +8 -8
- package/CHANGELOG.md +78 -0
- package/README.ko.md +6 -3
- package/README.md +31 -11
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +19 -1
- package/dist/args.js +14 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/autonomy.js +3 -3
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +15 -0
- package/dist/commands/handover.js +376 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/link.d.ts +12 -0
- package/dist/commands/link.js +121 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code-manage.d.ts +24 -0
- package/dist/commands/public-code-manage.js +217 -0
- package/dist/commands/public-code.d.ts +12 -1
- package/dist/commands/public-code.js +140 -45
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +14 -8
- package/dist/commands/s3.js +44 -10
- package/dist/commands/share-dispatch.js +14 -1
- package/dist/commands/share.d.ts +2 -0
- package/dist/commands/share.js +45 -35
- package/dist/commands/shares-sent.js +8 -3
- package/dist/crypto-surface.d.ts +35 -0
- package/dist/crypto-surface.js +13 -0
- package/dist/crypto.d.ts +8 -0
- package/dist/crypto.js +8 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/handover-codes.d.ts +25 -0
- package/dist/handover-codes.js +106 -0
- package/dist/handover.d.ts +62 -0
- package/dist/handover.js +217 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +13 -4
- package/dist/help.js +24 -3
- package/dist/links.d.ts +58 -0
- package/dist/links.js +206 -0
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +13 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/reads.js +20 -6
- package/dist/mcp-tools/share.js +47 -1
- package/dist/mcp-tools/tiers.js +16 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +23 -0
- package/dist/portable.js +19 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/public-code-activity.d.ts +35 -0
- package/dist/public-code-activity.js +88 -0
- package/dist/public-code-refusals.d.ts +25 -0
- package/dist/public-code-refusals.js +87 -0
- package/dist/public-codes.d.ts +76 -0
- package/dist/public-codes.js +182 -0
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +55 -1
- package/dist/risk.js +55 -3
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share-codes.d.ts +40 -0
- package/dist/share-codes.js +151 -0
- package/dist/share.d.ts +39 -0
- package/dist/share.js +54 -13
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/public-link.d.ts +41 -0
- package/dist/shared/lib/share/public-link.js +107 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +9 -4
- package/dist/upload-wallet-put.js +16 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/handover.md +54 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/link.md +27 -0
- package/docs/commands/mcp.md +2 -1
- package/docs/commands/public-code.md +25 -6
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +8 -3
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BodyRefusal, type Plan } from "./body-plan.ts";
|
|
2
|
+
import { type ChecksumAlgorithm } from "./checksum.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Takes `aws-chunked` framing off a body, checking each chunk signature and the trailer on the way.
|
|
5
|
+
*
|
|
6
|
+
* The format, per chunk: `<size in hex>[;chunk-signature=<64 hex>]\r\n<size bytes>\r\n`. A chunk of
|
|
7
|
+
* size 0 ends the data; after it comes `\r\n` (chunk-signed), or trailer lines `name:value\r\n` --
|
|
8
|
+
* followed, when chunk-signed, by `x-amz-trailer-signature:<64 hex>\r\n` -- and a last `\r\n`.
|
|
9
|
+
*
|
|
10
|
+
* ⚠ LENIENT IN EXACTLY ONE PLACE: the body may end without the final empty line once the last
|
|
11
|
+
* chunk and every promised trailer are in. The AWS SDK for JavaScript sends `0\r\n` and nothing
|
|
12
|
+
* more when it has no checksum to add; the bytes and their checks are complete by then, and a
|
|
13
|
+
* refusal would be over a line that carries nothing.
|
|
14
|
+
*/
|
|
15
|
+
export declare class Deframer {
|
|
16
|
+
private state;
|
|
17
|
+
private line;
|
|
18
|
+
private lineBytes;
|
|
19
|
+
private trailerBytes;
|
|
20
|
+
private remaining;
|
|
21
|
+
private crlf;
|
|
22
|
+
private decoded;
|
|
23
|
+
private chunkHash;
|
|
24
|
+
private chunkSignature;
|
|
25
|
+
private previous;
|
|
26
|
+
private trailerSignature;
|
|
27
|
+
private readonly trailerPairs;
|
|
28
|
+
readonly trailerValues: Map<ChecksumAlgorithm, Buffer<ArrayBufferLike>>;
|
|
29
|
+
private readonly plan;
|
|
30
|
+
private readonly signed;
|
|
31
|
+
private readonly emit;
|
|
32
|
+
constructor(plan: Plan, emit: (bytes: Buffer) => void);
|
|
33
|
+
feed(chunk: Buffer): BodyRefusal | null;
|
|
34
|
+
/** The body has ended: was it whole? */
|
|
35
|
+
finish(): BodyRefusal | null;
|
|
36
|
+
private takeLine;
|
|
37
|
+
private onChunkHeader;
|
|
38
|
+
private hmac;
|
|
39
|
+
/** One link of the chain: this chunk's signature over its bytes and the signature before it. */
|
|
40
|
+
private checkChunk;
|
|
41
|
+
private onTrailerLine;
|
|
42
|
+
/**
|
|
43
|
+
* Every promised trailer is in; for a chunk-signed body, the trailer signature holds.
|
|
44
|
+
*
|
|
45
|
+
* The string it signs is `AWS4-HMAC-SHA256-TRAILER`, the timestamp, the scope, the last chunk's
|
|
46
|
+
* signature, and the SHA-256 of the trailer headers as `name:value\n`, sorted by name.
|
|
47
|
+
*/
|
|
48
|
+
private closeTrailer;
|
|
49
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// The `aws-chunked` framing taken off a body, with each chunk signature and the trailer checked on
|
|
2
|
+
// the way. One of the parts of `body.ts`; only it imports this.
|
|
3
|
+
import { createHash, createHmac, timingSafeEqual } from "node:crypto";
|
|
4
|
+
import { BodyRefusal, incomplete, invalid } from "./body-plan.js";
|
|
5
|
+
import { algorithmOfHeader, checksumHeaderName, parseChecksumValue } from "./checksum.js";
|
|
6
|
+
const EMPTY_SHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
7
|
+
/** A chunk header or trailer line longer than this is not one a client wrote. */
|
|
8
|
+
const MAX_LINE_BYTES = 4096;
|
|
9
|
+
/** Nor is a trailer section longer than this: S3's trailers are a checksum and a signature. */
|
|
10
|
+
const MAX_TRAILER_BYTES = 16 * 1024;
|
|
11
|
+
function sameHex(a, b) {
|
|
12
|
+
const left = Buffer.from(a, "utf8");
|
|
13
|
+
const right = Buffer.from(b, "utf8");
|
|
14
|
+
return left.length === right.length && timingSafeEqual(left, right);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Takes `aws-chunked` framing off a body, checking each chunk signature and the trailer on the way.
|
|
18
|
+
*
|
|
19
|
+
* The format, per chunk: `<size in hex>[;chunk-signature=<64 hex>]\r\n<size bytes>\r\n`. A chunk of
|
|
20
|
+
* size 0 ends the data; after it comes `\r\n` (chunk-signed), or trailer lines `name:value\r\n` --
|
|
21
|
+
* followed, when chunk-signed, by `x-amz-trailer-signature:<64 hex>\r\n` -- and a last `\r\n`.
|
|
22
|
+
*
|
|
23
|
+
* ⚠ LENIENT IN EXACTLY ONE PLACE: the body may end without the final empty line once the last
|
|
24
|
+
* chunk and every promised trailer are in. The AWS SDK for JavaScript sends `0\r\n` and nothing
|
|
25
|
+
* more when it has no checksum to add; the bytes and their checks are complete by then, and a
|
|
26
|
+
* refusal would be over a line that carries nothing.
|
|
27
|
+
*/
|
|
28
|
+
export class Deframer {
|
|
29
|
+
state = "size";
|
|
30
|
+
line = [];
|
|
31
|
+
lineBytes = 0;
|
|
32
|
+
trailerBytes = 0;
|
|
33
|
+
remaining = 0;
|
|
34
|
+
crlf = 0;
|
|
35
|
+
decoded = 0;
|
|
36
|
+
chunkHash = null;
|
|
37
|
+
chunkSignature = "";
|
|
38
|
+
previous;
|
|
39
|
+
trailerSignature = null;
|
|
40
|
+
trailerPairs = [];
|
|
41
|
+
trailerValues = new Map();
|
|
42
|
+
plan;
|
|
43
|
+
signed;
|
|
44
|
+
emit;
|
|
45
|
+
constructor(plan, emit) {
|
|
46
|
+
this.plan = plan;
|
|
47
|
+
this.signed = plan.framing === "signed" || plan.framing === "signed-trailer";
|
|
48
|
+
this.emit = emit;
|
|
49
|
+
this.previous = plan.signing.seedSignature;
|
|
50
|
+
}
|
|
51
|
+
feed(chunk) {
|
|
52
|
+
let at = 0;
|
|
53
|
+
while (at < chunk.length) {
|
|
54
|
+
switch (this.state) {
|
|
55
|
+
case "size":
|
|
56
|
+
case "trailer": {
|
|
57
|
+
const newline = chunk.indexOf(0x0a, at);
|
|
58
|
+
const end = newline < 0 ? chunk.length : newline;
|
|
59
|
+
if (this.state === "trailer") {
|
|
60
|
+
this.trailerBytes += end - at + (newline < 0 ? 0 : 1);
|
|
61
|
+
if (this.trailerBytes > MAX_TRAILER_BYTES)
|
|
62
|
+
return invalid("the trailer is longer than any client writes");
|
|
63
|
+
}
|
|
64
|
+
this.lineBytes += end - at;
|
|
65
|
+
if (this.lineBytes > MAX_LINE_BYTES)
|
|
66
|
+
return invalid("a chunk header or trailer line is longer than any client writes");
|
|
67
|
+
if (end > at)
|
|
68
|
+
this.line.push(chunk.subarray(at, end));
|
|
69
|
+
if (newline < 0)
|
|
70
|
+
return null;
|
|
71
|
+
at = newline + 1;
|
|
72
|
+
const text = this.takeLine();
|
|
73
|
+
const refused = this.state === "size"
|
|
74
|
+
? this.onChunkHeader(text)
|
|
75
|
+
: this.onTrailerLine(text.endsWith("\r") ? text.slice(0, -1) : text);
|
|
76
|
+
if (refused !== null)
|
|
77
|
+
return refused;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "data": {
|
|
81
|
+
const take = Math.min(this.remaining, chunk.length - at);
|
|
82
|
+
const piece = chunk.subarray(at, at + take);
|
|
83
|
+
at += take;
|
|
84
|
+
this.remaining -= take;
|
|
85
|
+
this.chunkHash?.update(piece);
|
|
86
|
+
this.emit(piece);
|
|
87
|
+
if (this.remaining === 0) {
|
|
88
|
+
if (this.chunkHash !== null) {
|
|
89
|
+
const refused = this.checkChunk(this.chunkHash.digest("hex"));
|
|
90
|
+
this.chunkHash = null;
|
|
91
|
+
if (refused !== null)
|
|
92
|
+
return refused;
|
|
93
|
+
}
|
|
94
|
+
this.state = "data-end";
|
|
95
|
+
this.crlf = 0;
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case "data-end":
|
|
100
|
+
case "final-crlf": {
|
|
101
|
+
const expected = this.crlf === 0 ? 0x0d : 0x0a;
|
|
102
|
+
if (chunk[at] !== expected) {
|
|
103
|
+
return invalid(this.state === "data-end"
|
|
104
|
+
? "a chunk's bytes are not followed by CRLF, so its size does not match its data"
|
|
105
|
+
: "the last chunk is not followed by CRLF");
|
|
106
|
+
}
|
|
107
|
+
at += 1;
|
|
108
|
+
this.crlf += 1;
|
|
109
|
+
if (this.crlf === 2)
|
|
110
|
+
this.state = this.state === "data-end" ? "size" : "end";
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "end":
|
|
114
|
+
return invalid("bytes follow the end of the aws-chunked body");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
/** The body has ended: was it whole? */
|
|
120
|
+
finish() {
|
|
121
|
+
switch (this.state) {
|
|
122
|
+
case "size":
|
|
123
|
+
case "data":
|
|
124
|
+
case "data-end":
|
|
125
|
+
return incomplete("the body ended before its last chunk");
|
|
126
|
+
case "final-crlf":
|
|
127
|
+
case "end":
|
|
128
|
+
return null;
|
|
129
|
+
case "trailer": {
|
|
130
|
+
if (this.lineBytes > 0) {
|
|
131
|
+
const text = this.takeLine();
|
|
132
|
+
const line = text.endsWith("\r") ? text.slice(0, -1) : text;
|
|
133
|
+
// The final empty line, cut short of its LF: closing the trailer is all it would do.
|
|
134
|
+
if (line.length === 0)
|
|
135
|
+
return this.onTrailerLine(line);
|
|
136
|
+
const refused = this.onTrailerLine(line);
|
|
137
|
+
if (refused !== null)
|
|
138
|
+
return refused;
|
|
139
|
+
}
|
|
140
|
+
return this.closeTrailer();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
takeLine() {
|
|
145
|
+
const text = Buffer.concat(this.line).toString("latin1");
|
|
146
|
+
this.line = [];
|
|
147
|
+
this.lineBytes = 0;
|
|
148
|
+
return text;
|
|
149
|
+
}
|
|
150
|
+
onChunkHeader(raw) {
|
|
151
|
+
if (!raw.endsWith("\r"))
|
|
152
|
+
return invalid("a chunk header does not end in CRLF");
|
|
153
|
+
const text = raw.slice(0, -1);
|
|
154
|
+
const semicolon = text.indexOf(";");
|
|
155
|
+
const sizeText = semicolon < 0 ? text : text.slice(0, semicolon);
|
|
156
|
+
if (!/^[0-9a-fA-F]{1,13}$/.test(sizeText))
|
|
157
|
+
return invalid("a chunk does not start with its size in hex");
|
|
158
|
+
const size = parseInt(sizeText, 16);
|
|
159
|
+
if (this.signed) {
|
|
160
|
+
const match = semicolon < 0 ? null : /^chunk-signature=([0-9a-f]{64})$/.exec(text.slice(semicolon + 1));
|
|
161
|
+
const signature = match?.[1];
|
|
162
|
+
if (signature === undefined)
|
|
163
|
+
return invalid("a chunk of a chunk-signed body carries no chunk-signature");
|
|
164
|
+
this.chunkSignature = signature;
|
|
165
|
+
}
|
|
166
|
+
if (this.decoded + size > this.plan.size) {
|
|
167
|
+
return incomplete("the chunks carry more bytes than x-amz-decoded-content-length");
|
|
168
|
+
}
|
|
169
|
+
if (size === 0) {
|
|
170
|
+
if (this.signed) {
|
|
171
|
+
const refused = this.checkChunk(EMPTY_SHA256);
|
|
172
|
+
if (refused !== null)
|
|
173
|
+
return refused;
|
|
174
|
+
}
|
|
175
|
+
this.state = this.plan.framing === "signed" ? "final-crlf" : "trailer";
|
|
176
|
+
this.crlf = 0;
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
this.decoded += size;
|
|
180
|
+
this.remaining = size;
|
|
181
|
+
this.chunkHash = this.signed ? createHash("sha256") : null;
|
|
182
|
+
this.state = "data";
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
hmac(stringToSign) {
|
|
186
|
+
return createHmac("sha256", this.plan.signing.key).update(stringToSign).digest("hex");
|
|
187
|
+
}
|
|
188
|
+
/** One link of the chain: this chunk's signature over its bytes and the signature before it. */
|
|
189
|
+
checkChunk(dataHash) {
|
|
190
|
+
const { stamp, scope } = this.plan.signing;
|
|
191
|
+
const expected = this.hmac(["AWS4-HMAC-SHA256-PAYLOAD", stamp, scope, this.previous, EMPTY_SHA256, dataHash].join("\n"));
|
|
192
|
+
if (!sameHex(expected, this.chunkSignature)) {
|
|
193
|
+
return new BodyRefusal(403, "SignatureDoesNotMatch", "a chunk's signature does not match its bytes");
|
|
194
|
+
}
|
|
195
|
+
this.previous = expected;
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
onTrailerLine(line) {
|
|
199
|
+
if (line.length === 0) {
|
|
200
|
+
this.state = "end";
|
|
201
|
+
return this.closeTrailer();
|
|
202
|
+
}
|
|
203
|
+
if (this.trailerSignature !== null)
|
|
204
|
+
return invalid("only the end of the body may follow the trailer signature");
|
|
205
|
+
const colon = line.indexOf(":");
|
|
206
|
+
if (colon < 0)
|
|
207
|
+
return invalid("a trailer line is not name:value");
|
|
208
|
+
const name = line.slice(0, colon).trim().toLowerCase();
|
|
209
|
+
const value = line.slice(colon + 1).trim();
|
|
210
|
+
if (name === "x-amz-trailer-signature") {
|
|
211
|
+
if (this.plan.framing !== "signed-trailer")
|
|
212
|
+
return invalid("an unsigned trailer carries a trailer signature");
|
|
213
|
+
if (!/^[0-9a-f]{64}$/.test(value))
|
|
214
|
+
return invalid("the trailer signature is not 64 hex digits");
|
|
215
|
+
this.trailerSignature = value;
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
const algorithm = algorithmOfHeader(name);
|
|
219
|
+
if (algorithm === null || !this.plan.trailers.includes(algorithm)) {
|
|
220
|
+
return invalid(`the trailer ${name} was not declared in x-amz-trailer`);
|
|
221
|
+
}
|
|
222
|
+
if (this.trailerValues.has(algorithm))
|
|
223
|
+
return invalid(`the trailer ${name} arrives twice`);
|
|
224
|
+
const parsed = parseChecksumValue(algorithm, value);
|
|
225
|
+
if (parsed === null)
|
|
226
|
+
return invalid(`the trailer ${name} is not a ${algorithm} value`);
|
|
227
|
+
this.trailerValues.set(algorithm, parsed);
|
|
228
|
+
this.trailerPairs.push([name, value]);
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Every promised trailer is in; for a chunk-signed body, the trailer signature holds.
|
|
233
|
+
*
|
|
234
|
+
* The string it signs is `AWS4-HMAC-SHA256-TRAILER`, the timestamp, the scope, the last chunk's
|
|
235
|
+
* signature, and the SHA-256 of the trailer headers as `name:value\n`, sorted by name.
|
|
236
|
+
*/
|
|
237
|
+
closeTrailer() {
|
|
238
|
+
for (const algorithm of this.plan.trailers) {
|
|
239
|
+
if (!this.trailerValues.has(algorithm)) {
|
|
240
|
+
return invalid(`x-amz-trailer promised ${checksumHeaderName(algorithm)} and the body ended without it`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (this.plan.framing !== "signed-trailer")
|
|
244
|
+
return null;
|
|
245
|
+
if (this.trailerSignature === null)
|
|
246
|
+
return invalid("a chunk-signed trailer carries no x-amz-trailer-signature");
|
|
247
|
+
const canonical = [...this.trailerPairs]
|
|
248
|
+
.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))
|
|
249
|
+
.map(([name, value]) => `${name}:${value}\n`)
|
|
250
|
+
.join("");
|
|
251
|
+
const { stamp, scope } = this.plan.signing;
|
|
252
|
+
const expected = this.hmac([
|
|
253
|
+
"AWS4-HMAC-SHA256-TRAILER",
|
|
254
|
+
stamp,
|
|
255
|
+
scope,
|
|
256
|
+
this.previous,
|
|
257
|
+
createHash("sha256").update(canonical).digest("hex"),
|
|
258
|
+
].join("\n"));
|
|
259
|
+
if (!sameHex(expected, this.trailerSignature)) {
|
|
260
|
+
return new BodyRefusal(403, "SignatureDoesNotMatch", "the trailer signature does not match the trailer");
|
|
261
|
+
}
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BodyRefusal, type Plan } from "./body-plan.ts";
|
|
2
|
+
import { type ChecksumAlgorithm } from "./checksum.ts";
|
|
3
|
+
/** The digests and checksums of the decoded bytes, computed as they pass. */
|
|
4
|
+
export declare class Measure {
|
|
5
|
+
count: number;
|
|
6
|
+
private readonly sha256;
|
|
7
|
+
private readonly md5;
|
|
8
|
+
private readonly checksums;
|
|
9
|
+
constructor(plan: Plan);
|
|
10
|
+
update(bytes: Buffer): void;
|
|
11
|
+
/** The verdict on the whole body, in the order a client would want the first failure named. */
|
|
12
|
+
judge(plan: Plan, trailerValues: ReadonlyMap<ChecksumAlgorithm, Buffer>): BodyRefusal | null;
|
|
13
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// The digests and checksums of an upload's decoded bytes, and the verdict on them once the last
|
|
2
|
+
// byte is in. One of the parts of `body.ts`; only it imports this.
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { BodyRefusal, incomplete, invalid } from "./body-plan.js";
|
|
5
|
+
import { checksumHeaderName, runningChecksum } from "./checksum.js";
|
|
6
|
+
/** The digests and checksums of the decoded bytes, computed as they pass. */
|
|
7
|
+
export class Measure {
|
|
8
|
+
count = 0;
|
|
9
|
+
sha256;
|
|
10
|
+
md5;
|
|
11
|
+
checksums = new Map();
|
|
12
|
+
constructor(plan) {
|
|
13
|
+
this.sha256 = plan.payloadDigest === null ? null : createHash("sha256");
|
|
14
|
+
this.md5 = plan.contentMd5 === null ? null : createHash("md5");
|
|
15
|
+
for (const algorithm of plan.headerChecksums.keys())
|
|
16
|
+
this.checksums.set(algorithm, runningChecksum(algorithm));
|
|
17
|
+
for (const algorithm of plan.trailers)
|
|
18
|
+
this.checksums.set(algorithm, runningChecksum(algorithm));
|
|
19
|
+
}
|
|
20
|
+
update(bytes) {
|
|
21
|
+
this.count += bytes.length;
|
|
22
|
+
this.sha256?.update(bytes);
|
|
23
|
+
this.md5?.update(bytes);
|
|
24
|
+
for (const running of this.checksums.values())
|
|
25
|
+
running.update(bytes);
|
|
26
|
+
}
|
|
27
|
+
/** The verdict on the whole body, in the order a client would want the first failure named. */
|
|
28
|
+
judge(plan, trailerValues) {
|
|
29
|
+
if (this.count !== plan.size) {
|
|
30
|
+
return incomplete(`the body has ${this.count} bytes and the request declared ${plan.size}`);
|
|
31
|
+
}
|
|
32
|
+
if (this.sha256 !== null && this.sha256.digest("hex") !== plan.payloadDigest) {
|
|
33
|
+
return new BodyRefusal(400, "XAmzContentSHA256Mismatch", "The provided 'x-amz-content-sha256' header does not match what was computed.");
|
|
34
|
+
}
|
|
35
|
+
if (this.md5 !== null && plan.contentMd5 !== null && !this.md5.digest().equals(plan.contentMd5)) {
|
|
36
|
+
return new BodyRefusal(400, "BadDigest", "The Content-MD5 you specified did not match what we received.");
|
|
37
|
+
}
|
|
38
|
+
for (const [algorithm, running] of this.checksums) {
|
|
39
|
+
const expected = plan.headerChecksums.get(algorithm) ?? trailerValues.get(algorithm);
|
|
40
|
+
if (expected === undefined)
|
|
41
|
+
return invalid(`${checksumHeaderName(algorithm)} never arrived`);
|
|
42
|
+
if (!running.digest().equals(expected)) {
|
|
43
|
+
return new BodyRefusal(400, "BadDigest", `The ${algorithm.toUpperCase()} you specified did not match the calculated checksum.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
import { type ChecksumAlgorithm } from "./checksum.ts";
|
|
3
|
+
import { type SigningContext, type VerifiedAgainst } from "./sigv4.ts";
|
|
4
|
+
/** Why a body cannot be used, as the S3 error a client expects. */
|
|
5
|
+
export declare class BodyRefusal extends Error {
|
|
6
|
+
readonly status: number;
|
|
7
|
+
readonly code: string;
|
|
8
|
+
constructor(status: number, code: string, message: string);
|
|
9
|
+
}
|
|
10
|
+
export type Framing = "plain" | "signed" | "signed-trailer" | "unsigned-trailer";
|
|
11
|
+
/** Everything the headers promised, read before the first byte of the body. */
|
|
12
|
+
export interface Plan {
|
|
13
|
+
readonly framing: Framing;
|
|
14
|
+
readonly size: number;
|
|
15
|
+
/** The hex SHA-256 a plain body was signed with; null for `UNSIGNED-PAYLOAD` and framed bodies. */
|
|
16
|
+
readonly payloadDigest: string | null;
|
|
17
|
+
readonly contentMd5: Buffer | null;
|
|
18
|
+
/** `x-amz-checksum-*` values that arrived as headers. */
|
|
19
|
+
readonly headerChecksums: ReadonlyMap<ChecksumAlgorithm, Buffer>;
|
|
20
|
+
/** The checksums `x-amz-trailer` says will follow the last chunk. */
|
|
21
|
+
readonly trailers: readonly ChecksumAlgorithm[];
|
|
22
|
+
readonly signing: SigningContext;
|
|
23
|
+
}
|
|
24
|
+
export declare function invalid(message: string): BodyRefusal;
|
|
25
|
+
export declare function incomplete(message: string): BodyRefusal;
|
|
26
|
+
/** Read every promise the headers make, or say which one cannot be kept. */
|
|
27
|
+
export declare function planOf(req: IncomingMessage, verdict: Extract<VerifiedAgainst, {
|
|
28
|
+
ok: true;
|
|
29
|
+
}>,
|
|
30
|
+
/** `x-amz-checksum-*` headers are the object's, not this body's, and are not held against it. */
|
|
31
|
+
checksumsDescribeObject?: boolean): Plan | BodyRefusal;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// What an upload's headers promise about its body, read before the first byte of it: the framing,
|
|
2
|
+
// the object's size, and every digest and checksum the bytes will be held to. One of the parts of
|
|
3
|
+
// `body.ts`; only it and its other parts import this.
|
|
4
|
+
//
|
|
5
|
+
// ⚠ WHAT DECIDES THE FRAMING IS THE SIGNED PAYLOAD HASH, not `content-encoding`. The hash is inside
|
|
6
|
+
// the signature and says exactly one thing; `content-encoding` is often unsigned and may list
|
|
7
|
+
// several codings. A body that says `aws-chunked` while the signature declares a plain payload is
|
|
8
|
+
// refused, because reading it either way would be guessing which half of the request lied.
|
|
9
|
+
import { algorithmOfHeader, algorithmOfName, parseChecksumValue } from "./checksum.js";
|
|
10
|
+
import { STREAMING_PAYLOAD, STREAMING_PAYLOAD_TRAILER, STREAMING_UNSIGNED_PAYLOAD_TRAILER, UNSIGNED_PAYLOAD, } from "./sigv4.js";
|
|
11
|
+
/** Why a body cannot be used, as the S3 error a client expects. */
|
|
12
|
+
export class BodyRefusal extends Error {
|
|
13
|
+
status;
|
|
14
|
+
code;
|
|
15
|
+
constructor(status, code, message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "BodyRefusal";
|
|
18
|
+
this.status = status;
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function invalid(message) {
|
|
23
|
+
return new BodyRefusal(400, "InvalidRequest", message);
|
|
24
|
+
}
|
|
25
|
+
export function incomplete(message) {
|
|
26
|
+
return new BodyRefusal(400, "IncompleteBody", message);
|
|
27
|
+
}
|
|
28
|
+
function headerOf(req, name) {
|
|
29
|
+
const raw = req.headers[name];
|
|
30
|
+
return Array.isArray(raw) ? raw.join(",") : raw;
|
|
31
|
+
}
|
|
32
|
+
/** A declared length: the number, undefined when absent, null when it is not a length. */
|
|
33
|
+
function lengthOf(value) {
|
|
34
|
+
if (value === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
const trimmed = value.trim();
|
|
37
|
+
if (!/^\d{1,16}$/.test(trimmed))
|
|
38
|
+
return null;
|
|
39
|
+
const n = Number(trimmed);
|
|
40
|
+
return Number.isSafeInteger(n) ? n : null;
|
|
41
|
+
}
|
|
42
|
+
function framingOf(payloadHash) {
|
|
43
|
+
if (/^[0-9a-f]{64}$/.test(payloadHash) || payloadHash === UNSIGNED_PAYLOAD)
|
|
44
|
+
return "plain";
|
|
45
|
+
if (payloadHash === STREAMING_PAYLOAD)
|
|
46
|
+
return "signed";
|
|
47
|
+
if (payloadHash === STREAMING_PAYLOAD_TRAILER)
|
|
48
|
+
return "signed-trailer";
|
|
49
|
+
if (payloadHash === STREAMING_UNSIGNED_PAYLOAD_TRAILER)
|
|
50
|
+
return "unsigned-trailer";
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
/** Read every promise the headers make, or say which one cannot be kept. */
|
|
54
|
+
export function planOf(req, verdict,
|
|
55
|
+
/** `x-amz-checksum-*` headers are the object's, not this body's, and are not held against it. */
|
|
56
|
+
checksumsDescribeObject = false) {
|
|
57
|
+
const framing = framingOf(verdict.payloadHash);
|
|
58
|
+
if (framing === null) {
|
|
59
|
+
return invalid("x-amz-content-sha256 is not a hex SHA-256, UNSIGNED-PAYLOAD or one of the STREAMING- values");
|
|
60
|
+
}
|
|
61
|
+
const encodings = (headerOf(req, "content-encoding") ?? "").split(",").map((e) => e.trim().toLowerCase());
|
|
62
|
+
if (framing === "plain" && encodings.includes("aws-chunked")) {
|
|
63
|
+
return invalid("content-encoding says aws-chunked and x-amz-content-sha256 declares a plain payload");
|
|
64
|
+
}
|
|
65
|
+
let size;
|
|
66
|
+
if (framing === "plain") {
|
|
67
|
+
const length = lengthOf(headerOf(req, "content-length"));
|
|
68
|
+
const decoded = lengthOf(headerOf(req, "x-amz-decoded-content-length"));
|
|
69
|
+
if (length === null || decoded === null)
|
|
70
|
+
return invalid("a length header is not a whole number of bytes");
|
|
71
|
+
// ⛔ TWO LENGTHS THAT DISAGREE ARE REFUSED, NOT RESOLVED. A plain body has one length; a request
|
|
72
|
+
// that states two different ones has lied in one of them, and taking either would be judging
|
|
73
|
+
// the bytes against a number the client may not have meant.
|
|
74
|
+
if (length !== undefined && decoded !== undefined && length !== decoded) {
|
|
75
|
+
return invalid("Content-Length and x-amz-decoded-content-length disagree on a body that is not aws-chunked");
|
|
76
|
+
}
|
|
77
|
+
const declared = length ?? decoded;
|
|
78
|
+
if (declared === undefined) {
|
|
79
|
+
return new BodyRefusal(411, "MissingContentLength", "The upload has no Content-Length.");
|
|
80
|
+
}
|
|
81
|
+
size = declared;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// ⛔ REQUIRED, AS S3 REQUIRES IT. `content-length` of a framed body counts the framing, so
|
|
85
|
+
// without this the object's size is unknown until the last chunk -- and the one check that
|
|
86
|
+
// catches a body cut short at a chunk boundary is the count against it.
|
|
87
|
+
const decoded = lengthOf(headerOf(req, "x-amz-decoded-content-length"));
|
|
88
|
+
if (decoded === null)
|
|
89
|
+
return invalid("x-amz-decoded-content-length is not a whole number of bytes");
|
|
90
|
+
if (decoded === undefined) {
|
|
91
|
+
return new BodyRefusal(411, "MissingContentLength", "An aws-chunked upload needs x-amz-decoded-content-length.");
|
|
92
|
+
}
|
|
93
|
+
size = decoded;
|
|
94
|
+
}
|
|
95
|
+
let contentMd5 = null;
|
|
96
|
+
const md5 = headerOf(req, "content-md5");
|
|
97
|
+
if (md5 !== undefined) {
|
|
98
|
+
const trimmed = md5.trim();
|
|
99
|
+
const bytes = /^[A-Za-z0-9+/]{22}==$/.test(trimmed) ? Buffer.from(trimmed, "base64") : null;
|
|
100
|
+
if (bytes === null || bytes.length !== 16) {
|
|
101
|
+
return new BodyRefusal(400, "InvalidDigest", "Content-MD5 is not the base64 of an MD5 digest.");
|
|
102
|
+
}
|
|
103
|
+
contentMd5 = bytes;
|
|
104
|
+
}
|
|
105
|
+
// ⛔ AN UNKNOWN CHECKSUM IS REFUSED, NOT SKIPPED. A client that sent one believes it is being
|
|
106
|
+
// checked; passing the upload would be telling it so.
|
|
107
|
+
const headerChecksums = new Map();
|
|
108
|
+
for (const [name, raw] of Object.entries(req.headers)) {
|
|
109
|
+
if (!name.startsWith("x-amz-checksum-"))
|
|
110
|
+
continue;
|
|
111
|
+
const value = Array.isArray(raw) ? raw.join(",") : (raw ?? "");
|
|
112
|
+
const suffix = name.slice("x-amz-checksum-".length);
|
|
113
|
+
if (suffix === "type" || suffix === "mode")
|
|
114
|
+
continue;
|
|
115
|
+
if (checksumsDescribeObject)
|
|
116
|
+
continue;
|
|
117
|
+
if (suffix === "algorithm") {
|
|
118
|
+
if (algorithmOfName(value) === null)
|
|
119
|
+
return invalid(`unknown checksum algorithm: ${value}`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const algorithm = algorithmOfHeader(name);
|
|
123
|
+
if (algorithm === null)
|
|
124
|
+
return invalid(`unknown checksum algorithm: ${name}`);
|
|
125
|
+
const parsed = parseChecksumValue(algorithm, value);
|
|
126
|
+
if (parsed === null)
|
|
127
|
+
return invalid(`${name} is not a ${algorithm} value`);
|
|
128
|
+
headerChecksums.set(algorithm, parsed);
|
|
129
|
+
}
|
|
130
|
+
const sdkAlgorithm = headerOf(req, "x-amz-sdk-checksum-algorithm");
|
|
131
|
+
if (sdkAlgorithm !== undefined && algorithmOfName(sdkAlgorithm) === null) {
|
|
132
|
+
return invalid(`unknown checksum algorithm: ${sdkAlgorithm}`);
|
|
133
|
+
}
|
|
134
|
+
const trailers = [];
|
|
135
|
+
for (const raw of (headerOf(req, "x-amz-trailer") ?? "").split(",")) {
|
|
136
|
+
const name = raw.trim().toLowerCase();
|
|
137
|
+
if (name.length === 0)
|
|
138
|
+
continue;
|
|
139
|
+
const algorithm = algorithmOfHeader(name);
|
|
140
|
+
if (algorithm === null)
|
|
141
|
+
return invalid(`x-amz-trailer names ${name}, which is not a checksum S3 knows`);
|
|
142
|
+
if (trailers.includes(algorithm) || headerChecksums.has(algorithm)) {
|
|
143
|
+
return invalid(`${name} is declared twice`);
|
|
144
|
+
}
|
|
145
|
+
trailers.push(algorithm);
|
|
146
|
+
}
|
|
147
|
+
if (trailers.length > 0 && (framing === "plain" || framing === "signed")) {
|
|
148
|
+
return invalid("x-amz-trailer names a trailer and x-amz-content-sha256 declares a payload without one");
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
framing,
|
|
152
|
+
size,
|
|
153
|
+
payloadDigest: /^[0-9a-f]{64}$/.test(verdict.payloadHash) ? verdict.payloadHash : null,
|
|
154
|
+
contentMd5,
|
|
155
|
+
headerChecksums,
|
|
156
|
+
trailers,
|
|
157
|
+
signing: verdict.signing,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
import { type Readable } from "node:stream";
|
|
3
|
+
import { BodyRefusal } from "./body-plan.ts";
|
|
4
|
+
import type { ChecksumAlgorithm } from "./checksum.ts";
|
|
5
|
+
import type { VerifiedAgainst } from "./sigv4.ts";
|
|
6
|
+
export { BodyRefusal } from "./body-plan.ts";
|
|
7
|
+
/** A request body, decoded. */
|
|
8
|
+
export interface DecodedBody {
|
|
9
|
+
/** The object's bytes, with any `aws-chunked` framing removed. */
|
|
10
|
+
readonly stream: Readable;
|
|
11
|
+
/**
|
|
12
|
+
* How many bytes the object will have: `x-amz-decoded-content-length` for a framed body,
|
|
13
|
+
* `content-length` otherwise. Null when the request declared neither.
|
|
14
|
+
*/
|
|
15
|
+
readonly size: number | null;
|
|
16
|
+
/**
|
|
17
|
+
* Resolves once the whole body has been read and every rule the request carried held: the signed
|
|
18
|
+
* payload digest, each chunk signature, the trailing or header checksum, `Content-MD5`.
|
|
19
|
+
* Rejects with a `BodyRefusal` otherwise.
|
|
20
|
+
*/
|
|
21
|
+
readonly verified: Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* The `x-amz-checksum-*` values the body was held to, from its headers or its trailer, once
|
|
24
|
+
* `verified` has resolved; empty before then. A piece of a multipart upload keeps them, so the
|
|
25
|
+
* finish can be checked against them.
|
|
26
|
+
*/
|
|
27
|
+
readonly checksums?: () => ReadonlyMap<ChecksumAlgorithm, Buffer>;
|
|
28
|
+
}
|
|
29
|
+
/** How one request's body is read. */
|
|
30
|
+
export interface DecodeOptions {
|
|
31
|
+
/**
|
|
32
|
+
* `x-amz-checksum-*` headers describe the OBJECT the request makes, not this body — true of
|
|
33
|
+
* `CompleteMultipartUpload`, whose body is the part list. They are then left to the caller
|
|
34
|
+
* rather than held against the body, which they would never match.
|
|
35
|
+
*/
|
|
36
|
+
readonly checksumsDescribeObject?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Decode one request body, or say at once why it cannot be.
|
|
40
|
+
*
|
|
41
|
+
* ⚠ THE STREAM MUST BE READ. It is fed as the request arrives and pauses the request when nobody
|
|
42
|
+
* reads it, so a caller that awaits `verified` without consuming `stream` waits forever.
|
|
43
|
+
*/
|
|
44
|
+
export declare function decodeBody(req: IncomingMessage, verdict: Extract<VerifiedAgainst, {
|
|
45
|
+
ok: true;
|
|
46
|
+
}>, options?: DecodeOptions): DecodedBody | BodyRefusal;
|