|
58 | 58 | * uses, so the assertions are about what the endpoint SERVES and not about a |
59 | 59 | * derivation picked for convenience. |
60 | 60 | */ |
| 61 | +// [#35879] The served-payload pins boot the protocol, whose diagnostics load the |
| 62 | +// lint root lazily: pay that load during collection, never inside a clocked case. |
| 63 | +import '@objectstack/lint'; |
61 | 64 | import { describe, expect, it } from 'vitest'; |
62 | 65 | import { z } from 'zod'; |
63 | 66 | // [#5619] The producer's OWN write-verb dispatch decisions, so the fake engine |
64 | 67 | // below cannot accept a call ObjectQL itself refuses. |
65 | 68 | import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core'; |
66 | | -import { DEFAULT_METADATA_TYPE_REGISTRY, getMetadataTypeSchema } from '@objectstack/spec/kernel'; |
| 69 | +import { DEFAULT_METADATA_TYPE_REGISTRY, getMetadataTypeSchema, MetadataProtectionFields } from '@objectstack/spec/kernel'; |
67 | 70 | import { METADATA_FORM_REGISTRY } from '@objectstack/spec/system'; |
68 | 71 | import { ObjectStackProtocolImplementation } from './protocol.js'; |
69 | | -import { acceptsNothing } from './unauthorable-nodes.js'; |
| 72 | +import { acceptsNothing, isPlatformStampedKey } from './unauthorable-nodes.js'; |
70 | 73 | // [#19295] The emitter's OWN erased-authoring hook — the baseline below is |
71 | 74 | // derived with the code the server derives with, so the one difference this |
72 | 75 | // pin reads is the strip's. |
@@ -303,7 +306,7 @@ describe('#17502 — the served repeater row offers no column the parse door ref |
303 | 306 | expect(offenders).toEqual([]); |
304 | 307 | }); |
305 | 308 |
|
306 | | - it('over-drop guard: the served payload is its derivation MINUS unsatisfiable nodes, nothing else', async () => { |
| 309 | + it('over-drop guard: the served payload is its derivation MINUS unsatisfiable nodes and platform-stamped keys, nothing else', async () => { |
307 | 310 | // ⚠️ This is the direction the blast-radius pin in |
308 | 311 | // `protocol.meta-types-degenerate-derivation.test.ts` CANNOT see. Since |
309 | 312 | // #17502 its baseline is `stripUnauthorableProperties(preFixDerivation(type))`, |
@@ -332,9 +335,16 @@ describe('#17502 — the served repeater row offers no column the parse door ref |
332 | 335 | .map((before) => strippedDiff(before, after)) |
333 | 336 | .find((d) => d.other.length === 0); |
334 | 337 | if (!diff) { unexplained.push(type); continue; } |
335 | | - removedByType.set(type, diff.removed.map((r) => `${type}${r.path.slice(1)}`)); |
| 338 | + // [#35879] The strip's second class: an ADR-0010 key the platform |
| 339 | + // stamps, dropped from a `properties` map. Judged by its position |
| 340 | + // and name, never by its node — those nodes admit instances. |
| 341 | + const stamped = (path: string) => { |
| 342 | + const at = path.lastIndexOf('.properties.'); |
| 343 | + return at >= 0 && isPlatformStampedKey(path.slice(at + '.properties.'.length)); |
| 344 | + }; |
| 345 | + removedByType.set(type, diff.removed.filter((r) => !stamped(r.path)).map((r) => `${type}${r.path.slice(1)}`)); |
336 | 346 | for (const r of diff.removed) { |
337 | | - if (!acceptsNothing(r.node)) overDropped.push(`${type}${r.path.slice(1)}`); |
| 347 | + if (!acceptsNothing(r.node) && !stamped(r.path)) overDropped.push(`${type}${r.path.slice(1)}`); |
338 | 348 | } |
339 | 349 | } |
340 | 350 |
|
@@ -440,3 +450,114 @@ describe('#17502 — the removal is payload-only: every prescription channel sur |
440 | 450 | } |
441 | 451 | }); |
442 | 452 | }); |
| 453 | + |
| 454 | +/** |
| 455 | + * [#35879] The served payload offers no key the platform stamps. |
| 456 | + * |
| 457 | + * Every metadata type spreads the ADR-0010 protection envelope, and so does |
| 458 | + * every schema embedded in one, so the derivation lists `_lock`, `_lockReason`, |
| 459 | + * `_lockSource`, `_provenance`, `_packageId`, `_packageVersion` and |
| 460 | + * `_lockDocsUrl` among the properties Studio and the MCP draft door read as |
| 461 | + * the offer. None of them is an author's to write: a package's loader and the |
| 462 | + * registration stamp them (ADR-0010). Measured on `origin/main` at 7605e0c9 |
| 463 | + * before the strip: 308 such |
| 464 | + * nodes (44 envelopes) across 33 served types, `object` at its root, inside its |
| 465 | + * `fields` entries and in a `$defs` union, `view` in each union arm. |
| 466 | + * |
| 467 | + * The controls make each half fail on the tree before the fix: the pre-strip |
| 468 | + * derivation must carry the envelope where this pins its absence, and every |
| 469 | + * node the strip took must be an envelope node exactly as the envelope |
| 470 | + * declares it, at a place the whole envelope is spread, so a property that |
| 471 | + * merely shares a name is never what went missing. |
| 472 | + */ |
| 473 | +describe('[#35879] the served types offer no key the platform stamps', () => { |
| 474 | + const STAMPED = Object.keys(MetadataProtectionFields); |
| 475 | + |
| 476 | + /** Every `<path>.properties.<key>` in a document whose key is a stamped one, at any depth. */ |
| 477 | + function stampedPaths(node: unknown, path = '$'): string[] { |
| 478 | + if (Array.isArray(node)) return node.flatMap((n, i) => stampedPaths(n, `${path}[${i}]`)); |
| 479 | + if (!node || typeof node !== 'object') return []; |
| 480 | + const out: string[] = []; |
| 481 | + for (const [key, value] of Object.entries(node as Record<string, unknown>)) { |
| 482 | + if (key === 'default' || key === 'const' || key === 'enum' || key === 'examples') continue; |
| 483 | + if (key === 'properties' && value && typeof value === 'object') { |
| 484 | + for (const prop of Object.keys(value as Record<string, unknown>)) { |
| 485 | + if (STAMPED.includes(prop)) out.push(`${path}.properties.${prop}`); |
| 486 | + } |
| 487 | + } |
| 488 | + out.push(...stampedPaths(value, `${path}.${key}`)); |
| 489 | + } |
| 490 | + return out; |
| 491 | + } |
| 492 | + |
| 493 | + /** The node at a `$`-rooted dotted path with `[i]` indices, as `stampedPaths` spells it. */ |
| 494 | + function at(doc: unknown, path: string): unknown { |
| 495 | + const steps = path.slice(1).split(/\.|(?=\[)/).filter(Boolean); |
| 496 | + let node: any = doc; |
| 497 | + for (const step of steps) { |
| 498 | + const index = /^\[(\d+)\]$/.exec(step); |
| 499 | + node = index ? node?.[Number(index[1])] : node?.[step]; |
| 500 | + } |
| 501 | + return node; |
| 502 | + } |
| 503 | + |
| 504 | + it('control: the pre-strip derivation lists the whole envelope on `object` (root and field entry) and in every `view` arm', () => { |
| 505 | + const object = preStripDerivation('object') as any; |
| 506 | + expect(Object.keys(object.properties)).toEqual(expect.arrayContaining(STAMPED)); |
| 507 | + expect(Object.keys(object.properties.fields.additionalProperties.properties)).toEqual(expect.arrayContaining(STAMPED)); |
| 508 | + const view = preStripDerivation('view') as any; |
| 509 | + const arms = stampedPaths(view).filter((p) => p.endsWith('.properties._lock')); |
| 510 | + expect(arms.length).toBeGreaterThan(1); |
| 511 | + }); |
| 512 | + |
| 513 | + it('dark: no served type lists a stamped key in any properties map, at any depth', async () => { |
| 514 | + const served = await servedSchemas(); |
| 515 | + const offenders: string[] = []; |
| 516 | + for (const type of SERVED_TYPES) offenders.push(...stampedPaths(served.get(type), type)); |
| 517 | + // ⛔ An entry here is a key the endpoint offers that only the platform writes. |
| 518 | + expect(offenders).toEqual([]); |
| 519 | + }); |
| 520 | + |
| 521 | + it('lit: `object` and `view` still serve the keys an author writes beside them', async () => { |
| 522 | + const served = await servedSchemas(); |
| 523 | + const object = served.get('object') as any; |
| 524 | + for (const key of ['name', 'label', 'fields', 'enable']) expect(Object.keys(object.properties)).toContain(key); |
| 525 | + for (const key of ['type', 'label', 'required']) { |
| 526 | + expect(Object.keys(object.properties.fields.additionalProperties.properties)).toContain(key); |
| 527 | + } |
| 528 | + const view = served.get('view') as any; |
| 529 | + expect((view.anyOf as unknown[]).length).toBe(4); |
| 530 | + }); |
| 531 | + |
| 532 | + it('over-drop guard: every stamped node the strip took is the envelope\'s own node, at a place the whole envelope is spread', () => { |
| 533 | + // The envelope node each key derives to on its own: what a removed node |
| 534 | + // must equal, so a same-named property of another shape is never taken. |
| 535 | + const envelope = z.toJSONSchema(z.object(MetadataProtectionFields), { unrepresentable: 'any', override: markErasedAuthoringInput }) as any; |
| 536 | + let measured = 0; |
| 537 | + const foreign: string[] = []; |
| 538 | + for (const type of SERVED_TYPES) { |
| 539 | + for (const io of ['output', 'input'] as const) { |
| 540 | + const before = preStripDerivation(type, io); |
| 541 | + if (!before) continue; |
| 542 | + for (const path of stampedPaths(before, '$')) { |
| 543 | + measured += 1; |
| 544 | + const key = path.slice(path.lastIndexOf('.') + 1); |
| 545 | + const parent = at(before, path.slice(0, path.lastIndexOf('.properties.'))) as any; |
| 546 | + const whole = STAMPED.every((k) => k in (parent?.properties ?? {})); |
| 547 | + if (!whole || JSON.stringify(parent.properties[key]) !== JSON.stringify(envelope.properties[key])) { |
| 548 | + foreign.push(`${type}(${io})${path.slice(1)}`); |
| 549 | + } |
| 550 | + } |
| 551 | + } |
| 552 | + } |
| 553 | + expect(measured, 'the sweep read the envelope somewhere').toBeGreaterThan(0); |
| 554 | + // ⛔ An entry here is a property named like a stamp that is not the envelope's: it must not be withheld. |
| 555 | + expect(foreign).toEqual([]); |
| 556 | + }); |
| 557 | + |
| 558 | + it('the Zod shape still declares the envelope: a served body keeps re-parsing, nothing is removed upstream', () => { |
| 559 | + const object = getMetadataTypeSchema('object') as z.ZodTypeAny; |
| 560 | + const parsed = object.safeParse({ name: 'acct', label: 'Account', fields: {}, _packageId: 'com.acme.crm', _lock: 'none' }); |
| 561 | + expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true); |
| 562 | + }); |
| 563 | +}); |
0 commit comments