Skip to content

Commit 2019e19

Browse files
committed
fix(rest)!: the batch door judges each op's exposure on the object the data door serves
The cross-object batch door's per-op exposure gate now reads each op's object through the same by-name read as the /data/:object gate, moved into one private RestServer method both gates call, instead of keying the object list by declared name. Each distinct object is read once per request. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwgfUuNAvcUg3JzBiR
1 parent db7513a commit 2019e19

6 files changed

Lines changed: 83 additions & 46 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@objectstack/rest': patch
3+
---
4+
5+
fix(rest)!: the cross-object batch door judges each op's API exposure on the object the data door serves for its name (#27899)
6+
7+
Clause-②: no (narrowing) — on `POST {basePath}/batch`, an op that names a legacy `ns__short` object by its short name is now held to that object's own `enable` settings and refused with the data door's 404 or 405, before the transaction opens. No value, key or shape is added.
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export or type changes: the batch door's exposure gate now reads each op's object the way every /data/:object route's gate already reads it, and answers the two already-published refusals (404 OBJECT_API_DISABLED, 405 OBJECT_API_METHOD_NOT_ALLOWED) those routes answer. There is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** the cross-object batch door (`POST {basePath}/batch`) refuses an op it used to write (`@objectstack/rest`), per triage 6514615922.
13+
14+
- **Before.** The door's per-op exposure gate searched the object list for an item declared under the op's exact name. A legacy `ns__short` object named by its short name was not found there, so the op passed as an unknown object, and the engine, which resolves the short name, then wrote it. `enable.apiEnabled: false` and an `enable.apiMethods` list were not applied to it on this door, while every `/data/:object` route already applied them.
15+
- **Now.** The gate reads each op's object through the same by-name read as the `/data/:object` routes' gate, so it judges the object the data door serves for that name. Such an op is refused with the answer those routes give, status and body: 404 `OBJECT_API_DISABLED`, or 405 `OBJECT_API_METHOD_NOT_ALLOWED` for a write the object does not expose in bulk. The whole batch is refused before the transaction opens, so no op in it is written. Each distinct object is read once per request. No shipped object carries a `__` name.
16+
- **Who sees it.** A client that sends a batch op naming a legacy `ns__short` object by its short name, where that object sets `enable.apiEnabled: false` or an `enable.apiMethods` list that leaves the write or `bulk` out. The same op under the full name was already refused.
17+
18+
**The remedy.** Nothing changes for objects the API exposes. For a restricted `ns__short` object, the short name now answers on the batch door as it does on every `/data/:object` route: lift the object's `enable` restriction if the API should serve it.

‎packages/rest/src/batch-routes.ts‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface BatchRouteHost {
8585
enforceAuth: RestServer['enforceAuth'];
8686
enforceApiAccess: RestServer['enforceApiAccess'];
8787
enforceBatchSize: RestServer['enforceBatchSize'];
88-
loadObjectItems: RestServer['loadObjectItems'];
88+
loadObjectItem: RestServer['loadObjectItem'];
8989
}
9090

