@needmoretruth/nmts-cli 0.45.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 +61 -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 +8 -0
- package/dist/args.js +7 -1
- 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/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 +5 -0
- package/dist/commands/ls.js +9 -1
- 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.d.ts +2 -2
- package/dist/commands/put.js +8 -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/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 +4 -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 +12 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +15 -0
- package/dist/portable.js +11 -0
- 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/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 +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 +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -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
package/dist/s3/server.js
CHANGED
|
@@ -6,14 +6,41 @@
|
|
|
6
6
|
// account -- and the key it checks was printed on somebody's terminal. This is the same call the
|
|
7
7
|
// rest of the system made on 2026-08-20 when every container port was pulled back to loopback.
|
|
8
8
|
//
|
|
9
|
-
// ⚠
|
|
10
|
-
// `
|
|
11
|
-
//
|
|
12
|
-
|
|
9
|
+
// ⚠ WHICH OPERATION A REQUEST IS, IS DECIDED IN `routes.ts`, and each operation is answered in the
|
|
10
|
+
// file it names (`object-read.ts`, `object-write.ts`, `multipart.ts`); what a caller has to hand
|
|
11
|
+
// this is in `contract.ts`. What is here is the socket and the pair: the two things that decide
|
|
12
|
+
// who can reach the drive at all.
|
|
13
|
+
import { createServer, } from "node:http";
|
|
13
14
|
import { randomBytes } from "node:crypto";
|
|
14
|
-
import {
|
|
15
|
+
import { NmtsError } from "../errors.js";
|
|
16
|
+
import { addressOf } from "./address.js";
|
|
17
|
+
import { answerFor, failureReasonOf, failWith, refuse } from "./answer.js";
|
|
18
|
+
import { headerOf, mayTouch, NOT_YOURS } from "./call.js";
|
|
19
|
+
import { handle } from "./routes.js";
|
|
20
|
+
import { verifyAgainst } from "./sigv4.js";
|
|
21
|
+
// Why a response failed, for a server that writes its own log line instead of passing `log`.
|
|
22
|
+
export { failureReasonOf } from "./answer.js";
|
|
15
23
|
/** Where the drive is served. Loopback, always — see the note above. */
|
|
16
24
|
export const BIND_ADDRESS = "127.0.0.1";
|
|
25
|
+
/**
|
|
26
|
+
* How a Node server in front of this gateway is made -- `createServer(GATEWAY_SERVER_OPTIONS, …)`.
|
|
27
|
+
*
|
|
28
|
+
* ⛔ NO LIMIT ON THE WHOLE REQUEST. Node's default gives a request five minutes from its first byte
|
|
29
|
+
* to its last, and then cuts it: an upload of a few gigabytes over an ordinary line takes longer
|
|
30
|
+
* than that, and was cut part way every time. What stops a client that holds a connection open
|
|
31
|
+
* for nothing is the limit on headers, kept here as Node's default because setting the other to
|
|
32
|
+
* zero would silently take it to zero too, and the idle limit on a body (`BODY_IDLE_MS`).
|
|
33
|
+
*/
|
|
34
|
+
export const GATEWAY_SERVER_OPTIONS = { requestTimeout: 0, headersTimeout: 60_000 };
|
|
35
|
+
/**
|
|
36
|
+
* How long a request body may go without a byte arriving before the connection is closed.
|
|
37
|
+
*
|
|
38
|
+
* ⚠ ONLY WHILE THE BODY IS ARRIVING. Once its last byte is in, what follows -- a file being sealed
|
|
39
|
+
* and sent to the storage network, which can take minutes -- is this side's work, and the
|
|
40
|
+
* connection is not the client's to lose for it. Time the body spends waiting on this side (a
|
|
41
|
+
* disk that is slow to take it) is not counted either.
|
|
42
|
+
*/
|
|
43
|
+
export const BODY_IDLE_MS = 120_000;
|
|
17
44
|
/** A random pair, made fresh every time the gateway starts and stored nowhere. */
|
|
18
45
|
export function newCredential() {
|
|
19
46
|
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
@@ -23,26 +50,159 @@ export function newCredential() {
|
|
|
23
50
|
id += letters[byte % letters.length] ?? "A";
|
|
24
51
|
return { accessKeyId: id.slice(0, 20), secretAccessKey: randomBytes(30).toString("base64url") };
|
|
25
52
|
}
|
|
53
|
+
/** One log line, with why the response failed when its client was told less. */
|
|
54
|
+
function withReason(line, res) {
|
|
55
|
+
const why = failureReasonOf(res);
|
|
56
|
+
return why === undefined ? line : `${line} (${why})`;
|
|
57
|
+
}
|
|
58
|
+
function pathOf(req) {
|
|
59
|
+
const url = req.url ?? "/";
|
|
60
|
+
const at = url.indexOf("?");
|
|
61
|
+
return at < 0 ? url : url.slice(0, at);
|
|
62
|
+
}
|
|
63
|
+
/** Whether the request says a body follows it. */
|
|
64
|
+
function declaresBody(req) {
|
|
65
|
+
const length = req.headers["content-length"];
|
|
66
|
+
return req.headers["transfer-encoding"] !== undefined || (length !== undefined && length !== "0");
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Close the connection when the request's body stops arriving for `idleMs`, until its last byte is in.
|
|
70
|
+
*
|
|
71
|
+
* ⚠ IT WATCHES THE SOCKET'S COUNT OF BYTES, NOT THE BODY'S EVENTS. Listening for the body's data
|
|
72
|
+
* would start it flowing before whoever reads it is ready. While the body waits on this side --
|
|
73
|
+
* as many bytes buffered as the stream holds -- the quiet is this side's, and is not counted.
|
|
74
|
+
*/
|
|
75
|
+
export function watchBodyIdle(req, idleMs = BODY_IDLE_MS) {
|
|
76
|
+
if (req.complete || !declaresBody(req))
|
|
77
|
+
return;
|
|
78
|
+
let seen = req.socket.bytesRead;
|
|
79
|
+
let quietSince = Date.now();
|
|
80
|
+
const tick = setInterval(() => {
|
|
81
|
+
if (req.complete || req.destroyed) {
|
|
82
|
+
clearInterval(tick);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const read = req.socket.bytesRead;
|
|
86
|
+
if (read !== seen || req.readableLength >= req.readableHighWaterMark) {
|
|
87
|
+
seen = read;
|
|
88
|
+
quietSince = Date.now();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (Date.now() - quietSince >= idleMs) {
|
|
92
|
+
clearInterval(tick);
|
|
93
|
+
req.socket.destroy();
|
|
94
|
+
}
|
|
95
|
+
}, Math.max(10, Math.min(5_000, Math.floor(idleMs / 4))));
|
|
96
|
+
tick.unref();
|
|
97
|
+
req.once("end", () => clearInterval(tick));
|
|
98
|
+
req.once("close", () => clearInterval(tick));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* ⛔ TWO PAIRS WITH ONE ID ARE REFUSED BEFORE ANYTHING IS ANSWERED. Which secret, and which bucket
|
|
102
|
+
* restriction, a request is held to would otherwise be decided by the order of a list. The check
|
|
103
|
+
* at request time (`sigv4.ts`) answers neither pair for a list changed after this.
|
|
104
|
+
*/
|
|
105
|
+
function refuseDuplicatePairs(credentials) {
|
|
106
|
+
const seen = new Set();
|
|
107
|
+
for (const pair of credentials) {
|
|
108
|
+
if (seen.has(pair.accessKeyId)) {
|
|
109
|
+
throw new NmtsError("GATEWAY_CREDENTIALS: two pairs have the same `accessKeyId`, so which one signs is undecidable.", {
|
|
110
|
+
exitCode: 2,
|
|
111
|
+
nextStep: "Nothing is listening and nothing was opened. Give every pair its own id and make the gateway again.",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
seen.add(pair.accessKeyId);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
26
117
|
/**
|
|
27
118
|
* The gateway as a handler, which is the form that listens to nothing.
|
|
28
119
|
*
|
|
29
120
|
* ⛔ SEPARATE FROM `createGateway` BECAUSE WHO LISTENS IS NOT THIS FILE'S DECISION. The
|
|
30
121
|
* command-line tool binds loopback and says why at the top of this file; a business mounting
|
|
31
122
|
* this behind its own TLS has already made that decision, and a library that opened a socket of
|
|
32
|
-
* its own would be making it again, differently.
|
|
123
|
+
* its own would be making it again, differently. What it should make that socket with is
|
|
124
|
+
* `GATEWAY_SERVER_OPTIONS` and `checkContinueHandler`.
|
|
33
125
|
*/
|
|
34
126
|
export function gatewayHandler(options) {
|
|
127
|
+
refuseDuplicatePairs(options.credentials);
|
|
128
|
+
const log = options.log;
|
|
35
129
|
return (req, res) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
130
|
+
watchBodyIdle(req);
|
|
131
|
+
// ⛔ THE LOG HEARS WHY, THE CLIENT DOES NOT. A failure's own words -- a local path, a database's
|
|
132
|
+
// error, NMTS's refusal -- are answered with a fixed sentence (`answer.ts`), and the line
|
|
133
|
+
// `routes.ts` writes when the response closes carries them instead, with the path taken out.
|
|
134
|
+
const told = log === undefined ? options : { ...options, log: (line) => log(withReason(line, res)) };
|
|
135
|
+
handle(req, res, told).catch((error) => {
|
|
136
|
+
// A resolver that threw, or a list that could not be read: answered as what it was — a
|
|
137
|
+
// network that did not answer is `SlowDown`, not a fault of this gateway.
|
|
138
|
+
failWith(res, error, pathOf(req));
|
|
43
139
|
});
|
|
44
140
|
};
|
|
45
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* What the request's line and headers alone say it will be refused with, or null when nothing
|
|
144
|
+
* known before its body stands in its way: the signature, and whether the pair may touch the bucket.
|
|
145
|
+
*
|
|
146
|
+
* ⛔ ASKED BEFORE `100 Continue`, SO A REFUSED UPLOAD NEVER SENDS ITS BODY. A client that sends
|
|
147
|
+
* `Expect: 100-continue` waits for that line before sending gigabytes; Node sends it by itself
|
|
148
|
+
* unless a server asks first, and then a request whose signature does not hold uploaded its whole
|
|
149
|
+
* body to be refused at the end. A server that handles `Expect` itself calls this first.
|
|
150
|
+
*/
|
|
151
|
+
export function refusalBeforeBody(req, options) {
|
|
152
|
+
const url = req.url ?? "/";
|
|
153
|
+
const pathname = pathOf(req);
|
|
154
|
+
let verdict;
|
|
155
|
+
try {
|
|
156
|
+
verdict = verifyAgainst({ method: (req.method ?? "GET").toUpperCase(), url, headers: req.headers }, options.credentials, options.now?.() ?? Date.now());
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
if (!(error instanceof URIError))
|
|
160
|
+
throw error;
|
|
161
|
+
return { status: 400, code: "InvalidURI", message: "The query string is not valid percent-encoding." };
|
|
162
|
+
}
|
|
163
|
+
if (!verdict.ok)
|
|
164
|
+
return { status: 403, code: verdict.code, message: verdict.message };
|
|
165
|
+
let bucket;
|
|
166
|
+
try {
|
|
167
|
+
bucket = addressOf(pathname, headerOf(req, "host"), options.virtualHostBase).bucket;
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
const answer = answerFor(error);
|
|
171
|
+
return { status: answer.status, code: answer.code, message: answer.message };
|
|
172
|
+
}
|
|
173
|
+
if (bucket !== "" && !mayTouch(verdict.credential, bucket)) {
|
|
174
|
+
return { status: 403, code: "AccessDenied", message: NOT_YOURS };
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* A listener for Node's `checkContinue` event: `100 Continue` and then `then`, or the refusal the
|
|
180
|
+
* request would have earned, answered at once with `Connection: close` so its body is never read.
|
|
181
|
+
*/
|
|
182
|
+
export function checkContinueHandler(options, then) {
|
|
183
|
+
return (req, res) => {
|
|
184
|
+
let refusal;
|
|
185
|
+
try {
|
|
186
|
+
refusal = refusalBeforeBody(req, options);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
failWith(res, error, pathOf(req));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (refusal === null) {
|
|
193
|
+
res.writeContinue();
|
|
194
|
+
then(req, res);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const log = options.log;
|
|
198
|
+
if (log !== undefined)
|
|
199
|
+
res.once("close", () => log(`Refused ${res.statusCode}`));
|
|
200
|
+
refuse(res, refusal.status, refusal.code, refusal.message, pathOf(req));
|
|
201
|
+
};
|
|
202
|
+
}
|
|
46
203
|
export function createGateway(options) {
|
|
47
|
-
|
|
204
|
+
const handler = gatewayHandler(options);
|
|
205
|
+
const server = createServer(GATEWAY_SERVER_OPTIONS, handler);
|
|
206
|
+
server.on("checkContinue", checkContinueHandler(options, handler));
|
|
207
|
+
return server;
|
|
48
208
|
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** The literals a client may put in `x-amz-content-sha256` instead of a hex digest. */
|
|
2
|
+
export declare const UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD";
|
|
3
|
+
export declare const STREAMING_PAYLOAD = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";
|
|
4
|
+
export declare const STREAMING_PAYLOAD_TRAILER = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER";
|
|
5
|
+
export declare const STREAMING_UNSIGNED_PAYLOAD_TRAILER = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
|
|
6
|
+
export interface IncomingRequest {
|
|
7
|
+
readonly method: string;
|
|
8
|
+
/** Raw request target, path and query together, exactly as it came off the wire. */
|
|
9
|
+
readonly url: string;
|
|
10
|
+
readonly headers: Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
11
|
+
}
|
|
12
|
+
export type Refusal = {
|
|
13
|
+
readonly ok: false;
|
|
14
|
+
readonly code: string;
|
|
15
|
+
readonly message: string;
|
|
16
|
+
};
|
|
17
|
+
/** One refusal, shaped so it satisfies both verdict types — neither of which has an `ok: true`. */
|
|
18
|
+
export declare function refuse(code: string, message: string): Refusal;
|
|
19
|
+
export declare function headerValue(headers: IncomingRequest["headers"], name: string): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Whether `x-amz-content-sha256` holds something S3 accepts: a lowercase hex SHA-256, or one of
|
|
22
|
+
* the four literals.
|
|
23
|
+
*
|
|
24
|
+
* ⛔ ANYTHING ELSE IS REFUSED BEFORE THE BODY IS TOUCHED. The value decides how the body is read --
|
|
25
|
+
* as it is, or with `aws-chunked` framing to take off -- and a value this gateway does not know
|
|
26
|
+
* would leave that to a guess.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isPayloadHash(value: string): boolean;
|
|
29
|
+
export interface QueryPart {
|
|
30
|
+
readonly key: string;
|
|
31
|
+
readonly value: string;
|
|
32
|
+
}
|
|
33
|
+
/** The query's parts, decoded. Throws `URIError` on a broken percent escape. */
|
|
34
|
+
export declare function queryParts(rawQuery: string): QueryPart[];
|
|
35
|
+
export declare function canonicalOf(parts: readonly QueryPart[]): string;
|
|
36
|
+
/** `k=v&k2=v2` in the order AWS wants: encoded, sorted by key and then by value. */
|
|
37
|
+
export declare function canonicalQuery(rawQuery: string): string;
|
|
38
|
+
export interface AuthorizationParts {
|
|
39
|
+
readonly accessKeyId: string;
|
|
40
|
+
readonly date: string;
|
|
41
|
+
readonly region: string;
|
|
42
|
+
readonly service: string;
|
|
43
|
+
readonly signedHeaders: readonly string[];
|
|
44
|
+
readonly signature: string;
|
|
45
|
+
}
|
|
46
|
+
export type ScopeParts = Pick<AuthorizationParts, "accessKeyId" | "date" | "region" | "service">;
|
|
47
|
+
/** `AKID/20130524/us-east-1/s3/aws4_request` → its four named parts, or null. */
|
|
48
|
+
export declare function parseCredentialScope(credential: string): ScopeParts | null;
|
|
49
|
+
/**
|
|
50
|
+
* What is wrong with a credential scope's date, region and service, or null when nothing is.
|
|
51
|
+
*
|
|
52
|
+
* ⛔ THE SCOPE IS WHAT THE SIGNING KEY IS DERIVED FROM, SO A LOOSE ONE IS A KEY NOBODY MEANT. An
|
|
53
|
+
* empty date, or one that is not the request's own day, signs a request under a key that is not
|
|
54
|
+
* that day's; a service other than `s3` signs it under a key meant for another API. S3 refuses
|
|
55
|
+
* both, and so does this. ⚠ The region is not checked against anything: this gateway has none
|
|
56
|
+
* of its own and answers whichever one a client was configured with, as long as it names one.
|
|
57
|
+
* Whether the date is the request's day is checked where the timestamp is known, in `sigv4.ts`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function scopeProblem(scope: ScopeParts): string | null;
|
|
60
|
+
/**
|
|
61
|
+
* The headers S3 requires a signature to cover that this one leaves out: `host`, and every
|
|
62
|
+
* `x-amz-*` header the request carries. Sorted, lowercase; empty when there are none.
|
|
63
|
+
*
|
|
64
|
+
* ⛔ A HEADER THE SIGNATURE DOES NOT COVER IS ONE ANYBODY ON THE WAY CAN ADD. The operations act on
|
|
65
|
+
* headers -- `x-amz-copy-source` turns an upload into a copy of another file, and a presigned
|
|
66
|
+
* URL for "upload here" signs only its host. Without this rule whoever held that URL could add
|
|
67
|
+
* the header and read a file the signer never named, in a bucket the signer's pair may touch:
|
|
68
|
+
* for a business that presigns for all of its customers with one pair, any customer's file.
|
|
69
|
+
* `host` is required for the same reason in the other direction: a signature that does not name
|
|
70
|
+
* the host it was made for is one that can be replayed against any other.
|
|
71
|
+
*
|
|
72
|
+
* ⚠ TWO ARE EXEMPT BECAUSE THE SIGNATURE COVERS THEIR VALUE ANOTHER WAY, which is also why S3's
|
|
73
|
+
* own clients leave them out of a presigned URL. `x-amz-content-sha256` is the last line of the
|
|
74
|
+
* canonical request in both forms; `x-amz-date` is the timestamp in the string to sign -- in the
|
|
75
|
+
* header form only, since a presigned URL carries its date in the query and reads no header.
|
|
76
|
+
*/
|
|
77
|
+
export declare function unsignedHeaders(request: IncomingRequest, presented: Presented): string[];
|
|
78
|
+
export declare function signingKey(secret: string, date: string, region: string, service: string): Buffer;
|
|
79
|
+
/** `20260824T232759Z` → epoch milliseconds, or null if it is not that shape. */
|
|
80
|
+
export declare function amzDateToMs(stamp: string | undefined): number | null;
|
|
81
|
+
/** A signature as the request presented it, read from the header or from a presigned query. */
|
|
82
|
+
export interface Presented extends AuthorizationParts {
|
|
83
|
+
/** The request's timestamp: `x-amz-date`, or `X-Amz-Date` for a presigned URL. */
|
|
84
|
+
readonly stamp: string | undefined;
|
|
85
|
+
/** A presigned URL's lifetime in seconds; null for the header form, which has none. */
|
|
86
|
+
readonly expiresSeconds: number | null;
|
|
87
|
+
/** The query as it enters the canonical request -- for a presigned URL, without its signature. */
|
|
88
|
+
readonly canonicalQuery: string;
|
|
89
|
+
/** The declared payload hash; undefined when the header form left it out. */
|
|
90
|
+
readonly payloadHash: string | undefined;
|
|
91
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// The parts of Signature Version 4 that both ways of carrying a signature share: the payload
|
|
2
|
+
// literals, RFC 3986 encoding and the canonical query, the credential scope, the signing key and the
|
|
3
|
+
// timestamp. One of the parts of `sigv4.ts`, which re-exports what callers use; only it and
|
|
4
|
+
// `sigv4-presigned.ts` import this.
|
|
5
|
+
import { createHmac } from "node:crypto";
|
|
6
|
+
/** The literals a client may put in `x-amz-content-sha256` instead of a hex digest. */
|
|
7
|
+
export const UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD";
|
|
8
|
+
export const STREAMING_PAYLOAD = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";
|
|
9
|
+
export const STREAMING_PAYLOAD_TRAILER = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER";
|
|
10
|
+
export const STREAMING_UNSIGNED_PAYLOAD_TRAILER = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
|
|
11
|
+
/** One refusal, shaped so it satisfies both verdict types — neither of which has an `ok: true`. */
|
|
12
|
+
export function refuse(code, message) {
|
|
13
|
+
return { ok: false, code, message };
|
|
14
|
+
}
|
|
15
|
+
export function headerValue(headers, name) {
|
|
16
|
+
const raw = headers[name];
|
|
17
|
+
if (raw === undefined)
|
|
18
|
+
return undefined;
|
|
19
|
+
return Array.isArray(raw) ? raw.join(",") : String(raw);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether `x-amz-content-sha256` holds something S3 accepts: a lowercase hex SHA-256, or one of
|
|
23
|
+
* the four literals.
|
|
24
|
+
*
|
|
25
|
+
* ⛔ ANYTHING ELSE IS REFUSED BEFORE THE BODY IS TOUCHED. The value decides how the body is read --
|
|
26
|
+
* as it is, or with `aws-chunked` framing to take off -- and a value this gateway does not know
|
|
27
|
+
* would leave that to a guess.
|
|
28
|
+
*/
|
|
29
|
+
export function isPayloadHash(value) {
|
|
30
|
+
return (/^[0-9a-f]{64}$/.test(value) ||
|
|
31
|
+
value === UNSIGNED_PAYLOAD ||
|
|
32
|
+
value === STREAMING_PAYLOAD ||
|
|
33
|
+
value === STREAMING_PAYLOAD_TRAILER ||
|
|
34
|
+
value === STREAMING_UNSIGNED_PAYLOAD_TRAILER);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* RFC 3986 encoding, which is what SigV4 means by "URI-encode".
|
|
38
|
+
*
|
|
39
|
+
* ⚠ `encodeURIComponent` leaves `!'()*` alone and AWS does not, so those four are finished by hand.
|
|
40
|
+
* A query string that contains one of them and is encoded the JavaScript way produces a different
|
|
41
|
+
* canonical request from the client's, and the request is refused for no reason a person can see.
|
|
42
|
+
*/
|
|
43
|
+
function uriEncode(value) {
|
|
44
|
+
return encodeURIComponent(value).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
45
|
+
}
|
|
46
|
+
/** The query's parts, decoded. Throws `URIError` on a broken percent escape. */
|
|
47
|
+
export function queryParts(rawQuery) {
|
|
48
|
+
const parts = [];
|
|
49
|
+
for (const raw of rawQuery.split("&")) {
|
|
50
|
+
if (raw.length === 0)
|
|
51
|
+
continue;
|
|
52
|
+
const at = raw.indexOf("=");
|
|
53
|
+
const key = at < 0 ? raw : raw.slice(0, at);
|
|
54
|
+
const value = at < 0 ? "" : raw.slice(at + 1);
|
|
55
|
+
parts.push({ key: decodeURIComponent(key), value: decodeURIComponent(value) });
|
|
56
|
+
}
|
|
57
|
+
return parts;
|
|
58
|
+
}
|
|
59
|
+
export function canonicalOf(parts) {
|
|
60
|
+
const pairs = parts.map((p) => [uriEncode(p.key), uriEncode(p.value)]);
|
|
61
|
+
pairs.sort((a, b) => (a[0] === b[0] ? (a[1] < b[1] ? -1 : 1) : a[0] < b[0] ? -1 : 1));
|
|
62
|
+
return pairs.map(([k, v]) => `${k}=${v}`).join("&");
|
|
63
|
+
}
|
|
64
|
+
/** `k=v&k2=v2` in the order AWS wants: encoded, sorted by key and then by value. */
|
|
65
|
+
export function canonicalQuery(rawQuery) {
|
|
66
|
+
if (rawQuery.length === 0)
|
|
67
|
+
return "";
|
|
68
|
+
return canonicalOf(queryParts(rawQuery));
|
|
69
|
+
}
|
|
70
|
+
/** `AKID/20130524/us-east-1/s3/aws4_request` → its four named parts, or null. */
|
|
71
|
+
export function parseCredentialScope(credential) {
|
|
72
|
+
const scope = credential.split("/");
|
|
73
|
+
if (scope.length !== 5 || scope[4] !== "aws4_request")
|
|
74
|
+
return null;
|
|
75
|
+
const [accessKeyId, date, region, service] = scope;
|
|
76
|
+
if (accessKeyId === undefined || date === undefined || region === undefined || service === undefined) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return { accessKeyId, date, region, service };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* What is wrong with a credential scope's date, region and service, or null when nothing is.
|
|
83
|
+
*
|
|
84
|
+
* ⛔ THE SCOPE IS WHAT THE SIGNING KEY IS DERIVED FROM, SO A LOOSE ONE IS A KEY NOBODY MEANT. An
|
|
85
|
+
* empty date, or one that is not the request's own day, signs a request under a key that is not
|
|
86
|
+
* that day's; a service other than `s3` signs it under a key meant for another API. S3 refuses
|
|
87
|
+
* both, and so does this. ⚠ The region is not checked against anything: this gateway has none
|
|
88
|
+
* of its own and answers whichever one a client was configured with, as long as it names one.
|
|
89
|
+
* Whether the date is the request's day is checked where the timestamp is known, in `sigv4.ts`.
|
|
90
|
+
*/
|
|
91
|
+
export function scopeProblem(scope) {
|
|
92
|
+
if (!/^\d{8}$/.test(scope.date))
|
|
93
|
+
return "the credential's date is not eight digits, YYYYMMDD";
|
|
94
|
+
if (scope.region.length === 0)
|
|
95
|
+
return "the credential names no region";
|
|
96
|
+
if (scope.service !== "s3")
|
|
97
|
+
return 'the credential is scoped to a service other than "s3"';
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The headers S3 requires a signature to cover that this one leaves out: `host`, and every
|
|
102
|
+
* `x-amz-*` header the request carries. Sorted, lowercase; empty when there are none.
|
|
103
|
+
*
|
|
104
|
+
* ⛔ A HEADER THE SIGNATURE DOES NOT COVER IS ONE ANYBODY ON THE WAY CAN ADD. The operations act on
|
|
105
|
+
* headers -- `x-amz-copy-source` turns an upload into a copy of another file, and a presigned
|
|
106
|
+
* URL for "upload here" signs only its host. Without this rule whoever held that URL could add
|
|
107
|
+
* the header and read a file the signer never named, in a bucket the signer's pair may touch:
|
|
108
|
+
* for a business that presigns for all of its customers with one pair, any customer's file.
|
|
109
|
+
* `host` is required for the same reason in the other direction: a signature that does not name
|
|
110
|
+
* the host it was made for is one that can be replayed against any other.
|
|
111
|
+
*
|
|
112
|
+
* ⚠ TWO ARE EXEMPT BECAUSE THE SIGNATURE COVERS THEIR VALUE ANOTHER WAY, which is also why S3's
|
|
113
|
+
* own clients leave them out of a presigned URL. `x-amz-content-sha256` is the last line of the
|
|
114
|
+
* canonical request in both forms; `x-amz-date` is the timestamp in the string to sign -- in the
|
|
115
|
+
* header form only, since a presigned URL carries its date in the query and reads no header.
|
|
116
|
+
*/
|
|
117
|
+
export function unsignedHeaders(request, presented) {
|
|
118
|
+
const signed = new Set(presented.signedHeaders);
|
|
119
|
+
const missing = new Set();
|
|
120
|
+
if (!signed.has("host"))
|
|
121
|
+
missing.add("host");
|
|
122
|
+
for (const raw of Object.keys(request.headers)) {
|
|
123
|
+
const name = raw.toLowerCase();
|
|
124
|
+
if (!name.startsWith("x-amz-") || signed.has(name))
|
|
125
|
+
continue;
|
|
126
|
+
if (name === "x-amz-content-sha256")
|
|
127
|
+
continue;
|
|
128
|
+
if (name === "x-amz-date" && presented.expiresSeconds === null)
|
|
129
|
+
continue;
|
|
130
|
+
missing.add(name);
|
|
131
|
+
}
|
|
132
|
+
return [...missing].sort();
|
|
133
|
+
}
|
|
134
|
+
export function signingKey(secret, date, region, service) {
|
|
135
|
+
const kDate = createHmac("sha256", `AWS4${secret}`).update(date).digest();
|
|
136
|
+
const kRegion = createHmac("sha256", kDate).update(region).digest();
|
|
137
|
+
const kService = createHmac("sha256", kRegion).update(service).digest();
|
|
138
|
+
return createHmac("sha256", kService).update("aws4_request").digest();
|
|
139
|
+
}
|
|
140
|
+
/** `20260824T232759Z` → epoch milliseconds, or null if it is not that shape. */
|
|
141
|
+
export function amzDateToMs(stamp) {
|
|
142
|
+
if (stamp === undefined)
|
|
143
|
+
return null;
|
|
144
|
+
const m = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/.exec(stamp);
|
|
145
|
+
if (m === null)
|
|
146
|
+
return null;
|
|
147
|
+
return Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6]));
|
|
148
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type IncomingRequest, type Presented, type QueryPart, type Refusal } from "./sigv4-canonical.ts";
|
|
2
|
+
/** The longest a presigned URL may live: seven days, which is S3's own ceiling. */
|
|
3
|
+
export declare const MAX_PRESIGNED_EXPIRY_SECONDS = 604800;
|
|
4
|
+
/** Whether the query carries a signature, which makes the request a presigned one. */
|
|
5
|
+
export declare function isPresigned(parts: readonly QueryPart[]): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Read a presigned URL's parts out of its query.
|
|
8
|
+
*
|
|
9
|
+
* ⚠ THE PAYLOAD HASH IS `UNSIGNED-PAYLOAD` UNLESS THE CLIENT SAID OTHERWISE, as a header or --
|
|
10
|
+
* which is what the AWS SDK for JavaScript does -- as an `X-Amz-Content-Sha256` query parameter.
|
|
11
|
+
* A presigned URL is made before anybody knows what will be uploaded through it.
|
|
12
|
+
*/
|
|
13
|
+
export declare function presentedFromQuery(request: IncomingRequest, parts: readonly QueryPart[]): Presented | Refusal;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// A presigned URL: the signature read out of the query string instead of an `Authorization` header.
|
|
2
|
+
// One of the parts of `sigv4.ts`; only it imports this.
|
|
3
|
+
//
|
|
4
|
+
// ⛔ WHAT IS READ HERE IS WHERE THE PARTS ARE, NOT WHETHER THEY HOLD. Whether the signature holds is
|
|
5
|
+
// judged in `sigv4.ts`, by the same code for both forms. Whether the URL is still alive is judged
|
|
6
|
+
// there too, next to the clock-skew rule it bends.
|
|
7
|
+
import { canonicalOf, headerValue, parseCredentialScope, refuse, scopeProblem, UNSIGNED_PAYLOAD, } from "./sigv4-canonical.js";
|
|
8
|
+
/** The longest a presigned URL may live: seven days, which is S3's own ceiling. */
|
|
9
|
+
export const MAX_PRESIGNED_EXPIRY_SECONDS = 604_800;
|
|
10
|
+
/** The names of a presigned URL's parts. Case matters: S3 reads them exactly so. */
|
|
11
|
+
const Q_ALGORITHM = "X-Amz-Algorithm";
|
|
12
|
+
const Q_CREDENTIAL = "X-Amz-Credential";
|
|
13
|
+
const Q_DATE = "X-Amz-Date";
|
|
14
|
+
const Q_EXPIRES = "X-Amz-Expires";
|
|
15
|
+
const Q_SIGNED_HEADERS = "X-Amz-SignedHeaders";
|
|
16
|
+
const Q_SIGNATURE = "X-Amz-Signature";
|
|
17
|
+
const Q_SESSION = "X-Amz-Security-Token";
|
|
18
|
+
/** Whether the query carries a signature, which makes the request a presigned one. */
|
|
19
|
+
export function isPresigned(parts) {
|
|
20
|
+
return parts.some((p) => p.key === Q_ALGORITHM || p.key === Q_CREDENTIAL || p.key === Q_SIGNATURE);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Read a presigned URL's parts out of its query.
|
|
24
|
+
*
|
|
25
|
+
* ⚠ THE PAYLOAD HASH IS `UNSIGNED-PAYLOAD` UNLESS THE CLIENT SAID OTHERWISE, as a header or --
|
|
26
|
+
* which is what the AWS SDK for JavaScript does -- as an `X-Amz-Content-Sha256` query parameter.
|
|
27
|
+
* A presigned URL is made before anybody knows what will be uploaded through it.
|
|
28
|
+
*/
|
|
29
|
+
export function presentedFromQuery(request, parts) {
|
|
30
|
+
const malformed = (message) => refuse("AuthorizationQueryParametersError", message);
|
|
31
|
+
const names = [Q_ALGORITHM, Q_CREDENTIAL, Q_DATE, Q_EXPIRES, Q_SIGNED_HEADERS, Q_SIGNATURE, Q_SESSION];
|
|
32
|
+
if (names.some((name) => parts.filter((p) => p.key === name).length > 1)) {
|
|
33
|
+
return malformed("a presigned URL names one of its X-Amz-* parameters more than once");
|
|
34
|
+
}
|
|
35
|
+
const single = (name) => parts.find((p) => p.key === name)?.value;
|
|
36
|
+
const algorithm = single(Q_ALGORITHM);
|
|
37
|
+
const credential = single(Q_CREDENTIAL);
|
|
38
|
+
const stamp = single(Q_DATE);
|
|
39
|
+
const expires = single(Q_EXPIRES);
|
|
40
|
+
const signedHeaders = single(Q_SIGNED_HEADERS);
|
|
41
|
+
const signature = single(Q_SIGNATURE);
|
|
42
|
+
const token = single(Q_SESSION);
|
|
43
|
+
if (algorithm !== "AWS4-HMAC-SHA256")
|
|
44
|
+
return malformed("X-Amz-Algorithm must be AWS4-HMAC-SHA256");
|
|
45
|
+
if (credential === undefined ||
|
|
46
|
+
stamp === undefined ||
|
|
47
|
+
expires === undefined ||
|
|
48
|
+
signedHeaders === undefined ||
|
|
49
|
+
signature === undefined) {
|
|
50
|
+
return malformed("a presigned URL needs X-Amz-Credential, X-Amz-Date, X-Amz-Expires, X-Amz-SignedHeaders and X-Amz-Signature");
|
|
51
|
+
}
|
|
52
|
+
// ⛔ NO SESSION TOKENS. This gateway hands out plain pairs and nothing else, so a URL signed with
|
|
53
|
+
// temporary credentials was not signed with one of them, whatever the signature says.
|
|
54
|
+
if (token !== undefined) {
|
|
55
|
+
return refuse("InvalidToken", "this gateway issues no session tokens, so X-Amz-Security-Token cannot be honoured");
|
|
56
|
+
}
|
|
57
|
+
const scope = parseCredentialScope(credential);
|
|
58
|
+
if (scope === null)
|
|
59
|
+
return malformed("X-Amz-Credential is not <key>/<date>/<region>/<service>/aws4_request");
|
|
60
|
+
const problem = scopeProblem(scope);
|
|
61
|
+
if (problem !== null)
|
|
62
|
+
return malformed(`X-Amz-Credential is malformed: ${problem}`);
|
|
63
|
+
if (!/^\d{1,7}$/.test(expires))
|
|
64
|
+
return malformed("X-Amz-Expires must be a whole number of seconds");
|
|
65
|
+
const expiresSeconds = Number(expires);
|
|
66
|
+
if (expiresSeconds < 1 || expiresSeconds > MAX_PRESIGNED_EXPIRY_SECONDS) {
|
|
67
|
+
return malformed(`X-Amz-Expires must be between 1 and ${MAX_PRESIGNED_EXPIRY_SECONDS} seconds`);
|
|
68
|
+
}
|
|
69
|
+
const declared = parts.filter((p) => p.key.toLowerCase() === "x-amz-content-sha256");
|
|
70
|
+
if (declared.length > 1)
|
|
71
|
+
return malformed("X-Amz-Content-Sha256 appears more than once");
|
|
72
|
+
const payloadHash = headerValue(request.headers, "x-amz-content-sha256") ?? declared[0]?.value ?? UNSIGNED_PAYLOAD;
|
|
73
|
+
return {
|
|
74
|
+
...scope,
|
|
75
|
+
signedHeaders: signedHeaders.split(";").filter((h) => h.length > 0),
|
|
76
|
+
signature,
|
|
77
|
+
stamp,
|
|
78
|
+
expiresSeconds,
|
|
79
|
+
canonicalQuery: canonicalOf(parts.filter((p) => p.key !== Q_SIGNATURE)),
|
|
80
|
+
payloadHash,
|
|
81
|
+
};
|
|
82
|
+
}
|
package/dist/s3/sigv4.d.ts
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
+
import { type AuthorizationParts, type IncomingRequest } from "./sigv4-canonical.ts";
|
|
2
|
+
export { amzDateToMs, canonicalQuery, isPayloadHash, STREAMING_PAYLOAD, STREAMING_PAYLOAD_TRAILER, STREAMING_UNSIGNED_PAYLOAD_TRAILER, UNSIGNED_PAYLOAD, } from "./sigv4-canonical.ts";
|
|
3
|
+
export type { IncomingRequest } from "./sigv4-canonical.ts";
|
|
4
|
+
export { MAX_PRESIGNED_EXPIRY_SECONDS } from "./sigv4-presigned.ts";
|
|
1
5
|
/** How far a request's own timestamp may sit from ours before it is refused. AWS uses the same. */
|
|
2
6
|
export declare const MAX_CLOCK_SKEW_MS: number;
|
|
3
|
-
/** The literals a client may put in `x-amz-content-sha256` instead of a hex digest. */
|
|
4
|
-
export declare const UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD";
|
|
5
|
-
export declare const STREAMING_PAYLOAD = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";
|
|
6
|
-
export declare const STREAMING_PAYLOAD_TRAILER = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER";
|
|
7
|
-
export interface IncomingRequest {
|
|
8
|
-
readonly method: string;
|
|
9
|
-
/** Raw request target, path and query together, exactly as it came off the wire. */
|
|
10
|
-
readonly url: string;
|
|
11
|
-
readonly headers: Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
12
|
-
}
|
|
13
7
|
export interface GatewayCredential {
|
|
14
8
|
readonly accessKeyId: string;
|
|
15
9
|
readonly secretAccessKey: string;
|
|
@@ -22,9 +16,25 @@ export interface GatewayCredential {
|
|
|
22
16
|
*/
|
|
23
17
|
readonly buckets?: readonly string[] | undefined;
|
|
24
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* What a chunk-signed body needs to go on checking the request's signature: every chunk signature
|
|
21
|
+
* is an HMAC under the same key, over a string that names the same timestamp and scope and the
|
|
22
|
+
* signature before it -- starting from the request's own.
|
|
23
|
+
*/
|
|
24
|
+
export interface SigningContext {
|
|
25
|
+
/** The key derived for this request's date, region and service. */
|
|
26
|
+
readonly key: Buffer;
|
|
27
|
+
/** `20130524/us-east-1/s3/aws4_request`. */
|
|
28
|
+
readonly scope: string;
|
|
29
|
+
/** The request's timestamp, `20130524T000000Z`. */
|
|
30
|
+
readonly stamp: string;
|
|
31
|
+
/** The request's signature, which the first chunk signature chains from. */
|
|
32
|
+
readonly seedSignature: string;
|
|
33
|
+
}
|
|
25
34
|
export type Verified = {
|
|
26
35
|
readonly ok: true;
|
|
27
36
|
readonly payloadHash: string;
|
|
37
|
+
readonly signing: SigningContext;
|
|
28
38
|
} | {
|
|
29
39
|
readonly ok: false;
|
|
30
40
|
readonly code: string;
|
|
@@ -34,28 +44,18 @@ export type Verified = {
|
|
|
34
44
|
export type VerifiedAgainst = {
|
|
35
45
|
readonly ok: true;
|
|
36
46
|
readonly payloadHash: string;
|
|
47
|
+
readonly signing: SigningContext;
|
|
37
48
|
readonly credential: GatewayCredential;
|
|
38
49
|
} | {
|
|
39
50
|
readonly ok: false;
|
|
40
51
|
readonly code: string;
|
|
41
52
|
readonly message: string;
|
|
42
53
|
};
|
|
43
|
-
/** `k=v&k2=v2` in the order AWS wants: encoded, sorted by key and then by value. */
|
|
44
|
-
export declare function canonicalQuery(rawQuery: string): string;
|
|
45
|
-
interface AuthorizationParts {
|
|
46
|
-
readonly accessKeyId: string;
|
|
47
|
-
readonly date: string;
|
|
48
|
-
readonly region: string;
|
|
49
|
-
readonly service: string;
|
|
50
|
-
readonly signedHeaders: readonly string[];
|
|
51
|
-
readonly signature: string;
|
|
52
|
-
}
|
|
53
54
|
/** Pull apart `AWS4-HMAC-SHA256 Credential=…, SignedHeaders=…, Signature=…`. */
|
|
54
55
|
export declare function parseAuthorization(header: string | undefined): AuthorizationParts | null;
|
|
55
|
-
/** `20260824T232759Z` → epoch milliseconds, or null if it is not that shape. */
|
|
56
|
-
export declare function amzDateToMs(stamp: string | undefined): number | null;
|
|
57
56
|
/**
|
|
58
|
-
* Rebuild the string the client signed and check that the signature matches
|
|
57
|
+
* Rebuild the string the client signed and check that the signature matches -- in the
|
|
58
|
+
* `Authorization` header or in a presigned query, whichever the request carries.
|
|
59
59
|
*
|
|
60
60
|
* The clock is passed in rather than read here: a test that cannot choose "now" cannot check the
|
|
61
61
|
* skew rule at all, and that rule is the one that stops a captured request being replayed tomorrow.
|
|
@@ -64,9 +64,8 @@ export declare function verifySignature(request: IncomingRequest, credential: Ga
|
|
|
64
64
|
/**
|
|
65
65
|
* The whole check, against every pair a gateway answers to: which one signed, and whether it did.
|
|
66
66
|
*
|
|
67
|
-
* ⚠ THE THREE REFUSALS ARE DIFFERENT ON PURPOSE. "No
|
|
68
|
-
*
|
|
69
|
-
*
|
|
67
|
+
* ⚠ THE THREE REFUSALS ARE DIFFERENT ON PURPOSE. "No signature at all", "a key this gateway does
|
|
68
|
+
* not have" and "a signature that does not hold" are three different things for whoever is
|
|
69
|
+
* reading a client's logs, and none of them says anything about what is in the drive.
|
|
70
70
|
*/
|
|
71
71
|
export declare function verifyAgainst(request: IncomingRequest, credentials: readonly GatewayCredential[], now: number): VerifiedAgainst;
|
|
72
|
-
export {};
|