@ultimat3/storage 31.31.0 → 31.33.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/storage",
3
- "version": "31.31.0",
3
+ "version": "31.33.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.31.0"
46
+ "@ultimat3/core": "31.33.0"
47
47
  }
48
48
  }
package/src/attachment.ts CHANGED
@@ -1,8 +1,10 @@
1
- // Single responsibility: where an entity's files live, and how one that was never attached is
2
- // found again. The upload happens BEFORE the row it belongs to exists, so it lands under a
3
- // `pending/` segment inside the org and is promoted once there is an id — which makes an orphan
4
- // a fact about the key rather than a join nobody runs. Every key here goes through `scopedKey`,
5
- // so the tenant prefix is a construction, not a check somebody remembered to write.
1
+ // Single responsibility: where an entity's files live, and how one that was never attached is found
2
+ // again. The upload happens BEFORE the row it belongs to exists, so it lands under a `pending/`
3
+ // segment inside the org and is promoted once there is an id — which makes an orphan a fact about
4
+ // the key rather than a join nobody runs.
5
+ //
6
+ // Every key here goes through `scopedKey`, so the tenant prefix is a construction, not a check
7
+ // somebody remembered to write.
6
8
 
7
9
  import type { Clock } from '@ultimat3/core';
8
10
  import { assert, finiteCount, nanoid, renderThrowable, systemClock } from '@ultimat3/core';
@@ -9,6 +9,7 @@ import {
9
9
  type Page,
10
10
  type ResolveEnvironmentOptions,
11
11
  systemClock,
12
+ trimTrailingSlashes,
12
13
  } from '@ultimat3/core';
