# FileBucket — Design

Design document for FileBucket. For usage, see [README.md](./README.md).

**Package:** `@aws-blocks/bb-file-bucket`
**Type:** Primitive (new infrastructure)
**AWS Service:** Amazon S3

## Design Decisions

**D-FB-1: Buffer body type instead of ReadableStream**
**Decision:** `put()` accepts `Buffer | string`, `get()` returns `Buffer` in `FileContent.body`.
**Rationale:** ReadableStream adds complexity for the common case (small-to-medium files). Buffer is simpler to work with in Lambda (where the entire response must be buffered anyway). For large files, presigned URLs (`getUrl`/`putUrl`) are the recommended pattern. This favors client-safe return types.

**D-FB-2: Segregated internal storage in mock**
**Decision:** The mock stores user content, sidecar metadata, and version history under separate sibling roots inside `.bb-data/{fullId}/`:
```
content/{key}                     file body (byte-identical to what was written)
meta/{key}.json                   sidecar metadata
versions/{key}/{versionId}        version body
versions/{key}/{versionId}.json   version metadata
versions/{key}/__deleted__        delete marker (sentinel)
```
**Rationale:** Keeps file content byte-identical (no wrapping format) while guaranteeing internal bookkeeping can never collide with user keys. An earlier design co-located metadata as `{path}.__meta__.json` next to each file and relied on marker substrings, which meant a user key like `data.__meta__.json` or a directory named `x.__versions__/` could be silently hidden or shadowed by `scan()`. Because user content now lives only under `content/`, `scan()` walks that one root and yields everything with no marker-based filtering — arbitrary keys are supported, matching S3 semantics.

**D-FB-3: deleteBatch chunks at 1,000**
**Decision:** `deleteBatch()` internally chunks into groups of 1,000 and issues separate `DeleteObjects` calls.
**Rationale:** S3 `DeleteObjects` API supports max 1,000 keys per request. Chunking is transparent to the caller. Batch methods handle pagination internally.

**D-FB-4: Presigned URL default expiry of 3600 seconds**
**Decision:** Both `getUrl` and `putUrl` default to 1 hour expiry.
**Rationale:** Matches the S3 SDK default. Long enough for typical browser upload/download flows, short enough to limit exposure.

**D-FB-5: scan returns AsyncIterable**
**Decision:** `scan()` returns `AsyncIterable<FileInfo>` with internal pagination.
**Rationale:** AsyncIterable is used for unbounded result sets. S3 `ListObjectsV2` paginates at 1,000 keys; the iterable handles continuation tokens transparently.

**D-FB-6: Versioning is opt-in with runtime API support**
**Decision:** `versioned: true` enables S3 object versioning and unlocks version-aware methods (`listVersions`, `restoreVersion`, optional `versionId` on `get`/`delete`/`getUrl`/`getFileHandle`). Without the flag, the API surface is unchanged.
**Rationale:** Versioning adds storage cost and complexity. Making it opt-in keeps the default simple. When enabled, the runtime API exposes the full version lifecycle — listing, retrieving specific versions, permanent deletion of individual versions, and restoring old versions. `restoreVersion` is implemented as a CopyObject from the old version (S3 has no native restore), which creates a new version that becomes current.
**Update:** The **default flipped to ON** — see D-FB-10. The runtime API described here is unchanged; only the default value of `versioned` and its typing gate changed.

**D-FB-7: Mock versioning uses filesystem directories**
**Decision:** Versioned mock stores each version in `versions/{key}/v{n}` with monotonic IDs. Delete markers are `versions/{key}/__deleted__` sentinel files.
**Rationale:** Simple, inspectable, and matches the S3 semantics closely enough for local development. Monotonic IDs (`v1`, `v2`, ...) are deterministic and easy to reason about in tests, unlike S3's opaque version IDs. Version history lives under the segregated `versions/` root (see D-FB-2), so it never appears in `scan()` or collides with a user key.

