Changesets · Branches · Tags · Compare
| Changeset | 590460f7d20a4b8e5eb4ff0f79f52c2d7837db9721ff542c29f1be20d9cccb81 |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-08-25 15:14 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-08-25 15:14 -07:00 |
| Parents | e4b40f6efa03 |
2 files changed
modified packages/config-yaml/src/__tests__/index.test.ts +158 −0
| @@ -330,5 +330,163 @@ | ||
| 330 | 330 | ).toBe(true); |
| 331 | 331 | }); |
| 332 | 332 | |
| 333 | + it("should throw when a block is blocklisted", async () => { | |
| 334 | + const result = await unrollAssistant( | |
| 335 | + { | |
| 336 | + uriType: "slug", | |
| 337 | + fullSlug: { | |
| 338 | + ownerSlug: "test-org", | |
| 339 | + packageSlug: "agent", | |
| 340 | + versionSlug: "latest", | |
| 341 | + }, | |
| 342 | + }, | |
| 343 | + registry, | |
| 344 | + { | |
| 345 | + renderSecrets: true, | |
| 346 | + platformClient, | |
| 347 | + orgScopeId: "test-org", | |
| 348 | + currentUserSlug: "test-user", | |
| 349 | + onPremProxyUrl: null, | |
| 350 | + blocklistedBlocks: [ | |
| 351 | + { | |
| 352 | + ownerSlug: "test-org", | |
| 353 | + packageSlug: "gemini", | |
| 354 | + }, | |
| 355 | + ], | |
| 356 | + }, | |
| 357 | + ); | |
| 358 | + | |
| 359 | + // Should contain an error about the blocklisted block | |
| 360 | + expect(result.errors).toBeDefined(); | |
| 361 | + expect(result.errors?.length).toBeGreaterThan(0); | |
| 362 | + expect( | |
| 363 | + result.errors?.some(error => | |
| 364 | + error.message.includes("test-org/gemini is block listed and can not be used.") | |
| 365 | + ) | |
| 366 | + ).toBe(true); | |
| 367 | + }); | |
| 368 | + | |
| 369 | + it("should throw when a block is not on the allowlist", async () => { | |
| 370 | + const result = await unrollAssistant( | |
| 371 | + { | |
| 372 | + uriType: "slug", | |
| 373 | + fullSlug: { | |
| 374 | + ownerSlug: "test-org", | |
| 375 | + packageSlug: "agent", | |
| 376 | + versionSlug: "latest", | |
| 377 | + }, | |
| 378 | + }, | |
| 379 | + registry, | |
| 380 | + { | |
| 381 | + renderSecrets: true, | |
| 382 | + platformClient, | |
| 383 | + orgScopeId: "test-org", | |
| 384 | + currentUserSlug: "test-user", | |
| 385 | + onPremProxyUrl: null, | |
| 386 | + allowlistedBlocks: [ | |
| 387 | + { | |
| 388 | + ownerSlug: "test-org", | |
| 389 | + packageSlug: "docs", | |
| 390 | + }, | |
| 391 | + { | |
| 392 | + ownerSlug: "test-org", | |
| 393 | + packageSlug: "rules", | |
| 394 | + }, | |
| 395 | + { | |
| 396 | + ownerSlug: "test-org", | |
| 397 | + packageSlug: "claude35sonnet", | |
| 398 | + }, | |
| 399 | + ], | |
| 400 | + }, | |
| 401 | + ); | |
| 402 | + | |
| 403 | + // Should contain an error about the non-allowlisted block (gemini) | |
| 404 | + expect(result.errors).toBeDefined(); | |
| 405 | + expect(result.errors?.length).toBeGreaterThan(0); | |
| 406 | + expect( | |
| 407 | + result.errors?.some(error => | |
| 408 | + error.message.includes("test-org/gemini is block listed and can not be used.") | |
| 409 | + ) | |
| 410 | + ).toBe(true); | |
| 411 | + }); | |
| 412 | + | |
| 413 | + it("should allow blocks when they are on the allowlist", async () => { | |
| 414 | + const result = await unrollAssistant( | |
| 415 | + { | |
| 416 | + uriType: "slug", | |
| 417 | + fullSlug: { | |
| 418 | + ownerSlug: "test-org", | |
| 419 | + packageSlug: "agent", | |
| 420 | + versionSlug: "latest", | |
| 421 | + }, | |
| 422 | + }, | |
| 423 | + registry, | |
| 424 | + { | |
| 425 | + renderSecrets: true, | |
| 426 | + platformClient, | |
| 427 | + orgScopeId: "test-org", | |
| 428 | + currentUserSlug: "test-user", | |
| 429 | + onPremProxyUrl: null, | |
| 430 | + allowlistedBlocks: [ | |
| 431 | + { | |
| 432 | + ownerSlug: "test-org", | |
| 433 | + packageSlug: "docs", | |
| 434 | + }, | |
| 435 | + { | |
| 436 | + ownerSlug: "test-org", | |
| 437 | + packageSlug: "rules", | |
| 438 | + }, | |
| 439 | + { | |
| 440 | + ownerSlug: "test-org", | |
| 441 | + packageSlug: "gemini", | |
| 442 | + }, | |
| 443 | + { | |
| 444 | + ownerSlug: "test-org", | |
| 445 | + packageSlug: "claude35sonnet", | |
| 446 | + }, | |
| 447 | + ], | |
| 448 | + }, | |
| 449 | + ); | |
| 450 | + | |
| 451 | + // Should successfully unroll without errors for allowed blocks | |
| 452 | + expect(result.config?.models?.length).toBe(4); | |
| 453 | + expect(result.config?.rules?.length).toBe(2); | |
| 454 | + expect(result.config?.docs?.length).toBe(1); | |
| 455 | + }); | |
| 456 | + | |
| 457 | + it("should not affect file-based blocks with allow/blocklists", async () => { | |
| 458 | + const result = await unrollAssistant( | |
| 459 | + { | |
| 460 | + uriType: "file", | |
| 461 | + fileUri: "./src/__tests__/local-files/duplicate-test-agent.yaml", | |
| 462 | + }, | |
| 463 | + registry, | |
| 464 | + { | |
| 465 | + renderSecrets: true, | |
| 466 | + platformClient, | |
| 467 | + orgScopeId: "test-org", | |
| 468 | + currentUserSlug: "test-user", | |
| 469 | + onPremProxyUrl: null, | |
| 470 | + blocklistedBlocks: [ | |
| 471 | + { | |
| 472 | + ownerSlug: "test-org", | |
| 473 | + packageSlug: "docs", | |
| 474 | + }, | |
| 475 | + ], | |
| 476 | + injectBlocks: [ | |
| 477 | + { | |
| 478 | + uriType: "file", | |
| 479 | + fileUri: "./src/__tests__/local-files/rules.yaml", | |
| 480 | + }, | |
| 481 | + ], | |
| 482 | + }, | |
| 483 | + ); | |
| 484 | + | |
| 485 | + // File-based blocks should not be affected by blocklists | |
| 486 | + expect(result.config?.models?.length).toBe(1); | |
| 487 | + expect(result.config?.context?.length).toBe(1); | |
| 488 | + expect(result.config?.rules?.length).toBeGreaterThan(0); | |
| 489 | + }); | |
| 490 | + | |
| 333 | 491 | it.skip("should prioritize org over user / package secrets", () => {}); |
| 334 | 492 | }); |
modified packages/config-yaml/src/load/unroll.ts +68 −2
| @@ -6,8 +6,12 @@ | ||
| 6 | 6 | decodeFQSN, |
| 7 | 7 | decodePackageIdentifier, |
| 8 | 8 | encodeFQSN, |
| 9 | + encodePackageIdentifier, | |
| 10 | + encodePackageSlug, | |
| 9 | 11 | FQSN, |
| 10 | 12 | PackageIdentifier, |
| 13 | + PackageSlug, | |
| 14 | + packageSlugsEqual, | |
| 11 | 15 | } from "../interfaces/slugs.js"; |
| 12 | 16 | import { markdownToRule } from "../markdown/index.js"; |
| 13 | 17 | import { |
| @@ -197,6 +201,8 @@ | ||
| 197 | 201 | export interface BaseUnrollAssistantOptions { |
| 198 | 202 | renderSecrets: boolean; |
| 199 | 203 | injectBlocks?: PackageIdentifier[]; |
| 204 | + allowlistedBlocks?: PackageSlug[]; | |
| 205 | + blocklistedBlocks?: PackageSlug[]; | |
| 200 | 206 | } |
| 201 | 207 | |
| 202 | 208 | export interface DoNotRenderSecretsUnrollAssistantOptions |
| @@ -262,7 +268,13 @@ | ||
| 262 | 268 | config: unrolledAssistant, |
| 263 | 269 | configLoadInterrupted, |
| 264 | 270 | errors, |
| 265 | - } = await unrollBlocks(parsedYaml, registry, options.injectBlocks); | |
| 271 | + } = await unrollBlocks( | |
| 272 | + parsedYaml, | |
| 273 | + registry, | |
| 274 | + options.injectBlocks, | |
| 275 | + options.allowlistedBlocks, | |
| 276 | + options.blocklistedBlocks, | |
| 277 | + ); | |
| 266 | 278 | |
| 267 | 279 | // Back to a string so we can fill in template variables |
| 268 | 280 | const rawUnrolledYaml = YAML.stringify(unrolledAssistant); |
| @@ -300,10 +312,44 @@ | ||
| 300 | 312 | return { config: finalConfig, errors, configLoadInterrupted }; |
| 301 | 313 | } |
| 302 | 314 | |
| 315 | +function isPackageAllowed( | |
| 316 | + pkgId: PackageIdentifier, | |
| 317 | + allowlistedBlocks?: PackageSlug[], | |
| 318 | + blocklistedBlocks?: PackageSlug[], | |
| 319 | +): boolean { | |
| 320 | + // Only "slug" type blocks can be allow/block listed | |
| 321 | + if (pkgId.uriType !== "slug") { | |
| 322 | + return true; | |
| 323 | + } | |
| 324 | + | |
| 325 | + const packageSlug = { | |
| 326 | + ownerSlug: pkgId.fullSlug.ownerSlug, | |
| 327 | + packageSlug: pkgId.fullSlug.packageSlug, | |
| 328 | + }; | |
| 329 | + | |
| 330 | + if ( | |
| 331 | + allowlistedBlocks && | |
| 332 | + !allowlistedBlocks.some((block) => packageSlugsEqual(block, packageSlug)) | |
| 333 | + ) { | |
| 334 | + return false; | |
| 335 | + } | |
| 336 | + | |
| 337 | + if ( | |
| 338 | + blocklistedBlocks && | |
| 339 | + blocklistedBlocks.some((block) => packageSlugsEqual(block, packageSlug)) | |
| 340 | + ) { | |
| 341 | + return false; | |
| 342 | + } | |
| 343 | + | |
| 344 | + return true; | |
| 345 | +} | |
| 346 | + | |
| 303 | 347 | export async function unrollBlocks( |
| 304 | 348 | assistant: ConfigYaml, |
| 305 | 349 | registry: Registry, |
| 306 | 350 | injectBlocks: PackageIdentifier[] | undefined, |
| 351 | + allowlistedBlocks?: PackageSlug[], | |
| 352 | + blocklistedBlocks?: PackageSlug[], | |
| 307 | 353 | ): Promise<ConfigResult<AssistantUnrolled>> { |
| 308 | 354 | const errors: ConfigValidationError[] = []; |
| 309 | 355 | |
| @@ -336,8 +382,28 @@ | ||
| 336 | 382 | // "uses/with" block |
| 337 | 383 | if ("uses" in unrolledBlock) { |
| 338 | 384 | try { |
| 385 | + const blockIdentifier = decodePackageIdentifier(unrolledBlock.uses); | |
| 386 | + | |
| 387 | + if ( | |
| 388 | + !isPackageAllowed( | |
| 389 | + blockIdentifier, | |
| 390 | + allowlistedBlocks, | |
| 391 | + blocklistedBlocks, | |
| 392 | + ) | |
| 393 | + ) { | |
| 394 | + throw new Error( | |
| 395 | + `${blockIdentifier.uriType === 'slug' | |
| 396 | + ? encodePackageSlug({ | |
| 397 | + ownerSlug: blockIdentifier.fullSlug.ownerSlug, | |
| 398 | + packageSlug: blockIdentifier.fullSlug.packageSlug, | |
| 399 | + }) | |
| 400 | + : encodePackageIdentifier(blockIdentifier) | |
| 401 | + } is block listed and can not be used.`, | |
| 402 | + ); | |
| 403 | + } | |
| 404 | + | |
| 339 | 405 | const blockConfigYaml = await resolveBlock( |
| 340 | - decodePackageIdentifier(unrolledBlock.uses), | |
| 406 | + blockIdentifier, | |
| 341 | 407 | unrolledBlock.with, |
| 342 | 408 | registry, |
| 343 | 409 | ); |