13
14
  import {
14
15
  assertListOptions,
@@ -139,7 +140,7 @@ export interface LocalDriverOptions {
139
140
  }
140
141
 
141
142
  export function localDriver(options: LocalDriverOptions): StorageDriver {
142
- const root = options.root.replace(/\/+$/, '');
143
+ const root = trimTrailingSlashes(options.root);
143
144
  // `=== undefined`, never `??`: `??` coalesces on `null` too, so an explicitly blanked key in a
144
145
  // decoded JSON config took the default instead of the refusal `finiteCount` is here to raise.
145
146
  const maxPutBytes = finiteCount(
@@ -1,8 +1,10 @@
1
1
  // Single responsibility: the s3 disk's SIGNED requests — the ones `Bun.S3Client` has no option for
2
- // (`x-amz-meta-*`, `Cache-Control`, object lock, a ranged GET read off its own `Content-Range`) — built,
3
- // signed through core's one SigV4 signer and sent with `fetch`. The endpoint, the addressing style,
4
- // the region and the credentials are read from the SAME `S3DriverOptions` the Bun client is built
5
- // from, so the two transports of one disk cannot point at two buckets.
2
+ // (`x-amz-meta-*`, `Cache-Control`, object lock, a ranged GET read off its own `Content-Range`) —
3
+ // built, signed through core's one SigV4 signer and sent with `fetch`.
4
+ //
5
+ // The endpoint, the addressing style, the region and the credentials are read from the SAME
6
+ // `S3DriverOptions` the Bun client is built from, so the two transports of one disk cannot point at
7
+ // two buckets.
6
8
 
7
9
  import {
8
10
  type AwsCredentials,
@@ -12,6 +14,7 @@ import {
12
14
  presignAwsUrl,
13
15
  signAwsRequest,
14
16
  systemClock,
17
+ trimTrailingSlashes,
15
18
  } from '@ultimat3/core';
16
19
  import type { SignedPart } from './signed-url';
17
20
 
@@ -206,7 +209,7 @@ export function s3ObjectUrl(options: S3WireOptions, key: string): URL {
206
209
  if (options.forcePathStyle === false) {
207
210
  return new URL(`${base.protocol}//${options.bucket}.${base.host}/${path}`);
208
211
  }
209
- const prefix = base.pathname.replace(/\/+$/, '');
212
+ const prefix = trimTrailingSlashes(base.pathname);
210
213
  return new URL(
211
214
  `${base.protocol}//${base.host}${prefix}/${encodeURIComponent(options.bucket)}/${path}`,
212
215
  );
package/src/driver-s3.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // Single responsibility: the production disk over Bun's native S3 client. One driver covers AWS,
2
2
  // Cloudflare R2 and any S3-compatible gateway — the difference is `endpoint`, `region` and
3
3
  // `forcePathStyle`, nothing else.
4
+ //
4
5
  // The client is built lazily on first use so importing this module never opens a socket, and
5
6
  // credentials arrive as env var NAMES: a literal key in app.config.ts is a key in git.
6
7
 
package/src/grant.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  // Single responsibility: minting ONE presigned PUT for ONE file, with the tenant prefix and the
2
- // policy already inside the signature. The client never names the key — it asks for a grant and
3
- // is told one — so a caller cannot aim an upload at another org's prefix, at an attached row it
4
- // does not own, or at a size the policy never allowed. Everything this returns is derived; the
5
- // only client input that survives is the filename's extension, and only if it survives a regex.
2
+ // policy already inside the signature. The client never names the key — it asks for a grant and is
3
+ // told one — so a caller cannot aim an upload at another org's prefix, at an attached row it does
4
+ // not own, or at a size the policy never allowed.
5
+ //
6
+ // Everything this returns is derived; the only client input that survives is the filename's
7
+ // extension, and only if it survives a regex.
6
8
 
7
9
  import type { Clock } from '@ultimat3/core';
8
10
  import { finiteCount, systemClock } from '@ultimat3/core';
package/src/signed-url.ts CHANGED
@@ -1,10 +1,18 @@
1
- // Single responsibility: time-limited signed URLs for direct upload and download.
2
- // The HMAC covers the *constraints* (key, method, expiry, max size, content type), not just
3
- // the key — otherwise a client that receives a URL for a 2MB PNG edits `?x-max=` and uploads
4
- // a 2GB executable. Verification never throws and never short-circuits on expiry before the
5
- // signature, so a forged URL can never learn "the signature was fine, just late".
1
+ // Single responsibility: time-limited signed URLs for direct upload and download. The HMAC covers
2
+ // the *constraints* (key, method, expiry, max size, content type), not just the key — otherwise a
3
+ // client that receives a URL for a 2MB PNG edits `?x-max=` and uploads a 2GB executable.
4
+ //
5
+ // Verification never throws and never short-circuits on expiry before the signature, so a forged
6
+ // URL can never learn "the signature was fine, just late".
6
7
 
7
- import { assert, type Clock, finiteCount, systemClock, timingSafeEqual } from '@ultimat3/core';
8
+ import {
9
+ assert,
10
+ type Clock,
11
+ finiteCount,
12
+ systemClock,
13
+ timingSafeEqual,
14
+ trimTrailingSlashes,
15
+ } from '@ultimat3/core';
8
16
  import type { SignedUrlMethod } from './driver';
9
17
  import { assertSafeKey, isSafeKey } from './path';
10
18
 
@@ -97,7 +105,7 @@ export type SignedUrlVerification =
97
105
  | { readonly ok: true; readonly constraints: SignedUrlConstraints }
98
106
  | { readonly ok: false; readonly reason: SignedUrlFailure; readonly detail: string };
99
107
 
100
- const trimBase = (base: string): string => base.replace(/\/+$/, '');
108
+ const trimBase = (base: string): string => trimTrailingSlashes(base);
101
109
 
102
110
  /**
103
111
  * The PATH a base mounts at — what a route sees, and so the only part of a base both halves can
@@ -6,6 +6,7 @@ import {
6
6
  devSecretsRefused,
7
7
  type ResolveEnvironmentOptions,
8
8
  resolveEnvironment,
9
+ timingSafeEqual,
9
10
  } from '@ultimat3/core';
10
11
  import { signingSecretMissing } from './errors';
11
12
 
@@ -37,7 +38,10 @@ export const STORAGE_SIGNING_SECRET_KEY = 'STORAGE_SIGNING_SECRET';
37
38
  export function usesDevStorageSecret(options?: Pick<ResolveEnvironmentOptions, 'env'>): boolean {
38
39
  const source = options?.env ?? (process.env as Record<string, string | undefined>);
39
40
  const configured = source[STORAGE_SIGNING_SECRET_KEY];
40
- return configured === undefined || configured === '' || configured === DEV_SIGNING_SECRET;
41
+ // `timingSafeEqual` though the other side is published: the left side is the live secret.
42
+ return (
43
+ configured === undefined || configured === '' || timingSafeEqual(configured, DEV_SIGNING_SECRET)
44
+ );
41
45
  }
42
46
 
43
47
  /** The two options the rule reads; `LocalDriverOptions` carries both. */
@@ -63,7 +67,7 @@ export function resolveSigningSecret(disk: string, options: SigningSecretOptions
63
67
  const env = options.env ?? (process.env as Record<string, string | undefined>);
64
68
  const supplied = options.signingSecret ?? env[STORAGE_SIGNING_SECRET_KEY];
65
69
  const configured =
66
- supplied === undefined || supplied === '' || supplied === DEV_SIGNING_SECRET
70
+ supplied === undefined || supplied === '' || timingSafeEqual(supplied, DEV_SIGNING_SECRET)
67
71
  ? undefined
68
72
  : supplied;
69
73
  // FAILS CLOSED, through core's one rule (`devSecretsRefused`): a process that names no
package/src/storage.ts CHANGED
@@ -1,8 +1,10 @@
1
- // Single responsibility: named disks (Laravel's model) and the one module-level accessor.
2
- // Call sites name a disk, never a driver — swapping `local` for `s3` in app.config.ts must
3
- // not touch a single `storage.disk('uploads').put(...)` call. This file is also where a driver
4
- // LEARNS its disk name (`registerAs`), because the registry is the only holder of that fact and
5
- // a signed URL's `:disk` segment has to be the name the mounted route resolves.
1
+ // Single responsibility: named disks (Laravel's model) and the one module-level accessor. Call
2
+ // sites name a disk, never a driver — swapping `local` for `s3` in app.config.ts must not touch a
3
+ // single `storage.disk('uploads').put(...)` call.
4
+ //
5
+ // This file is also where a driver LEARNS its disk name (`registerAs`), because the registry is the
6
+ // only holder of that fact and a signed URL's `:disk` segment has to be the name the mounted route
7
+ // resolves.
6
8
 
7
9
  import { ConfigInvalidError } from '@ultimat3/core';
8
10
  import type { StorageDriver } from './driver';
package/src/upload.ts CHANGED
@@ -1,11 +1,12 @@
1
- // Single responsibility: the constraint policy for direct-to-storage uploads — size,
2
- // allowlist, checksum — and the content-type sniff that enforces it.
3
- // WHY sniff: `Content-Type` is attacker-controlled. A `.png` that is really an HTML document
4
- // is a stored-XSS delivery vehicle the moment any surface serves it back with the declared
5
- // type, so the magic bytes decide and a contradiction is rejected outright — including the
6
- // contradiction of NO answer at all, where the declared type is one a signature could have
7
- // confirmed. A type no rule can ever confirm (`text/csv`, `application/json`) is still accepted
8
- // on the client's word: there is nothing to compare it against.
1
+ // Single responsibility: the constraint policy for direct-to-storage uploads — size, allowlist,
2
+ // checksum — and the content-type sniff that enforces it. WHY sniff: `Content-Type` is
3
+ // attacker-controlled.
4
+ //
5
+ // A `.png` that is really an HTML document is a stored-XSS delivery vehicle the moment any surface
6
+ // serves it back with the declared type, so the magic bytes decide and a contradiction is rejected
7
+ // outright — including the contradiction of NO answer at all, where the declared type is one a
8
+ // signature could have confirmed. A type no rule can ever confirm (`text/csv`, `application/json`)
9
+ // is still accepted on the client's word: there is nothing to compare it against.
9
10
 
10
11
  import { assert, finiteCount } from '@ultimat3/core';
11
12
  import { sha256Base64 } from './driver';