**D-FB-8: Bucket name validated at synth — error, never truncate/hash**
**Decision:** The derived bucket name (`scope.fullId`) is validated against S3's naming rules (`bucket-name.ts`) before the bucket is constructed. An invalid name throws a `ValidationFailed` error with an actionable message. The same validator runs in the mock constructor so local dev (`bb dev`) fails identically — parity. `FileBucket.fromExisting(...)` skips validation since the name is externally owned.
**Rationale:** S3 bucket names are globally unique and immutable. Truncating to fit 63 chars risks collisions, and a name that shifts between deploys (e.g. after a hash input changes) would orphan or replace the customer's data — a far worse outcome than a fast, fixable synth error. Erroring puts the fix in the developer's hands (shorten a scope id once; the name is then stable forever) and matches the manual-shortening pattern already used in `bb-agent`. This deliberately differs from DynamoDB-backed BBs (KVStore/DistributedTable) which `substring(0, 255)` — DynamoDB's 255 limit is generous and table names are internal, disposable, and not globally unique, so silent truncation is acceptable there.
**Update:** The **length** rule is superseded by D-FB-18 — an over-long `fullId` is now shortened deterministically instead of rejected. Every other naming rule here is unchanged and still errors.

**D-FB-9: TLS enforced unconditionally (enforceSSL)**
**Decision:** Every provisioned bucket (the data bucket and the opt-in access-log bucket) sets `enforceSSL: true`, so CDK attaches a bucket policy denying any request where `aws:SecureTransport` is `false`. Not configurable.
**Rationale:** All FileBucket traffic — SDK calls and presigned URLs alike — is already HTTPS, so enforcing TLS closes the in-transit exposure gap with zero functional cost. There is no legitimate reason a FileBucket consumer needs plaintext S3 access, so this is a hard default rather than an option. Covered by the `enforces SSL` CDK test.

**D-FB-10: Versioning default ON — supersedes D-FB-6's opt-in default**
**Decision:** `versioned` now defaults to `true`; consumers opt out with the literal `versioned: false`. The version-aware runtime API and its option typings (`GetOptionsFor` et al.) remain unchanged in shape, but the conditional types now select the non-versioned (optionless) form only for the literal `versioned: false` — a non-literal `boolean` or an absent value resolves to the versioned-aware form, matching the new runtime default.
**Rationale:** Accidental overwrites and deletes are unrecoverable without versioning; defaulting it on makes the safe choice the default and matches the "secure by default" posture of D-FB-9. This is a **behavior/breaking change** for existing consumers (buckets that were non-versioned by default now enable versioning, which adds storage cost for prior versions), so it ships with a `minor` changeset bump under the pre-1.0 (0.x) convention that a minor signals a breaking change. The literal-`false` typing gate is a deliberate consequence of `extends { versioned: false }`: TypeScript cannot narrow a widened `boolean` to the `false` branch, so the versioned-aware typings are the safe fallback.

**D-FB-11: Opt-in server access logging with a dedicated locked-down log bucket**
**Decision:** `accessLogging: true` provisions a second, dedicated S3 bucket (all public access blocked, S3-managed encryption, `enforceSSL`) that receives the main bucket's server access logs under the `access-logs/` prefix. Logs expire via a lifecycle rule after `logRetentionDays` (default `DEFAULT_ACCESS_LOG_RETENTION_DAYS = 90`). `logRetentionDays` is validated at synth: when access logging is enabled, a non-positive or non-integer value throws — a degenerate `Duration.days(0)`/negative lifecycle would otherwise only surface at deploy. The value is inert (and therefore not validated) when `accessLogging` is off.
**Rationale:** Access logging is off by default because it has a real cost (extra bucket, log storage) and most consumers don't need an audit trail; making it opt-in keeps the default lean. The log bucket is kept **separate** from the data bucket so log delivery can't loop back onto the bucket being logged. Validating `logRetentionDays` at synth follows the same fail-fast principle as D-FB-8 (bucket name) and the CORS guard (D-FB-12): misconfiguration fails loud at synth, not minutes into a deploy.
**Update (superseded by D-FB-13):** The per-block `logRetentionDays` option was **removed**. Access-log retention is no longer configured per-block; it now derives from the framework-wide `scope.defaults.logRetention` (`RetentionDays.ONE_WEEK` in sandbox / `ONE_YEAR` in production; `RetentionDays.INFINITE` omits the expiry rule so logs are kept indefinitely). The `accessLogging` toggle itself likewise falls back to `scope.defaults.accessLogging` when not set per-block. The dedicated locked-down log bucket and its separation from the data bucket (the substance of this decision) are unchanged — only the *source* of the retention/enable posture moved from a per-block number to the stack `BlocksDefaults`. The synth-time validation that formerly guarded `logRetentionDays` no longer applies (there is no per-block retention input); the equivalent guard now lives on `noncurrentVersionExpirationDays` — see D-FB-14.

