|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import { describe, it, expect } from 'vitest'; |
10 | | -import { DataScopeManager } from '../DataScopeManager'; |
| 10 | +import { DataScopeManager, type RowLevelFilter } from '../DataScopeManager'; |
11 | 11 |
|
12 | 12 | describe('DataScopeManager', () => { |
13 | 13 | describe('Scope Registration', () => { |
@@ -208,4 +208,88 @@ describe('DataScopeManager', () => { |
208 | 208 | expect(count).toBe(1); |
209 | 209 | }); |
210 | 210 | }); |
| 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 | + }); |
211 | 295 | }); |
0 commit comments