Skip to content

Commit 1cf0d93

Browse files
committed
fix(mcp,runtime): the MCP data paths ask the data door's internal-field evaluation gate before the engine
Claude-Session: https://claude.ai/code/session_017xRBcVy4VXmkQjn2MGeaWk Co-authored-by: Claude <noreply@anthropic.com>
1 parent dd6349e commit 1cf0d93

3 files changed

Lines changed: 95 additions & 6 deletions

File tree

‎packages/mcp/src/stdio-data-bridge.ts‎

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,20 @@
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

96108
import { 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

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13288,6 +13288,16 @@ export class ObjectStackProtocolImplementation implements
1328813288
* ever returning the value — is untouched, exactly as #7823 kept the engine's
1328913289
* write results whole while the generic-data-path ingress stripped them.
1329013290
*
13291+
* PUBLIC, and asked by the generic data mouths that hand a caller's query
13292+
* to the engine without passing through {@link findData}: the MCP stdio
13293+
* transport's `query_records` / `aggregate_records` and the HTTP MCP
13294+
* bridge's `aggregate_records` arm (`callData`'s `aggregate`). Each asks
13295+
* this method on the `protocol` service it already resolves, before the
13296+
* engine, so a filter, sort, group key or aggregation over an `internal`
13297+
* field gets this door's refusal, word for word, on every transport —
13298+
* one definition, never a copy beside it (route-ownership rule 1). The
13299+
* engine's privileged consumers above still never reach it.
13300+
*
1329113301
* Positions, with the ONE flag reader ({@link collectInternalWriteResponseFields},
1329213302
* `@objectstack/core`'s cross-package twin of objectql's
1329313303
* `collectInternalReadFields` — identical rule, imported because this package
@@ -13327,7 +13337,7 @@ export class ObjectStackProtocolImplementation implements
1332713337
* door). A dotted sort inside an `expand` entry is admitted but orders a
1332813338
* batch the expansion re-keys by id with no `limit`, so it changes no answer.
1332913339
*/
13330-
private assertNoInternalFieldEvaluated(object: string, options: Record<string, any>): void {
13340+
assertNoInternalFieldEvaluated(object: string, options: Record<string, any>): void {
1333113341
// [#25467 cut 3] The body moved to `protocol-query-validation.ts` (see `QueryValidationHost`).
1333213342
const host: ThisParameterType<typeof assertNoInternalFieldEvaluated> = {
1333313343
groupByFieldNames: (...a) => this.groupByFieldNames(...a),

‎packages/runtime/src/action-execution.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,14 +843,30 @@ export async function callData(deps: ActionExecutionDeps,
843843
const engine = (await deps.getObjectQL(requestContext, scopeId))
844844
?? await deps.resolveService(requestContext, 'objectql', scopeId).catch(() => null);
845845
if (engine && typeof engine.aggregate === 'function') {
846-
// [#22823] This arm takes no protocol, so it is the engine's door on
846+
// [#22823] This arm runs on no protocol data method, so it is the engine's door on
847847
// every assembly: each nested-relation condition's TARGET in `where`
848848
// is asked before the engine aggregates, as the query fallback asks
849849
// it (the engine lowers relation conditions in `where` only, never
850850
// in an aggregation's own `filter`).
851851
if (exposureGated) {
852852
await refuseUnservedRelationTargets(deps, requestContext, engine, params.object, params.where, scopeId);
853853
}
854+
// The data door's evaluate refusal for a field declared
855+
// `internal: true` — a filter (`where`, an aggregation's own
856+
// `filter`), a group key or an aggregation operand on it. The query
857+
// arm meets it inside `findData`; this arm never reaches `findData`,
858+
// so it asks the protocol's ONE gate itself, before the engine, and
859+
// throws the door's `400 INVALID_FIELD` word for word. ⛔ Never a
860+
// copy of the gate here (route-ownership rule 1). Every caller, as
861+
// at the door; a host with no protocol has no door to agree with
862+
// and is served as before.
863+
if (protocol && typeof protocol.assertNoInternalFieldEvaluated === 'function') {
864+
protocol.assertNoInternalFieldEvaluated(params.object, {
865+
where: params.where,
866+
groupBy: params.groupBy,
867+
aggregations: params.aggregations,
868+
});
869+
}
854870
const rows = await engine.aggregate(
855871
params.object,
856872
{

0 commit comments

Comments
 (0)