@ultimat3/storage 31.1.0 → 31.3.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/CLAUDE.md CHANGED
@@ -145,7 +145,7 @@ Gotchas:
145
145
  - **`serverSideEncryption` is declared and refused by BOTH drivers.** Full rule: `docs/history/storage.md`.
146
146
  - **The `uploadScanner` port is the ONE scan path** (`As of 31.0.0`; the `quarantine: true` segment
147
147
  and `releaseQuarantine` are retired). An out-of-band decision — a review, an async vendor scan —
148
- is the verdict `hold`, keyed by `ScanInput.pendingKey`: `X_STORAGE_QUARANTINED` (409), upload
148
+ is the verdict `hold`, keyed by `(ScanInput.pendingKey, ScanInput.revision)` — the etag, or the bytes' sha256 (31.3; `pendingKey` alone cleared a rewrite): `X_STORAGE_QUARANTINED` (409), upload
149
149
  kept pending, attempt deleted, no policy overrides it. A scanner that holds declares `holds: true`
150
150
  (else its `hold` is `X_INVARIANT`), and its outage then refuses even under `'accept'`. A targeted
151
151
  grant is refused while a scanner is installed (`X_STORAGE_TARGET_UNSCANNED`): it skips promotion.
package/README.md CHANGED
@@ -58,7 +58,7 @@ const stored = [...sessions.objects().values()].map((bytes) => new TextDecoder()
58
58
 
59
59
  Server-side only, like both other drivers: nothing an island imports reaches it.
60
60
 
61
- Every call on a disk `defineStorage` resolves is a `storage.<verb>` event on `@ultimat3/core`'s instrumentation bus (`put`, `get`, `stat`, `copy`, `delete`, `list`; `{ disk, key, bytes?, durationMs, error? }`) inside a `storage.<verb>` client span. `disk()` hands out that instrumented view, so it is not `===` the driver you declared — compare `.name`. A driver you hold directly reports nothing.
61
+ Every call on a disk `defineStorage` resolves is a `storage.<verb>` event on `@ultimat3/core`'s instrumentation bus (`put`, `get`, `stat`, `copy`, `delete`, `list`; `{ disk, key, bytes?, durationMs, error? }`) inside a `storage.<verb>` client span. `disk()` hands out that instrumented view, so it is not `===` the driver you declared — compare `.name`. A driver you hold directly reports nothing. A method patched in a test — assigned on the disk, or `spyOn(driver, 'put')` on the driver you declared — runs on the next call and is still reported. `spyOn(disk, …)` does nothing: Bun's `spyOn` never writes through a Proxy (as of bun 1.4.4), so spy on the driver.
62
62
 
63
63
  `region` is what requests are signed for; unset, that is `auto`. An endpoint that expects another
64
64
  region refuses the first write, list or delete with `X_CONFIG_INVALID`, and the fix names the
@@ -515,7 +515,7 @@ export const runtime = {
515
515
  } satisfies RuntimeOverrides;
516
516
  ```
517
517
 
518
- `UploadScanner` (`@ultimat3/storage/adapter`) is `{ name, holds?, scan({ key, pendingKey, size, contentType, body }) }`,
518
+ `UploadScanner` (`@ultimat3/storage/adapter`) is `{ name, holds?, scan({ key, pendingKey, revision, size, contentType, body }) }`,
519
519
  `body` a `ReadableStream`. `memoryUploadScanner()` is the reference: it flags the EICAR test string
520
520
  (`EICAR_TEST_STRING`) anywhere in the stream, across chunk boundaries. Hold your adapter to the
521
521
  contract with `behavesLike(uploadScannerConformance, () => yours)` from
@@ -526,20 +526,22 @@ latency budget are the app's (axiom 8). No scanner installed, nothing is scanned
526
526
 
527
527
  Magic-byte sniffing closes stored XSS. It is **not** malware scanning: `application/zip` is the
528
528
  OOXML container, so a macro-laden `.docx` passes `validateUpload` as a clean one does. A verdict
529
- that is not ready inside the confirm is the scanner's `hold`, keyed by `pendingKey` — the upload's
530
- own key, stable across retries (`key` is the per-attempt copy). One scan path; the v30
529
+ that is not ready inside the confirm is the scanner's `hold`, keyed by `(pendingKey, revision)` —
530
+ the upload's own key, stable across retries (`key` is the per-attempt copy), and WHICH bytes are
531
+ under it: the confirmed upload's etag, or `sha256:<hex>` on a disk with none. The upload URL stays
532
+ valid after the sniff, so a decision keyed by `pendingKey` alone clears whatever is PUT there next. One scan path; the v30
531
533
  `grantUpload({ quarantine: true })` + `releaseQuarantine` flow is retired.
532
534
 
533
535
  ```ts
534
536
  import type { UploadScanner } from '@ultimat3/storage/adapter';
535
537
 
536
538
  // `approved` is your app's own store — a table a moderator's action writes.
537
- export const reviewScanner = (approved: (key: string) => Promise<boolean>): UploadScanner => ({
539
+ export const reviewScanner = (approved: (upload: string) => Promise<boolean>): UploadScanner => ({
538
540
  name: 'review',
539
541
  holds: true,
540
- async scan({ pendingKey, body }) {
542
+ async scan({ pendingKey, revision, body }) {
541
543
  await body.cancel();
542
- return (await approved(pendingKey))
544
+ return (await approved(`${pendingKey}@${revision}`))
543
545
  ? { verdict: 'clean' }
544
546
  : { verdict: 'hold', reason: 'awaiting a moderator' };
545
547
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/storage",
3
- "version": "31.1.0",
3
+ "version": "31.3.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.1.0"
46
+ "@ultimat3/core": "31.3.0"
47
47
  }
48
48
  }
package/src/attachment.ts CHANGED
@@ -223,7 +223,13 @@ async function scannedAttempt(
223
223
  pending.etag === '' ? undefined : { ifMatch: pending.etag },
224
224
  );
225
225
  await assertCopyIsWhatWasJudged(disk, pending, attempt, input.policy);
226
- await scanAttempt({ disk, pendingKey: input.key, attempt, policy: input.policy });
226
+ await scanAttempt({
227
+ disk,
228
+ pendingKey: input.key,
229
+ attempt,
230
+ revision: pending.etag,
231
+ policy: input.policy,
232
+ });
227
233
  return attempt;
228
234
  }
229
235
 
@@ -51,15 +51,38 @@ function report(name: `storage.${StorageVerb}`, event: StorageEvent): void {
51
51
  * The driver, every contract method wrapped and every other member (a test disk's `objects()`,
52
52
  * `multipart`, `registerAs`) passed through bound to the driver. The driver's calls to its OWN
53
53
  * methods stay unwrapped: one caller's call is one event.
54
+ *
55
+ * A wrapper is reused only while the driver still holds the method it wraps. A cache keyed on the
56
+ * name alone kept answering the original after a test patched `put` to simulate an outage; keyed
57
+ * on the method, a patch — written through the disk or onto the declared driver — runs on the next
58
+ * call, still reported. Writing back a wrapper this disk handed out writes the method it wraps, so
59
+ * `const put = disk.put; …; disk.put = put` restores one event per call, never two.
54
60
  */
55
61
  export function instrumentedDisk(disk: string, driver: StorageDriver): StorageDriver {
56
- const wrapped = new Map<PropertyKey, unknown>();
57
- return new Proxy(driver, {
62
+ const wrapped = new Map<PropertyKey, { readonly method: unknown; readonly bound: unknown }>();
63
+ /** Each wrapper handed out, to the method it wraps: what a restore writes back. */
64
+ const unwrapped = new WeakMap<object, unknown>();
65
+ const raw = (value: unknown): unknown =>
66
+ typeof value === 'function' ? (unwrapped.get(value) ?? value) : value;
67
+ const view: StorageDriver = new Proxy(driver, {
68
+ // Only a write ON the disk patches the driver. An object inheriting from the disk
69
+ // (`Object.create(disk)`) reaches this trap as `receiver` and keeps the member as its own;
70
+ // written onto the driver, a child's `stat` calling `disk.stat` called itself.
71
+ set: (target, property, value, receiver) =>
72
+ receiver === view
73
+ ? Reflect.set(target, property, raw(value), target)
74
+ : Reflect.set(target, property, value, receiver),
75
+ defineProperty: (target, property, descriptor) =>
76
+ Reflect.defineProperty(
77
+ target,
78
+ property,
79
+ 'value' in descriptor ? { ...descriptor, value: raw(descriptor.value) } : descriptor,
80
+ ),
58
81
  get(target, property) {
59
- const known = wrapped.get(property);
60
- if (known !== undefined) return known;
61
82
  const value: unknown = Reflect.get(target, property, target);
62
83
  if (typeof value !== 'function') return value;
84
+ const known = wrapped.get(property);
85
+ if (known?.method === value) return known.bound;
63
86
  // A Proxy `get` is handed every name, `constructor` included: an own key or no verb.
64
87
  const verb =
65
88
  typeof property === 'string' && Object.hasOwn(VERB_OF, property)
@@ -73,10 +96,12 @@ export function instrumentedDisk(disk: string, driver: StorageDriver): StorageDr
73
96
  call(disk, verb, keyOf(property as string, args), driver.name, () =>
74
97
  Promise.resolve(method.apply(target, args)),
75
98
  );
76
- wrapped.set(property, bound);
99
+ wrapped.set(property, { method: value, bound });
100
+ unwrapped.set(bound, value);
77
101
  return bound;
78
102
  },
79
103
  });
104
+ return view;
80
105
  }
81
106
 
82
107
  async function call(
@@ -23,6 +23,8 @@ export interface ScanAttemptInput {
23
23
  readonly pendingKey: string;
24
24
  /** The attempt copy under `SCAN_SEGMENT`, already sniffed. */
25
25
  readonly attempt: StorageObject;
26
+ /** The confirmed upload's etag; `''` when its disk keeps none, and the bytes are hashed instead. */
27
+ readonly revision: string;
26
28
  readonly policy: UploadPolicy;
27
29
  }
28
30
 
@@ -39,6 +41,14 @@ async function removeAll(disk: StorageDriver, keys: readonly string[]): Promise<
39
41
  return removed;
40
42
  }
41
43
 
44
+ /** `sha256:<hex>` of the attempt, streamed: the revision of an upload whose disk keeps no etag. */
45
+ async function digestOf(disk: StorageDriver, attempt: StorageObject): Promise<string> {
46
+ const hasher = new Bun.CryptoHasher('sha256');
47
+ const body = await disk.stream(attempt.key);
48
+ for await (const chunk of body) hasher.update(chunk);
49
+ return `sha256:${hasher.digest('hex')}`;
50
+ }
51
+
42
52
  /** True when a scanner is installed — `promoteAttachment` stages an attempt copy only then. */
43
53
  export const scanning = (): boolean => uploadScanner() !== undefined;
44
54
 
@@ -57,10 +67,12 @@ export async function scanAttempt(input: ScanAttemptInput): Promise<void> {
57
67
  try {
58
68
  // Inside the `try`: an attempt that cannot be READ for the scanner (an S3 GET reset, an
59
69
  // etag-pinned read refused) is no verdict too, never a raw error past the cleanup.
70
+ const revision = input.revision === '' ? await digestOf(disk, attempt) : input.revision;
60
71
  body = await disk.stream(attempt.key, attempt.etag === '' ? undefined : { etag: attempt.etag });
61
72
  verdict = await scanner.scan({
62
73
  key: attempt.key,
63
74
  pendingKey: input.pendingKey,
75
+ revision,
64
76
  size: attempt.size,
65
77
  contentType: attempt.contentType,
66
78
  body,
@@ -14,6 +14,15 @@ export interface ScanInput {
14
14
  * attempt.
15
15
  */
16
16
  readonly pendingKey: string;
17
+ /**
18
+ * WHICH bytes under `pendingKey` this is: the confirmed upload's etag, or `sha256:<hex>` of the
19
+ * bytes on a disk that keeps none. The upload URL stays valid after the sniff, so the object under
20
+ * `pendingKey` can be rewritten between two confirms — a held verdict, and the decision that
21
+ * clears it, MUST be keyed by `(pendingKey, revision)`: an approval of one revision is no
22
+ * approval of the next. Always set by `promoteAttachment`; optional only so a scanner's own test
23
+ * written before 31.3 still compiles.
24
+ */
25
+ readonly revision?: string | undefined;
17
26
  readonly size: number;
18
27
  /** The type the object is stored under, already sniffed against its bytes. */
19
28
  readonly contentType: string;
@@ -28,7 +37,7 @@ export interface ScanInput {
28
37
  *
29
38
  * `hold` is a decision too: not yet cleared, by something outside this call — a human review, a
30
39
  * vendor's asynchronous sandbox. The upload stays pending and promotion answers
31
- * `X_STORAGE_QUARANTINED` until the scanner answers `clean` for that `pendingKey`. Neither `hold`
40
+ * `X_STORAGE_QUARANTINED` until the scanner answers `clean` for that `pendingKey` AND `revision`. Neither `hold`
32
41
  * nor `infected` is something a policy can wave through.
33
42
  */
34
43
  export type ScanVerdict =