Skip to content

Commit 34997b8

Browse files
committed
fix(rest)!: /meta list refuses a bracketed object key on views and a bracketed package key on the list and book tree
Claude-Session: https://claude.ai/code/session_01ADHkmPfJsJeWNxeaZkvyEM Co-authored-by: Claude <noreply@anthropic.com>
1 parent 919e71b commit 34997b8

3 files changed

Lines changed: 117 additions & 31 deletions

File tree

‎packages/rest/src/meta-collection-routes.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,16 @@ export function registerMetaCollectionRoutes(this: MetaCollectionRouteHost, scop
639639
// [#23214] The list lives beside the shared decision, so the
640640
// runtime dispatcher's twin refuses the same names.
641641
if (refuseRepeatedQueryParams(req, res, metaReadGate.META_QUERY_ARITY_DOORS.list)) return;
642+
// A bracketed `package` key (`?package[]=`) is read by no
643+
// binding below, so it scoped nothing: refused here, by the
644+
// decision the dispatcher's twin asks too.
645+
const bracketedPackage = metaReadGate.metaBracketedPackageRefusal('list', req.query);
646+
if (bracketedPackage) {
647+
res.status(bracketedPackage.status).json({
648+
error: { code: bracketedPackage.code, message: bracketedPackage.message },
649+
});
650+
return;
651+
}
642652
// [#22188] Read through {@link metaItemPackageBinding}:
643653
// `all`, this list's "show everything" scope, names no package.
644654
const packageId = metaItemPackageBinding(req.query?.package);

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

Lines changed: 97 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,13 +2416,14 @@ const LAYERED_VIEW_MEMBER_MESSAGE =
24162416
* VALIDATION_ERROR`, the code the recognition and arity rules answer
24172417
* everywhere; `rule` names which of the three refused, because `RestServer`'s
24182418
* item read answers the third at a different point (see
2419-
* {@link metaItemQueryRefusal}).
2419+
* {@link metaItemQueryRefusal}). The arity doors answer a fourth,
2420+
* `bracketed-parameter` ({@link metaBracketedPackageRefusal}).
24202421
*/
24212422
export interface MetaItemQueryRefusal {
24222423
readonly status: 400;
24232424
readonly code: 'VALIDATION_ERROR';
24242425
readonly message: string;
2425-
readonly rule: 'unknown-parameter' | 'repeated-parameter' | 'layered-member';
2426+
readonly rule: 'unknown-parameter' | 'repeated-parameter' | 'layered-member' | 'bracketed-parameter';
24262427
}
24272428

24282429
/**
@@ -2556,13 +2557,51 @@ export const META_QUERY_ARITY_DOORS: Readonly<Record<MetaQueryArityDoor, readonl
25562557
* each transport writes in its own envelope. It returns the first name of the
25572558
* door's {@link META_QUERY_ARITY_DOORS} entry that was supplied more than once,
25582559
* as `400 VALIDATION_ERROR` with `repeatedQueryParamMessage`'s sentence, which
2559-
* is the answer `RestServer`'s door gives through `refuseRepeatedQueryParams`.
2560-
* It returns `undefined` otherwise, and has then unwrapped a one-element array
2561-
* in place. ⛔ Ask it before any read of the door's query.
2560+
* is the answer `RestServer`'s door gives through `refuseRepeatedQueryParams`;
2561+
* then a bracketed `package` key ({@link metaBracketedPackageRefusal}), which
2562+
* `RestServer`'s door asks right after its multiplicity gate. It returns
2563+
* `undefined` otherwise, and has then unwrapped a one-element array in place.
2564+
* ⛔ Ask it before any read of the door's query.
25622565
*/
25632566
export function metaQueryArityRefusal(door: MetaQueryArityDoor, query: unknown): MetaItemQueryRefusal | undefined {
25642567
if (!query || typeof query !== 'object') return undefined;
2565-
return repeatedOnceRefusal(query as Record<string, unknown>, META_QUERY_ARITY_DOORS[door]);
2568+
return repeatedOnceRefusal(query as Record<string, unknown>, META_QUERY_ARITY_DOORS[door])
2569+
?? metaBracketedPackageRefusal(door, query);
2570+
}
2571+
2572+
/**
2573+
* The answer of the two arity doors that read `?package=` — the list and the
2574+
* book tree — to a BRACKETED spelling of it: `?package[]=ID`, `?package[0]=ID`,
2575+
* any key that opens `package[`. A client that serialises arrays qs-style sends
2576+
* it; every adapter hands the key through as spelled, so the door's
2577+
* `metaItemPackageBinding(query.package)` read no package, and a caller who
2578+
* named ONE package was answered across every package. `400
2579+
* VALIDATION_ERROR`, in the list chain's narrowing-filter frame
2580+
* ({@link listFilterRefusalMessage}), naming the key as sent; beside a plain
2581+
* `?package=` too (two spellings of one scope). Asked at the door's entry,
2582+
* before any read — where the binding is read — by both transports:
2583+
* {@link metaQueryArityRefusal} for the dispatcher, after
2584+
* `refuseRepeatedQueryParams` for `RestServer`.
2585+
*
2586+
* Every by-name `/meta` door (the item read, `/layers`, the save, the reset,
2587+
* `/publish`) and the diagnostics sweep already refuse the key: it is not in
2588+
* their closed set (route rule 5). `_drafts` and `state` read no `package`.
2589+
*/
2590+
export function metaBracketedPackageRefusal(door: MetaQueryArityDoor, query: unknown): MetaItemQueryRefusal | undefined {
2591+
if (!META_QUERY_ARITY_DOORS[door].includes('package')) return undefined;
2592+
const key = bracketedKeyOf(query, 'package');
2593+
if (key === undefined) return undefined;
2594+
const tree = door === 'book-tree';
2595+
return {
2596+
status: 400,
2597+
code: 'VALIDATION_ERROR',
2598+
message: listFilterRefusalMessage(
2599+
key, 'package', "the 'package' filter takes exactly one package id, spelled ?package=ID",
2600+
"a bracketed 'package' key", tree ? 'tree' : 'list',
2601+
tree ? 'read the tree across every package' : 'list across every package',
2602+
),
2603+
rule: 'bracketed-parameter',
2604+
};
25662605
}
25672606

25682607
/**
@@ -3422,31 +3461,56 @@ function refuseEmptyListFilter(param: 'id' | 'object', names: 'app' | 'object',
34223461
}
34233462

34243463
/**
3425-
* The app list's answer to a BRACKETED spelling of its one-app filter —
3426-
* `?id[]=NAME`, `?id[0]=NAME`, any key that opens `id[`. A client that
3427-
* serialises arrays qs-style sends it; every adapter hands the key through as
3428-
* spelled (`readQuery` in `plugin-hono-server`, `readDispatchQuery` in
3429-
* `@objectstack/hono`), so step 3 of {@link createMetaListAnswer}, which reads
3430-
* `query.id`, never saw it, and the caller who asked for one app was answered
3431-
* every app it may see. Refused with {@link refuseEmptyListFilter}'s answer —
3432-
* one sentence frame, `400 VALIDATION_ERROR` — naming the key as sent: the
3433-
* filter takes exactly one name, spelled `?id=NAME`. A bracketed key beside a
3434-
* plain `?id=` is refused too: the request carries two spellings of one filter.
3435-
*
3436-
* Only the app list reads `id`, so only it judges the bracketed spelling; on
3437-
* every other type the key is not read, as no other `id` spelling is.
3464+
* The list chain's answer to a BRACKETED spelling of a one-name filter —
3465+
* `?id[]=NAME` or `?id[0]=NAME` on the app list (step 3), `?object[]=NAME` or
3466+
* `?object[0]=NAME` on the view list (step 4): any key that opens `param[`. A
3467+
* client that serialises arrays qs-style sends it; every adapter hands the key
3468+
* through as spelled (`readQuery` in `plugin-hono-server`, `readDispatchQuery`
3469+
* in `@objectstack/hono`), so the step, which reads `query[param]`, never saw
3470+
* it, and the caller who asked for one app, or for one object's views, was
3471+
* answered every item it may see. Refused with {@link refuseEmptyListFilter}'s
3472+
* answer — one sentence frame, `400 VALIDATION_ERROR` — naming the key as
3473+
* sent: the filter takes exactly one name, spelled `?param=NAME`. A bracketed
3474+
* key beside a plain one is refused too: the request carries two spellings of
3475+
* one filter.
3476+
*
3477+
* Only the type whose step reads `param` judges its bracketed spelling; on
3478+
* every other type the key is not read, as no other spelling of it is.
34383479
*/
3439-
function refuseBracketedAppIdFilter(query: Readonly<Record<string, unknown>> | undefined): void {
3440-
if (!query || typeof query !== 'object') return;
3441-
const key = Object.keys(query).find((k) => k.startsWith('id['));
3480+
function refuseBracketedListFilter(
3481+
query: Readonly<Record<string, unknown>> | undefined,
3482+
param: 'id' | 'object',
3483+
names: 'app' | 'object',
3484+
lists: 'app' | 'view',
3485+
): void {
3486+
const key = bracketedKeyOf(query, param);
34423487
if (key === undefined) return;
34433488
throwListFilterRefusal(
3444-
'id', key, 'app', 'app',
3445-
"the 'id' filter takes exactly one app name, spelled ?id=NAME", "a bracketed 'id' key",
3489+
param, key, names, lists,
3490+
`the '${param}' filter takes exactly one ${names} name, spelled ?${param}=NAME`, `a bracketed '${param}' key`,
34463491
);
34473492
}
34483493

3449-
/** The one sentence frame of the list chain's narrowing-filter refusals. */
3494+
/** The first query key that opens `param[` (a qs-style array spelling of `param`), or `undefined`. */
3495+
function bracketedKeyOf(query: unknown, param: string): string | undefined {
3496+
if (!query || typeof query !== 'object') return undefined;
3497+
return Object.keys(query).find((k) => k.startsWith(`${param}[`));
3498+
}
3499+
3500+
/** The one sentence frame of the `/meta` list doors' narrowing-filter refusals. */
3501+
function listFilterRefusalMessage(
3502+
key: string,
3503+
names: string,
3504+
got: string,
3505+
notAFilter: string,
3506+
served: 'list' | 'tree',
3507+
unscoped: string,
3508+
): string {
3509+
return `Query parameter '${key}' names no ${names}; ${got}. The ${served} was not served: ${notAFilter} `
3510+
+ `is not a filter. Name one ${names}, or leave the parameter out to ${unscoped}.`;
3511+
}
3512+
3513+
/** {@link listFilterRefusalMessage}, thrown, so each transport's own `catch` shapes it. */
34503514
function throwListFilterRefusal(
34513515
param: 'id' | 'object',
34523516
key: string,
@@ -3456,8 +3520,7 @@ function throwListFilterRefusal(
34563520
notAFilter: string,
34573521
): never {
34583522
const err: any = new Error(
3459-
`Query parameter '${key}' names no ${names}; ${got}. The list was not served: ${notAFilter} `
3460-
+ `is not a filter. Name one ${names}, or leave the parameter out to list every ${lists} you can see.`,
3523+
listFilterRefusalMessage(key, names, got, notAFilter, 'list', `list every ${lists} you can see`),
34613524
);
34623525
err.status = 400;
34633526
err.code = 'VALIDATION_ERROR';
@@ -3558,7 +3621,8 @@ async function describeMetaPositionList(sources: MetaListAnswerSources, list: an
35583621
* list, never a 404.
35593622
* 4. `view` — `?object=<object>` keeps the independent views bound to that
35603623
* object, sorted by `order` then `name` (the switcher); [#28548] an EMPTY
3561-
* value is refused (`400 VALIDATION_ERROR`), never read as absent.
3624+
* value is refused (`400 VALIDATION_ERROR`), never read as absent, as is a
3625+
* bracketed key (`?object[]=`, `?object[0]=`), never read as no filter.
35623626
* 5. `doc` — the ADR-0046 locale collapse (the request's own preference; the
35633627
* `translations` map is dropped whatever the locale).
35643628
* 6. `doc` — the ADR-0046 content slim, unless `?include=content`.
@@ -3614,8 +3678,8 @@ export function createMetaListAnswer(
36143678

36153679
// 3. [#7566] `?id=<app>`. [#28548] An EMPTY one is refused, never
36163680
// read as absent (see {@link refuseEmptyListFilter}), and so is a
3617-
// bracketed spelling of the key (see {@link refuseBracketedAppIdFilter}).
3618-
if (metaType === 'app') refuseBracketedAppIdFilter(query);
3681+
// bracketed spelling of the key (see {@link refuseBracketedListFilter}).
3682+
if (metaType === 'app') refuseBracketedListFilter(query, 'id', 'app', 'app');
36193683
const appIdFilter = metaType === 'app' ? query?.id : undefined;
36203684
if (appIdFilter === '') refuseEmptyListFilter('id', 'app', 'app');
36213685
if (typeof appIdFilter === 'string') {
@@ -3628,7 +3692,9 @@ export function createMetaListAnswer(
36283692
}
36293693

36303694
// 4. `?object=<object>` — the view switcher. [#28548] An EMPTY one is
3631-
// refused, never read as absent (see {@link refuseEmptyListFilter}).
3695+
// refused, never read as absent (see {@link refuseEmptyListFilter}),
3696+
// and so is a bracketed spelling of the key, as step 3's is.
3697+
if (metaType === 'view') refuseBracketedListFilter(query, 'object', 'object', 'view');
36323698
if (metaType === 'view' && query?.object === '') refuseEmptyListFilter('object', 'object', 'view');
36333699
if (metaType === 'view' && query?.object) {
36343700
const obj = String(query.object);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ export function registerMetaItemReadRoutes(this: MetaItemReadRouteHost, scope: M
383383
// [#6877] One package scopes the book lookup. [#23214] The
384384
// list lives beside the shared decision, as the list door's.
385385
if (refuseRepeatedQueryParams(req, res, metaReadGate.META_QUERY_ARITY_DOORS['book-tree'])) return;
386+
// A bracketed `package` key (`?package[]=`) is read by no
387+
// binding below, so it scoped nothing: refused here, by the
388+
// decision the dispatcher's twin asks too.
389+
const bracketedPackage = metaReadGate.metaBracketedPackageRefusal('book-tree', req.query);
390+
if (bracketedPackage) {
391+
res.status(bracketedPackage.status).json({
392+
error: { code: bracketedPackage.code, message: bracketedPackage.message },
393+
});
394+
return;
395+
}
386396
// [#22188] Read through {@link metaItemPackageBinding}: `all` names no package.
387397
const packageId = metaItemPackageBinding(req.query?.package);
388398
// [#20408] The route's whole answer is

0 commit comments

Comments
 (0)