@ultimat3/storage 31.7.0 → 31.9.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/README.md CHANGED
@@ -453,9 +453,9 @@ the process's one registry, which is the app's `defineStorage` when it declared
453
453
  (`decideGrant`, one function), every call held to the actor's org. `StorageDriver.multipart` is
454
454
  optional (`MultipartCapability`, `multipart.ts`); every shipped disk has one: s3 over its signed
455
455
  wire (`driver-s3-multipart*.ts`), local and memory over a part store (`multipart-staged.ts`), whose
456
- part URLs carry `x-upload`/`x-part` inside the HMAC and land through `acceptSignedPart`. In the
457
- browser, `uploadFile({ file, grant, multipart })` switches at `partThresholdBytes` (64 MiB): four
458
- parts in flight, each retried with core's `retry`, `resume: grant` sends only what `listParts`
456
+ part URLs carry `x-upload`/`x-part`/`x-len` inside the HMAC — a part is held to its exact signed
457
+ length, as s3 holds it — and land through `acceptSignedPart`. In the browser,
458
+ `uploadFile({ file, grant, multipart })` switches at `partThresholdBytes` (64 MiB): four parts in flight, each retried with core's `retry`, `resume: grant` sends only what `listParts`
459
459
  lacks. `completeUpload` reads the etags on the server, so no bucket CORS exposes `ETag`.
460
460
  `signPart` and `completeUpload` REQUIRE the grant's `policy`: a part no object under `maxBytes`
461
461
  holds, or an assembled total past it, is `X_STORAGE_TOO_LARGE`. The declared `size` is the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/storage",
3
- "version": "31.7.0",
3
+ "version": "31.9.0",
4
4
  "description": "Named disks over Bun.file and Bun.s3: safe keys, signed URLs, sniffed uploads",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -43,6 +43,6 @@
43
43
  "test": "bun test"
44
44
  },
45
45
  "dependencies": {
46
- "@ultimat3/core": "31.7.0"
46
+ "@ultimat3/core": "31.9.0"
47
47
  }
48
48
  }
@@ -1,5 +1,5 @@
1
1
  // Single responsibility: the one decision a part PUT on `/_storage` is made of, on a disk that signs
2
- // its own part URLs — verified, held to the actor's tenant, bounded by the signed length, then
2
+ // its own part URLs — verified, held to the actor's tenant, held to the exact signed length, then
3
3
  // handed to the disk's open session. Transport-free, as `accept.ts` is: the route owns the request.
4
4
 
5
5
  import { type SignedRequestInput, signedUploadConstraints } from './accept';
@@ -24,6 +24,14 @@ export async function acceptSignedPart(input: AcceptSignedPartInput): Promise<Up
24
24
  }
25
25
  const size = input.bytes.byteLength;
26
26
  if (maxBytes !== undefined && size > maxBytes) throw tooLarge(key, size, maxBytes);
27
+ // Exact, as s3 holds a part to its signed `content-length`: a short part would pass here and
28
+ // fail in production, and a Complete would then assemble an object of the wrong size.
29
+ if (size !== part.length) {
30
+ throw signedUrlRejected(
31
+ 'length-mismatch',
32
+ `part ${part.partNumber} of "${key}" is ${size}B and the signature covers ${String(part.length)}B`,
33
+ );
34
+ }
27
35
  const receive = input.disk.multipart?.receivePart;
28
36
  if (receive === undefined) throw multipartUnsupported(input.disk.name, key);
29
37
  return receive.call(input.disk.multipart, key, part.uploadId, part.partNumber, input.bytes);
package/src/driver.ts CHANGED
@@ -111,7 +111,8 @@ export interface SignedUrlOptions {
111
111
  * The exact size of a `PUT`, inside the signature. On `s3Driver` the URL then signs
112
112
  * `content-length`, so the provider refuses any other length, a chunked body with none included
113
113
  * (403 `SignatureDoesNotMatch`). The local disks hold it as a ceiling (`x-max`): they count what
114
- * arrives, so a shorter body passes there and not on s3. `driver-parity.test.ts` pins both.
114
+ * arrives, so a shorter body passes there and not on s3 (a part URL signs it exactly: `x-len`).
115
+ * `driver-parity.test.ts` pins both.
115
116
  */
116
117
  readonly contentLength?: number | undefined;
117
118
  readonly contentType?: string | undefined;
@@ -77,7 +77,8 @@ export function stagedMultipart(options: StagedMultipartOptions): MultipartCapab
77
77
  contentLength,
78
78
  maxBytes: contentLength,
79
79
  expiresInMs,
80
- part: { uploadId, partNumber },
80
+ // The exact length rides inside the signature: `acceptSignedPart` refuses any other.
81
+ part: { uploadId, partNumber, length: contentLength },
81
82
  }),
82
83
 
