|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0015 §10, ADR-0095 D1] The one reading of the organization scope an |
| 5 | + * external object states, and the three ways out every door spells. |
| 6 | + * |
| 7 | + * Under a walled tenancy posture (`isolated` or `group`) every external object |
| 8 | + * (ADR-0015 `external`: an object bound to a datasource whose schema the |
| 9 | + * platform does not own) states its organization scope, in one of three ways: |
| 10 | + * its own `organization_id` field, `tenancy: { enabled: false }`, or |
| 11 | + * `systemFields: { tenant: false }`. The rule, the boot refusal of an object |
| 12 | + * stating none, the Layer 0 denial of one registered after the boot and the |
| 13 | + * write wall's refusal live in `@objectstack/plugin-security`'s |
| 14 | + * `external-organization-scope.ts`, whose module docblock is the full account. |
| 15 | + * |
| 16 | + * ## One predicate, every door |
| 17 | + * |
| 18 | + * {@link externalOrganizationScopeOf} is the one reading of the declaration. |
| 19 | + * The plugin's boot refusal enumerates the registry with it, its |
| 20 | + * `getObjectSecurityMeta` reads it for the Layer 0 input |
| 21 | + * (`externalOrganizationScopeUndeclared`, through |
| 22 | + * {@link externalOrganizationScopeIsUndeclared}), and the sync-in pull in |
| 23 | + * `@objectstack/service-automation` reads it to decide whose rows it may copy. |
| 24 | + * They cannot disagree, because none re-derives the question. |
| 25 | + * |
| 26 | + * ## Why it lives here, in `@objectstack/core` |
| 27 | + * |
| 28 | + * The reading is pure and everything it composes already sits at this layer: |
| 29 | + * {@link isFederatedObject} (`./tenant-field.ts`), the spec's |
| 30 | + * `isTenancyDisabled`, and `@objectstack/metadata-core`'s |
| 31 | + * {@link TENANT_SCOPE_FIELD_DEF} through {@link hasPhantomTenantAnchor}. A |
| 32 | + * service that needs the answer then depends on the kernel, which it already |
| 33 | + * does, rather than on the security plugin's package; the plugin re-exports |
| 34 | + * these bindings under its own module paths, so its public surface is |
| 35 | + * unchanged and every importer resolves to the same functions. |
| 36 | + */ |
| 37 | + |
| 38 | +import { isTenancyDisabled } from '@objectstack/spec/data'; |
| 39 | + |
| 40 | +// [#6562] The injected-column DEFINITION table lives in `@objectstack/metadata-core` |
| 41 | +// (the registry that provisions the columns re-exports it from there, and the `/meta` |
| 42 | +// read path consumes the same one). Importing the constant — rather than restating |
| 43 | +// its shape here — is what makes the provenance test track the producer instead of a |
| 44 | +// copy that can drift silently. |
| 45 | +import { TENANT_SCOPE_FIELD_DEF } from '@objectstack/metadata-core'; |
| 46 | + |
| 47 | +import { isFederatedObject } from './tenant-field.js'; |
| 48 | + |
| 49 | +/** The one column the Layer 0 wall keys on. */ |
| 50 | +const TENANT_SCOPE_COLUMN = 'organization_id'; |
| 51 | + |
| 52 | +/** Pick a field definition out of either registered `fields` shape. */ |
| 53 | +function readFieldDef(schema: unknown, name: string): unknown { |
| 54 | + const fields = (schema as { fields?: unknown } | null | undefined)?.fields; |
| 55 | + if (Array.isArray(fields)) return fields.find((f) => (f as { name?: unknown } | null)?.name === name); |
| 56 | + if (fields && typeof fields === 'object') return (fields as Record<string, unknown>)[name]; |
| 57 | + return undefined; |
| 58 | +} |
| 59 | + |
| 60 | +// ── Provenance of a federated object's `organization_id` (#7835) ──────────── |
| 61 | + |
| 62 | +/** |
| 63 | + * Structural identity against the shipped constant. Flat by construction — |
| 64 | + * every value in {@link TENANT_SCOPE_FIELD_DEF} is a primitive — so a flat |
| 65 | + * comparison is exact rather than a shortcut, and an extra or missing key is a |
| 66 | + * mismatch: any mismatch answers "not the platform's anchor", which leaves the |
| 67 | + * Layer 0 wall enforcing. The fail direction is toward isolation, never toward |
| 68 | + * exposure. |
| 69 | + * |
| 70 | + * The array shape carries an additional `name` key that the object shape |
| 71 | + * expresses as the map key; it is excluded so both shapes reach the same |
| 72 | + * verdict about the same column. |
| 73 | + */ |
| 74 | +function equalsShippedDef(def: unknown, shipped: Readonly<Record<string, unknown>>): boolean { |
| 75 | + if (!def || typeof def !== 'object' || Array.isArray(def)) return false; |
| 76 | + const actual = { ...(def as Record<string, unknown>) }; |
| 77 | + delete actual.name; |
| 78 | + const shippedKeys = Object.keys(shipped); |
| 79 | + if (Object.keys(actual).length !== shippedKeys.length) return false; |
| 80 | + return shippedKeys.every((k) => actual[k] === shipped[k]); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Does this object's `organization_id` exist only in the registry — i.e. is it |
| 85 | + * the platform's injected anchor on an object whose storage the platform never |
| 86 | + * provisioned? |
| 87 | + * |
| 88 | + * `applySystemFields` injects `organization_id` into every object that has not |
| 89 | + * opted out, federated ones included, but the platform provisions no storage |
| 90 | + * for a federated object. The question is provenance, answered by identity |
| 91 | + * against the shipped declaration: `applySystemFields` spreads |
| 92 | + * {@link TENANT_SCOPE_FIELD_DEF} verbatim and an authored field of the same |
| 93 | + * name suppresses the injection, so a registered def equal to the constant can |
| 94 | + * only have come from the platform (`@objectstack/plugin-security`'s |
| 95 | + * `federated-phantom-anchors.ts`, which re-exports this binding, carries the |
| 96 | + * full account). |
| 97 | + * |
| 98 | + * `true` ⇒ Layer 0 must treat the object as carrying NO tenant column, because |
| 99 | + * it does not. `false` for every local object (the platform DID provision the |
| 100 | + * column there) and for a federated object whose author declared a real remote |
| 101 | + * `organization_id`. |
| 102 | + */ |
| 103 | +export function hasPhantomTenantAnchor(schema: unknown): boolean { |
| 104 | + if (!isFederatedObject(schema)) return false; |
| 105 | + return equalsShippedDef(readFieldDef(schema, TENANT_SCOPE_COLUMN), TENANT_SCOPE_FIELD_DEF); |
| 106 | +} |
| 107 | + |
| 108 | +// ── The declaration an external object makes ────────────────────────────── |
| 109 | + |
| 110 | +/** |
| 111 | + * What an external object states about its organization scope. |
| 112 | + * |
| 113 | + * - `tenant-field-unenforceable` — `tenancy.tenantField` names a column other |
| 114 | + * than `organization_id`, which the wall does not read on an external object |
| 115 | + * (judged first: a tenant field the wall cannot read is refused whatever |
| 116 | + * else the object declares, unless it opted out of tenancy altogether). |
| 117 | + * - `shared` — the wall's own two opt-out clauses, `tenancy.enabled: false` or |
| 118 | + * `systemFields.tenant: false`; the whole-block `systemFields: false` is in |
| 119 | + * neither and states nothing. |
| 120 | + * - `walled` — it declares its own `organization_id` (any definition other |
| 121 | + * than the platform's injected one; a parsed copy of the shipped definition |
| 122 | + * carries the parser's defaults and so counts as the author's). An object |
| 123 | + * with its own column is never answered `shared` unless it also opted out, |
| 124 | + * because the wall walls it. |
| 125 | + * - `undeclared` — none of the above. |
| 126 | + */ |
| 127 | +export type ExternalOrganizationScope = 'walled' | 'shared' | 'undeclared' | 'tenant-field-unenforceable'; |
| 128 | + |
| 129 | +/** The declared `tenancy.tenantField`, when it names a column other than the wall's. */ |
| 130 | +export function declaredForeignTenantField(schema: unknown): string | null { |
| 131 | + const declared = (schema as { tenancy?: { tenantField?: unknown } } | null | undefined)?.tenancy?.tenantField; |
| 132 | + if (typeof declared !== 'string' || declared === '' || declared === TENANT_SCOPE_COLUMN) return null; |
| 133 | + return declared; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * The declaration an external object makes, or `null` for an object that is |
| 138 | + * not external (the question does not arise: the platform provisions its |
| 139 | + * columns, so its `organization_id` is real wherever it is registered). |
| 140 | + */ |
| 141 | +export function externalOrganizationScopeOf(schema: unknown): ExternalOrganizationScope | null { |
| 142 | + if (!isFederatedObject(schema)) return null; |
| 143 | + // The wall's two opt-out clauses, exactly as `getObjectSecurityMeta` reads |
| 144 | + // `tenancyDisabled`: an object that opted out is shared whatever column it |
| 145 | + // carries, because the wall composes nothing on it. |
| 146 | + const systemFields = (schema as { systemFields?: unknown }).systemFields; |
| 147 | + const optedOut = |
| 148 | + isTenancyDisabled(schema) || |
| 149 | + (!!systemFields && typeof systemFields === 'object' && (systemFields as { tenant?: unknown }).tenant === false); |
| 150 | + if (!optedOut && declaredForeignTenantField(schema) !== null) return 'tenant-field-unenforceable'; |
| 151 | + if (optedOut) return 'shared'; |
| 152 | + if (readFieldDef(schema, TENANT_SCOPE_COLUMN) !== undefined && !hasPhantomTenantAnchor(schema)) return 'walled'; |
| 153 | + return 'undeclared'; |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Does Layer 0 deny this object under a walled posture because it is external |
| 158 | + * and states no enforceable organization scope? `false` for every local |
| 159 | + * object and for an external object that declared `walled` or `shared`. |
| 160 | + */ |
| 161 | +export function externalOrganizationScopeIsUndeclared(schema: unknown): boolean { |
| 162 | + const scope = externalOrganizationScopeOf(schema); |
| 163 | + return scope === 'undeclared' || scope === 'tenant-field-unenforceable'; |
| 164 | +} |
| 165 | + |
| 166 | +/** The three ways out, as every refusal and warning spells them. */ |
| 167 | +export const EXTERNAL_SCOPE_REMEDY = |
| 168 | + 'Declare one of three on each: an `organization_id` field of its own, when the remote rows carry the ' + |
| 169 | + 'organization column and the driver returns it (a remote column of another name is mapped onto it with ' + |
| 170 | + "`external.columnMap: { <remote column>: 'organization_id' }`) — the rows are then walled by organization; " + |
| 171 | + '`tenancy: { enabled: false }` — the object is shared by every organization; or ' + |
| 172 | + '`systemFields: { tenant: false }` — the same opt-out, spelled on the system fields.'; |
0 commit comments