|
6 | 6 | EnvironmentArtifactSchema, |
7 | 7 | Sha256DigestSchema, |
8 | 8 | } from './environment-artifact.zod'; |
| 9 | +import { PluginPermissionsSchema } from '../kernel/manifest.zod'; |
9 | 10 |
|
10 | 11 | import { |
11 | 12 | EXPORT_ENTRY_POINTS, |
@@ -246,3 +247,148 @@ describe('EnvironmentArtifactSchema (wire shape)', () => { |
246 | 247 | }); |
247 | 248 | }); |
248 | 249 | }); |
| 250 | + |
| 251 | +// ─── grantedPermissions (#14865) ──────────────────────────────────────────── |
| 252 | +// |
| 253 | +// The artifact-contract half of #11333 option A / the #13457 batch ruling: the |
| 254 | +// consented four-class permission set `{ services, hooks, network, fs }` rides |
| 255 | +// the envelope — written by the cloud control plane at consent-compile time, |
| 256 | +// read by the loader at materialize time. Before this key was declared, |
| 257 | +// `EnvironmentArtifactSchema` (a plain `z.object`) STRIPPED it at the artifact |
| 258 | +// door with no error — so the first pin is "the declared key survives", and its |
| 259 | +// positive control is "an unknown sibling is still stripped": the door must not |
| 260 | +// have gone passthrough to admit this key. Every pin here is a schema-reachable |
| 261 | +// parse, not a type-level assertion. |
| 262 | + |
| 263 | +describe('grantedPermissions — install-time granted set per plugin manifest `id` (#14865)', () => { |
| 264 | + const granted = { |
| 265 | + '@acme/plugin-crm': { |
| 266 | + services: ['object', 'http'], |
| 267 | + hooks: ['record.beforeInsert'], |
| 268 | + network: ['api.acme.com'], |
| 269 | + fs: [], |
| 270 | + }, |
| 271 | + '@acme/plugin-reports': {}, |
| 272 | + }; |
| 273 | + |
| 274 | + it('the declared key survives parse and round-trips the exact map', () => { |
| 275 | + const parsed = EnvironmentArtifactSchema.parse({ ...wireMinimal, grantedPermissions: granted }); |
| 276 | + expect(parsed).toHaveProperty('grantedPermissions'); |
| 277 | + expect(parsed.grantedPermissions).toEqual(granted); |
| 278 | + expect(Object.keys(parsed.grantedPermissions ?? {})).toEqual(['@acme/plugin-crm', '@acme/plugin-reports']); |
| 279 | + }); |
| 280 | + |
| 281 | + it('pure control: with no grantedPermissions present at all, an unknown top-level sibling is stripped (green before and after this key; red only if the door goes passthrough)', () => { |
| 282 | + const parsed = EnvironmentArtifactSchema.parse({ ...wireMinimal, notAnEnvelopeKey: { anything: 1 } }); |
| 283 | + expect(parsed).not.toHaveProperty('notAnEnvelopeKey'); |
| 284 | + expect(Object.keys(parsed).sort()).toEqual(['checksum', 'commitId', 'environmentId', 'metadata', 'schemaVersion']); |
| 285 | + }); |
| 286 | + |
| 287 | + it('positive control: an unknown top-level sibling is STILL stripped — the door admits the declared key, not everything', () => { |
| 288 | + const parsed = EnvironmentArtifactSchema.parse({ |
| 289 | + ...wireMinimal, |
| 290 | + grantedPermissions: granted, |
| 291 | + grantedPermissionz: granted, // near-miss spelling |
| 292 | + notAnEnvelopeKey: { anything: 1 }, |
| 293 | + }); |
| 294 | + expect(parsed.grantedPermissions).toEqual(granted); |
| 295 | + expect(parsed).not.toHaveProperty('grantedPermissionz'); |
| 296 | + expect(parsed).not.toHaveProperty('notAnEnvelopeKey'); |
| 297 | + expect(Object.keys(parsed).sort()).toEqual( |
| 298 | + ['checksum', 'commitId', 'environmentId', 'grantedPermissions', 'metadata', 'schemaVersion'], |
| 299 | + ); |
| 300 | + }); |
| 301 | + |
| 302 | + describe('absent ≠ `{}` — never collapsed (there is no `.default({})`, and there must not be)', () => { |
| 303 | + it('absent stays absent: no consent record', () => { |
| 304 | + const parsed = EnvironmentArtifactSchema.parse(wireMinimal); |
| 305 | + expect(Object.keys(parsed)).not.toContain('grantedPermissions'); |
| 306 | + expect(parsed.grantedPermissions).toBeUndefined(); |
| 307 | + }); |
| 308 | + |
| 309 | + it('`{}` stays `{}`: consent-bearing, consented to nothing', () => { |
| 310 | + const parsed = EnvironmentArtifactSchema.parse({ ...wireMinimal, grantedPermissions: {} }); |
| 311 | + expect(Object.keys(parsed)).toContain('grantedPermissions'); |
| 312 | + expect(parsed.grantedPermissions).toEqual({}); |
| 313 | + }); |
| 314 | + |
| 315 | + it('a per-plugin `{}` entry stays `{}`: that plugin consented to nothing', () => { |
| 316 | + const parsed = EnvironmentArtifactSchema.parse({ |
| 317 | + ...wireMinimal, |
| 318 | + grantedPermissions: { '@acme/plugin-reports': {} }, |
| 319 | + }); |
| 320 | + expect(parsed.grantedPermissions).toEqual({ '@acme/plugin-reports': {} }); |
| 321 | + }); |
| 322 | + |
| 323 | + it('the two readings stay distinguishable on the PARSED value, not only on the input', () => { |
| 324 | + const absent = EnvironmentArtifactSchema.parse(wireMinimal); |
| 325 | + const empty = EnvironmentArtifactSchema.parse({ ...wireMinimal, grantedPermissions: {} }); |
| 326 | + expect('grantedPermissions' in absent).toBe(false); |
| 327 | + expect('grantedPermissions' in empty).toBe(true); |
| 328 | + }); |
| 329 | + }); |
| 330 | + |
| 331 | + describe('value shape = the strict PluginPermissionsSchema (kernel/manifest.zod.ts)', () => { |
| 332 | + it('is the SAME declaration as the manifest requested set — identity, not a lookalike', () => { |
| 333 | + const record = EnvironmentArtifactSchema.shape.grantedPermissions.unwrap(); |
| 334 | + expect(record.valueType).toBe(PluginPermissionsSchema); |
| 335 | + }); |
| 336 | + |
| 337 | + it('refuses an unknown permission CLASS with `unrecognized_keys` at the plugin path, rather than granting it silently', () => { |
| 338 | + const result = EnvironmentArtifactSchema.safeParse({ |
| 339 | + ...wireMinimal, |
| 340 | + grantedPermissions: { '@acme/plugin-crm': { services: ['object'], shell: ['*'] } }, |
| 341 | + }); |
| 342 | + expect(result.success).toBe(false); |
| 343 | + if (result.success) return; |
| 344 | + const issue = result.error.issues.find((i) => i.code === 'unrecognized_keys'); |
| 345 | + expect(issue?.path).toEqual(['grantedPermissions', '@acme/plugin-crm']); |
| 346 | + expect((issue as { keys?: string[] } | undefined)?.keys).toEqual(['shell']); |
| 347 | + }); |
| 348 | + |
| 349 | + it('refuses a non-object per-plugin value and a non-record map', () => { |
| 350 | + for (const bad of [ |
| 351 | + { '@acme/plugin-crm': ['object'] }, |
| 352 | + { '@acme/plugin-crm': 'object' }, |
| 353 | + { '@acme/plugin-crm': null }, |
| 354 | + ['object'], |
| 355 | + 'object', |
| 356 | + ]) { |
| 357 | + expect( |
| 358 | + EnvironmentArtifactSchema.safeParse({ ...wireMinimal, grantedPermissions: bad }).success, |
| 359 | + `must refuse ${JSON.stringify(bad)}`, |
| 360 | + ).toBe(false); |
| 361 | + } |
| 362 | + }); |
| 363 | + |
| 364 | + it('accepts all four consented classes exactly as the manifest declaration spells them', () => { |
| 365 | + const full = { services: ['object'], hooks: ['record.beforeInsert'], network: ['api.acme.com'], fs: ['/tmp'] }; |
| 366 | + const parsed = EnvironmentArtifactSchema.parse({ ...wireMinimal, grantedPermissions: { '@acme/plugin-crm': full } }); |
| 367 | + expect(parsed.grantedPermissions?.['@acme/plugin-crm']).toEqual(full); |
| 368 | + }); |
| 369 | + }); |
| 370 | + |
| 371 | + describe('key = the plugin manifest `id` — the documented assumption, with the residual risk pinned on the contract text', () => { |
| 372 | + // The schema cannot distinguish a manifest `id` from a control-plane |
| 373 | + // `package_id` (both are strings), so this is a pin on the CONTRACT TEXT a |
| 374 | + // producer reads: the key's own description must name the manifest `id` as |
| 375 | + // the key, name `package_id` as what it is not, and state absent vs `{}`. |
| 376 | + // If the description stops saying so, the assumption is no longer |
| 377 | + // documented on the surface that carries it. |
| 378 | + it('the key description names the manifest `id` as the key, rules out `package_id`, and states absent vs `{}`', () => { |
| 379 | + const description = EnvironmentArtifactSchema.shape.grantedPermissions.description ?? ''; |
| 380 | + expect(description).toMatch(/keyed by the plugin manifest `id`/); |
| 381 | + expect(description).toMatch(/not the control-plane `package_id`/); |
| 382 | + expect(description).toMatch(/Absent = no consent record/); |
| 383 | + expect(description).toMatch(/`\{\}` = consent-bearing and consented to nothing/); |
| 384 | + }); |
| 385 | + |
| 386 | + it('any string key parses — which IS the residual risk: a `package_id`-shaped key is accepted and would simply never be looked up', () => { |
| 387 | + const parsed = EnvironmentArtifactSchema.parse({ |
| 388 | + ...wireMinimal, |
| 389 | + grantedPermissions: { pkg_01HABCDE: { services: ['object'] } }, |
| 390 | + }); |
| 391 | + expect(parsed.grantedPermissions).toEqual({ pkg_01HABCDE: { services: ['object'] } }); |
| 392 | + }); |
| 393 | + }); |
| 394 | +}); |
0 commit comments