Skip to content

Commit ed2d826

Browse files
committed
fix(lint): judge an object's defaultRowScope in the RLS predicate and org-axis rules
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D6F9axfoPzR7t8G1J7Ytzz
1 parent 31ce45c commit ed2d826

6 files changed

Lines changed: 576 additions & 124 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@objectstack/lint": patch
3+
---
4+
5+
fix(lint): the RLS predicate and org-axis rules judge an object's `defaultRowScope`
6+
7+
Clause-②: no
8+
9+
- **What changed.** `validateRlsPredicateEnforceability` now judges `objects[].defaultRowScope.using` and `.check` with the same passes it runs on a permission set's `rowLevelSecurity[]` policy, with the declaring object as the target. A default whose predicate does not lower, does not parse, overruns a parse bound, names a column the object does not declare, reads a `current_user` key nothing resolves, or (for a `select` / `all` read scope) is refused by the engine's filter admission is reported under the same id, at the same `error` severity, at `objects[N].defaultRowScope.<clause>`. `validateOrgAxisRedLines` rule ① (`org-axis-permission-inheritance`) also judges a default that reads `parent_organization_id`.
10+
- **A new authoring-time finding on a shape that was silent.** Until now these rules walked `permissions[].rowLevelSecurity[]` only, so `os validate`, `os build` and `os lint` passed such a default. The security plugin's row-policy collection hands an object's default to the same compiler an authored policy reaches, so a predicate that does not lower is dropped at request time and the object reads as zero rows for every caller the default applies to. A stack that declares such a default now fails these commands; the fix is the same rewrite the finding prescribes for a permission-set policy. A default that lowers, and an object with no default, report nothing new.
11+
- **How the finding reads.** `where` names the default and its object (`default row scope "NAME" of object "OBJECT"`), never a permission set. `os explain` for the RLS predicate ids now says what a dropped `using` does for both sources: a permission set's policy denies the holders of that set, and a default, applied only when the caller's sets carry no row policy for the object and operation, denies every caller it applies to.

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,10 +1207,13 @@ const RLS_UNCOMPILABLE_DROP =
12071207
'CLOSED, which is why such a policy is survivable; it reads as an authorization and behaves as a refusal.';
12081208