83
84
  async listParts(key, uploadId): Promise<readonly UploadedPart[]> {
package/src/signed-url.ts CHANGED
@@ -33,14 +33,28 @@ export const SIGNED_URL_PARAMS = {
33
33
  signature: 'x-sig',
34
34
  uploadId: 'x-upload',
35
35
  partNumber: 'x-part',
36
+ partLength: 'x-len',
36
37
  } as const;
37
38
 
38
39
  /** The session and part a part URL writes into — inside the signature, so neither can be edited. */
39
40
  export interface SignedPart {
40
41
  readonly uploadId: string;
41
42
  readonly partNumber: number;
43
+ /**
44
+ * The part's exact byte count. Required by `buildSignedUrl` — a local disk refuses any other
45
+ * length, as s3 refuses a body that is not its signed `content-length`. Optional in the type
46
+ * only because s3 binds the length through its presign instead and never reads this field.
47
+ */
48
+ readonly length?: number | undefined;
42
49
  }
43
50
 
51
+ /**
52
+ * A part signs under its own version line, so no single-PUT string can ever read as a part's.
53
+ * `v3` since the line carries the exact length: a `v2-part` URL bound only a ceiling, so a local
54
+ * disk took a short part s3 refuses, and it is refused now as `malformed` (no `x-len`).
55
+ */
56
+ const SIGNED_PART_VERSION = 'v3-part';
57
+
44
58
  /** `MAX_PARTS` restated as the parse's bound: `multipart.ts` imports this module's types. */
45
59
  const MAX_SIGNED_PART = 10_000;
46
60
  const SIGNED_UPLOAD_ID = /^[A-Za-z0-9._~-]{1,128}$/;
@@ -109,11 +123,11 @@ export function signedUrlBasePath(baseUrl: string): string {
109
123
  */
110
124
  export function canonicalRequest(constraints: SignedUrlConstraints, basePath: string): string {
111
125
  const part = constraints.part;
112
- // A part signs under its own version line, so no single-PUT string can ever read as a part's:
113
- // the two domains share no canonical form, and a single URL's bytes are what they always were.
114
- const tail = part === undefined ? [] : [part.uploadId, String(part.partNumber)];
126
+ // The two domains share no canonical form, and a single URL's bytes are what they always were.
127
+ const tail =
128
+ part === undefined ? [] : [part.uploadId, String(part.partNumber), String(part.length)];
115
129
  return [
116
- part === undefined ? SIGNED_URL_VERSION : `${SIGNED_URL_VERSION}-part`,
130
+ part === undefined ? SIGNED_URL_VERSION : SIGNED_PART_VERSION,
117
131
  basePath,
118
132
  constraints.method,
119
133
  constraints.key,
@@ -198,6 +212,7 @@ export async function buildSignedUrl(input: SignedUrlInput): Promise<string> {
198
212
  if (constraints.part !== undefined) {
199
213
  params.set(SIGNED_URL_PARAMS.uploadId, constraints.part.uploadId);
200
214
  params.set(SIGNED_URL_PARAMS.partNumber, String(constraints.part.partNumber));
215
+ params.set(SIGNED_URL_PARAMS.partLength, String(constraints.part.length));
201
216
  }
202
217
  const base = trimBase(input.baseUrl ?? DEFAULT_SIGNED_URL_BASE);
203
218
  params.set(
@@ -274,27 +289,39 @@ const isSignedPart = (part: SignedPart): boolean =>
274
289
  SIGNED_UPLOAD_ID.test(part.uploadId) &&
275
290
  Number.isSafeInteger(part.partNumber) &&
276
291
  part.partNumber >= 1 &&
277
- part.partNumber <= MAX_SIGNED_PART;
292
+ part.partNumber <= MAX_SIGNED_PART &&
293
+ Number.isSafeInteger(part.length) &&
294
+ (part.length ?? -1) >= 0;
278
295
 
279
296
  /** The mint's screen: a part this module could not parse back is a URL dead on arrival. */
280
297
  function assertSignedPart(part: SignedPart): SignedPart {
281
298
  assert(
282
299
  isSignedPart(part),
283
- `buildSignedUrl was handed a part that is not an upload id and a part number 1..${MAX_SIGNED_PART}`,
284
- "buildSignedUrl({ secret, key, method: 'PUT', part: { uploadId: crypto.randomUUID(), partNumber: 1 } })",
300
+ `buildSignedUrl was handed a part that is not an upload id, a part number 1..${MAX_SIGNED_PART} and a whole byte length`,
301
+ "buildSignedUrl({ secret, key, method: 'PUT', part: { uploadId: crypto.randomUUID(), partNumber: 1, length: chunk.byteLength } })",
285
302
  );
286
- return { uploadId: part.uploadId, partNumber: part.partNumber };
303
+ return { uploadId: part.uploadId, partNumber: part.partNumber, length: part.length };
287
304
  }
288
305
 
289
- /** Both parameters or neither; one alone is a URL this module never minted. */
306
+ /**
307
+ * All three parameters or none; fewer is a URL this module never minted — or one minted before
308
+ * the length was signed (`v2-part`), which bound only a ceiling and is refused rather than honoured.
309
+ */
290
310
  function parsePart(url: URL): SignedPart | 'malformed' | undefined {
291
311
  const uploadId = url.searchParams.get(SIGNED_URL_PARAMS.uploadId);
292
312
  const rawNumber = url.searchParams.get(SIGNED_URL_PARAMS.partNumber);
293
- if (uploadId === null && rawNumber === null) return undefined;
294
- if (uploadId === null || rawNumber === null || !/^[1-9][0-9]{0,4}$/.test(rawNumber)) {
313
+ const rawLength = url.searchParams.get(SIGNED_URL_PARAMS.partLength);
314
+ if (uploadId === null && rawNumber === null && rawLength === null) return undefined;
315
+ if (
316
+ uploadId === null ||
317
+ rawNumber === null ||
318
+ rawLength === null ||
319
+ !/^[1-9][0-9]{0,4}$/.test(rawNumber) ||
320
+ !/^(0|[1-9][0-9]{0,15})$/.test(rawLength)
321
+ ) {
295
322
  return 'malformed';
296
323
  }
297
- const part = { uploadId, partNumber: Number(rawNumber) };
324
+ const part = { uploadId, partNumber: Number(rawNumber), length: Number(rawLength) };
298
325
  return isSignedPart(part) ? part : 'malformed';
299
326
  }
300
327