**D-FB-12: Wildcard-origin CORS + mutating method rejected at synth**
**Decision:** A CORS rule whose `allowedOrigins` includes `'*'` and whose `allowedMethods` includes a mutating method (`PUT`/`POST`/`DELETE`, the `MUTATING_CORS_METHODS` set) throws a synth-time `Error`. Wildcard + safe methods (`GET`/`HEAD`) is allowed; explicit origins + mutating methods is allowed.
**Rationale:** A wildcard origin on a state-changing method lets any website issue authenticated cross-origin writes/deletes against the bucket — a CSRF-shaped exposure. Rather than silently deploying it, FileBucket fails loud at synth with an actionable message pointing the developer at explicit origins. A hard error (not a warning) was chosen deliberately: this PR supersedes the prior behavior where such a rule deployed unchallenged, and the user opted for the strict gate over a soft warning.

**D-FB-13: Posture knobs route through the framework `BlocksDefaults` — supersedes the per-block `logRetentionDays` and the `sandboxMode`-context removal read**
**Decision:** `removalPolicy`, `accessLogging`, and access-log retention are resolved from the stack-wide `BlocksDefaults` model (exposed as `scope.defaults`) rather than from per-block day-numbers or a `sandboxMode` CDK context read. Each posture knob follows the framework's `options?.field ?? scope.defaults.field` contract (the same pattern `bb-kv-store` uses for `removalPolicy`/`deletionProtection`, and documented on the `Scope.defaults` getter in `@aws-blocks/core/cdk`):
- `removalPolicy` — an explicit per-block `'destroy'|'retain'` still wins; when omitted it falls back to `scope.defaults.removalPolicy` (`DESTROY` in the sandbox preset, `RETAIN` in production). This replaces the previous `sandboxMode`-derived removal.
- `accessLogging` — falls back to `scope.defaults.accessLogging` when not set per-block, so a production-postured stack can opt every FileBucket into logging without a per-block flag.
- **access-log retention** — derives from `scope.defaults.logRetention` (a `RetentionDays` enum: `ONE_WEEK` sandbox / `ONE_YEAR` production). `RetentionDays` is a numeric enum whose member value *is* the day count, so it maps directly to `Duration.days(...)`; the one non-day member `INFINITE` omits the lifecycle expiry rule (logs kept forever) rather than expiring at a spurious 9999 days.
**Rationale:** Addresses review comment (C): posture should come from the one stack-level model every block already consumes, not from a grab-bag of per-block numbers and a legacy `sandboxMode` context flag. Centralizing on `BlocksDefaults` means a stack picks `BlocksPresets.sandbox`/`.production` once and every FileBucket inherits a coherent removal + logging + retention posture, while per-block overrides remain available for the knobs that still accept them (`removalPolicy`, `accessLogging`). This also removes the last consumer of the `sandboxMode` context read from this package.