12091209
/**
1210-
* What a dropped `using` does. The INSERT half: the ADR-0058 D4 write check
1211-
* takes its set from `writeCheckPolicies`, so when no applicable policy for the
1212-
* insert declares a `check`, every applicable policy's `using` is compiled as
1213-
* its check. Measured through the real `SecurityPlugin` on an `insert` and an
1210+
* What a dropped `using` does, for both sources a policy has: a permission
1211+
* set's `rowLevelSecurity[]` and an object's `defaultRowScope` (#24452), which
1212+
* the security plugin's row-policy collection applies alone, only when the
1213+
* held sets carry no row policy for the object and operation. The INSERT
1214+
* half: the ADR-0058 D4 write check takes its set from `writeCheckPolicies`,
1215+
* so when no applicable policy for the insert declares a `check`, every
1216+
* applicable policy's `using` is compiled as its check. Measured through the real `SecurityPlugin` on an `insert` and an
12141217
* `all` policy, for all three kinds of drop (an unlowerable shape, an
12151218
* unresolved `current_user.*`, an undeclared column): with nothing else
12161219
* compiling in that set, every single-record insert is refused (403); with
@@ -1221,8 +1224,13 @@ const RLS_DROPPED_USING =
12211224
'A dropped `using`: when it is the only applicable policy for that object and operation, `compileFilter` ' +
12221225
'returns the `RLS_DENY_FILTER` sentinel instead, which is AND-ed onto the where clause, so every select / ' +
12231226
'update / delete on the object matches ZERO rows and the object disappears for every holder of the ' +
1224-
'permission set. When other policies also apply, this one just vanishes from the OR and grants none of the ' +
1225-
'access it appears to. On an `insert` or `all` policy the same `using` is also the single-record INSERT check ' +
1227+
'permission set that carries the policy. An object\'s `defaultRowScope` is applied only when the caller\'s ' +
1228+
'permission sets carry no row policy for that object and operation, so it is always the only applicable ' +
1229+
'policy: a dropped default makes the object disappear for every caller it applies to, and a caller whose ' +
1230+
'permission set carries its own row policy for the object is not affected. When other policies also apply, ' +
1231+
'which only a permission set\'s policy can meet, this one just vanishes from the OR and grants none of the ' +
1232+
'access it appears to. ' +
1233+
'On an `insert` or `all` policy the same `using` is also the single-record INSERT check ' +
12261234
'whenever no applicable policy for the insert declares a `check` (ADR-0058 D4): when nothing else in that set ' +
12271235
'compiles, every single-record insert it governs fails with `PermissionDeniedError`; when another policy\'s ' +
12281236
'`using` compiles, that one alone decides the insert.';
@@ -1324,7 +1332,9 @@ const RLS_PREDICATE_UNENFORCEABLE_EXPLANATION: RuleExplanation = {
13241332
'An RLS predicate is compiled to a row filter (ADR-0056 D4), so only the pushdown subset lowers. A ' +
13251333
'function call (`size(…)`, `has(…)`), arithmetic, a ternary, a related-record path, a list literal under ' +
13261334
'`==` / `!=` or the bare `current_user` root used as a value is outside it, and the compiler refuses it. ' +
1327-
'The finding quotes the verdict half of the compiler\'s own refusal.',
1335+
'The finding quotes the verdict half of the compiler\'s own refusal. It judges both places a row policy ' +
1336+
'is declared, which the runtime compiles alike: a permission set\'s `rowLevelSecurity` entry and an ' +
1337+
'object\'s `defaultRowScope`, whose `path` starts at `objects[N].defaultRowScope`.',
13281338
'The lowerable subset is: `==` `!=` `>` `<` `>=` `<=`, `in`, `&&` `||` `!`, `== null` / `!= null`, and the ' +
13291339
'string methods `startsWith` / `endsWith` / `contains` — over SINGLE-column field paths (ADR-0058 D2), ' +
13301340
'compared against a literal or a `current_user.*` value. Three traps in particular: (1) a function call — ' +

‎packages/lint/src/validate-org-axis-red-lines.test.ts‎

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { describe, it, expect } from 'vitest';
66
import { ObjectStackSchema } from '@objectstack/spec';
77
import { ObjectSchema } from '@objectstack/spec/data';
88
import {
9+
DefaultRowScopeSchema,
910
PermissionSetSchema,
1011
RowLevelSecurityPolicySchema,
1112
ShareRecipientType,
@@ -293,6 +294,70 @@ describe('validateOrgAxisRedLines — ① no permission inheritance on the org a
293294
).toEqual([]);
294295
});
295296

297+
// [#24452] An object's default row scope is a Layer-1 policy the security
298+
// plugin applies when the caller's held sets carry no row policy for the
299+
// object, so a default that walks the org parent is the same second
300+
// hierarchy. The fixture passes `ObjectSchema` (the key's own door).
301+
it('flags an object `defaultRowScope` whose `using` walks the org parent, naming the object, not a set', () => {
302+
const findings = validateOrgAxisRedLines({
303+
objects: [
304+
objectFixture({
305+
name: 'work_order',
306+
defaultRowScope: {
307+
name: 'work_order_child_orgs',
308+
operation: 'select',
309+
using: 'organization_id IN (current_user.parent_organization_id)',
310+
},
311+
}),
312+
],
313+
});
314+
expect(findings).toHaveLength(1);
315+
expect(findings[0]).toMatchObject({
316+
severity: 'error',
317+
rule: ORG_AXIS_PERMISSION_INHERITANCE,
318+
where: 'default row scope "work_order_child_orgs" of object "work_order"',
319+
path: 'objects[0].defaultRowScope.using',
320+
});
321+
expect(findings[0].message).toMatch(/^RLS using reads `parent_organization_id`/);
322+
expect(`${findings[0].where} ${findings[0].message}`).not.toMatch(/permission set/);
323+
expect(findings[0].hint).toMatch(/accessible_org_ids/);
324+
});
325+
326+
it('flags a default row scope `check` that walks the org parent', () => {
327+
const findings = validateOrgAxisRedLines({
328+
objects: {
329+
work_order: objectFixture({
330+
name: 'work_order',
331+
defaultRowScope: {
332+
name: 'work_order_hq_writes',
333+
operation: 'insert',
334+
check: "parent_organization_id = 'org_hq'",
335+
},
336+
}),
337+
},
338+
});
339+
expect(findings.map((f) => [f.rule, f.path])).toEqual([
340+
[ORG_AXIS_PERMISSION_INHERITANCE, 'objects[0].defaultRowScope.check'],
341+
]);
342+
});
343+
344+
it('stays silent on a default row scope scoped by membership (CONTROL)', () => {
345+
expect(
346+
rules({
347+
objects: [
348+
objectFixture({
349+
name: 'work_order',
350+
defaultRowScope: {
351+
name: 'work_order_my_orgs',
352+
operation: 'all',
353+
using: 'organization_id IN (current_user.accessible_org_ids)',
354+
},
355+
}),
356+
],
357+
}),
358+
).toEqual([]);
359+
});
360+
296361
it('stays silent on membership-based and business-unit scoping (the sanctioned paths)', () => {
297362
expect(
298363
rules({
@@ -665,10 +730,16 @@ const READ_SURFACES: Array<{
665730
},
666731
{
667732
receiver: 'object',
668-
expected: ['systemFields', 'tenancy'],
733+
expected: ['defaultRowScope', 'name', 'systemFields', 'tenancy'],
669734
declaredBy: 'ObjectSchema',
670735
keys: () => shapeKeys(ObjectSchema),
671736
},
737+
{
738+
receiver: 'scope',
739+
expected: ['name'],
740+
declaredBy: 'DefaultRowScopeSchema',
741+
keys: () => shapeKeys(DefaultRowScopeSchema),
742+
},
672743
{
673744
receiver: 'rule',
674745
expected: ['condition', 'name', 'object', 'sharedWith'],
@@ -691,15 +762,19 @@ describe('validateOrgAxisRedLines — reads only keys the spec declares (meta-te
691762
expect(read.filter((k) => !declared.includes(k))).toEqual([]);
692763
});
693764

694-
it('the RLS clause list is spelled from `RowLevelSecurityPolicySchema` keys', () => {
695-
// `policy[clause]` is a COMPUTED read, so the scan above cannot see it; the
696-
// word list it indexes with is checked here instead.
697-
const match = /for \(const clause of \[([^\]]*)\] as const\)/.exec(RULE_CODE);
698-
expect(match, 'the clause loop moved — update this guard').not.toBeNull();
699-
const clauses = [...match![1].matchAll(/'([^']+)'/g)].map((m) => m[1]);
700-
expect(clauses).toEqual(['using', 'check']);
701-
const declared = shapeKeys(RowLevelSecurityPolicySchema);
702-
expect(clauses.filter((c) => !declared.includes(c))).toEqual([]);
765+
it('each RLS clause list is spelled from the keys its policy schema declares', () => {
766+
// `policy[clause]` / `scope[clause]` are COMPUTED reads, so the scan above
767+
// cannot see them; the word lists they index with are checked here instead,
768+
// one per loop: the permission-set policy's, then the default row scope's.
769+
const loops = [...RULE_CODE.matchAll(/for \(const clause of \[([^\]]*)\] as const\)/g)];
770+
expect(loops, 'a clause loop moved — update this guard').toHaveLength(2);
771+
const schemas = [RowLevelSecurityPolicySchema, DefaultRowScopeSchema];
772+
loops.forEach((loop, i) => {
773+
const clauses = [...loop[1].matchAll(/'([^']+)'/g)].map((m) => m[1]);
774+
expect(clauses).toEqual(['using', 'check']);
775+
const declared = shapeKeys(schemas[i]);
776+
expect(clauses.filter((c) => !declared.includes(c))).toEqual([]);
777+
});
703778
});
704779
});
705780

@@ -781,6 +856,21 @@ const REACHABILITY_CORPUS: Array<{ label: string; stack: unknown }> = [
781856
],
782857
},
783858
},
859+
{
860+
label: '① object default row scope `using`',
861+
stack: {
862+
objects: [
863+
objectFixture({
864+
name: 'work_order',
865+
defaultRowScope: {
866+
name: 'work_order_child_orgs',
867+
operation: 'select',
868+
using: 'organization_id IN (current_user.parent_organization_id)',
869+
},
870+
}),
871+
],
872+
},
873+
},
784874
{
785875
label: '① sharing-rule `condition`',
786876
stack: {
@@ -829,9 +919,11 @@ const REACHABILITY_CORPUS: Array<{ label: string; stack: unknown }> = [
829919
describe('validateOrgAxisRedLines — every branch is reachable by a spec-valid stack (meta-test, #5009)', () => {
830920
it('maps every `findings.push` site in the source', () => {
831921
const sites = pushSites();
832-
// ① permission-set RLS, ① sharing rule, ② cross-org BU grant. The fourth —
922+
// ① permission-set RLS, ① object default row scope (#24452), ① sharing
923+
// rule, ② cross-org BU grant. The old object-level list —
833924
// `objects[].rowLevelSecurity[].${clause}` — is gone: no such surface.
834-
expect(sites).toHaveLength(3);
925+
expect(sites).toHaveLength(4);
926+
expect(sites.map((s) => s.pathTemplate)).toContain('objects[${oIndex}].defaultRowScope.${clause}');
835927
expect(sites.map((s) => s.pathTemplate)).not.toContain(
836928
'objects[${oIndex}].rowLevelSecurity[${pIndex}].${clause}',
837929
);

‎packages/lint/src/validate-org-axis-red-lines.ts‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@
5454
* | `permissions[]` | `ObjectStackSchema` |
5555
* | `permissions[].rowLevelSecurity[]` | `PermissionSetSchema` |
5656
* | `…[].using` / `…[].check` | `RowLevelSecurityPolicySchema` |
57+
* | `objects[].defaultRowScope` | `ObjectSchema` |
58+
* | `….using` / `….check` / `….name` | `DefaultRowScopeSchema` |
5759
* | `sharingRules[]` | `ObjectStackSchema` |
5860
* | `sharingRules[].condition` / `.sharedWith` / `.object` | `SharingRuleSchema` |
59-
* | `objects[].tenancy` / `.systemFields` | `ObjectSchema` |
61+
* | `objects[].tenancy` / `.systemFields` / `.name` | `ObjectSchema` |
6062
*
6163
* NOT read, and each for a reason that is a schema fact:
6264
*
@@ -66,18 +68,20 @@
6668
* - `sharingRules[].objectName` — `SharingRuleSchema` is `.strict()` and knows
6769
* it only as a rejected name; `object` is required, so the canonical read can
6870
* never be missing on a rule that parsed.
69-
* - `objects[].rowLevelSecurity` / `objects[].rls` — **object-level RLS is not
70-
* an authoring surface at all.** `ObjectSchema` declares neither key (nor
71-
* does `authorable-surface.json` list one: the sole entry is
72-
* `security/PermissionSet:rowLevelSecurity`), and `ObjectSchema` is
71+
* - `objects[].rowLevelSecurity` / `objects[].rls` — **a LIST of object-level
72+
* RLS policies is not an authoring surface at all.** `ObjectSchema` declares
73+
* neither key, and `ObjectSchema` is
7374
* `.strict()`, so a stack carrying one does not parse — it is refused with
7475
* "Unrecognized key(s) on this object". Until #5009 this file walked that
7576
* non-existent surface for ~20 lines, complete with an `objects[N].
7677
* rowLevelSecurity[M].using` diagnostic path. Nothing could reach it, and the
7778
* cost was never the missed finding: the next author to read this rule (human
7879
* or AI) came away believing object-level RLS was a real authorization
7980
* surface and wrote more code against it (#5008 nearly did). RLS policies
80-
* live on a permission set; that is the branch above.
81+
* live on a permission set; that is the branch above. The one
82+
* object-level policy `ObjectSchema` does declare is a single
83+
* `defaultRowScope` (#24452), read in the table above and judged beside the
84+
* permission-set branch.
8185
*
8286
* Alias tolerance belongs at the schema's refusal, not in a consumer (Prime
8387
* Directive #12) — where it also converts a loud, named rejection into a
@@ -208,7 +212,8 @@ export function validateOrgAxisRedLines(stack: unknown): OrgAxisFinding[] {
208212
// RLS policies live on a PERMISSION SET (`permissions[].rowLevelSecurity`) —
209213
// the one place `ObjectSchema` does not offer and `PermissionSetSchema` does.
210214
// See the `## Scope` table above for the object-level surface that looked
211-
// like a second home for them and never was (#5009).
215+
// like a second home for them and never was (#5009). The one object-level
216+
// policy that IS declared, an object's single `defaultRowScope`, follows.
212217
const permissionSets = recordsOf(cfg.permissions);
213218
permissionSets.forEach((ps, psIndex) => {
214219
recordsOf(ps.rowLevelSecurity).forEach((policy, pIndex) => {
@@ -228,6 +233,32 @@ export function validateOrgAxisRedLines(stack: unknown): OrgAxisFinding[] {
228233
});
229234
});
230235

236+
// [#24452] An object's DEFAULT row scope (`objects[].defaultRowScope`) is
237+
// the same policy shape without `object`, and the security plugin's
238+
// row-policy collection applies it as a Layer-1 policy when the caller's
239+
// held sets carry no row policy for the object. A `using` / `check` there
240+
// that reads the org parent is the same second hierarchy, so it is judged
241+
// the same way. It is the one object-level policy key `ObjectSchema`
242+
// declares — see the `## Scope` table.
243+
recordsOf(cfg.objects).forEach((object, oIndex) => {
244+
const scope = object.defaultRowScope as AnyRec | undefined;
245+
if (!scope || typeof scope !== 'object' || Array.isArray(scope)) return;
246+
for (const clause of ['using', 'check'] as const) {
247+
if (!str(scope[clause]).includes(ORG_PARENT_FIELD)) continue;
248+
const scopeName = str(scope.name);
249+
findings.push({
250+
severity: 'error',
251+
rule: ORG_AXIS_PERMISSION_INHERITANCE,
252+
where: `default row scope${scopeName ? ` "${scopeName}"` : ''} of object "${str(object.name) || oIndex}"`,
253+
path: `objects[${oIndex}].defaultRowScope.${clause}`,
254+
message:
255+
`RLS ${clause} reads \`${ORG_PARENT_FIELD}\`, which builds a permission hierarchy along the ` +
256+
`organization axis. ADR-0105 D6 forbids it: the org tree is a REPORTING dimension only.`,
257+
hint: INHERITANCE_HINT,
258+
});
259+
}
260+
});
261+
231262
// Sharing rules — the predicate and the recipient may both reach for the org
232263
// parent, so both slots are scanned.
233264
//

0 commit comments

Comments
 (0)