Skip to content

Commit f85110d

Browse files
committed
fix(lint): resolve FLS keys against the object as the registry serves it, extension fields included
security-fls-unknown-field read an object's declared fields only, so a permission set naming a field that an objectExtensions entry adds (a sibling package's in a multi-package artifact, or the stack's own) was refused, although the runtime serves the field and the key masks it. The FLS-key index is now built over objectsAsServed(stack): the shared extension fold in object-graph.ts, mirroring the objectql registry (fields by name, one entry per package per object, extend verbatim). The controlled-by-parent merge (#23210) and the membership-key check read the same fold instead of their own copies. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzfKwxFVA5m6bAjSpxf
1 parent cfb602c commit f85110d

7 files changed

Lines changed: 372 additions & 113 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/lint': patch
3+
---
4+
5+
fix(lint): a permission set's field key on a field that an `objectExtensions` entry adds, a sibling package's in the same artifact included, is no longer refused as `security-fls-unknown-field` (#30303)
6+
7+
Clause-②: yes (widening: the FLS-key check accepts extension-added fields)
8+
Metadata-protocol: none
9+
10+
- **Before.** The FLS-key check resolved `object.field` against the object's declared `fields` only. On a multi-package artifact, an app package's permission set naming `crm_contract.o2c_probe_note`, a field its module package adds to `crm_contract` through `objectExtensions`, failed `os build`, `os validate` and `os lint` with "crm_contract declares no field o2c_probe_note", although the runtime serves the field and the key masks it. A single-package stack's own extension fields were refused the same way.
11+
- **Now.** The key resolves against the object as the registry serves it: its declared fields plus every field an `objectExtensions` entry of any body of the artifact adds to it, folded as `@objectstack/objectql` folds them (fields by name; one entry per package per object, a later entry replacing an earlier one; `extend` naming the object verbatim). A field that no body declares or adds keeps the same refusal, with the same rule id, severity and message, and so does a field an entry only patches through `fieldPatches`, which adds no field. The hint's nearest name reads the served fields too.
12+
- **One fold.** The controlled-by-parent merge (#23210), the membership-key column check and the FLS-key check now read extensions through one shared fold in `object-graph.ts`.

‎packages/lint/src/object-graph.ts‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,134 @@ export function indexObjectGraph(stack: unknown): ObjectGraph {
365365
return graph;
366366
}
367367

368+
/**
369+
* One `objectExtensions` entry as the registry registers it — see
370+
* {@link objectExtensionsByTarget}.
371+
*/
372+
export interface VisibleObjectExtension {
373+
/** The position of the package that ships it: registration order across packages. */
374+
carrier: number;
375+
/** The entry's path, e.g. `objectExtensions[0]` or `packages[1].manifest.objectExtensions[0]`. */
376+
path: string;
377+
/** `ext.priority ?? 200`, the registry's default. */
378+
priority: number;
379+
/** Its field records, name folded in ({@link recordsOf}). */
380+
fields: AnyRec[];
381+
}
382+
383+
/**
384+
* [#30303] Every `objectExtensions` entry of a stack, by the object it
385+
* `extend`s, as the registry registers and folds them. This is the ONE place
386+
* this package reads extensions for an object's merged field set: the
387+
* `controlled_by_parent` merge of `validate-security-posture.ts` (#23210),
388+
* the membership-key columns of `validate-membership-key-declarations.ts`,
389+
* and the served field set {@link objectsAsServed} hands the FLS-key check.
390+
*
391+
* Read as the registry reads a manifest, since `@objectstack/lint` imports no
392+
* runtime (`ObjectQL.registerApp`'s `objectExtensions` loop,
393+
* `SchemaRegistry.registerObject`'s `extend` arm and `foldExtenders`, in
394+
* `@objectstack/objectql`):
395+
*
396+
* - Source: with `packages[]`, every body's `objectExtensions`
397+
* (`packages[j].manifest`, `carrier` = `j`, the artifact's registration
398+
* order); without it, the stack's own (`carrier` 0). An artifact's top level
399+
* repeats its bodies' collections, so it is not read beside them.
400+
* - `extend` names its target verbatim.
401+
* - ONE entry per package per target: registering an extension removes that
402+
* package's earlier one, so a later entry of the same package replaces an
403+
* earlier one whole, its fields included.
404+
* - Fold order: ascending `priority` (default 200), equal priorities in
405+
* package order. Each list is returned in that order.
406+
*
407+
* Which targets to judge is the caller's question: an entry is listed here
408+
* whether or not the stack declares its target.
409+
*/
410+
export function objectExtensionsByTarget(stack: unknown): Map<string, VisibleObjectExtension[]> {
411+
const byTarget = new Map<string, VisibleObjectExtension[]>();
412+
if (!isRec(stack)) return byTarget;
413+
// The receivers are named as `validate-security-posture.test.ts`'s key-read
414+
// meta-test tables them (`stack`, `entry`, `body`, `ext`, `obj`, `f`): that
415+
// scan reads this fold as part of the rules that read through it.
416+
const collect = (entries: unknown, carrier: number, at: string) => {
417+
if (!Array.isArray(entries)) return;
418+
entries.forEach((ext: unknown, i: number) => {
419+
if (!isRec(ext) || typeof ext.extend !== 'string') return;
420+
const kept = (byTarget.get(ext.extend) ?? []).filter((other) => other.carrier !== carrier);
421+
kept.push({
422+
carrier,
423+
path: `${at}objectExtensions[${i}]`,
424+
priority: typeof ext.priority === 'number' ? ext.priority : 200,
425+
fields: recordsOf(ext.fields),
426+
});
427+
byTarget.set(ext.extend, kept);
428+
});
429+
};
430+
if (Array.isArray(stack.packages)) {
431+
stack.packages.forEach((entry: unknown, j: number) => {
432+
const body = isRec(entry) ? entry.manifest : undefined;
433+
if (isRec(body)) collect(body.objectExtensions, j, `packages[${j}].manifest.`);
434+
});
435+
} else {
436+
collect(stack.objectExtensions, 0, '');
437+
}
438+
for (const list of byTarget.values()) {
439+
list.sort((left, right) => left.priority - right.priority || left.carrier - right.carrier);
440+
}
441+
return byTarget;
442+
}
443+
444+
/**
445+
* [#30303] A stack's `objects` as the registry SERVES them: each with every
446+
* {@link objectExtensionsByTarget} entry that extends it folded in, the way
447+
* `mergeObjectDefinitions` merges `fields` (by name, additively, a later
448+
* writer replacing the whole field). An object nothing extends is returned as
449+
* it is.
450+
*
451+
* Only the fields move. The registry plans an object's injected system
452+
* columns over its base layer (`foldExtenders` reads
453+
* `resolveInjectedSystemColumns(baseDefinition)`), and that plan reads no
454+
* field, so {@link indexObjectGraph} over these objects injects what it
455+
* injects over the declared ones. `fieldPatches` add no field (they patch the
456+
* owner's own), so they are not read.
457+
*
458+
* A field two packages write at the priority that decides it is merged (its
459+
* name is served whichever wins) and named in `tied`, for a caller whose
460+
* verdict depends on which definition wins: registration order decides that,
461+
* and a stack does not state it.
462+
*/
463+
export function objectsAsServed(stack: unknown): { objects: AnyRec[]; tied: Map<string, Set<string>> } {
464+
const objects = isRec(stack) ? recordsOf(stack.objects) : [];
465+
const tied = new Map<string, Set<string>>();
466+
const extensions = objectExtensionsByTarget(stack);
467+
if (extensions.size === 0) return { objects, tied };
468+
469+
const served = objects.map((obj) => {
470+
const name = strName(obj.name);
471+
const exts = name ? extensions.get(name) : undefined;
472+
if (!name || !exts) return obj;
473+
const fields = new Map<string, AnyRec>();
474+
for (const f of recordsOf(obj.fields)) {
475+
const key = strName(f.name);
476+
if (key) fields.set(key, f);
477+
}
478+
const lastWriter = new Map<string, VisibleObjectExtension>();
479+
const tiedKeys = new Set<string>();
480+
for (const ext of exts) {
481+
for (const f of ext.fields) {
482+
const key = strName(f.name);
483+
if (!key) continue;
484+
if (lastWriter.get(key)?.priority === ext.priority) tiedKeys.add(key);
485+
else tiedKeys.delete(key);
486+
lastWriter.set(key, ext);
487+
fields.set(key, f);
488+
}
489+
}
490+
if (tiedKeys.size > 0) tied.set(name, tiedKeys);
491+
return { ...obj, fields: [...fields.values()] };
492+
});
493+
return { objects: served, tied };
494+
}
495+
368496
/** What a path resolved to, or precisely how it failed. */
369497
export type FieldPathVerdict =
370498
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,8 @@ const SECURITY_FLS_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = {
819819
'Unlike an unqualified key (`security-fls-unqualified-key`) this one looks correct in review, and ' +
820820
'it is exactly what a field rename leaves behind; an empty remainder is what a half-finished edit ' +
821821
'leaves. A key with more dots is judged on its whole remainder, because FLS keys address columns, ' +
822-
'never joins.',
822+
'never joins. The columns are the object\'s as the registry serves it: the fields it declares and ' +
823+
'every field an `objectExtensions` entry adds to it, from any package of the same artifact.',
823824
'Not judged: an object this stack does not define, an object with no readable field map, and a ' +
824825
'registry-injected system column, which is real and addressable. To fix it, point the key at a ' +
825826
'field the object declares, or delete the entry: an entry that cannot match is not protection, ' +

‎packages/lint/src/validate-membership-key-declarations.ts‎

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import {
6767
indexObjectGraph,
6868
isUnjudgeable,
69+
objectsAsServed,
6970
recordsOf,
7071
resolveFieldPath,
7172
suggestName,
@@ -205,75 +206,6 @@ export function stackObjectNamesOf(stack: unknown): Set<string> {
205206
return names;
206207
}
207208

208-
/** One `objectExtensions` entry, as the registry folds it. */
209-
interface VisibleExtension {
210-
/** Package order: registration order across packages. */
211-
carrier: number;
212-
/** `priority ?? 200`, the registry's default. */
213-
priority: number;
214-
fields: AnyRec[];
215-
}
216-
217-
/**
218-
* The stack's objects with every `objectExtensions` entry folded in, as the
219-
* registry merges them (this file's header). A field two packages write at the
220-
* priority that decides it is left out of the merged map and named in
221-
* `tied`, so its verdict is "not judged" rather than either writer's.
222-
*/
223-
function mergedObjectsOf(stack: AnyRec): { objects: AnyRec[]; tied: Map<string, Set<string>> } {
224-
const objects = recordsOf(stack.objects);
225-
const tied = new Map<string, Set<string>>();
226-
const names = new Set(objects.map((obj) => str(obj.name)).filter(Boolean));
227-
const extensions = new Map<string, VisibleExtension[]>();
228-
const collect = (entries: unknown, carrier: number) => {
229-
if (!Array.isArray(entries)) return;
230-
for (const entry of entries) {
231-
if (!isRec(entry) || typeof entry.extend !== 'string' || !names.has(entry.extend)) continue;
232-
const list = extensions.get(entry.extend) ?? [];
233-
// One per package per target: the later entry wins.
234-
const kept = list.filter((ext) => ext.carrier !== carrier);
235-
kept.push({
236-
carrier,
237-
priority: typeof entry.priority === 'number' ? entry.priority : 200,
238-
fields: recordsOf(entry.fields),
239-
});
240-
extensions.set(entry.extend, kept);
241-
}
242-
};
243-
if (Array.isArray(stack.packages)) {
244-
stack.packages.forEach((pkg: unknown, j: number) => {
245-
if (isRec(pkg) && isRec(pkg.manifest)) collect(pkg.manifest.objectExtensions, j);
246-
});
247-
} else {
248-
collect(stack.objectExtensions, 0);
249-
}
250-
if (extensions.size === 0) return { objects, tied };
251-
252-
const merged = objects.map((obj) => {
253-
const name = str(obj.name);
254-
const exts = extensions.get(name);
255-
if (!exts) return obj;
256-
const fields = new Map<string, AnyRec>();
257-
for (const f of recordsOf(obj.fields)) if (str(f.name)) fields.set(str(f.name), f);
258-
const lastWriter = new Map<string, VisibleExtension>();
259-
const tiedKeys = new Set<string>();
260-
for (const ext of [...exts].sort((a, b) => a.priority - b.priority || a.carrier - b.carrier)) {
261-
for (const f of ext.fields) {
262-
const key = str(f.name);
263-
if (!key) continue;
264-
const previous = lastWriter.get(key);
265-
if (previous && previous.priority === ext.priority) tiedKeys.add(key);
266-
else tiedKeys.delete(key);
267-
lastWriter.set(key, ext);
268-
fields.set(key, f);
269-
}
270-
}
271-
if (tiedKeys.size > 0) tied.set(name, tiedKeys);
272-
return { ...obj, fields: [...fields.values()] };
273-
});
274-
return { objects: merged, tied };
275-
}
276-
277209
/** U's verdict: `null` when it is a single-valued user reference, else the reason. */
278210
function userFieldFault(meta: GraphField): string | null {
279211
const isUserRef =
@@ -332,7 +264,7 @@ export function validateMembershipKeyDeclarations(
332264
const declared = declaredMembershipKeysOf(stack);
333265
if (declared.length === 0) return findings;
334266

335-
const { objects: merged, tied } = mergedObjectsOf(stack);
267+
const { objects: merged, tied } = objectsAsServed(stack);
336268
const graph = indexObjectGraph({ objects: merged });
337269
const stackNames = stackObjectNamesOf(stack);
338270
const owners = options.membershipKeyOwners;

0 commit comments

Comments
 (0)