**D-FB-14: Noncurrent-version expiration bounded by default (90 days)**
**Decision:** When versioning is enabled (the default — D-FB-10), the main bucket gets a lifecycle rule (`ExpireNoncurrentVersions`) that permanently expires **noncurrent** object versions after `noncurrentVersionExpirationDays` (default `DEFAULT_NONCURRENT_VERSION_EXPIRATION_DAYS = 90`). The value's FORMAT is validated at synth whenever the option is provided, regardless of `versioned`: a non-positive or non-integer value throws (a degenerate `Duration.days(0)`/negative would otherwise only surface at deploy — same fail-fast principle as D-FB-8/D-FB-12). The rule itself is only APPLIED when versioning is on. There is no "disable" sentinel; to drop the rule entirely, disable versioning (`versioned: false`), which removes it along with versioning. The rule is inert on the mock and browser runtimes (no AWS resource).
**Rationale:** Addresses review comment (A): defaulting versioning ON (D-FB-10) makes overwrites/deletes recoverable but lets prior versions accrue storage cost without bound. Capping noncurrent versions at 90 days by default keeps the safe-by-default posture affordable — the common case (recover from a recent bad write) is well within 90 days, while stale versions no longer pile up indefinitely. It pairs with, and offsets the cost concern raised by, the versioning-default-on change rather than reopening that decision.

**D-FB-15: No CORS by default — `corsRules` is explicit, not derived**
**Decision:** A FileBucket created without `corsRules` gets no CORS configuration. CORS origins are not derived from `scope.defaults.allowedOrigins`, not seeded with `http://localhost:3000` by the sandbox posture, and not injected by `Hosting` from its distribution URL. Browser access to a deployed bucket requires an explicit `corsRules` entry; the README's "Browser Uploads and Downloads (CORS)" section documents this.
**Rationale:** Considered and rejected: code defaults (a sandbox-posture localhost origin, or Hosting adding its `distributionUrl` to the FileBuckets it fronts, the way it injects `CORS_HOSTING_ORIGINS` for the API). Each adds configuration overhead — a new `BlocksDefaults` knob or Hosting→FileBucket wiring, plus an opt-out for buckets that should stay server-only — for what most buckets don't need, since most FileBuckets are only accessed server-side. Deriving from the existing inputs also doesn't work: `defaults.allowedOrigins` holds regex patterns that have no S3 `AllowedOrigin` equivalent, and the Hosting URL is a token from a construct created after the backend in `index.cdk.ts`. Documenting the one explicit rule keeps the bucket's exposure visible in the app's own code.

