8989 * this engine-only reader served it as stored — to an administrator's key, a
9090 * member being refused by the engine as usual. The family's one projection is
9191 * now applied here and the evaluate shapes are refused; see
92- * {@link storedMetadataBodyRefusal} and {@link serveStoredMetadataRows}. What
93- * remains of the divergence above is genuinely not security.
92+ * {@link storedMetadataBodyRefusal} and {@link serveStoredMetadataRows}.
93+ *
94+ * ⚠️ A THIRD limb was security as well: the EVALUATE positions of a field
95+ * declared `internal: true`. The engine omits such a field from every row, but
96+ * it still evaluates one named in a filter or a sort, and the protocol's data
97+ * door is where those positions are refused — for every caller, administrators
98+ * included, before the engine is asked. This engine-only bridge evaluated them.
99+ * `query` and `aggregate` now ask that door's ONE gate of the `protocol`
100+ * service before the engine ({@link refuseInternalFieldEvaluation}), so a
101+ * filter, a sort, a group key or an aggregation over such a field answers the
102+ * door's `400 INVALID_FIELD`, word for word, on this transport as on REST and
103+ * on the HTTP bridge. ⛔ Never a copy of that gate here: a second definition is
104+ * the fork this note exists to close. What remains of the divergence above is
105+ * genuinely not security.
94106 */
95107
96108import { omitInternalFieldsFromWriteResponse , relationConditionSites } from '@objectstack/core' ;
@@ -133,8 +145,8 @@ import {
133145} from './metadata-completeness.js' ;
134146
135147/**
136- * [#25138] The object reads this transport asks of the `protocol` service.
137- * Every member is optional: the slot is filled by name from a host-owned
148+ * [#25138] The object reads this transport asks of the `protocol` service,
149+ * and the data door's internal-field gate beside them. Every member is optional: the slot is filled by name from a host-owned
138150 * registry, and a host may fill it with a partial object (the same duck-typed
139151 * probe `plugin.ts` applies to `getMetaItems` for the skill read).
140152 * `getMetaItemForExecution` is the server-only by-name face the REST exposure
@@ -148,6 +160,14 @@ export interface StdioProtocolObjectReads {
148160 getMetaItem ?( request : { type : string ; name : string } ) : Promise < unknown > ;
149161 /** The by-name read without the serving decorations. */
150162 getMetaItemForExecution ?( request : { type : string ; name : string } ) : Promise < unknown > ;
163+ /**
164+ * The data door's evaluate refusal for a field declared `internal: true`:
165+ * throws its `400 INVALID_FIELD` when `query` filters, sorts, groups or
166+ * aggregates on one, and returns otherwise. Not a read, but asked of the
167+ * same service at the same moment, so it rides this slot
168+ * ({@link refuseInternalFieldEvaluation}).
169+ */
170+ assertNoInternalFieldEvaluated ?( object : string , query : Record < string , unknown > ) : void ;
151171}
152172
153173/** [#25138] Where this transport reads an object's definition from. */
@@ -548,6 +568,38 @@ async function enforceRelationTargetExposure(
548568 }
549569}
550570
571+ /**
572+ * The data door's evaluate refusal for a field declared `internal: true`,
573+ * asked of the `protocol` service before the engine reads anything: the
574+ * protocol's own `assertNoInternalFieldEvaluated`, the gate `findData` asks
575+ * for REST and for the HTTP bridge's `query_records` — ⛔ never a copy of it
576+ * here (route-ownership rule 1). It throws that door's `400 INVALID_FIELD`,
577+ * naming the object, the field and the position in the door's own sentence,
578+ * for a filter (`where`, each aggregation's own `filter`, a nested-relation
579+ * condition judged on its target), a sort, a group key or an aggregation
580+ * operand on such a field — for every principal this transport admits. A
581+ * query over ordinary fields passes untouched.
582+ *
583+ * Asked whatever the caller's context, as the door asks it: the engine's
584+ * privileged lookups that legitimately match on such a field call the engine
585+ * directly and never reach this bridge.
586+ *
587+ * Structural absence answers nothing: on a host that registers no protocol,
588+ * or one whose protocol has no such face, there is no data door whose verdict
589+ * this transport could match, and the call proceeds to the engine as before.
590+ * An object the protocol's registry does not hold is likewise left to the
591+ * engine's existence answer, as the door leaves it.
592+ */
593+ function refuseInternalFieldEvaluation (
594+ reads : StdioObjectReads ,
595+ object : string ,
596+ query : Record < string , unknown > ,
597+ ) : void {
598+ const protocol = protocolOf ( reads ) ;
599+ if ( typeof protocol ?. assertNoInternalFieldEvaluated !== 'function' ) return ;
600+ protocol . assertNoInternalFieldEvaluated ( object , query ) ;
601+ }
602+
551603// ---------------------------------------------------------------------------
552604// [#21207] The stored-metadata-body family, at this engine-only reader
553605// ---------------------------------------------------------------------------
@@ -954,6 +1006,9 @@ export function createStdioDataBridge(deps: StdioDataBridgeDeps): McpDataBridge
9541006 // [#21207] …and the same for a stored content-hash column.
9551007 const hashRefusal = storedMetadataHashRefusal ( object , { where : opts ?. where , orderBy : opts ?. orderBy } ) ;
9561008 if ( hashRefusal ) throw hashRefusal ;
1009+ // …and a filter or sort on an `internal: true` field, by the data door's
1010+ // own gate, after the stored-metadata refusals as the door orders them.
1011+ refuseInternalFieldEvaluation ( reads , object , { where : opts ?. where , orderBy : opts ?. orderBy } ) ;
9571012 const read = storedMetadataBodyReadFields ( object , opts ?. fields ) ;
9581013 const query : Record < string , unknown > = { } ;
9591014 if ( opts ?. where ) query . where = opts . where ;
@@ -1077,6 +1132,14 @@ export function createStdioDataBridge(deps: StdioDataBridgeDeps): McpDataBridge
10771132 aggregations : opts ?. aggregations ,
10781133 } ) ;
10791134 if ( hashRefusal ) throw hashRefusal ;
1135+ // …and a filter (`where` or an aggregation's own), a group key or an
1136+ // aggregation operand on an `internal: true` field, by the data door's
1137+ // own gate ({@link refuseInternalFieldEvaluation}).
1138+ refuseInternalFieldEvaluation ( reads , object , {
1139+ where : opts ?. where ,
1140+ groupBy : opts ?. groupBy ,
1141+ aggregations : opts ?. aggregations ,
1142+ } ) ;
10801143 // No casts: `McpDataBridge.aggregate` declares the engine's own
10811144 // `EngineAggregateOptions` slices since #8032, so the honest call
10821145 // compiles — the two `as unknown as` casts this line used to carry
0 commit comments