@needmoretruth/nmts-cli 0.44.0 → 0.46.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 +2 -2
- package/CHANGELOG.md +73 -0
- package/README.ko.md +6 -3
- package/README.md +15 -3
- 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 +14 -0
- package/dist/args.js +11 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/get.d.ts +2 -0
- package/dist/commands/get.js +13 -1
- package/dist/commands/handover.d.ts +13 -0
- package/dist/commands/handover.js +328 -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/ls.d.ts +7 -0
- package/dist/commands/ls.js +27 -6
- package/dist/commands/public-code.d.ts +6 -0
- package/dist/commands/public-code.js +46 -0
- 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-thumbnail.d.ts +4 -0
- package/dist/commands/put-thumbnail.js +119 -0
- package/dist/commands/put-wallet.d.ts +2 -0
- package/dist/commands/put-wallet.js +3 -2
- package/dist/commands/put.d.ts +10 -2
- package/dist/commands/put.js +13 -7
- package/dist/commands/receive.js +3 -3
- package/dist/commands/s3.js +44 -10
- package/dist/crypto-surface.d.ts +8 -0
- package/dist/crypto-surface.js +3 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.js +3 -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/drive-edit/trash.js +5 -3
- package/dist/drive-edit/tree.d.ts +6 -0
- package/dist/drive-edit/tree.js +10 -0
- package/dist/drive-erase.js +2 -2
- package/dist/drive-paths.d.ts +2 -2
- package/dist/drive-paths.js +2 -2
- package/dist/handover.d.ts +44 -0
- package/dist/handover.js +199 -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 +8 -2
- package/dist/help.js +16 -3
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +14 -9
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/files.js +21 -2
- package/dist/mcp-tools/reads.js +9 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +17 -1
- package/dist/portable.js +13 -1
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- 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 +30 -0
- package/dist/risk.js +23 -2
- 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.d.ts +5 -0
- package/dist/share.js +1 -1
- 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/manifest-codec.d.ts +11 -31
- package/dist/shared/lib/drive/manifest-codec.js +15 -23
- package/dist/shared/lib/drive/manifest-index.d.ts +10 -1
- package/dist/shared/lib/drive/manifest-index.js +30 -8
- package/dist/shared/lib/drive/manifest-wire-extras.d.ts +50 -0
- package/dist/shared/lib/drive/manifest-wire-extras.js +65 -0
- package/dist/shared/lib/drive/preview-classify.d.ts +17 -0
- package/dist/shared/lib/drive/preview-classify.js +96 -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/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 +11 -4
- package/dist/upload-wallet-put.js +17 -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/get.md +3 -0
- package/docs/commands/handover.md +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/ls.md +5 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +23 -1
- package/docs/commands/share.md +3 -1
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
|
@@ -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;
|
package/dist/s3/body.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// The bytes of an upload as the object will hold them: whatever framing the client wrapped them in
|
|
2
|
+
// taken off, and every integrity promise the request made checked before anything is stored.
|
|
3
|
+
//
|
|
4
|
+
// ⛔ ONE PLACE FOR EVERY WAY A BODY CAN ARRIVE. A plain body with a hex digest, `UNSIGNED-PAYLOAD`,
|
|
5
|
+
// a presigned URL, and the three `aws-chunked` forms (chunk-signed, chunk-signed with a trailer,
|
|
6
|
+
// unsigned with a trailer) all end here and leave as the same shape. The protocol layer never
|
|
7
|
+
// reads `content-encoding` or a trailer itself; a second reader would be a second place for the
|
|
8
|
+
// framing to be stored as somebody's file. `body-plan.ts` (what the headers promise),
|
|
9
|
+
// `body-deframe.ts` (the framing and its signatures) and `body-measure.ts` (digests and
|
|
10
|
+
// checksums) are this module's parts, and nothing else imports them.
|
|
11
|
+
//
|
|
12
|
+
// ⛔ `verified` IS THE GATE, NOT A REPORT. The stream can be spooled while it arrives, but nothing
|
|
13
|
+
// may be stored until `verified` resolves: a digest, a chunk signature or a checksum can only be
|
|
14
|
+
// judged once the last byte is in.
|
|
15
|
+
//
|
|
16
|
+
// ⛔ THE STREAM NEVER FAILS; `verified` DOES. When the body turns out to be wrong halfway, `verified`
|
|
17
|
+
// rejects first and the stream then simply ends, early. A caller that spools and awaits
|
|
18
|
+
// `verified` alongside therefore always sees the refusal with its S3 status and code, instead of
|
|
19
|
+
// a stream error it would have to translate -- or, worse, a short file and a success.
|
|
20
|
+
import { PassThrough } from "node:stream";
|
|
21
|
+
import { Deframer } from "./body-deframe.js";
|
|
22
|
+
import { Measure } from "./body-measure.js";
|
|
23
|
+
import { BodyRefusal, incomplete, planOf } from "./body-plan.js";
|
|
24
|
+
export { BodyRefusal } from "./body-plan.js";
|
|
25
|
+
/**
|
|
26
|
+
* Decode one request body, or say at once why it cannot be.
|
|
27
|
+
*
|
|
28
|
+
* ⚠ THE STREAM MUST BE READ. It is fed as the request arrives and pauses the request when nobody
|
|
29
|
+
* reads it, so a caller that awaits `verified` without consuming `stream` waits forever.
|
|
30
|
+
*/
|
|
31
|
+
export function decodeBody(req, verdict, options = {}) {
|
|
32
|
+
const plan = planOf(req, verdict, options.checksumsDescribeObject === true);
|
|
33
|
+
if (plan instanceof BodyRefusal)
|
|
34
|
+
return plan;
|
|
35
|
+
const held = new Map();
|
|
36
|
+
const out = new PassThrough();
|
|
37
|
+
const measure = new Measure(plan);
|
|
38
|
+
let settled = false;
|
|
39
|
+
let accept = () => undefined;
|
|
40
|
+
let reject = () => undefined;
|
|
41
|
+
const verified = new Promise((resolve, fail) => {
|
|
42
|
+
accept = resolve;
|
|
43
|
+
reject = fail;
|
|
44
|
+
});
|
|
45
|
+
// ⚠ Whoever awaits `verified` still sees the refusal; this only keeps an unawaited one -- a caller
|
|
46
|
+
// that gave up on the upload already -- from being reported as an unhandled rejection.
|
|
47
|
+
verified.catch(() => undefined);
|
|
48
|
+
const refuse = (refusal) => {
|
|
49
|
+
if (settled)
|
|
50
|
+
return;
|
|
51
|
+
settled = true;
|
|
52
|
+
reject(refusal);
|
|
53
|
+
if (!out.destroyed && !out.writableEnded)
|
|
54
|
+
out.end();
|
|
55
|
+
// The rest of the request is read and dropped, so the refusal can still be answered on this
|
|
56
|
+
// connection rather than the client seeing it reset mid-upload.
|
|
57
|
+
req.resume();
|
|
58
|
+
};
|
|
59
|
+
const emit = (bytes) => {
|
|
60
|
+
measure.update(bytes);
|
|
61
|
+
if (out.destroyed)
|
|
62
|
+
return;
|
|
63
|
+
if (!out.write(bytes))
|
|
64
|
+
req.pause();
|
|
65
|
+
};
|
|
66
|
+
out.on("drain", () => {
|
|
67
|
+
if (!settled)
|
|
68
|
+
req.resume();
|
|
69
|
+
});
|
|
70
|
+
out.on("close", () => {
|
|
71
|
+
if (!settled)
|
|
72
|
+
refuse(incomplete("the body's reader stopped before the end"));
|
|
73
|
+
});
|
|
74
|
+
const deframer = plan.framing === "plain" ? null : new Deframer(plan, emit);
|
|
75
|
+
req.on("data", (chunk) => {
|
|
76
|
+
if (settled)
|
|
77
|
+
return;
|
|
78
|
+
if (deframer === null) {
|
|
79
|
+
if (measure.count + chunk.length > plan.size) {
|
|
80
|
+
refuse(incomplete("the body is longer than its declared length"));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
emit(chunk);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const refused = deframer.feed(chunk);
|
|
87
|
+
if (refused !== null)
|
|
88
|
+
refuse(refused);
|
|
89
|
+
});
|
|
90
|
+
req.on("end", () => {
|
|
91
|
+
if (settled)
|
|
92
|
+
return;
|
|
93
|
+
const refused = deframer?.finish() ?? null;
|
|
94
|
+
const trailerValues = deframer?.trailerValues ?? new Map();
|
|
95
|
+
const verdictOnBytes = refused ?? measure.judge(plan, trailerValues);
|
|
96
|
+
if (verdictOnBytes !== null) {
|
|
97
|
+
refuse(verdictOnBytes);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
for (const [algorithm, value] of plan.headerChecksums)
|
|
101
|
+
held.set(algorithm, value);
|
|
102
|
+
for (const algorithm of plan.trailers) {
|
|
103
|
+
const value = trailerValues.get(algorithm);
|
|
104
|
+
if (value !== undefined)
|
|
105
|
+
held.set(algorithm, value);
|
|
106
|
+
}
|
|
107
|
+
settled = true;
|
|
108
|
+
accept();
|
|
109
|
+
out.end();
|
|
110
|
+
});
|
|
111
|
+
req.on("error", () => refuse(incomplete("the connection closed before the body was complete")));
|
|
112
|
+
req.on("close", () => {
|
|
113
|
+
if (!settled)
|
|
114
|
+
refuse(incomplete("the connection closed before the body was complete"));
|
|
115
|
+
});
|
|
116
|
+
return { stream: out, size: plan.size, verified, checksums: () => held };
|
|
117
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { DriveSource, GatewayOptions, WriteMeta } from "./contract.ts";
|
|
3
|
+
import type { GatewayCredential, VerifiedAgainst } from "./sigv4.ts";
|
|
4
|
+
export interface Call {
|
|
5
|
+
readonly req: IncomingMessage;
|
|
6
|
+
readonly res: ServerResponse;
|
|
7
|
+
readonly method: string;
|
|
8
|
+
readonly query: URLSearchParams;
|
|
9
|
+
readonly bucket: string;
|
|
10
|
+
readonly key: string;
|
|
11
|
+
/** The path as it arrived, which is what an error document names as its resource. */
|
|
12
|
+
readonly resource: string;
|
|
13
|
+
readonly verdict: Extract<VerifiedAgainst, {
|
|
14
|
+
ok: true;
|
|
15
|
+
}>;
|
|
16
|
+
readonly options: GatewayOptions;
|
|
17
|
+
readonly source: DriveSource;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What a caller signed with a pair it may not use here.
|
|
21
|
+
*
|
|
22
|
+
* ⛔ THE SAME ANSWER WHETHER OR NOT THE BUCKET EXISTS, which is why the restriction is checked
|
|
23
|
+
* before the resolver is asked. Answering `NoSuchBucket` for a name the caller may not touch
|
|
24
|
+
* would turn this gateway into a way of asking "does this business have a customer called…",
|
|
25
|
+
* one guess at a time.
|
|
26
|
+
*/
|
|
27
|
+
export declare const NOT_YOURS = "That access key may not use that bucket.";
|
|
28
|
+
/** Whether the pair that signed is allowed anywhere near this bucket name. */
|
|
29
|
+
export declare function mayTouch(credential: GatewayCredential, bucket: string): boolean;
|
|
30
|
+
export declare function headerOf(req: IncomingMessage, name: string): string | undefined;
|
|
31
|
+
/** What the client said about the object it is writing, as the drive is handed it. */
|
|
32
|
+
export declare function metaOf(req: IncomingMessage): WriteMeta;
|
|
33
|
+
/** The one sentence a write gets from `nmts s3` when this machine has not agreed to spending. */
|
|
34
|
+
export declare function readOnlyOnThisMachine(): string;
|
|
35
|
+
/** Why this drive takes no writes, in the words of whoever runs the gateway. */
|
|
36
|
+
export declare function readOnlyBecause(options: GatewayOptions): string;
|
package/dist/s3/call.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// One request as the operations see it: already signed for, already addressed, its drive resolved.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ EVERYTHING HERE HAS BEEN DECIDED BY `routes.ts` BEFORE AN OPERATION RUNS — who signed, which
|
|
4
|
+
// bucket and key, and that the pair may touch that bucket. An operation that needs a second
|
|
5
|
+
// bucket (a copy's source) asks `mayTouch` itself, and asks it before the resolver, for the
|
|
6
|
+
// reason `NOT_YOURS` gives.
|
|
7
|
+
/**
|
|
8
|
+
* What a caller signed with a pair it may not use here.
|
|
9
|
+
*
|
|
10
|
+
* ⛔ THE SAME ANSWER WHETHER OR NOT THE BUCKET EXISTS, which is why the restriction is checked
|
|
11
|
+
* before the resolver is asked. Answering `NoSuchBucket` for a name the caller may not touch
|
|
12
|
+
* would turn this gateway into a way of asking "does this business have a customer called…",
|
|
13
|
+
* one guess at a time.
|
|
14
|
+
*/
|
|
15
|
+
export const NOT_YOURS = "That access key may not use that bucket.";
|
|
16
|
+
/** Whether the pair that signed is allowed anywhere near this bucket name. */
|
|
17
|
+
export function mayTouch(credential, bucket) {
|
|
18
|
+
const only = credential.buckets;
|
|
19
|
+
return only === undefined || only.includes(bucket);
|
|
20
|
+
}
|
|
21
|
+
export function headerOf(req, name) {
|
|
22
|
+
const raw = req.headers[name];
|
|
23
|
+
return Array.isArray(raw) ? raw.join(",") : raw;
|
|
24
|
+
}
|
|
25
|
+
/** What the client said about the object it is writing, as the drive is handed it. */
|
|
26
|
+
export function metaOf(req) {
|
|
27
|
+
const storageClass = headerOf(req, "x-amz-storage-class")?.trim().toUpperCase();
|
|
28
|
+
return {
|
|
29
|
+
storageClass: storageClass === undefined || storageClass === "" ? null : storageClass,
|
|
30
|
+
contentType: headerOf(req, "content-type") ?? null,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** The one sentence a write gets from `nmts s3` when this machine has not agreed to spending. */
|
|
34
|
+
export function readOnlyOnThisMachine() {
|
|
35
|
+
return ("This gateway is read only. Uploading spends credits, and this machine has not agreed to " +
|
|
36
|
+
"spending — `nmts consent grant spend`, run by the person whose account this is, is what " +
|
|
37
|
+
"changes that. Nothing was written.");
|
|
38
|
+
}
|
|
39
|
+
/** Why this drive takes no writes, in the words of whoever runs the gateway. */
|
|
40
|
+
export function readOnlyBecause(options) {
|
|
41
|
+
return options.readOnlyBecause ?? readOnlyOnThisMachine();
|
|
42
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type ChecksumAlgorithm = "crc32" | "crc32c" | "crc64nvme" | "sha1" | "sha256";
|
|
2
|
+
export declare const CHECKSUM_ALGORITHMS: readonly ChecksumAlgorithm[];
|
|
3
|
+
/** `crc32c` → `x-amz-checksum-crc32c`. */
|
|
4
|
+
export declare function checksumHeaderName(algorithm: ChecksumAlgorithm): string;
|
|
5
|
+
/** `x-amz-checksum-crc32c` → `crc32c`; anything else → null. Case does not matter. */
|
|
6
|
+
export declare function algorithmOfHeader(name: string): ChecksumAlgorithm | null;
|
|
7
|
+
/** `CRC32C` (as `x-amz-sdk-checksum-algorithm` spells it) → `crc32c`; anything else → null. */
|
|
8
|
+
export declare function algorithmOfName(name: string): ChecksumAlgorithm | null;
|
|
9
|
+
/**
|
|
10
|
+
* The bytes a checksum header's value stands for, or null when it is not canonical base64 of the
|
|
11
|
+
* right length for its algorithm.
|
|
12
|
+
*
|
|
13
|
+
* ⚠ `Buffer.from(…, "base64")` accepts almost anything and silently drops what it cannot read, so
|
|
14
|
+
* the shape is checked first: a value that decodes "leniently" to the right length is still not
|
|
15
|
+
* a value a client computed.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseChecksumValue(algorithm: ChecksumAlgorithm, value: string): Buffer | null;
|
|
18
|
+
/** A checksum being computed while bytes stream past. */
|
|
19
|
+
export interface RunningChecksum {
|
|
20
|
+
update(bytes: Uint8Array): void;
|
|
21
|
+
/** The finished value, big-endian for the CRCs, as S3 encodes it before base64. */
|
|
22
|
+
digest(): Buffer;
|
|
23
|
+
}
|
|
24
|
+
export declare function runningChecksum(algorithm: ChecksumAlgorithm): RunningChecksum;
|
|
25
|
+
/** The whole value for some bytes, base64 as it travels in a header or a trailer. */
|
|
26
|
+
export declare function checksumOf(algorithm: ChecksumAlgorithm, bytes: Uint8Array): string;
|