Skip to content

Commit 7e37c11

Browse files
committed
fix(objectql): narrow the search expansion to the caller's queryable fields (WIP)
1 parent b77bb37 commit 7e37c11

2 files changed

Lines changed: 129 additions & 23 deletions

File tree

‎packages/objectql/src/engine.ts‎

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,14 +2068,22 @@ function assertProjectionHasNoDottedPaths(
20682068
* `INVALID_FIELD`, the code #4254's explicit-override gate answers on this
20692069
* same axis (and the one a search naming a formula title gets), not
20702070
* `INVALID_QUERY`: the request is well-formed, and what cannot be honoured is
2071-
* a fact about the object's FIELDS. 400 on every door, and the same answer for
2072-
* every caller — it is derived from the declaration, never from the caller's
2073-
* permissions (a narrowing by field-level security happens later, in the
2074-
* predicate guard, and `searchAll` hands the engine only the queryable subset).
2071+
* a fact about the object's FIELDS. 400 on every door.
2072+
*
2073+
* [#25672] `withheld`: the declared set is not empty, but every field in it is
2074+
* one the caller may not query, so the narrowing in `expandSearchOnAst` left
2075+
* nothing. Same code and status — the search cannot be honoured for this
2076+
* caller either way — with a message that names the cause, not a declaration
2077+
* fault the object does not have.
20752078
*/
2076-
function emptySearchSetError(object: string, schema: any, internalExcluded: boolean): Error {
2079+
function emptySearchSetError(object: string, schema: any, internalExcluded: boolean, withheld = false): Error {
20772080
const declared: unknown[] = Array.isArray(schema?.searchableFields) ? schema.searchableFields : [];
2078-
const err: any = new Error(
2081+
const err: any = withheld ? new Error(
2082+
`Object '${object}' has no field this caller may search, so 'search' cannot be honoured on it: `
2083+
+ `every field it searches is one field-level security does not let this caller query. `
2084+
+ 'Answering every row instead would read as "everything matched". Grant read access to one of '
2085+
+ `its searchable fields, or filter on a field this caller can read.`,
2086+
) : new Error(
20792087
`Object '${object}' has no searchable field, so 'search' cannot be honoured on it`
20802088
+ (declared.length > 0
20812089
? `: its 'searchableFields' (${declared.join(', ')}) names no stored field`
@@ -14956,7 +14964,11 @@ export class ObjectQL implements IObjectQLEngine {
1495614964
* would hand the driver a key it does not read, which is the same silent drop
1495714965
* one layer down.
1495814966
*/
14959-
private expandSearchOnAst(ast: QueryAST, schema: ServiceObject | undefined): void {
14967+
private async expandSearchOnAst(
14968+
ast: QueryAST,
14969+
schema: ServiceObject | undefined,
14970+
context: ExecutionContext | undefined,
14971+
): Promise<void> {
1496014972
// The `$search`/`$searchFields` OData spellings are NOT read here: the
1496114973
// protocol layer normalizes them to the bare keys before the engine
1496214974
// (protocol.ts findData), and a direct engine call carrying one is an
@@ -14989,14 +15001,30 @@ export class ObjectQL implements IObjectQLEngine {
1498915001
Object.entries(schema.fields as Record<string, unknown>)
1499015002
.filter(([name]) => !internalFields.includes(name)),
1499115003
);
14992-
const searchFilter = expandSearchToFilter(raw, {
15004+
const expandOptions = {
1499315005
fields: scanFields,
1499415006
searchableFields: (schema as any).searchableFields,
1499515007
requestedFields,
1499615008
// [ADR-0079] `nameField` is the canonical primary-title pointer;
1499715009
// `displayNameField` is the deprecated alias (still honored).
1499815010
displayField: (schema as any).nameField ?? (schema as any).displayNameField,
14999-
});
15011+
};
15012+
// [#25672] …and never a column the CALLER may not query. The field-read
15013+
// predicate guard (plugin-security) runs after this expansion and judges
15014+
// the expanded `$or` as the caller's own predicate, so one hidden column
15015+
// in the server-resolved set refused the whole search with 403 —
15016+
// `sys_user`'s `role` / `ban_reason` / `last_login_ip` broke every user
15017+
// picker for members. The set is narrowed to the security service's
15018+
// `getQueryableFields` answer, the published complement of what that
15019+
// guard refuses (one derivation, no second field-permission rule here),
15020+
// asked with the context the middleware will see. A field the caller
15021+
// NAMED in `searchFields` is not withheld, so the guard still refuses it
15022+
// loudly. The guard itself is unchanged, and still judges every
15023+
// predicate the caller wrote.
15024+
const queryableFields = normalizeSearch(raw).query.trim()
15025+
? await this.resolveSearchQueryableFields(ast.object, context)
15026+
: undefined;
15027+
const searchFilter = expandSearchToFilter(raw, { ...expandOptions, queryableFields });
1500015028
if (searchFilter) {
1500115029
ast.where = ast.where ? { $and: [ast.where, searchFilter] } : searchFilter;
1500215030
} else if (normalizeSearch(raw).query.trim()) {
@@ -15011,14 +15039,47 @@ export class ObjectQL implements IObjectQLEngine {
1501115039
// the count) and every door (REST, dispatcher, SDK, a hook's
1501215040
// `ctx.api`). An explicit override intersected to nothing falls back
1501315041
// to the allowed set inside `resolveSearchFields`, so it reaches this
15014-
// only when that set is empty too.
15015-
throw emptySearchSetError(ast.object, schema, internalFields.length > 0);
15042+
// only when that set is empty too. [#25672] A set the caller's
15043+
// queryable fields withheld down to nothing gets the same answer: the
15044+
// declared set expands without the narrowing, so the refusal says so.
15045+
const withheld = queryableFields !== undefined && expandSearchToFilter(raw, expandOptions) !== null;
15046+
throw emptySearchSetError(ast.object, schema, internalFields.length > 0, withheld);
1501615047
}
1501715048
}
1501815049
delete (ast as any).search;
1501915050
delete (ast as any).searchFields;
1502015051
}
1502115052

15053+
/**
15054+
* [#25672] The fields `context` may query on `object`, asked of the
15055+
* registered `security` service's `getQueryableFields` — the answer the
15056+
* field-read predicate guard is the complement of. `undefined` narrows
15057+
* nothing, and is the answer when no context is carried (a context-less
15058+
* read is never asked about, as `searchAll` does), when no security service
15059+
* is registered or it has no such method, or when it gives no answer: the
15060+
* guard still judges the search, so an absent answer can only leave the
15061+
* request as strict as it was. A throw from the method propagates.
15062+
*/
15063+
private async resolveSearchQueryableFields(
15064+
object: string,
15065+
context: ExecutionContext | undefined,
15066+
): Promise<ReadonlySet<string> | undefined> {
15067+
if (context === undefined) return undefined;
15068+
// Read from the engine slot at use (see SECURITY_SERVICE_SOURCE_SLOT), never cached.
15069+
const source = (this as unknown as Record<symbol, unknown>)[SECURITY_SERVICE_SOURCE_SLOT];
15070+
if (typeof source !== 'function') return undefined;
15071+
let svc: Partial<Pick<ISecurityService, 'getQueryableFields'>> | undefined;
15072+
try {
15073+
svc = source() ?? undefined;
15074+
} catch {
15075+
// The in-repo kernels throw on a name nothing registered: no service.
15076+
return undefined;
15077+
}
15078+
if (typeof svc?.getQueryableFields !== 'function') return undefined;
15079+
const queryable = await svc.getQueryableFields(object, context as any);
15080+
return queryable === undefined ? undefined : new Set(queryable);
15081+
}
15082+
1502215083
/**
1502315084
* The aggregate and count verbs' door into {@link expandSearchOnAst} — ⛔ not
1502415085
* a second expander. `QuerySchema.search` (ADR-0061 D1) is declared beside
@@ -15037,11 +15098,15 @@ export class ObjectQL implements IObjectQLEngine {
1503715098
* `opCtx.options` unexpanded. The common path — no search key — returns the
1503815099
* same reference and allocates nothing.
1503915100
*/
15040-
private expandSearchOnOptions<T extends EngineAggregateOptions | EngineCountOptions>(object: string, query: T): T {
15101+
private async expandSearchOnOptions<T extends EngineAggregateOptions | EngineCountOptions>(
15102+
object: string,
15103+
query: T,
15104+
options: EngineReadOptions | undefined,
15105+
): Promise<T> {
1504115106
if (query.search === undefined && query.searchFields === undefined) return query;
1504215107
const { search, searchFields, ...rest } = query;
1504315108
const carrier = { object, where: rest.where, search, searchFields } as QueryAST;
15044-
this.expandSearchOnAst(carrier, this._registry.getObject(object));
15109+
await this.expandSearchOnAst(carrier, this._registry.getObject(object), mergeReadContext(query.context, options?.context));
1504515110
if (carrier.where === undefined) return rest as T;
1504615111
return { ...rest, where: carrier.where } as T;
1504715112
}
@@ -15196,7 +15261,7 @@ export class ObjectQL implements IObjectQLEngine {
1519615261
// fields needed to compute the formulas after fetch.
1519715262
const _findSchema = this._registry.getObject(object);
1519815263

15199-
this.expandSearchOnAst(ast, _findSchema);
15264+
await this.expandSearchOnAst(ast, _findSchema, mergeReadContext(query?.context, options?.context));
1520015265
// [#7642] The caller's OWN projection, captured before any planning pass
1520115266
// rewrites it — the only thing that can answer "did this caller ask for
1520215267
// `__search`?". See `stripSearchCompanionFromRead`.
@@ -15517,7 +15582,7 @@ export class ObjectQL implements IObjectQLEngine {
1551715582
const _findOneSchema = this._registry.getObject(objectName);
1551815583
// Before the guard below, so a `search` that resolves to a real filter
1551915584
// counts as the predicate it is (#4419).
15520-
this.expandSearchOnAst(ast, _findOneSchema);
15585+
await this.expandSearchOnAst(ast, _findOneSchema, mergeReadContext(query?.context, options?.context));
1552115586
this.requireFindOnePredicate(objectName, ast);
1552215587
// [#7095] Same refusal as `find`, and it matters MORE here: `findOne`
1552315588
// applies `limit: 1`, so `orderBy` is the whole of "which record" — a
@@ -21776,7 +21841,7 @@ export class ObjectQL implements IObjectQLEngine {
2177621841
// [#22790] ADR-0061 `search` → the rows are searched before they are
2177721842
// counted, by the one expander `find` runs, at the same point in the
2177821843
// sequence — so the count is the number of rows `find` returns for the bag.
21779-
if (query) query = this.expandSearchOnOptions(object, query);
21844+
if (query) query = await this.expandSearchOnOptions(object, query, options);
2178021845
const driver = this.getDriver(object);
2178121846

2178221847
// The AST must ride on the opCtx so the security/sharing middlewares can
@@ -21919,7 +21984,7 @@ export class ObjectQL implements IObjectQLEngine {
2191921984
// ADR-0061 `search` → the rows are searched BEFORE they are grouped, by
2192021985
// the one expander `find` runs, at the same point in the sequence (after
2192121986
// the `where` doors above, before the AST is built and tokens resolve).
21922-
query = this.expandSearchOnOptions(object, query);
21987+
query = await this.expandSearchOnOptions(object, query, options);
2192321988
this.rejectCredentialAggregation(object, query);
2192421989
// [#20783] …and a `groupBy` entry naming a structured-JSON field (`json`,
2192521990
// `composite`, `address`, …) is refused `INVALID_FIELD` / 400 here, before

‎packages/objectql/src/search-filter.ts‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,20 @@ export {
9292
} from '@objectstack/spec/data';
9393
export type { SearchFieldMeta } from '@objectstack/spec/data';
9494

95-
/** Historical name for {@link SearchFieldResolutionOptions} — same shape. */
96-
export type ExpandSearchOptions = SearchFieldResolutionOptions;
95+
/** {@link SearchFieldResolutionOptions}, plus the caller's queryable-field answer. */
96+
export interface ExpandSearchOptions extends SearchFieldResolutionOptions {
97+
/**
98+
* [#25672] The fields the CALLER may query on — the security service's
99+
* `getQueryableFields` answer, the exact complement of what the engine's
100+
* field-read predicate guard refuses. When present, a field the server
101+
* RESOLVED into the search set that is not in it is withheld, so the
102+
* expansion never names a column the guard would refuse as a filter oracle.
103+
* A field the caller named in `requestedFields` is never withheld: an
104+
* explicit narrowing to a hidden field reaches the guard and is refused
105+
* loudly there. Absent = no answer, nothing withheld.
106+
*/
107+
queryableFields?: ReadonlySet<string>;
108+
}
97109

98110
export interface NormalizedSearch {
99111
query: string;
@@ -197,6 +209,31 @@ function companionWithinSearchFields(searchFields: readonly string[], opts: Expa
197209
return sources.every((f) => inSet.has(f));
198210
}
199211

212+
/**
213+
* [#25672] Withhold from the resolved search set every field the caller may
214+
* not query, except one the caller named itself. The resolved set is the
215+
* server's choice (declared `searchableFields` → auto-default), so a hidden
216+
* column in it is the server widening the caller's predicate past what the
217+
* caller could write — and the predicate guard refused the WHOLE search for
218+
* it (`sys_user`'s `role` / `ban_reason` / `last_login_ip` broke every user
219+
* picker for members). Narrowing happens AFTER resolution, never on the field
220+
* map going in: a declaration whose fields are all hidden must stay empty,
221+
* not fall through to the auto-default and search different columns.
222+
*/
223+
function withholdUnqueryable(
224+
searchFields: string[],
225+
requestedFields: string | string[] | undefined,
226+
queryable: ReadonlySet<string> | undefined,
227+
): string[] {
228+
if (!queryable) return searchFields;
229+
const named = new Set(
230+
typeof requestedFields === 'string'
231+
? requestedFields.split(',').map((f) => f.trim())
232+
: requestedFields ?? [],
233+
);
234+
return searchFields.filter((f) => queryable.has(f) || named.has(f));
235+
}
236+
200237
/**
201238
* Expand a `$search` term into a `{ $or: [...] }` (single term) or
202239
* `{ $and: [{ $or: [...] }, ...] }` (multi-term) filter. Returns `null` in
@@ -209,15 +246,19 @@ function companionWithinSearchFields(searchFields: readonly string[], opts: Expa
209246
* that with `400 INVALID_FIELD` rather than leaving `where` untouched, which
210247
* would answer the search with every row. This function stays pure and
211248
* non-throwing; the judgement lives at the call site every verb reaches.
249+
* [#25672] A set that `queryableFields` withheld down to nothing is this
250+
* case too.
212251
*/
213252
export function expandSearchToFilter(raw: unknown, opts: ExpandSearchOptions): any | null {
214253
const { query, fields: requested } = normalizeSearch(raw);
215254
if (!query || !query.trim()) return null;
216255

217-
const searchFields = resolveSearchFields({
218-
...opts,
219-
requestedFields: requested ?? opts.requestedFields,
220-
});
256+
const requestedFields = requested ?? opts.requestedFields;
257+
const searchFields = withholdUnqueryable(
258+
resolveSearchFields({ ...opts, requestedFields }),
259+
requestedFields,
260+
opts.queryableFields,
261+
);
221262
if (searchFields.length === 0) return null;
222263

223264
const terms = query.trim().split(/\s+/).filter(Boolean);

0 commit comments

Comments
 (0)