Skip to content

Commit 0d5e75c

Browse files
committed
fix(metadata-protocol): refuse an empty projection and a count flag that is neither true nor false
The list normalizer read both as the absent default: a blank or comma-only $select became [] and was served as no projection (every column), and a $count other than true/false stayed a string the opt-out never matched, so the count ran. Both are now refused 400 VALIDATION_ERROR naming the parameter and the value, in findData, which every list door shares; getData's select takes the same projection rule. The accepted count values are read from the spec's QueryTransportParamsSchema.count. Claude-Session: https://claude.ai/code/session_01FEkQ5kRR5RttkLiWWAkDA5 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4274e2e commit 0d5e75c

2 files changed

Lines changed: 91 additions & 11 deletions

File tree

‎packages/metadata-protocol/src/protocol.ts‎

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ import {
156156
foldQueryAliasSlots,
157157
QUERY_TRANSPORT_ALIAS_SLOTS, QUERY_TRANSPORT_DOLLAR_ALIASES, QUERY_TRANSPORT_DOLLAR_PARAMS,
158158
QueryPageWindowParamSchema,
159+
QueryTransportParamsSchema,
159160
type QueryAliasConflict,
160161
type DroppedFieldsEvent, type QueryAST, type EngineQueryOptionsParsed,
161162
type ValidationAdvisoryEvent,
@@ -4619,6 +4620,73 @@ function pageWindowValue(value: unknown, param: string): number {
46194620
throw err;
46204621
}
46214622

4623+
/**
4624+
* A refused query value as the refusal quotes it: JSON, so a blank or
4625+
* whitespace-only value is visible, and bounded, so a long one cannot bury
4626+
* the sentence around it.
4627+
*/
4628+
function quotedQueryValue(value: unknown): string {
4629+
// `String` for a number: `JSON.stringify(Infinity)` is `null`.
4630+
const shown = typeof value === 'number' ? String(value) : JSON.stringify(value) ?? String(value);
4631+
return shown.length > 64 ? `${shown.slice(0, 64)}…` : shown;
4632+
}
4633+
4634+
/**
4635+
* [#26644] A projection (`fields`, spelled `$select` / `select` on the wire) as
4636+
* the list of field names the engine reads, or a refusal naming the parameter
4637+
* the caller wrote.
4638+
*
4639+
* A comma string is split and trimmed as it always was; what is refused is a
4640+
* value that names NO field — a blank or comma-only string, or an empty array.
4641+
* It used to become `[]`, which `assertProjectionFieldsExist` has nothing to
4642+
* check in and every driver serves as no projection: every column, under a
4643+
* 200. Absent means "every column"; an empty projection asks for none, so
4644+
* reading it as absent is the widening {@link pageWindowValue} refuses on the
4645+
* row axis (a blank `$top`), here on the column axis.
4646+
*
4647+
* One rule for both routes that read a projection: the list's `findData` and
4648+
* the by-id `getData`.
4649+
*/
4650+
function projectionFieldsValue(value: string | readonly unknown[], param: string): unknown[] {
4651+
const fields = typeof value === 'string'
4652+
? value.split(',').map((s) => s.trim()).filter(Boolean)
4653+
: [...value];
4654+
if (fields.length > 0) return fields;
4655+
const err: any = new Error(
4656+
`Query parameter '${param}' names no field; got ${quotedQueryValue(value)}. The query was not run: `
4657+
+ 'an empty projection was read as no projection, which answers every column. Name at least one '
4658+
+ 'field, or leave the parameter out to read every column.',
4659+
);
4660+
err.status = 400;
4661+
err.code = 'VALIDATION_ERROR';
4662+
err.param = param;
4663+
throw err;
4664+
}
4665+
4666+
/**
4667+
* [#26644] The response's total-count flag (`count`, spelled `$count` on the
4668+
* wire) as the boolean `findData` reads its opt-out from, or a refusal naming
4669+
* the parameter the caller wrote.
4670+
*
4671+
* The accept set is the spec's `QueryTransportParamsSchema.count`, parsed here
4672+
* rather than restated: the boolean, or the two strings a querystring carries
4673+
* it as (`null` is absent). Any other value used to stay as it was, so the
4674+
* `options.count === false` opt-out read it as "count anyway" — `?$count=maybe`
4675+
* reported a `total` exactly like `?$count=true`.
4676+
*/
4677+
function countFlagValue(value: unknown, param: string): boolean | null | undefined {
4678+
const flag = QueryTransportParamsSchema.shape.count.safeParse(value);
4679+
if (flag.success) return typeof flag.data === 'string' ? flag.data === 'true' : flag.data;
4680+
const err: any = new Error(
4681+
`Query parameter '${param}' must be true or false; got ${quotedQueryValue(value)}. The query was not `
4682+
+ 'run: any other value was read as absent, which counts and reports a total.',
4683+
);
4684+
err.status = 400;
4685+
err.code = 'VALIDATION_ERROR';
4686+
err.param = param;
4687+
throw err;
4688+
}
4689+
46224690
/**
46234691
* [#7321] Refuse a repeated occurrence of any list-query parameter this
46244692
* normalizer reads as single-valued, and normalise the benign one-element array
@@ -12961,10 +13029,14 @@ export class ObjectStackProtocolImplementation implements
1296113029
// querystring param arrives as a bare string — `?fields=name` or the
1296213030
// folded `?select=` / `$select` spellings — which drivers' `.map()`
1296313031
// calls over `query.fields` would otherwise throw on.
13032+
//
13033+
// [#26644] …and a value naming no field is refused rather than becoming
13034+
// `[]`, which every driver serves as no projection at all (see
13035+
// {@link projectionFieldsValue}).
1296413036
const projectionKey = slotParam('fields');
12965-
if (typeof options.fields === 'string') {
12966-
options.fields = options.fields.split(',').map((s: string) => s.trim()).filter(Boolean);
12967-
} else if (options.fields !== undefined && !Array.isArray(options.fields)) {
13037+
if (typeof options.fields === 'string' || Array.isArray(options.fields)) {
13038+
options.fields = projectionFieldsValue(options.fields, projectionKey);
13039+
} else if (options.fields !== undefined) {
1296813040
delete options.fields;
1296913041
}
1297013042
// [#4226] Unknown projection columns are refused rather than dropped —
@@ -13138,11 +13210,12 @@ export class ObjectStackProtocolImplementation implements
1313813210
);
1313913211
if (narrowedSearchFields) options.searchFields = narrowedSearchFields;
1314013212

13141-
// Boolean fields
13142-
for (const key of ['distinct', 'count']) {
13143-
if (options[key] === 'true') options[key] = true;
13144-
else if (options[key] === 'false') options[key] = false;
13145-
}
13213+
// Boolean fields. [#26644] `count` is CHECKED, not merely lowered: a
13214+
// value that is neither true nor false is refused rather than left to
13215+
// read as "count anyway" below (see {@link countFlagValue}).
13216+
if (options.distinct === 'true') options.distinct = true;
13217+
else if (options.distinct === 'false') options.distinct = false;
13218+
if (options.count != null) options.count = countFlagValue(options.count, slotParam('count'));
1314613219

1314713220
// [#2926 ⑩] Every supported OData-style `$` alias has been consumed and
1314813221
// deleted above ($top/$skip/$orderby/$select/$count/$search/
@@ -13536,9 +13609,13 @@ export class ObjectStackProtocolImplementation implements
1353613609
// widened projection would recreate, one route over, the very split
1353713610
// ("two routes, opposite answers for one input") this issue was filed
1353813611
// about.
13539-
if (request.select) {
13540-
queryOptions.fields = typeof request.select === 'string'
13541-
? request.select.split(',').map((s: string) => s.trim()).filter(Boolean)
13612+
//
13613+
// [#26644] …including the empty projection: a blank `?select=` was
13614+
// falsy and skipped, a comma-only one became `[]` — both answered every
13615+
// column. Asked on any value present, as the list path asks it.
13616+
if (request.select != null) {
13617+
queryOptions.fields = typeof request.select === 'string' || Array.isArray(request.select)
13618+
? projectionFieldsValue(request.select, 'select')
1354213619
: request.select;
1354313620
this.assertProjectionFieldsExist(request.object, queryOptions.fields, 'select');
1354413621
}

‎packages/runtime/src/domains/data-empty-projection-count.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protoco
3434
import { declaredEnvelopeForThrow } from '@objectstack/plugin-hono-server';
3535
import { resolveThrownHttpError } from '@objectstack/types';
3636
import { HttpDispatcher } from '../http-dispatcher.js';
37+
import { installDefaultOrganization } from '../default-organization.testkit.js';
3738

3839
const PREFIX = '/api/v1';
3940
const PACKAGE_ID = 'objectstack-test';
@@ -83,6 +84,8 @@ describe('[#26644] the dispatcher /data doors — an empty projection and an unr
8384
await engine.init();
8485
engine.registry.registerObject(INQUIRY as any, PACKAGE_ID);
8586
await engine.syncSchemas();
87+
// A real `single` install's Default Organization (ADR-0131 D9, D1).
88+
await installDefaultOrganization(engine);
8689
for (const id of ['r1', 'r2', 'r3']) await engine.insert('inquiry', { id, name: `n-${id}`, note: `x-${id}` } as any);
8790
for (const verb of ['find', 'findOne', 'count', 'aggregate'] as const) {
8891
const real = driver[verb].bind(driver);

0 commit comments

Comments
 (0)