Skip to content

Commit c67c1df

Browse files
committed
fix(objectql,cloud-connection,service-automation): the installed-package classifier trusts only what a registering door wrote
registerApp and the residual door refuse a collection item whose body _packageId differs from the package registering it; install-local registers every packages[] member as 'marketplace'; the manifest service records each residual owner's door; a stamp no registration answers for is refused its package-relative refs. Claude-Session: https://claude.ai/code/session_017xRBcVBKAFk1ntXSjvPt7U Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2be3fd4 commit c67c1df

5 files changed

Lines changed: 331 additions & 63 deletions

File tree

‎packages/cloud-connection/src/marketplace-install-local-plugin.ts‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,37 @@ function declaredDatasources(body: unknown): any[] {
650650
: [];
651651
}
652652

653+
/**
654+
* [#28534] The artifact as this door hands it to the `manifest` service: every
655+
* `packages[]` entry that states a `source` other than `'marketplace'` states
656+
* `'marketplace'` instead; every other key, and every entry stating none or
657+
* `'marketplace'`, is handed through as received (by reference when nothing
658+
* changes).
659+
*
660+
* The `manifest` service stamps an entry's own `source` over the door's
661+
* (`ArtifactPackageSchema.source`: the producer that MERGED an artifact knows
662+
* each member's origin — a control plane folding installs into a boot
663+
* artifact), so a bundle installed here whose member said `'code'` registered
664+
* that member as the host's own code. Every package this door registers
665+
* reached the runtime through it, whatever its entry says about how it reached
666+
* the artifact, so the door's word is the origin; overridden rather than
667+
* refused because the entry's statement is about the artifact's assembly, not
668+
* wrong in itself, and because the rehydrate must register a ledger entry
669+
* written before this rule without refusing it. The rule lives at this door,
670+
* not in the `manifest` service, so a boot that registers a merged artifact
671+
* stating no source keeps every member's own statement.
672+
*/
673+
export function asMarketplaceRegistration<T>(artifact: T): T {
674+
if (!isPlainObject(artifact) || !Array.isArray(artifact.packages)) return artifact;
675+
const restated = (entry: unknown): boolean =>
676+
isPlainObject(entry) && entry.source !== undefined && entry.source !== 'marketplace';
677+
if (!artifact.packages.some(restated)) return artifact;
678+
return {
679+
...artifact,
680+
packages: artifact.packages.map((entry: unknown) => (restated(entry) ? { ...(entry as object), source: 'marketplace' } : entry)),
681+
} as T;
682+
}
683+
653684
/**
654685
* The seed datasets a cached manifest bundles: its `data[]` entries that name
655686
* an object and carry records. One reading for the two doors that match seed
@@ -1018,8 +1049,11 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
10181049
// the package as soon as rehydrate reports success.
10191050
// [#23972] Every ledger entry is a marketplace install, so its
10201051
// registry row says so (`source: 'marketplace'`) after a restart
1021-
// exactly as it did when it was installed.
1022-
await manifestService!.register(converted, { source: 'marketplace' });
1052+
// exactly as it did when it was installed — every member of a
1053+
// `packages[]` bundle included, whatever its entry states
1054+
// ([#28534], {@link asMarketplaceRegistration}), a ledger entry
1055+
// written before that rule included.
1056+
await manifestService!.register(asMarketplaceRegistration(converted), { source: 'marketplace' });
10231057
// Sync schemas so the driver creates tables for the newly-
10241058
// registered objects (idempotent — already-synced tables
10251059
// are no-ops).
@@ -1947,8 +1981,10 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
19471981
// metadata service — a caller that reads metadata right after a
19481982
// 200 (AI describe_object, Studio object list) must see them.
19491983
// [#23972] The registry row records where this package came
1950-
// from: the marketplace (`source: 'marketplace'`).
1951-
await manifestService.register(converted, { source: 'marketplace' });
1984+
// from: the marketplace (`source: 'marketplace'`) — for every
1985+
// member of a `packages[]` bundle too, whatever its entry states
1986+
// ([#28534], {@link asMarketplaceRegistration}).
1987+
await manifestService.register(asMarketplaceRegistration(converted), { source: 'marketplace' });
19521988
} catch (err: any) {
19531989
const registerError = String(err?.message ?? err);
19541990
// For offline file imports we treat a register failure as a hard

‎packages/objectql/src/engine.ts‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4812,6 +4812,63 @@ export function permissionSetOwnerRegistrationRefusal(
48124812
return err;
48134813
}
48144814

4815+
/**
4816+
* [#28534] The registration door's refusal of a collection item whose body
4817+
* already carries a `_packageId` other than the package it is registered under
4818+
* (`ownerId`; none for a manifest that carries no id).
4819+
*
4820+
* `_packageId` is the ADR-0010 envelope stamp: the package that registered the
4821+
* item, written by the door (`stampPackage` here, `registerItem` behind it,
4822+
* both through `applyProtection`). `applyProtection` writes it only while it is
4823+
* `undefined`, and every item schema that spreads `MetadataProtectionFields`
4824+
* (a connector entry among them) declares the key, so a body could arrive
4825+
* wearing ANOTHER package's id and keep it: an installed package naming the
4826+
* host's own id, or an id no package holds, and every reader that asks an
4827+
* item's owner (the connector materializer judging whose root a file ref
4828+
* resolves against, the disabled-package filter, the ADR-0130 artifact gates)
4829+
* then reads a stamp the body wrote about itself.
4830+
*
4831+
* Refused rather than overwritten, beside the two other body-against-key
4832+
* refusals of the same loop (the divergent view-container `name`, the
4833+
* permission set's `packageId`): resolving a disagreement between the body and
4834+
* the key it is filed under silently, in either direction, files the item
4835+
* under something nobody wrote (commit d0ee598e6). A stamp EQUAL to the owner
4836+
* is accepted — it is what an item re-registered under its own package (a
4837+
* ledger rehydrate, an artifact the metadata door already stamped in place
4838+
* with the same body id) carries. `null` and the empty string are stamps too:
4839+
* neither is overwritten by `applyProtection`, so either would survive as an
4840+
* owner no row answers.
4841+
*
4842+
* `VALIDATION_ERROR` / 400, `issues[]` at `_packageId`: the envelope of
4843+
* {@link permissionSetOwnerRegistrationRefusal}. Exported for the stack's
4844+
* RESIDUAL registration (`registerUnclaimedTopLevelItems` in `plugin.ts`),
4845+
* which files top-level items under the residual owner and refuses the same
4846+
* stamp in this same envelope. ⛔ Never rebuild the envelope at a second door.
4847+
*/
4848+
export function collectionItemOwnerStampRefusal(
4849+
item: unknown,
4850+
type: string,
4851+
name: string,
4852+
ownerId: string | undefined,
4853+
): (Error & { code: 'VALIDATION_ERROR'; status: 400; httpStatus: 400; issues: Array<{ path: string; message: string; code: string }> }) | undefined {
4854+
if (!item || typeof item !== 'object') return undefined;
4855+
const stamped = (item as { _packageId?: unknown })._packageId;
4856+
if (stamped === undefined || stamped === ownerId) return undefined;
4857+
const owner = ownerId === undefined ? 'no package' : `package '${ownerId}'`;
4858+
const issueMessage = `carries _packageId ${JSON.stringify(stamped)}, but it is registered under ${owner}`;
4859+
const err = new Error(
4860+
`Drop \`_packageId\` from ${type} '${name}': it ${issueMessage}. The package an item belongs to is the one `
4861+
+ 'that registers it, stamped by the registration; a body never names its own owner.',
4862+
) as Error & {
4863+
code: 'VALIDATION_ERROR'; status: 400; httpStatus: 400; issues: Array<{ path: string; message: string; code: string }>;
4864+
};
4865+
err.code = 'VALIDATION_ERROR';
4866+
err.status = 400;
4867+
err.httpStatus = 400;
4868+
err.issues = [{ path: '_packageId', message: issueMessage, code: 'custom' }];
4869+
return err;
4870+
}
4871+
48154872
/**
48164873
* [#5320] The registration door's refusal of a `views:` entry that is not a
48174874
* view CONTAINER (`isViewContainerShaped` is false: a ViewItem's
@@ -5049,6 +5106,40 @@ export class ObjectQL implements IObjectQLEngine {
50495106
// Package manifests registry (for defaultDatasource lookup)
50505107
private manifests = new Map<string, any>();
50515108

5109+
// [#28534] Where each ADR-0130 residual owner came from — see
5110+
// {@link recordResidualOwner}.
5111+
private readonly residualOwnerSources = new Map<string, 'code' | 'marketplace'>();
5112+
5113+
/**
5114+
* [#28534] Record the origin of an artifact's RESIDUAL owner: the id the
5115+
* residual rule files a multi-package stack's unclaimed top-level items
5116+
* under (`unclaimedTopLevel`, `@objectstack/metadata`), as the door that
5117+
* registered the artifact states it — `'code'` for the deployment's own
5118+
* boot, `'marketplace'` for an install.
5119+
*
5120+
* The residual is not a package, so no package row records that id (unless
5121+
* a body carries it, and then the body's row answers), yet its items wear it
5122+
* as their `_packageId`. Without this record a reader asking an item's
5123+
* origin could not tell the host's own residual from an installed bundle's.
5124+
* Called by the `manifest` service, the one place that knows both the
5125+
* residual's id and the door's statement. Once an id is recorded as
5126+
* `'marketplace'` it stays so for this engine: an install can never turn
5127+
* into the host's own by re-registering under the same id.
5128+
*/
5129+
recordResidualOwner(ownerId: string, source: 'code' | 'marketplace'): void {
5130+
if (this.residualOwnerSources.get(ownerId) === 'marketplace') return;
5131+
this.residualOwnerSources.set(ownerId, source);
5132+
}
5133+
5134+
/**
5135+
* [#28534] The origin {@link recordResidualOwner} recorded for a residual
5136+
* owner id, or `undefined` when no artifact this engine registered named it
5137+
* as its residual's owner.
5138+
*/
5139+
residualOwnerSource(ownerId: string): 'code' | 'marketplace' | undefined {
5140+
return this.residualOwnerSources.get(ownerId);
5141+
}
5142+
50525143
// Datasource definitions by name (ADR-0015): carries schemaMode +
50535144
// external.allowWrites so the write gate (Gate 3) can enforce federation
50545145
// ownership, and external.credentialsRef so a sys_secret reference sweep can
@@ -8921,6 +9012,10 @@ export class ObjectQL implements IObjectQLEngine {
89219012
this.logger.debug('Registering seed data datasets', { id, count: seedData.length });
89229013
for (const dataset of seedData) {
89239014
if (dataset.object) {
9015+
// [#28534] The collection loop's owner-stamp refusal, for the
9016+
// one collection filed outside it.
9017+
const ownerStamp = collectionItemOwnerStampRefusal(dataset, 'data', String(dataset.object), id);
9018+
if (ownerStamp) throw ownerStamp;
89249019
this._registry.registerItem('data', stampPackage(dataset, id, version), 'object' as any, id);
89259020
}
89269021
}
@@ -9113,6 +9208,15 @@ export class ObjectQL implements IObjectQLEngine {
91139208
const refusal = permissionSetOwnerRegistrationRefusal(item, itemName, ownerId);
91149209
if (refusal) throw refusal;
91159210
}
9211+
// [#28534] …and an item of ANY collection whose body already wears
9212+
// a `_packageId` other than `ownerId`: `stampPackage` and
9213+
// `registerItem` stamp the owner only while the key is unset, so
9214+
// the body's own stamp used to win. See
9215+
// {@link collectionItemOwnerStampRefusal}.
9216+
{
9217+
const refusal = collectionItemOwnerStampRefusal(item, pluralToSingular(key), itemName, ownerId);
9218+
if (refusal) throw refusal;
9219+
}
91169220
const toRegister = item.name === itemName ? item : { ...item, name: itemName };
91179221
// [#5320] The `views:` tighten — containers ONLY, the contract the
91189222
// stack schema has always declared (`stack.zod.ts`,
@@ -9186,6 +9290,8 @@ export class ObjectQL implements IObjectQLEngine {
91869290
this.logger.warn('Skipping viewItems entry without a derivable name', { id: ownerId });
91879291
continue;
91889292
}
9293+
const ownerStamp = collectionItemOwnerStampRefusal(body, 'view', itemName, ownerId);
9294+
if (ownerStamp) throw ownerStamp;
91899295
const toRegister = body.name === itemName ? body : { ...body, name: itemName };
91909296
this._registry.registerItem('view', stampPackage(toRegister, ownerId, ownerVersion), 'name' as any, ownerId);
91919297
}

