Skip to content

Commit 9d07ffa

Browse files
committed
fix(lint): read the extension door's objects without injected system columns
The object_extension door hands the objects as the registry serves them, with the injected system columns (created_at, owner_id, ...) declared, so the null-guard gate judged record.created_at as a declared nullable field and refused an extension rule os build accepts. On that write each object is now read through stripInjectedSystemColumns, as the authored-site expression judge does. Pinned at the gate and the real door (accepted at both; a declared nullable field still refused at both). Comments, the changeset and the semantic entry now say that the leaf of a one-hop read through an extension-added reference is not judged, that the door's 422 keeps the stack path, that os lint narrows too, and that the null-guard gate judges ordering and arithmetic operands only. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibyToDkPxCjvNiWsv6e
1 parent 15d0747 commit 9d07ffa

5 files changed

Lines changed: 102 additions & 27 deletions

File tree

‎.changeset/33154-object-extension-validation-rules-judged.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55

66
fix(lint): `os build` and the runtime `object_extension` publish door judge each validation rule an object extension adds by the object's own rule pass, against the extended object as served
77

8-
Clause-②: no (narrowing) — `os build`, `os validate` and the `object_extension` publish door (Studio, `PUT /api/v1/meta/object_extension/TARGET`, the MCP and AI authoring doors) now refuse an extension whose validation rule carries a `condition` or `when` the same rule on the object itself is refused for. No exported type or signature changes. Measured first-party: 0 extension entries carry a validation rule (`examples/app-showcase` one entry, three plain fields; `packages/plugins/plugin-auth` one, a `secret` field; HotCRM none), so nothing first-party is newly refused.
8+
Clause-②: no (narrowing) — `os build`, `os validate`, `os lint` and the `object_extension` publish door (Studio, `PUT /api/v1/meta/object_extension/TARGET`, the MCP and AI authoring doors) now refuse an extension whose validation rule carries a `condition` or `when` the same rule on the object itself is refused for. No exported type or signature changes. Measured first-party: 0 extension entries carry a validation rule (`examples/app-showcase` one entry, three plain fields; `packages/plugins/plugin-auth` one, a `secret` field; HotCRM none), so nothing first-party is newly refused.
99
Metadata-protocol: narrowed
1010
Skills-docs: none owed — this PR edits `content/docs/deployment/validating-metadata.mdx` (the object extension door paragraph names the rule pass); `skills/objectstack-data` teaches validation rules on the object and `defineObjectExtension({ extend, fields })` without saying an extension's rules are exempt, so the build and the door now give the verdict the object's own rules already get.
1111

1212
<!-- adr-0087: registered object-extension-validation-rule-expression-judged -->
1313

14-
**BREAKING** accept-set narrowing of `os build`, `os validate` and the runtime `object_extension` publish door.
14+
**BREAKING** accept-set narrowing of `os build`, `os validate`, `os lint` and the runtime `object_extension` publish door.
1515

16-
**Before:** the registry merges an extension's `validations` onto its target object by name, so every save of the target runs them, but the expression rule judged only the rules an object declares. A rule an extension added with a misspelt field (`record.nmae`), an unregistered function (`sqrt(…)`) or an unguarded nullable operand was accepted by the build and published with a 200, and failed only when an end user saved a record.
16+
**Before:** the registry merges an extension's `validations` onto its target object by name, so every save of the target runs them, but the expression rule judged only the rules an object declares. A rule an extension added with a misspelt field (`record.nmae`), an unregistered function (`sqrt(…)`) or an unguarded nullable operand was accepted by `os build`, `os validate` and `os lint` and published with a 200, and failed only when an end user saved a record.
1717

18-
**Now:** each rule an `objectExtensions` entry adds is judged by the object's own rule pass (its `condition` and `when`, every nested `then` / `otherwise` branch, the `parent` root verdict and the null-guard gate), with the same rule id `expression-invalid`, against the extended object as served (its own fields and every field an extension adds). A finding is located at the entry: `object 'crm_account' · objectExtensions[0] · validation 'no_blocked__c'`, key path `objectExtensions[0].validations[0].condition`. At the door the refusal is `422 INVALID_METADATA`, the door still runs one pass (the extension walk), and a republish is judged without the package's own stored extension, which the write replaces.
18+
**Now:** each rule an `objectExtensions` entry adds is judged by the object's own rule pass (its `condition` and `when`, every nested `then` / `otherwise` branch, the `parent` root verdict and the null-guard gate), with the same rule id `expression-invalid`, in `os build`, `os validate` and `os lint` as at the door. A `record.FIELD` read resolves against the extended object as served (its own fields and every field an extension adds). The door reads the objects without the system columns the registry injects, as the build does, so a column such as `created_at` is a known field and is not judged nullable at either. A finding is located at the entry: `object 'crm_account' · objectExtensions[0] · validation 'no_blocked__c'`, key path `objectExtensions[0].validations[0].condition`. At the door the refusal is `422 INVALID_METADATA`, the door still runs one pass (the extension walk), and a republish is judged without the package's own stored extension, which the write replaces.
1919

