Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 83c77dc

Browse files
Sam Altmanclaude
andauthored
fix(core): DataScopeManager denies a row on an operator it does not implement (#7748)
`evaluateFilter`'s `default` arm returned `true`, so a stored `RowLevelFilter` carrying an operator outside the nine the switch implements passed every record the rule existed to hide — silently. The arm now returns `false`, the answer `evaluateCondition` in @object-ui/permissions already gives from its own `default` arm. Red-first test exercises the runtime path stored JSON takes (cast past the closed union, deliberately) and pins the fail-closed answer for an operator outside every published vocabulary and for the spec's published spellings that have no arm. Claude-Session: https://claude.ai/code/session_01KbJQ1y1J12nZxYzFWhP8Q3 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 81a2eb1 commit 83c77dc

3 files changed

Lines changed: 114 additions & 4 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@object-ui/core': minor
3+
---
4+
5+
`DataScopeManager` now **denies** a row when a row-level scope rule carries an operator its evaluator does not implement. It used to **admit** the row.
6+
7+
Behaviour change on a permission boundary, stated plainly. `evaluateFilter` implements nine operator spellings — `eq`, `ne`, `gt`, `lt`, `gte`, `lte`, `in`, `nin`, `contains` — and its `default` arm returned `true`, so a stored `RowLevelFilter` carrying any other spelling passed every record the rule existed to hide, silently: no error, no console line, only a result set that was too large, which looks exactly like a correctly configured permissive scope. The arm now returns `false`, the answer `evaluateCondition` in `@object-ui/permissions` already gives from its own `default` arm. Because `applyFilters` ANDs a scope's rules, one unrecognised rule now denies every row in that scope.
8+
9+
Who this reaches, measured on this release's base rather than assumed. The `RowLevelFilter['operator']` union is closed, so no TypeScript caller can write an unimplemented spelling, and no code in this repository constructs a `RowLevelFilter` outside the evaluator's own test. The path that changes is scope configuration read back from stored or hand-written JSON and handed to `setFilters` / `registerScopeWithConfig`, where the operator arrives as a plain string the type never checked. A deployment holding such a rule with a spelling outside the nine — including the spec's canonical `equals` / `not_equals` / `greater_than` / `starts_with` and the null-ness family `is_null` / `is_not_null`, none of which have an arm — sees fewer rows from that scope after upgrading, never more. Those spellings are not implemented here; they are refused instead of admitted. Whether to canonicalise them through the spec's `canonicalAstOperator` is left open on objectui#7378.
10+
11+
Graded `minor` because a release reader can observe the narrowing on stored data; the declared type is unchanged and the set of spellings the evaluator accepts has not widened.

‎packages/core/src/data-scope/DataScopeManager.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import type { DataScope, DataContext, DataSource } from '@object-ui/types';
2525
export interface RowLevelFilter {
2626
/** Field to filter on */
2727
field: string;
28-
/** Filter operator */
28+
/**
29+
* Filter operator. The set is closed: a rule whose operator is outside it
30+
* (possible for a rule read back from stored JSON, which the type does not
31+
* guard) evaluates to `false` and denies the row.
32+
*/
2933
operator: 'eq' | 'ne' | 'gt' | 'lt' | 'gte' | 'lte' | 'in' | 'nin' | 'contains';
3034
/** Filter value */
3135
value: any;
@@ -236,7 +240,10 @@ export class DataScopeManager implements DataContext {
236240
}
237241

238242
/**
239-
* Evaluate a single filter condition against a field value
243+
* Evaluate a single filter condition against a field value.
244+
*
245+
* An operator the switch does not implement evaluates to `false` (fail
246+
* closed); see the `default` arm.
240247
*/
241248
function evaluateFilter(fieldValue: any, operator: RowLevelFilter['operator'], filterValue: any): boolean {
242249
switch (operator) {
@@ -259,7 +266,15 @@ function evaluateFilter(fieldValue: any, operator: RowLevelFilter['operator'], f
259266
case 'contains':
260267
return typeof fieldValue === 'string' && fieldValue.includes(String(filterValue));
261268
default:
262-
return true;
269+
// Fail closed. A row-level rule this evaluator cannot answer must not
270+
// admit the row it exists to hide: the same answer `evaluateCondition`
271+
// in @object-ui/permissions gives from its own `default` arm, and the
272+
// opposite of the admit-all this arm used to return. The declared union
273+
// above keeps TypeScript callers off this arm; a rule read back from
274+
// stored JSON arrives as a plain string and is not protected by it.
275+
// Silent, like the sibling: the caller sees a narrower result set, not
276+
// a thrown error (objectui#7378).
277+
return false;
263278
}
264279
}
265280

‎packages/core/src/data-scope/__tests__/DataScopeManager.test.ts‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { describe, it, expect } from 'vitest';
10-
import { DataScopeManager } from '../DataScopeManager';
10+
import { DataScopeManager, type RowLevelFilter } from '../DataScopeManager';
1111

1212
describe('DataScopeManager', () => {
1313
describe('Scope Registration', () => {
@@ -208,4 +208,88 @@ describe('DataScopeManager', () => {
208208
expect(count).toBe(1);
209209
});
210210
});
211+
212+
describe('Unknown operator fails closed (objectui#7378)', () => {
213+
// Why the cast is here, and why it must stay: `RowLevelFilter['operator']`
214+
// is a closed nine-member union, so TypeScript refuses `'equals'` or
215+
// `'is_null'` at a call site. That protects TypeScript callers and nothing
216+
// else. A scope rule read back from stored JSON reaches `applyFilters` as a
217+
// plain string, and the switch keys on that string at runtime, exactly the
218+
// way it is spelled below. The cast reproduces the path stored data takes;
219+
// it is the point of these tests, not a shortcut to be "cleaned up". A
220+
// test that only spells the nine declared operators cannot reach the
221+
// `default` arm at all.
222+
const storedRule = (field: string, operator: string, value: unknown): RowLevelFilter =>
223+
({ field, operator, value }) as unknown as RowLevelFilter;
224+
225+
it('does not admit a record it cannot evaluate (operator outside every published vocabulary)', () => {
226+
const manager = new DataScopeManager();
227+
manager.registerScope('test', { data: [] });
228+
manager.setFilters('test', [storedRule('status', 'not_an_operator', 'active')]);
229+
230+
const result = manager.applyFilters('test', [
231+
{ id: 1, status: 'active' },
232+
{ id: 2, status: 'inactive' },
233+
]);
234+
235+
// Fail closed: a rule the evaluator cannot answer denies every row in
236+
// the scope, the same answer `evaluateCondition` in @object-ui/permissions
237+
// gives from its own `default` arm. Before the fix this returned both
238+
// rows, `{ id: 2 }` included, with no error and no console line.
239+
expect(result).toEqual([]);
240+
});
241+
242+
it('does not let an unrecognised rule widen a scope another rule narrows (AND semantics)', () => {
243+
const manager = new DataScopeManager();
244+
manager.registerScope('test', { data: [] });
245+
manager.setFilters('test', [
246+
{ field: 'status', operator: 'eq', value: 'active' },
247+
storedRule('tenant', 'not_an_operator', 'acme'),
248+
]);
249+
250+
const result = manager.applyFilters('test', [
251+
{ id: 1, status: 'active', tenant: 'acme' },
252+
{ id: 2, status: 'active', tenant: 'other' },
253+
{ id: 3, status: 'inactive', tenant: 'acme' },
254+
]);
255+
256+
// Before the fix the unknown `tenant` rule evaluated to `true`, so the
257+
// `status` rule alone decided and `{ id: 2, tenant: 'other' }` passed —
258+
// the row the second rule existed to hide. Now nothing passes.
259+
expect(result).toEqual([]);
260+
});
261+
262+
// The spec's published vocabularies (`VIEW_FILTER_OPERATORS` from
263+
// `@objectstack/spec/ui`, `VALID_AST_OPERATORS` from `@objectstack/spec/data`)
264+
// carry spellings this switch has no arm for: the canonical forms of the
265+
// implemented abbreviations, and the whole null-ness family. Measured on
266+
// @objectstack/spec 17.2.0: 18 of the 20 view operators and 44 of the 53
267+
// AST operators have no arm. Until they are either implemented or refused
268+
// by name, they MUST take the fail-closed arm rather than the admit-all one.
269+
// A change that implements these spellings rewrites the expectations below
270+
// to the evaluated result; it never deletes the cases. Every case is
271+
// chosen so that a CORRECT evaluation of the spelling admits at least one
272+
// of the two rows: an implementation that lands without rewriting the
273+
// expectation turns red here instead of passing by coincidence.
274+
it.each([
275+
['equals', 'status', 'active'],
276+
['not_equals', 'status', 'active'],
277+
['greater_than', 'age', 20],
278+
['not_in', 'status', ['inactive']],
279+
['starts_with', 'status', 'act'],
280+
['is_null', 'status', null],
281+
['is_not_null', 'status', null],
282+
])('denies rather than admits for the published-but-unimplemented spelling %s', (operator, field, value) => {
283+
const manager = new DataScopeManager();
284+
manager.registerScope('test', { data: [] });
285+
manager.setFilters('test', [storedRule(field, operator, value)]);
286+
287+
const result = manager.applyFilters('test', [
288+
{ id: 1, status: 'active', age: 30 },
289+
{ id: 2, status: null, age: 10 },
290+
]);
291+
292+
expect(result).toEqual([]);
293+
});
294+
});
211295
});

0 commit comments

Comments
 (0)