@@ -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
0 commit comments