9191
/** One operation of the cross-object batch, as the spec contract parses it. */
@@ -279,25 +279,31 @@ export function registerBatchEndpoints(this: BatchRouteHost, basePath: string):
279279
// Enforce object-level API exposure (enable.apiEnabled /
280280
// apiMethods) for EVERY op BEFORE opening the transaction — the
281281
// batch write surface must honour the same per-object gate as the
282-
// single-record routes (ADR-0049 / #1889). Metadata is fetched
283-
// once; each distinct (object, action) is checked once.
282+
// single-record routes (ADR-0049 / #1889). Each distinct object
283+
// is read once; each distinct (object, action) is checked once.
284+
//
285+
// [#27899] Read through the single-record routes' own read
286+
// (`loadObjectItem`), so an op is judged on the object the data
287+
// door serves for its name: a legacy `ns__short` object named by
288+
// its short name is that object, as `registry.getObject`
289+
// resolves it. The object LIST keyed by declared name found
290+
// nothing under the short name, and the op passed on as unknown
291+
// to an engine that does resolve it.
284292
const p = await this.resolveProtocol(environmentId, req);
285-
const items = await this.loadObjectItems(p, environmentId);
286-
if (items.length > 0) {
287-
const byName = new Map<string, any>(items.map((o: any) => [o?.name, o]));
288-
const checked = new Set<string>();
289-
for (const op of ops) {
290-
const key = `${op.object}\u0000${op.action}`;
291-
if (checked.has(key)) continue;
292-
checked.add(key);
293-
const obj = byName.get(op.object);
294-
if (!obj) continue; // unknown object → surfaced by the op inside the tx
295-
// [#3391] Cross-object batch is a bulk surface: gate each op
296-
// as `bulk ∧ child(op.action)` — the object must grant the
297-
// `bulk` primitive AND the specific write it performs.
298-
const denial = apiAccessDenialFromEnable(obj.enable, op.object, 'bulk', { bulkChild: op.action });
299-
if (denial) { res.status(denial.status).json(denial.body); return; }
300-
}
293+
const objects = new Map<string, any>();
294+
const checked = new Set<string>();
295+
for (const op of ops) {
296+
const key = `${op.object}\u0000${op.action}`;
297+
if (checked.has(key)) continue;
298+
checked.add(key);
299+
if (!objects.has(op.object)) objects.set(op.object, await this.loadObjectItem(p, environmentId, op.object));
300+
const obj = objects.get(op.object);
301+
if (!obj) continue; // unknown object → surfaced by the op inside the tx
302+
// [#3391] Cross-object batch is a bulk surface: gate each op
303+
// as `bulk ∧ child(op.action)` — the object must grant the
304+
// `bulk` primitive AND the specific write it performs.
305+
const denial = apiAccessDenialFromEnable(obj.enable, op.object, 'bulk', { bulkChild: op.action });
306+
if (denial) { res.status(denial.status).json(denial.body); return; }
301307
}
302308

303309
// A write to `sys_api_key` from a request an API key

‎packages/rest/src/meta-item-read-gate.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,10 @@ export type NavDocAudienceGate = (entry: any) => boolean;
440440
* Load the object metadata items for the current protocol/environment,
441441
* coerced to a plain array. Returns `[]` when metadata is unavailable so
442442
* callers fail OPEN (the data call itself needs the same metadata and will
443-
* surface any real error). Shared by the cross-object batch route (all ops,
444-
* fetched once) and the nav-servability gate; the single-object exposure gate
445-
* reads its one object by name ({@link loadObjectItem}).
443+
* surface any real error). Read by the nav-servability gate, and by
444+
* {@link loadObjectItem} for a protocol with no by-name read; the exposure
445+
* gate of every data door, the cross-object batch route's per-op gate
446+
* included, reads its object by name through {@link loadObjectItem}.
446447
*/
447448
export async function loadObjectItems(source: MetaReadGateListSource): Promise<any[]> {
448449
try {

‎packages/rest/src/objectql-slot-consumer-census.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
*
8888
* ⚠️ The healthy control is deliberately driven with `{ operations: [] }` and
8989
* not with a real op. On the MULTI-KERNEL wiring a real op continues past this
90-
* door's engine line into `resolveProtocol`/`loadObjectItems`, which this
90+
* door's engine line into `resolveProtocol`/`loadObjectItem`, which this
9191
* harness's auth-only kernel cannot serve — measured, that answers 500
9292
* INTERNAL_ERROR for a HEALTHY engine too, which would make a 500 read here
9393
* ambiguous between "the seam answered" and "the harness ran out of kernel".

‎packages/rest/src/rest-server.ts‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,12 +2692,7 @@ export class RestServer {
26922692
// [#24031] The ONE object this gate judges, read by name — never the
26932693
// whole object list, whose decoration diagnosed every object on every
26942694
// request. A protocol with no by-name read is asked the list.
2695-
const listSource = this.metaListSource(p, environmentId);
2696-
const getObjectItem = this.objectItemReader(p, environmentId);
2697-
const obj = await metaReadGate.loadObjectItem(
2698-
getObjectItem ? { ...listSource, getObjectItem } : listSource,
2699-
objectName,
2700-
);
2695+
const obj = await this.loadObjectItem(p, environmentId, objectName);
27012696
// [#3770] Unknown object → no declared exposure policy to enforce here;
27022697
// the data path's registry gate 404s it. See the doc comment above.
27032698
if (!obj) return false;
@@ -2901,14 +2896,31 @@ export class RestServer {
29012896
}
29022897

29032898
/**
2904-
* Load the object metadata items for the current protocol/environment,
2905-
* coerced to a plain array — `loadObjectItems` in
2906-
* `./meta-item-read-gate.ts` (fail OPEN and logged, #3545). Shared by
2907-
* `enforceApiAccess` (one object), the cross-object batch route (all ops,
2908-
* fetched once) and the nav-servability gate.
2899+
* The ONE object the API-exposure gate judges for `name`, or `undefined`
2900+
* when no object is behind it — `loadObjectItem` in
2901+
* `./meta-item-read-gate.ts` (fail OPEN and logged, #3545), over this
2902+
* protocol's by-name read where it has one ({@link objectItemReader}),
2903+
* else its list read.
2904+
*
2905+
* [#27899] The one read of every data door's exposure gate:
2906+
* `enforceApiAccess` asks it for the route's `:object`, the cross-object
2907+
* batch route for each op's object. The by-name read resolves a legacy
2908+
* `ns__short` object from its short name as the data door's
2909+
* `registry.getObject` does, so both doors judge the object the data call
2910+
* then serves; the batch route used to key the object LIST by declared
2911+
* name instead, and the list carries no such resolution.
29092912
*/
2910-
private async loadObjectItems(p: RestProtocol, environmentId: string | undefined): Promise<any[]> {
2911-
return metaReadGate.loadObjectItems(this.metaListSource(p, environmentId));
2913+
private async loadObjectItem(
2914+
p: RestProtocol,
2915+
environmentId: string | undefined,
2916+
name: string,
2917+
): Promise<any | undefined> {
2918+
const listSource = this.metaListSource(p, environmentId);
2919+
const getObjectItem = this.objectItemReader(p, environmentId);
2920+
return metaReadGate.loadObjectItem(
2921+
getObjectItem ? { ...listSource, getObjectItem } : listSource,
2922+
name,
2923+
);
29122924
}
29132925

29142926
/**
@@ -3649,9 +3661,9 @@ export class RestServer {
36493661
* protocol has no list read at all.
36503662
*
36513663
* [#25783] The `object` list on the protocol's EXECUTION face where it has
3652-
* one: its readers — the nav-servability gate on `/meta/app`, the batch
3653-
* route's exposure gate, the by-name gate's list fallback — read each
3654-
* object's `enable` and serve none of it, and the served face diagnosed
3664+
* one: its readers — the nav-servability gate on `/meta/app` and the
3665+
* by-name exposure gate's list fallback — read each object's `enable` and
3666+
* serve none of it, and the served face diagnosed
36553667
* every object of the registry on every such request (a full schema
36563668
* `safeParse` each). Same sources, merge and governance either way; the
36573669
* `book` and `doc` lists stay on the served face, unchanged.
@@ -6213,7 +6225,7 @@ export class RestServer {
62136225
enforceAuth: (...a) => this.enforceAuth(...a),
62146226
enforceApiAccess: (...a) => this.enforceApiAccess(...a),
62156227
enforceBatchSize: (...a) => this.enforceBatchSize(...a),
6216-
loadObjectItems: (...a) => this.loadObjectItems(...a),
6228+
loadObjectItem: (...a) => this.loadObjectItem(...a),
62176229
};
62186230
registerBatchEndpoints.call(host, basePath);
62196231
}

‎packages/rest/src/rest.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4053,27 +4053,27 @@ describe('RestServer — object API exposure (apiEnabled / apiMethods)', () => {
40534053
// legitimately-empty registry stays silent — so a persistent outage, during
40544054
// which the gate silently allows every op, is no longer invisible.
40554055
describe('metadata-unavailable fail-open observability (#3545)', () => {
4056-
it('loadObjectItems fails OPEN (returns []) AND logs a THROWN metadata read', async () => {
4056+
it('loadObjectItem fails OPEN (no object) AND logs a THROWN metadata read', async () => {
40574057
const { rest, protocol } = setup(undefined);
40584058
protocol.getMetaItems = vi.fn().mockRejectedValue(new Error('metadata store down'));
40594059
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
40604060
try {
4061-
const items = await (rest as any).loadObjectItems(protocol, undefined);
4062-
expect(items).toEqual([]); // fail-open preserved: gate abstains
4061+
const item = await (rest as any).loadObjectItem(protocol, undefined, 'widget');
4062+
expect(item).toBeUndefined(); // fail-open preserved: gate abstains
40634063
expect(warnSpy).toHaveBeenCalled();
40644064
expect(String(warnSpy.mock.calls[0]?.[0] ?? '')).toContain('api-exposure gate');
40654065
} finally {
40664066
warnSpy.mockRestore();
40674067
}
40684068
});
40694069

4070-
it('loadObjectItems stays SILENT on a legitimately-empty registry (no false alarm)', async () => {
4070+
it('loadObjectItem stays SILENT on a legitimately-empty registry (no false alarm)', async () => {
40714071
const { rest, protocol } = setup(undefined);
40724072
protocol.getMetaItems = vi.fn().mockResolvedValue([]); // empty, not thrown
40734073
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
40744074
try {
4075-
const items = await (rest as any).loadObjectItems(protocol, undefined);
4076-
expect(items).toEqual([]);
4075+
const item = await (rest as any).loadObjectItem(protocol, undefined, 'widget');
4076+
expect(item).toBeUndefined();
40774077
expect(warnSpy).not.toHaveBeenCalled(); // distinguishes real fault from empty
40784078
} finally {
40794079
warnSpy.mockRestore();

0 commit comments

Comments
 (0)