‎packages/objectql/src/plugin.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {
44
ObjectQL,
55
SECURITY_SERVICE_SOURCE_SLOT,
6+
collectionItemOwnerStampRefusal,
67
permissionSetOwnerRegistrationRefusal,
78
viewsEntryNotContainerRefusal,
89
type UnservableControlledByParentMaster,
@@ -953,6 +954,14 @@ export class ObjectQLPlugin implements Plugin {
953954
// the residual is not a package, and when the id names one of the
954955
// bodies (a composed stack keeps one member's manifest) that body's
955956
// record must not be replaced.
957+
// [#28534] …and the door's statement of where that owner came from,
958+
// recorded before anything of it registers: no package row answers the
959+
// id, and a reader of an item's origin must tell the host's own
960+
// residual from an installed bundle's (`ObjectQL.recordResidualOwner`).
961+
// The residual is not an artifact entry, so it states no source of its
962+
// own; the door's word, `code` when it states none, is its origin.
963+
const residualOwnerId = residual?.ownerId ?? residualItems?.ownerId;
964+
if (residualOwnerId !== undefined) ql.recordResidualOwner(residualOwnerId, options?.source ?? 'code');
956965
if (residual) this.registerUnclaimedTopLevelObjects(ctx, residual, ordered);
957966
// [ADR-0015 D7, ADR-0130 D4] Every other residual item, the same way
958967
// and in the same place: after every body, under the residual rule's
@@ -3153,12 +3162,22 @@ export class ObjectQLPlugin implements Plugin {
31533162
const stamp = (item: Record<string, unknown>): Record<string, unknown> => ownerVersion === undefined
31543163
? { ...item }
31553164
: applyProtection({ ...item } as any, { packageId: ownerId, packageVersion: ownerVersion });
3165+
// [#28534] The body seam's owner-stamp refusal, in its envelope and at its
3166+
// place in the order (after the name and permission-set judges): a residual
3167+
// item wearing a `_packageId` other than the residual's id kept it through
3168+
// `stamp` and `registerItem` alike, which stamp only an unset key
3169+
// ({@link collectionItemOwnerStampRefusal}, `engine.ts`).
3170+
const refuseOwnerStamp = (item: Record<string, unknown>, type: string, name: string | undefined): void => {
3171+
const refusal = collectionItemOwnerStampRefusal(item, type, name ?? '(unnamed)', ownerId);
3172+
if (refusal) throw refusal;
3173+
};
31563174
for (const { type, item } of residual.items) {
31573175
if (type === 'view' && isAggregatedViewContainer(item)) {
31583176
const nameRefusal = viewContainerNameRefusal(item, 'manifest', ownerId);
31593177
if (nameRefusal) throw nameRefusal;
31603178
const object = deriveViewContainerObject(item);
31613179
if (!object) continue;
3180+
refuseOwnerStamp(item, type, object);
31623181
const container = stamp({ ...item, name: object });
31633182
ql.registry.registerItem('view', container, 'name' as any, ownerId);
31643183
for (const view of expandViewContainer(object, container)) {
@@ -3178,6 +3197,7 @@ export class ObjectQLPlugin implements Plugin {
31783197
// entry that is not container-shaped (`viewKind`/`config`/flattened
31793198
// overlay keys) is refused — `views:` carries containers only (#5320).
31803199
const name = (item.name as string | undefined) || (type === 'view' ? deriveViewContainerObject(item) : undefined);
3200+
refuseOwnerStamp(item, type, name);
31813201
const toRegister = item.name === name ? item : { ...item, name };
31823202
if (type === 'view' && !isViewContainerShaped(toRegister)) {
31833203
throw viewsEntryNotContainerRefusal(name, 'manifest', ownerId);

0 commit comments

Comments
 (0)