**D-FB-16: Encryption at rest is selectable — SSE-S3 default, SSE-KMS with a dedicated or imported CMK**
**Decision:** `encryption` accepts the same values as `KVStore` / `DistributedTable`: `'aws-managed'` (default, SSE-S3), `'customer-managed'` (SSE-KMS with a dedicated key this block creates, rotation on, sharing the bucket's removal policy), or `FileBucket.fromKmsKey(arn)` (SSE-KMS with an existing key). The value is parsed by core's shared `resolveKmsEncryption` (`@aws-blocks/core/cdk/internal`), the same parser the DynamoDB resolver now delegates to, so an unrecognized value warns (`@aws-blocks/bb-file-bucket:UnknownEncryption`) and falls back to SSE-S3 exactly as it does for the tables. SSE-KMS buckets enable S3 Bucket Keys. `grantReadWrite` on a bucket with an `encryptionKey` also grants the runtime role the key permissions, so presigned URLs (signed by that role) keep working. The access-log bucket stays SSE-S3 because S3 server access logging can't deliver to an SSE-KMS target.
**Rationale:** `'aws-managed'` maps to SSE-S3 rather than the `aws/s3` KMS key so the default is unchanged (no new KMS charges or request throttling for existing buckets). The dedicated key is created by this block instead of by `s3.Bucket` so rotation is always on and its removal policy can be set to the bucket's — a sandbox teardown doesn't leave a billed key behind, and a retained bucket is never left without its key.

**D-FB-17: Object Lock with a default retention, rejected at synth when it can't work**
**Decision:** `objectLock: { mode: 'governance' | 'compliance', retentionDays }` enables S3 Object Lock with a default retention on the bucket. The shared validator (`validateFileBucketOptions`, so the mock rejects the same shapes) rejects `versioned: false`, an unknown `mode`, and a `retentionDays` outside 1–36500. The CDK layer additionally rejects a resolved DESTROY removal policy — including the sandbox preset's default — because locked versions can't be emptied, so the auto-delete handler and the stack teardown would fail; the error tells the user to pass `removalPolicy: 'retain'`. Object Lock is only settable at bucket creation (CloudFormation can't enable it on an existing bucket), which is documented on the option. `putUrl()` / `createUploadHandle()` throw `PresignedUploadUnsupported` on an `objectLock` bucket, in both the mock and the AWS runtime (they read the same `objectLock` option the CDK layer does): verified against real S3 (us-west-2), an upload to a bucket with a default retention needs a `Content-MD5` or `x-amz-checksum-*` header, and an unsigned one is rejected (`AccessDenied: There were headers present in the request which were not signed`), so a URL presigned before the body is known always fails with `400 InvalidRequest`. Server-side `put()` works because the SDK sends a CRC32 checksum.
**Rationale:** A default retention (rather than enable-only) is what gives write-once protection without code changes at every write site; enable-only would protect nothing until something set per-object retention, which the API doesn't expose. Silently downgrading DESTROY to RETAIN was rejected: it would leave buckets behind on teardown without the user choosing it. Browser uploads to an Object Lock bucket would need a presigned POST whose policy admits a client-computed checksum field; that is a separate upload protocol and is left as a follow-up.

**D-FB-18: Over-long derived names are shortened (prefix + hash), not rejected — supersedes D-FB-8's length rule**
**Decision:** The bucket name is `deriveBucketName(fullId)` (`bucket-name.ts`). A `fullId` of 63 characters or fewer is used byte-identical. A longer one becomes its first characters (trailing `-`/`.` trimmed), then `-`, then the first 8 hex characters of `sha256(fullId)` — at most 63 characters, the same pattern `bb-auth` uses for its hosted-UI domain prefix. The CDK, AWS-runtime, and mock layers each call `deriveBucketName` on the same `fullId`, so they resolve the same name without handing it between them (the mock registers its `mock-` twin). The result still goes through `validateBucketName`, so the other S3 rules (lowercase, allowed characters, begin/end) still fail synth.
**Rationale:** Long stack names plus nested block ids routinely exceed 63 characters — e.g. an `Agent`'s internal `sn` session bucket under a production stack name — and the developer often doesn't control those ids, so "shorten a scope id" was no fix. D-FB-8's two concerns still hold, and this design meets both: the name is a pure function of `fullId`, so it never shifts between deploys; and the hash covers the whole `fullId`, so two scope chains that share a long prefix stay distinct (a residual 32-bit collision would fail at deploy on S3's global uniqueness, never silently share a bucket). Identity is preserved: names that already fit are untouched, and over-long names used to fail synth, so no deployed bucket carries one. `deriveBucketName`'s output is frozen once deployed and pinned in `bucket-name.test.ts`.

## Infrastructure (CDK)

Creates a single S3 bucket (plus a dedicated log bucket when `accessLogging` is enabled):

- **Bucket name:** Derived from `scope.fullId` (the bucket id joined to its parent scope ids with `-`); used as-is when it fits S3's 63 characters, else shortened to a truncated prefix plus an 8-character hash — see D-FB-18. Validated at synth against S3's naming rules — see D-FB-8.
- **Block public access:** All four settings enabled (BLOCK_ALL)
- **Encryption:** SSE-S3 by default; SSE-KMS with a dedicated rotating CMK (`'customer-managed'`) or an imported key (`FileBucket.fromKmsKey`), with S3 Bucket Keys on. See D-FB-16.
- **Object Lock:** Off by default; `options.objectLock` enables it with a default retention (requires versioning and a retained bucket). See D-FB-17.
- **TLS:** Enforced unconditionally (`enforceSSL: true`) — a bucket policy denies any request where `aws:SecureTransport` is `false`. Not configurable. See D-FB-9.
- **Versioning:** Enabled by default (secure default); opt out via `options.versioned: false`. See D-FB-10. When on, a lifecycle rule expires noncurrent versions after `options.noncurrentVersionExpirationDays` (default 90, validated as a positive integer at synth) to bound version-storage cost. See D-FB-14.
- **Server access logging:** Enabled per-block via `options.accessLogging`, falling back to `scope.defaults.storageAccessLogging`, then `scope.defaults.accessLogging`, when omitted; when on, provisions a separate, locked-down log bucket whose logs expire after the stack posture's `scope.defaults.logRetention` (`ONE_WEEK` sandbox / `ONE_YEAR` production; `INFINITE` = no expiry). See D-FB-11 and D-FB-13.
- **CORS:** Configured from `options.corsRules` if provided; otherwise the bucket has no CORS configuration, so browser `fetch()` of presigned URLs is blocked. Not derived from `scope.defaults.allowedOrigins` (API-only regex patterns) or the Hosting domain — see D-FB-15. A bare wildcard origin (`'*'`) combined with a mutating method (PUT/POST/DELETE) is rejected at synth. See D-FB-12.
- **Lifecycle rules:** Configured from `options.lifecycleRules` if provided (merged with the noncurrent-version expiration rule above)
- **Removal policy:** Resolved from `options.removalPolicy` (`'destroy'|'retain'`) when set, else the stack posture default `scope.defaults.removalPolicy` (DESTROY sandbox / RETAIN production) — replacing the former `sandboxMode` context read. See D-FB-13.
- **Auto-delete objects:** Enabled when removal policy is DESTROY
- **Permissions:** `grantReadWrite` to the parent scope's handler automatically

## Mock Implementation

- Files stored on the local filesystem at `.bb-data/{scope.fullId}/` via `getMockDataDir()` from core.
- Internal data is segregated into sibling roots so it can never collide with user keys (see D-FB-2):
  - `content/{key}` — file body, byte-identical to what was written.
  - `meta/{key}.json` — sidecar metadata.
  - `versions/{key}/{versionId}` (+ `.json` sidecars, `__deleted__` marker) — version history.
- Path mapping for both the mock and the dev file-server is centralized in `paths.ts` so they stay in lockstep.
- Data persists across dev server restarts. Customers can wipe with `rm -rf .bb-data`.
- Presigned URLs are served by the dev file-server at `/.bb-file-bucket/{scope.fullId}/{path}?token=...`. The path segments are URL-encoded; the server decodes them and validates an HMAC token scoped to method, path, and expiry. The HMAC secret (`LOCAL_FILE_SECRET` in `tokens.ts`) is a **per-process random value** — the token-minting mock and the validating dev file-server share the same in-process module instance, so tokens are unforgeable without being a hardcoded, source-visible literal.
- Downloads are served with `X-Content-Type-Options: nosniff` and `Content-Disposition: attachment`. The stored body and its `Content-Type` are caller-controlled, so serving them inline would make the dev file-server a stored-XSS vector (an uploaded `text/html`/SVG payload executing in the app origin). Forcing a download + disabling MIME sniffing is dev-server hardening only; deployed content is served via S3 presigned URLs, where the stored `Content-Type` is caller-chosen and no equivalent `Content-Disposition`/`nosniff` is applied — do not assume this dev-server behavior reflects the deployed path.
- `scan()` recursively walks only the `content/` root and yields every file it finds — no marker-based filtering — so user keys are unrestricted.
- The dev file-server's PUT handler delegates to the registered `FileBucket` instance (via a process-global registry) so uploads get versioning, key validation, and metadata. There is no direct-write fallback; an unregistered bucket fails loud with a 500.
- Key length validated against S3's 1,024-byte limit (warns, does not reject).
- Object keys are validated identically on the mock and the AWS runtime via the shared, filesystem-independent `assertValidKey` (`validation.ts`), called from every key-accepting method on both layers. It rejects (`ValidationFailed`) an empty/non-string key, a leading slash, an empty path segment (interior `a//b` or trailing `a/`, which the mock's `path.join` would otherwise collapse while S3 keeps literal), ASCII control characters, and a `..`/`.` path segment. The mock additionally applies a filesystem realpath/symlink containment guard (`assertContainedPath`) as local-only defense. `deleteBatch` validates every key up front on both layers, so a mixed valid/invalid batch deletes nothing.
- Versioning fully supported: each `put` writes to `versions/{key}/v{n}`, delete without `versionId` places a `__deleted__` sentinel, `listVersions` reads the versions directory, `restoreVersion` copies an old version via `put`.

### Mock vs AWS Behavior Differences

| Behavior Difference | Impact | Mitigation |
|------------|--------|------------|
| No lifecycle rules | Objects never expire or transition locally | No mitigation — lifecycle rules are a background S3 process |
| No CORS enforcement | Browser requests succeed regardless of origin locally; a missing `corsRules` only fails after deploy | CORS is enforced by the browser + S3, not the mock. The README's "Browser Uploads and Downloads (CORS)" section documents that `corsRules` is required for deployed browser access |
| No storage classes | Transition rules have no effect locally | No mitigation — storage classes are a cost optimization |
| No multipart upload | Large files use simple write locally | No mitigation — mock uses `fs.writeFile` regardless of size |
| Presigned URLs are localhost-only | URLs only work against the local dev server | No mitigation — expected behavior for local development |
| No encryption or Object Lock retention | `encryption` has no effect locally (files are stored unencrypted on disk), and locked versions can still be deleted with `delete(path, { versionId })` | The options are validated identically and presigned uploads are rejected on an `objectLock` bucket in both runtimes (D-FB-17), so a shape that fails `cdk synth` or a deployed upload also fails locally. Retention is enforced by S3 only |
| No IAM enforcement | Permission errors only surface in AWS | No mitigation — IAM is handled by CDK grants automatically |
| Filesystem path limits | Some OS path length limits differ from S3 key limits (1,024 bytes) | Mock validates key length and warns when it exceeds 1,024 bytes |
| Non-atomic `put()` | On a versioned bucket, `put()` performs several separate `writeFileSync` calls (version body, version metadata, current body, current metadata, delete-marker cleanup). A crash or process kill mid-`put()` can leave torn state — a body with no metadata sidecar, or a version body with no `.json`. Real S3 `PutObject` is atomic per object. | No mitigation today — the filesystem layout has no transaction boundary. Acceptable for a dev mock (re-running `put()` heals it). See Open Question 4 (storage engine). |
| Monotonic version IDs | Mock uses `v1`, `v2`, ... vs S3's opaque IDs | No impact — customer code should treat version IDs as opaque strings |
| Unknown `versionId` raw error names differ | The mock signals an unresolvable `versionId` directly (missing sidecar → `null` on `get`, no-op on `delete`, `NoSuchVersion` on `restoreVersion`), while S3 raises different **raw** codes depending on the operation and id shape: `InvalidArgument` ("Invalid version id specified") on `GetObject`/`DeleteObject`, `InvalidRequest` on `CopyObject`, and `NoSuchVersion` only for a well-formed id that no longer exists (verified against real S3, us-west-2). | Normalized in the AWS runtime so the **observable** contract matches the mock: `get()` returns `null`, `delete()` is a no-op, and `restoreVersion()` throws a clean `FileBucketErrors.VersionNotFound` (`NoSuchVersion`) with no `$metadata`. The raw S3 codes are folded only when a `versionId` was supplied, so an unrelated `InvalidArgument` still surfaces. Covered by `src/parity.test.ts` (both runtimes) and the cross-runtime suite. |
| Content-Type signed into presigned PUT URLs | When `putUrl`/`createUploadHandle` are given a `contentType`, the AWS SDK signs `content-type` as a required header, so real S3 returns `403 SignatureDoesNotMatch` if the uploaded request's `Content-Type` differs from (or omits) the signed value. The dev file-server enforces the same check (`src/file-server.ts`) so an upload that would fail in prod also fails locally with 403, rather than silently succeeding. Uploads via `createUploadHandle().upload()` always send the signed header, so the typed-handle path round-trips in both environments. Covered by `src/file-server.test.ts`. |
