Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .changeset/storage-upload-bound-built-in-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
'@objectstack/service-storage': patch
---

fix(service-storage)!: every upload bound resolves by one precedence, and unset never means unlimited

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No key of any authorable schema is added, removed, renamed or re-shaped and no stored row changes: `max_upload_mb` keeps its manifest key, type and declared default, and what moves is the bound a storage door enforces when no layer sets one, plus one optional constructor option (`maxUploadMb`) on the plugin, `mountStorageRoutes` and `registerStorageRoutes`. There is nothing for `objectstack migrate meta` to rewrite. -->

**BREAKING** for a host whose storage doors had no upload bound because nothing set one. Each such host now enforces 100 MB per upload.

**The rule.** The three storage Limits keys (`presigned_ttl`, `session_ttl`, `max_upload_mb`) resolve by one precedence, highest first: a saved setting (an admin save or an env override), the host's option, the settings manifest's default, the built-in default. Unset never means unlimited.

- **Built-in default.** `max_upload_mb`'s built-in default is 100 MB (counted as MiB), the number the settings manifest declares. A drift pin holds the two equal.
- **Host option.** `maxUploadMb` on the `StorageServicePlugin` constructor, on `mountStorageRoutes` and on `registerStorageRoutes` is the host layer, exactly like `presignedTtl` and `sessionTtl`. It takes a positive number of MB, or `'unlimited'` for no bound. Any other value (`0`, a negative or non-finite number, a numeric string, any other string) refuses construction or the mount.
- **Failure paths.** A settings read that fails, and a saved value that is present but not a positive number, take the host option or else the built-in default, never no bound.
- **Coverage.** The bound applies wherever an upload is judged: the byte doors (the chunk `PUT`, the presigned local raw `PUT`, the upload-link `PUT` and its several-file form), the declared-size doors (presign, chunked start), and the server-side file verbs (`field-files`, `file-attachments` and the upload link's several-file spend).
- **Boot line.** Each host door says, once at boot or mount, the bound in force and the layer it came from: the plugin through its logger, `mountStorageRoutes` and `registerStorageRoutes` through the `logger` they are given. With an explicit `'unlimited'` the line is a `warn`, and it is said even when no logger was passed, on `console.warn`.

**Who moves.** Each of these moves from no upload bound to 100 MB:

- a DevPlugin-composed server;
- `--preset minimal` with storage;
- a `mountStorageRoutes` host over a bare adapter or over a plugin bound to no settings;
- an embedder that passes `bindToSettings: false`;
- a settings-bound host while its settings read fails;
- a host whose saved or env-sourced `max_upload_mb` is present but not a positive number (it was dropped, and nothing stood in its place).

Remedies:

- **A host that constructs the plugin** (`bindToSettings: false` embedders, `--preset minimal`, settings-fault hosts): one option, `new StorageServicePlugin({ maxUploadMb: 500 })` for another bound, or `maxUploadMb: 'unlimited'` to keep accepting uploads of any size (boot then says so at `warn`). A `mountStorageRoutes` or `registerStorageRoutes` host passes the same option for that mount's doors.
- **A DevPlugin-composed server**: DevPlugin constructs the storage plugin with no options, so turn its storage toggle off and mount the plugin yourself: `new DevPlugin({ services: { storage: false }, extraPlugins: [new StorageServicePlugin({ maxUploadMb: 500 })] })`, or list the `StorageServicePlugin` instance in the stack's own `plugins`, where an explicit instance wins.
- **A host with an invalid saved value**: correct the saved `max_upload_mb` (Setup's File Storage settings, or the env override) to a positive number. The saved layer cannot spell "no bound"; to accept any size, remove the saved value and pass the host option `maxUploadMb: 'unlimited'`.

**Unchanged.** A settings-bound host with nothing saved, which is the default `os serve`, already enforced the manifest's 100 MB and still does. A saved `max_upload_mb` still wins over every host option. The 18.5.0 note "with `max_upload_mb` unset a byte door states no bound and reads the whole body" no longer holds: with `max_upload_mb` unset a byte door states the host's bound or else the built-in 100 MB, and it states no bound only under an explicit host `'unlimited'`.
7 changes: 5 additions & 2 deletions content/docs/api/error-catalog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,14 @@ The standard catalog names no `413` member.
- **An upload over the File Storage settings' `max_upload_mb`.** The presigned-upload door (the
declared `size`), the chunked-upload start (the declared `totalSize`), the local adapter's raw
upload (the body) and the chunk door (the bytes the upload already holds plus the chunk) refuse
it before anything is stored. The message names the size, the limit and the setting.
it before anything is stored. The message names the size, the limit and the setting. With no
saved value, the bound is the storage plugin's `maxUploadMb` option, then the setting's declared
default, then the built-in 100 MB: an upload bound is never unset.
- **An import over its route's row ceiling** — 5,000 rows on the synchronous route, 50,000 on the
job route ([size limits](/docs/data-modeling/import-mappings#size-limits)).

**Fix:** Raise the setting or send a smaller file; split the import across requests. Resending
**Fix:** Raise the setting (or, with nothing saved, the host's `maxUploadMb`) or send a smaller
file; split the import across requests. Resending
the same request cannot succeed.
**Retry:** `no_retry`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ describe('the request-body limit leaves each upload door its own bound, over a r
const rootDir = mkdtempSync(join(tmpdir(), 'body-limit-upload-doors-'));
stack = await bootStack(attachmentsFixtureStack as never, {
security: attachmentsFixtureSecurity(),
// `bindToSettings:false` keeps the constructor rootDir and leaves
// `max_upload_mb` unset, so each door's own bound is none at all: what
// is measured here is that the transport limit does not stand in for it.
// `bindToSettings:false` keeps the constructor rootDir and reads no
// `max_upload_mb`, so each door's own bound is the built-in 100 MB, far
// above the transport's 10 MiB: what is measured here is that the
// transport limit does not stand in for it.
extraPlugins: [
new StorageServicePlugin({ adapter: 'local', local: { rootDir }, bindToSettings: false }),
new MCPServerPlugin(),
Expand Down
42 changes: 34 additions & 8 deletions packages/services/service-storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,43 @@ doors whenever the plugin is bound to settings (the default):

| Setting | Default | Applies to |
|:---|:---|:---|
| `max_upload_mb` | 100 (MB, counted as MiB) | Every upload door: the presigned upload's declared `size`, the chunked upload's declared `totalSize`, the local raw PUT body and each chunk's running total. Over it ⇒ `413` with code `PAYLOAD_TOO_LARGE`, before anything is stored. |
| `max_upload_mb` | 100 (MB, counted as MiB) | Every upload door: the presigned upload's declared `size`, the chunked upload's declared `totalSize`, the local raw PUT body, each chunk's running total, the upload-link PUT (and each file of its several-file form), and the server-side file verbs. Over it ⇒ `413` with code `PAYLOAD_TOO_LARGE`, before anything is stored. |
| `presigned_ttl` | 3600 s | Presigned upload URLs and non-gated download URLs issued from then on. |
| `session_ttl` | 86400 s | Chunked upload sessions started from then on. |

Per key, a saved value (an admin save or an env override) wins over the
constructor's `presignedTtl` / `sessionTtl`, and those win over the namespace's
declared default. A save reaches the doors without a restart; URLs and sessions
already issued keep their lifetime. With no settings namespace bound
(`bindToSettings: false`, or no settings service) the constructor TTLs apply and
no upload size limit is enforced. On the S3 adapter a presigned upload is judged
by its declared size — the bytes go straight to the bucket.
All three keys resolve by one precedence, highest first: a saved value (an
admin save or an env override), then the host's option on the plugin's
constructor (or on `mountStorageRoutes`) — `presignedTtl`, `sessionTtl`,
`maxUploadMb` — then the namespace's declared default, then the built-in
default (3600 s, 86400 s, 100 MB). A save reaches the doors without a restart;
URLs and sessions already issued keep their lifetime.

**The upload bound is never unset.** With no settings namespace bound
(`bindToSettings: false`, no settings service, a settings read that failed,
or `mountStorageRoutes` over a bare adapter) and no host option, every upload
door enforces the built-in 100 MB — the number the manifest declares. No limit
is an explicit host choice:

```typescript
new StorageServicePlugin({ maxUploadMb: 500 }); // the host's bound, in MB
new StorageServicePlugin({ maxUploadMb: 'unlimited' }); // no bound, announced at warn on boot
```

Anything else (`0`, a negative or non-finite number, a number too small to
bound one byte, a numeric string, any other string) refuses construction. Each
host door — the plugin, `mountStorageRoutes`, `registerStorageRoutes` — says once
the bound in force and the layer it came from; an explicit `'unlimited'` is said
at `warn`, on `console.warn` when the door was given no logger.

Who moved from no bound to 100 MB, and the remedy:

| Host | Remedy |
|:---|:---|
| Constructs the plugin with no settings bound (`bindToSettings: false`, `--preset minimal`), or its settings read fails | `maxUploadMb: 500` or `'unlimited'` on the plugin |
| `mountStorageRoutes` / `registerStorageRoutes` over a bare adapter or an unbound plugin | the same option on that call |
| DevPlugin-composed server (DevPlugin builds the plugin with no options) | `new DevPlugin({ services: { storage: false }, extraPlugins: [new StorageServicePlugin({ maxUploadMb: 500 })] })`, or list the instance in the stack's `plugins` |
| A saved or env-sourced `max_upload_mb` that is not a positive number | correct the saved value; the saved layer cannot spell "no bound", so to accept any size remove it and pass `maxUploadMb: 'unlimited'` | On the S3 adapter a presigned upload is judged by
its declared size — the bytes go straight to the bucket.

## REST API Endpoints

Expand Down
1 change: 1 addition & 0 deletions packages/services/service-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@objectstack/objectql": "workspace:*",
"@objectstack/driver-sql": "workspace:*",
"@objectstack/plugin-audit": "workspace:*",
"@objectstack/service-settings": "workspace:*",
"@types/node": "^26.6.5",
"tsx": "^4.23.15",
"typescript": "^6.0.3",
Expand Down
15 changes: 13 additions & 2 deletions packages/services/service-storage/src/field-file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import {
// The upload doors' own size verdict and refusal, asked here rather than
// restated.
import {
parseHostMaxUploadMb,
resolveStorageLimits,
uploadSizeRefusal,
UPLOAD_TOO_LARGE_CODE,
UPLOAD_TOO_LARGE_STATUS,
type HostMaxUploadMb,
type StorageLimitsSnapshot,
} from './storage-limits.js';
// The upload doors' own storage-key layout.
Expand Down Expand Up @@ -81,6 +83,12 @@ export interface FieldFileServiceDeps {
getStorage(): IStorageService | null;
/** The `storage` namespace's Limits group as last read — the upload doors' own source. */
getLimitsSnapshot(): StorageLimitsSnapshot | undefined;
/**
* The host's upload bound (the plugin's `maxUploadMb`, already validated) —
* the host layer of the doors' own precedence; absent ⇒ the namespace
* default, else the built-in 100 MB.
*/
hostMaxUploadMb?: HostMaxUploadMb;
logger: FileReferenceLogger;
}

Expand Down Expand Up @@ -178,7 +186,10 @@ function isSlot(owner: FieldFileSlot | null, slot: FieldFileSlot): boolean {
}

export class FieldFileService implements IFieldFileService {
constructor(private readonly deps: FieldFileServiceDeps) {}
constructor(private readonly deps: FieldFileServiceDeps) {
// The host bound is judged where the verb is built, as at every host door.
parseHostMaxUploadMb(deps.hostMaxUploadMb, 'FieldFileService');
}

async storeFile(input: StoreFieldFileInput): Promise<StoredFieldFile> {
const request = validateStoreInput(input);
Expand All @@ -190,7 +201,7 @@ export class FieldFileService implements IFieldFileService {
const size = request.data.byteLength;
const tooLarge = uploadSizeRefusal(
size,
resolveStorageLimits(this.deps.getLimitsSnapshot()).maxUploadBytes,
resolveStorageLimits(this.deps.getLimitsSnapshot(), { maxUploadMb: this.deps.hostMaxUploadMb }).maxUploadBytes,
(n) => `The file ${request.filename} is ${n} bytes`,
);
if (tooLarge !== null) refuse(UPLOAD_TOO_LARGE_CODE, UPLOAD_TOO_LARGE_STATUS, tooLarge);
Expand Down
13 changes: 12 additions & 1 deletion packages/services/service-storage/src/file-attachment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ import { createWallOrganizationResolver } from './backfill-sys-file-organization
// The upload doors' own size verdict and refusal, asked here rather than
// restated.
import {
parseHostMaxUploadMb,
resolveStorageLimits,
uploadSizeRefusal,
UPLOAD_TOO_LARGE_CODE,
UPLOAD_TOO_LARGE_STATUS,
type HostMaxUploadMb,
type StorageLimitsSnapshot,
} from './storage-limits.js';
// The upload doors' own storage-key layout, and the commit door's question
Expand Down Expand Up @@ -140,6 +142,12 @@ export interface FileAttachmentServiceDeps {
getStorage(): IStorageService | null;
/** The `storage` namespace's Limits group as last read — the upload doors' own source. */
getLimitsSnapshot(): StorageLimitsSnapshot | undefined;
/**
* The host's upload bound (the plugin's `maxUploadMb`, already validated) —
* the host layer of the doors' own precedence; absent ⇒ the namespace
* default, else the built-in 100 MB.
*/
hostMaxUploadMb?: HostMaxUploadMb;
logger: { warn(message: string): void; debug?(message: string): void };
/** The sharing service, for the attach rule's verdict ({@link FileAttachmentService.assertMayAttach}). */
getSharing?(): AttachmentSharingLike | null | undefined;
Expand Down Expand Up @@ -315,13 +323,16 @@ export class FileAttachmentService
private readonly recordFiles: RecordFileAccess | null;

constructor(private readonly deps: FileAttachmentServiceDeps) {
// The host bound is judged where the verb is built, as at every host door.
parseHostMaxUploadMb(deps.hostMaxUploadMb, 'FileAttachmentService');
const bound = deps.recordFiles;
this.recordFiles = bound
? new RecordFileAccess({
...bound,
getEngine: () => deps.getEngine(),
getStorage: () => deps.getStorage(),
getLimitsSnapshot: () => deps.getLimitsSnapshot(),
hostMaxUploadMb: deps.hostMaxUploadMb,
attachments: this,
getSharing: () => deps.getSharing?.(),
getSecurity: () => deps.getSecurity?.(),
Expand Down Expand Up @@ -594,7 +605,7 @@ export class FileAttachmentService
const size = request.data.byteLength;
const tooLarge = uploadSizeRefusal(
size,
resolveStorageLimits(this.deps.getLimitsSnapshot()).maxUploadBytes,
resolveStorageLimits(this.deps.getLimitsSnapshot(), { maxUploadMb: this.deps.hostMaxUploadMb }).maxUploadBytes,
(n) => `The file is ${n} bytes`,
);
if (tooLarge !== null) refuse(UPLOAD_TOO_LARGE_CODE, UPLOAD_TOO_LARGE_STATUS, tooLarge);
Expand Down
5 changes: 5 additions & 0 deletions packages/services/service-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export type {
// group as last read. Types only — the reading and the resolution stay inside
// the package, so the precedence has one definition.
export type { StorageLimitsSnapshot, StorageLimitReading } from './storage-limits.js';
// The host's upload bound (`maxUploadMb` on the plugin, `mountStorageRoutes` and
// `registerStorageRoutes`): its type, so a host can type the option, and the
// refusal a malformed one boots into, so a host can recognise it by class.
export type { HostMaxUploadMb } from './storage-limits.js';
export { InvalidMaxUploadMbError } from './storage-limits.js';
// [#15169] The host door: the storage routes composed from a kernel and
// mounted on an HTTP surface the host owns — for kernels with no `http-server`
// service (cloud's per-environment tenant kernels). Published as ONE entry
Expand Down
Loading
Loading