20-
The null-guard gate is part of that pass, and it is the narrowing most likely to meet a working tenant: an extension rule that reads a nullable field with no guard, such as `record.credit > 5000000` on a `number` field with no `required: true` and no `defaultValue`, is now refused. At runtime that predicate aborts on every save where the field is empty, and the save is refused fail-closed. The repository's own suite carried one such rule and now guards it.
20+
The null-guard gate is part of that pass, and it is the narrowing most likely to meet a working tenant: an extension rule that applies an ordering or arithmetic operator (`>`, `<`, `+`, …) to a nullable field with no guard, such as `record.credit > 5000000` on a declared `number` field with no `required: true` and no `defaultValue`, is now refused. An equality such as `record.credit == null` is not. At runtime that predicate aborts on every save where the field is empty, and the save is refused fail-closed. The repository's own suite carried one such rule and now guards it.
2121

22-
**Unchanged:** the object save door, an object's own rules, a draft save (never gated), a stored row (judged only when it is published again), and the regex and JSON-schema checks of a `format` / `json_schema` rule, which still judge an object's own rules only.
22+
**Unchanged:** the object save door, an object's own rules, a draft save (never gated), a stored row (judged only when it is published again), and the regex and JSON-schema checks of a `format` / `json_schema` rule, which still judge an object's own rules only. The leaf of a one-hop read through a reference an extension adds (`record.manager__c.nmae`) is still not judged, at the build or the door: the hop checks read the declared objects. A hop through a reference the target declares is judged.
2323

24-
**Fix:** before building or publishing again, repair each refused rule the way you would on a rule declared on the same object: read `record.FIELD` for a field the object serves, call only registered functions, guard a nullable operand with `!= null` (`record.credit != null && record.credit > 5000000`), and read through a reference only in the shape a declared rule may.
24+
**Fix:** before building or publishing again, repair each refused rule the way you would on a rule declared on the same object: read `record.FIELD` for a field the object serves, call only registered functions, guard a nullable operand of an ordering or arithmetic operator with `!= null` (`record.credit != null && record.credit > 5000000`), and read through a reference only in the shape a declared rule may.

‎packages/lint/src/runtime-gate.object-formula-writes.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* the #22019 block of `packages/metadata-protocol/src/protocol.runtime-authoring-gate.test.ts`.
3939
*/
4040
import { describe, expect, it } from 'vitest';
41+
import { applyInjectedSystemColumns } from '@objectstack/metadata-core';
4142
import { EXPRESSION_INVALID, runAuthoringRules } from './authoring-rules.js';
4243
import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor } from './runtime-gate.js';
4344
import { runStackExpressionPasses, validateStackExpressions } from './validate-expressions.js';
@@ -520,6 +521,23 @@ describe('#33154 — an extension\'s validation rules are judged at the build an
520521
}
521522
});
522523

