@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 +2 -2
- package/src/attachment.ts +7 -5
- package/src/driver-local.ts +2 -1
- package/src/driver-s3-signed.ts +8 -5
- package/src/driver-s3.ts +1 -0
- package/src/grant.ts +6 -4
- package/src/signed-url.ts +15 -7
- package/src/signing-secret.ts +6 -2
- package/src/storage.ts +7 -5
- package/src/upload.ts +9 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/storage",
|
|
3
|
-
"version": "31.
|
|
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.
|
|
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
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
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';
|
package/src/driver-local.ts
CHANGED
|
@@ -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
|
|
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(
|
package/src/driver-s3-signed.ts
CHANGED
|
@@ -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`) —
|
|
3
|
-
// signed through core's one SigV4 signer and sent with `fetch`.
|
|
4
|
-
//
|
|
5
|
-
//
|
|
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
|
|
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
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
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
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
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 {
|
|
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
|
|
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
|
package/src/signing-secret.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
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
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
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';
|