524+
it('⭐ PARITY — the door reads the objects as served, injected system columns and all: an unguarded ordering over `created_at` is accepted there as at the build, one over a declared nullable field is refused at both', () => {
525+
// As the registry serves them: `created_at`, `owner_id`, … declared.
526+
const served = [applyInjectedSystemColumns(target), applyInjectedSystemColumns(staff)];
527+
expect(Object.keys(served[0]!.fields)).toContain('created_at');
528+
for (const [condition, refused] of [
529+
["record.created_at > timestamp('2020-01-01T00:00:00Z')", false],
530+
['record.amount > 0', true],
531+
] as const) {
532+
const item = extension([script('since', condition)]);
533+
const atDoor = gateExtension(item, served);
534+
const atBuild = buildFindings({ objects: [target, staff], objectExtensions: [item] });
535+
536+
expect([...atDoor.errors, ...atDoor.advisories], condition).toEqual(atBuild);
537+
expect(atBuild.length > 0, `${condition}: ${dump(atBuild)}`).toBe(refused);
538+
}
539+
});
540+
523541
it('⭐ CONTROL — a valid rule, a rule reading a field the same extension adds, and a one-hop read publish clean', () => {
524542
for (const item of [
525543
extension([script('ok', 'record.amount != null && record.amount < 0')]),

‎packages/lint/src/validate-expressions.ts‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ import type { FlowCelEntrance } from './flow-cel-root-scope.js';
158158
import { injectedColumnDefsFor, injectedColumnsFor, unprovisionedInjectedColumnsFor } from './system-fields.js';
159159
import { findUnguardedNullableOperands, nullGuardMessage, provenNonNullKeys } from './validate-null-guards.js';
160160
import type { NullGuardOptions, NullGuardOutcome } from './validate-null-guards.js';
161+
import { stripInjectedSystemColumns } from '@objectstack/metadata-core';
161162
import { objectExtensionsByTarget, objectsAsServed, objectsWithoutTiedFields, recordsOf } from './object-graph.js';
162163
import { storedExpressionInputIssues } from './stored-expression-inputs.js';
163164
import { formulaCycleIssues } from './formula-cycle-issues.js';
@@ -2270,8 +2271,12 @@ export interface StackExpressionOptions {
22702271
* `objectsAsServed`, the build's own fold — and located at the entry, as the
22712272
* build locates it. [#33154] The same walk judges each validation rule the
22722273
* written entry adds, by the object walk's own rule pass
2273-
* (`judgeValidationRule`), against the same served target and located at
2274-
* the entry (`object 'T' · objectExtensions[0] · validation 'N'`). The
2274+
* (`judgeValidationRule`), its `record.FIELD` reads resolved against the
2275+
* same served fields (the leaf of a hop through a reference an extension
2276+
* adds is not judged; see the walk), and located at the entry
2277+
* (`object 'T' · objectExtensions[0] · validation 'N'`). On this write the
2278+
* live objects are read without the system columns the registry injects
2279+
* (`stripInjectedSystemColumns`), as the build reads them. The
22752280
* object walk reads an empty list (the carried objects are the universe the
22762281
* field or rule resolves in, not the subject) and so does every other pass;
22772282
* the function returns after the extension walk.
@@ -2359,6 +2364,17 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
23592364
if (options.runtimeWriteType === 'page') {
23602365
return actionCopyTemplateIssues(stack, { actions: false, objects: false, pages: true });
23612366
}
2367+
// [#33154] The extension door hands the objects as the registry SERVES
2368+
// them, and a served definition carries the system columns the registry
2369+
// injects (`created_at`, `owner_id`, …) as declared fields; the build reads
2370+
// the objects as authored. Unstripped, the null-guard gate judged
2371+
// `record.created_at` as a declared nullable field and refused at the door
2372+
// an extension rule `os build` accepts. So on that write each object is read
2373+
// through `stripInjectedSystemColumns` (`@objectstack/metadata-core`, the
2374+
// write path's own strip of exactly those, as `authored-site-expression.ts`
2375+
// reads the served objects), and the indexes re-derive the injected columns
2376+
// themselves, as they do at the build.
2377+
if (extensionWrite) stack = { ...stack, objects: recordsOf(stack.objects).map((o) => stripInjectedSystemColumns(o)) };
23622378
const objects = recordsOf(stack.objects);
23632379
// [#30471] The field indexes read the objects as the registry SERVES them,
23642380
// every `objectExtensions` field of any body folded in (`objectsAsServed`,
@@ -4064,9 +4080,9 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
40644080
// [#33072] The `object_extension` publish door runs this walk and nothing
40654081
// else: its snapshot carries the written entry as the sole member of
40664082
// `objectExtensions` and the door's live objects as `objects`, so the field
4067-
// (and, #33154, the rule) is judged against its target as served and
4068-
// located where the build locates it (`objectExtensions[0]` for a one-entry
4069-
// stack).
4083+
// (and, #33154, the rule's field reads) is judged against its target as
4084+
// served and located where the build locates it (`objectExtensions[0]` for
4085+
// a one-entry stack).
40704086
for (const [target, entries] of objectWrite ? [] : objectExtensionsByTarget(stack)) {
40714087
// The target's position in `objects`, which is its position in
40724088
// `typedObjects` too (see `objectPosition` above); -1 when undeclared.
@@ -4108,14 +4124,21 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
41084124
// [#33154] Each validation rule the entry adds, by the object walk's
41094125
// own rule pass ({@link judgeValidationRule}): the registry merges an
41104126
// extension's `validations` onto the target by name, so every save of
4111-
// the target runs it as it runs the target's own rules. Judged against
4112-
// the target as served, through the same indexes, so a read of a field
4113-
// this or a sibling extension adds resolves and a field nothing serves
4114-
// is unknown; an undeclared target is judged on what does not depend on
4115-
// its fields, as its fields' formulas are. Located at the entry
4116-
// (`object 'T' · objectExtensions[0] · validation 'N'`), the key path at
4117-
// the rule's slot under the entry, which the door re-spells
4118-
// item-relative (`validations.0.condition`). The index is the authored
4127+
// the target runs it as it runs the target's own rules. A `record.FIELD`
4128+
// read resolves against the target's fields as served (`fieldIndex`,
4129+
// over `objectsAsServed`), so a field this or a sibling extension adds
4130+
// resolves and a field nothing serves is unknown; an undeclared target
4131+
// is judged on what does not depend on its fields, as its fields'
4132+
// formulas are. Not every check reads the fold: the hop checks
4133+
// (`refuseUndeclaredHopField`, `storedMetadataBodyReferencesFor`) read
4134+
// the declared objects, so the leaf of a one-hop read through a
4135+
// reference an extension ADDS is not judged here (`record.manager__c.
4136+
// nmae` passes), while a hop through a reference the target declares
4137+
// is. Located at the entry (`object 'T' · objectExtensions[0] ·
4138+
// validation 'N'`), the key path at the rule's slot under the entry
4139+
// (`objectExtensions[0].validations[0].condition`): the door's 422
4140+
// carries that path as is, and only a draft read's `_diagnostics`
4141+
// re-spell it item-relative (`validations.0.condition`). The index is the authored
41194142
// one: `ObjectExtensionSchema` declares `validations` an array of rule
41204143
// records, and `recordsOf` hands an array's records back in order.
41214144
// Counted by hand, as `objectPosition` is, so the #5017 key scan sees

‎packages/runtime/src/domains/meta-object-extension-item.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { SqlDriver } from '@objectstack/driver-sql';
3535
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
3636
import { SysMetadata, SysMetadataHistoryObject, SysMetadataAuditObject } from '@objectstack/platform-objects/metadata';
3737
import { ObjectExtensionSchema } from '@objectstack/spec/data';
38+
import { runAuthoringRules } from '@objectstack/lint';
3839
import { HttpDispatcher } from '../http-dispatcher.js';
3940
// The [#29594] rehearsal boots the real dispatcher, whose package door reaches
4041
// `import('@objectstack/metadata')` in standalone-stack.ts; pay that load
@@ -500,6 +501,34 @@ describe('[#33154] the publish door judges an extension\'s validation rule as `o
500501
expect(await rig.rows()).toEqual([{ name: TARGET, package_id: PAIR, state: 'active' }]);
501502
}, 60_000);
502503

504+
it('⭐ PARITY — an unguarded ordering over the injected `created_at` publishes at the door, as `os build` accepts the same entry; one over a declared nullable field is refused at both', async () => {
505+
/** `os build`'s findings at the extension, over the account as authored. */
506+
const atBuild = (item: unknown) => {
507+
const stack = { objects: baseManifest().objects, objectExtensions: [item] };
508+
return runAuthoringRules('build', { normalized: stack, parsed: stack } as any)
509+
.filter((f) => f.where.includes('objectExtensions'))
510+
.map((f) => ({ rule: f.rule, where: f.where, path: f.path }));
511+
};
512+
const since = ruleBody("record.created_at > timestamp('2020-01-01T00:00:00Z')");
513+
expect(atBuild(since)).toEqual([]);
514+
515+
const rig = await boot();
516+
// Served, the account declares the columns the registry injects.
517+
expect((await rig.fieldsOn()).registry).toContain('created_at');
518+
await rig.saveAndPublish(PAIR, since);
519+
expect(servedRules(rig)).toContain('no_blocked__c');
520+
expect(await rig.rows()).toEqual([{ name: TARGET, package_id: PAIR, state: 'active' }]);
521+
522+
// The null-guard gate still judges a field the account declares nullable.
523+
const unguarded = ruleBody('record.bonus__c > 100', { bonus__c: { type: 'number', label: 'Bonus' } });
524+
const refusedAtBuild = atBuild(unguarded);
525+
expect(refusedAtBuild).toHaveLength(1);
526+
await rig.protocol.saveMetaItem({ type: TYPE, name: TARGET, item: unguarded, mode: 'draft', packageId: PAIR });
527+
const err: any = await rig.protocol.publishMetaItem({ type: TYPE, name: TARGET, packageId: PAIR }).catch((e: any) => e);
528+
expect({ code: err?.code, status: err?.status }).toEqual({ code: 'INVALID_METADATA', status: 422 });
529+
expect((err.issues ?? []).map((i: any) => ({ rule: i.rule, where: i.where, path: i.path }))).toEqual(refusedAtBuild);
530+
}, 60_000);
531+
503532
it('⭐ LIT (republish) — dropping a field the extension\'s own rule still reads is refused: the stored self lends it nothing', async () => {
504533
const rig = await boot();
505534
const BONUS = { bonus__c: { type: 'number', label: 'Bonus' } };

0 commit comments

Comments
 (0)