From c259c982babbd9fe93c0da8d51cac55dbcb66c0a Mon Sep 17 00:00:00 2001 From: Himanshu Date: Tue, 1 Dec 2026 15:18:03 +0530 Subject: [PATCH] fix(runtime): allowlist the A2A protocol headers on A2A runtimes AgentCore Runtime only forwards request headers that are in the runtime's requestHeaderAllowlist, so an A2A server never received A2A-Version or A2A-Extensions. When the protocol is A2A, add both headers to the allowlist in `add runtime` and in the A2A Strands template (case-insensitive, headers the user passed are kept, other protocols are unchanged). Closes #2981 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../templates/runtime-docs/python/README.md | 5 +++ .../runtime-docs/typescript/README.md | 5 +++ .../__snapshots__/manager.test.ts.snap | 4 ++ src/core/project/manager.test.ts | 1 + src/core/project/templates/runtime.test.ts | 16 +++++++ src/core/project/templates/runtime.ts | 17 ++++++- src/core/project/templates/runtimeDocs.ts | 8 ++-- .../project/add/runtime/index.test.ts | 45 ++++++++++++++++++- src/projectSchemas/runtime.test.ts | 21 +++++++++ src/projectSchemas/runtime.ts | 23 ++++++++++ 10 files changed, 139 insertions(+), 6 deletions(-) diff --git a/src/assets/templates/runtime-docs/python/README.md b/src/assets/templates/runtime-docs/python/README.md index 4bb709a3f3..febf2414b2 100644 --- a/src/assets/templates/runtime-docs/python/README.md +++ b/src/assets/templates/runtime-docs/python/README.md @@ -159,6 +159,11 @@ Add `bedrock-agentcore[a2a] ~= 1.24.0` (the version line with `serve_a2a`) and `agentcore dev` sets `A2A_PORT` when it needs a local port other than the deployed service-contract default of 9000; `serve_a2a` reads it automatically. +AgentCore forwards only allowlisted request headers to your server, so the CLI +puts the A2A protocol headers (`A2A-Version` and `A2A-Extensions`) in this +runtime's `requestHeaderAllowlist` in `agentcore.json`. Keep them there, or your +server never receives them. + Full contract: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-a2a-protocol-contract.html {{/if}} {{#if (eq protocol "AGUI")}} diff --git a/src/assets/templates/runtime-docs/typescript/README.md b/src/assets/templates/runtime-docs/typescript/README.md index b559444f57..d1e4bb68c5 100644 --- a/src/assets/templates/runtime-docs/typescript/README.md +++ b/src/assets/templates/runtime-docs/typescript/README.md @@ -112,6 +112,11 @@ dev), takes JSON-RPC 2.0 messages (such as `message/send`) on `POST /`, publishes its agent card at `GET /.well-known/agent-card.json`, and answers health checks on `GET /ping`. +AgentCore forwards only allowlisted request headers to your server, so the CLI +puts the A2A protocol headers (`A2A-Version` and `A2A-Extensions`) in this +runtime's `requestHeaderAllowlist` in `agentcore.json`. Keep them there, or your +server never receives them. + Full contract: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-a2a-protocol-contract.html {{/if}} {{#if (eq protocol "AGUI")}} diff --git a/src/core/project/__snapshots__/manager.test.ts.snap b/src/core/project/__snapshots__/manager.test.ts.snap index e33bd9075f..0547e3a338 100644 --- a/src/core/project/__snapshots__/manager.test.ts.snap +++ b/src/core/project/__snapshots__/manager.test.ts.snap @@ -193,6 +193,10 @@ exports[`FsProjectManager.create snapshots the Strands A2A project manifest and "modelProvider": "Bedrock", "name": "a2a_python_strands", "protocol": "A2A", + "requestHeaderAllowlist": [ + "A2A-Version", + "A2A-Extensions", + ], "runtimeVersion": "PYTHON_3_14", }, ], diff --git a/src/core/project/manager.test.ts b/src/core/project/manager.test.ts index 85a3660aa2..199097a45d 100644 --- a/src/core/project/manager.test.ts +++ b/src/core/project/manager.test.ts @@ -3372,6 +3372,7 @@ describe("docs-only runtimes", () => { runtimeVersion: "PYTHON_3_14", entrypoint: "main.py", protocol: "A2A", + requestHeaderAllowlist: ["A2A-Version", "A2A-Extensions"], }, ]); expect((await readdir(join(project.rootPath, "app", "search"))).sort()).toEqual([ diff --git a/src/core/project/templates/runtime.test.ts b/src/core/project/templates/runtime.test.ts index 30ad917f02..7ae75f7afb 100644 --- a/src/core/project/templates/runtime.test.ts +++ b/src/core/project/templates/runtime.test.ts @@ -117,6 +117,22 @@ describe("runtime templates", () => { expect(await Bun.file(join(appDir, ".dockerignore")).exists()).toBe(isContainer); }); + // AgentCore Runtime forwards only allowlisted request headers, so an A2A runtime must allow the + // A2A protocol headers; the other protocols leave the allowlist as the user set it. + test.each<[RuntimeTemplateShortcutName, Partial, string[] | undefined]>([ + ["a2a-python-strands", {}, ["A2A-Version", "A2A-Extensions"]], + [ + "a2a-python-strands", + { requestHeaderAllowlist: ["X-Custom-Header"] }, + ["X-Custom-Header", "A2A-Version", "A2A-Extensions"], + ], + ["agent-python-strands", {}, undefined], + ["mcp-python-fastmcp", { requestHeaderAllowlist: ["X-Custom-Header"] }, ["X-Custom-Header"]], + ])("%s writes the request header allowlist %p", async (template, infra, expected) => { + const { runtime } = await addTemplate("my_agent", template, { infra }); + expect(runtime.requestHeaderAllowlist).toEqual(expected); + }); + // Build starts the agent in a box under opentelemetry-instrument, so a box template keeps OTEL on // as every other template does, and writes no instrumentation. test.each([ diff --git a/src/core/project/templates/runtime.ts b/src/core/project/templates/runtime.ts index 330c181c9c..63da2b0857 100644 --- a/src/core/project/templates/runtime.ts +++ b/src/core/project/templates/runtime.ts @@ -1,7 +1,11 @@ import { FsTreeNode } from "./fsTree"; import type { AssetSource } from "../source"; import type { RuntimeResourceConfig } from "../../../handlers/project/add/runtime/types"; -import { DEFAULT_MODEL_IDS, type ProjectRuntime } from "../../../projectSchemas/runtime"; +import { + DEFAULT_MODEL_IDS, + withProtocolRequestHeaders, + type ProjectRuntime, +} from "../../../projectSchemas/runtime"; import { mergeSpecEntries } from "./spec"; import type { SpecEntries, TemplateRenderer, TemplateResolver } from "./types"; import type { @@ -527,7 +531,16 @@ const getTemplateResolvers = (assetSource: AssetSource, templateRenderer: Templa tree, spec: mergeSpecEntries([ { - runtimes: [{ ...buildRuntimeSpec(input), protocol: "A2A" as const }], + runtimes: [ + { + ...buildRuntimeSpec(input), + protocol: "A2A" as const, + requestHeaderAllowlist: withProtocolRequestHeaders( + "A2A", + input.requestHeaderAllowlist, + ), + }, + ], ...(memory && { memories: [memory] }), }, modelScaffold.spec, diff --git a/src/core/project/templates/runtimeDocs.ts b/src/core/project/templates/runtimeDocs.ts index 0bbcad4998..f94badee95 100644 --- a/src/core/project/templates/runtimeDocs.ts +++ b/src/core/project/templates/runtimeDocs.ts @@ -2,7 +2,7 @@ import { FsTreeNode } from "./fsTree"; import type { AssetSource } from "../source"; import type { TemplateRenderer } from "./types"; import type { RuntimeDocsConfig } from "../../../handlers/project/add/runtime/types"; -import type { ProjectRuntime } from "../../../projectSchemas/runtime"; +import { withProtocolRequestHeaders, type ProjectRuntime } from "../../../projectSchemas/runtime"; import { NodeRuntimeSchema, PythonRuntimeSchema, @@ -136,6 +136,8 @@ export async function runtimeDocsTree( */ export function buildDocsRuntimeSpec(input: RuntimeDocsConfig): ProjectRuntime { const { name, language, ...infra } = input; + const protocol = infra.protocol ?? "HTTP"; + const requestHeaderAllowlist = withProtocolRequestHeaders(protocol, infra.requestHeaderAllowlist); return { name, codeLocation: `app/${name}` as ProjectRuntime["codeLocation"], @@ -153,7 +155,7 @@ export function buildDocsRuntimeSpec(input: RuntimeDocsConfig): ProjectRuntime { customDockerBuildArgs: input.customDockerBuildArgs, }), entrypoint: docsRuntimeEntrypoint(language), - protocol: infra.protocol ?? "HTTP", + protocol, ...(infra.description && { description: infra.description }), ...(infra.executionRoleArn && { executionRoleArn: infra.executionRoleArn }), ...(infra.additionalPolicies?.length && { additionalPolicies: infra.additionalPolicies }), @@ -165,7 +167,7 @@ export function buildDocsRuntimeSpec(input: RuntimeDocsConfig): ProjectRuntime { ...(infra.authorizerConfiguration && { authorizerConfiguration: infra.authorizerConfiguration, }), - ...(infra.requestHeaderAllowlist && { requestHeaderAllowlist: infra.requestHeaderAllowlist }), + ...(requestHeaderAllowlist && { requestHeaderAllowlist }), ...(infra.lifecycleConfiguration && { lifecycleConfiguration: infra.lifecycleConfiguration }), ...(infra.resourceConfigurations && { resourceConfigurations: infra.resourceConfigurations }), ...(infra.hooksConfiguration && { hooksConfiguration: infra.hooksConfiguration }), diff --git a/src/handlers/project/add/runtime/index.test.ts b/src/handlers/project/add/runtime/index.test.ts index f27904dab1..6317749ad7 100644 --- a/src/handlers/project/add/runtime/index.test.ts +++ b/src/handlers/project/add/runtime/index.test.ts @@ -505,7 +505,12 @@ describe("project add runtime", () => { [ "typescript A2A", ["--language", "typescript", "--protocol", "A2A"], - { entrypoint: "main.js", protocol: "A2A", runtimeVersion: "NODE_22" }, + { + entrypoint: "main.js", + protocol: "A2A", + runtimeVersion: "NODE_22", + requestHeaderAllowlist: ["A2A-Version", "A2A-Extensions"], + }, /agent-card\.json/, [/runtime-http-/, /runtime-mcp-/, /runtime-agui-/], ], @@ -574,6 +579,44 @@ describe("project add runtime", () => { }, ); + test("an A2A runtime adds the A2A protocol headers to the headers it allows", async () => { + const { projectRoot, cleanup } = await initProject(); + cleanups.push(cleanup); + await run([ + "add", + "runtime", + "--name", + "a2a_agent", + "--language", + "python", + "--protocol", + "A2A", + "--request-header-allowlist", + "X-Custom-Header", + ]); + await run([ + "add", + "runtime", + "--name", + "mcp_agent", + "--language", + "python", + "--protocol", + "MCP", + "--request-header-allowlist", + "X-Custom-Header", + ]); + + expect((await runtimeOf(projectRoot, "a2a_agent"))?.requestHeaderAllowlist).toEqual([ + "X-Custom-Header", + "A2A-Version", + "A2A-Extensions", + ]); + expect((await runtimeOf(projectRoot, "mcp_agent"))?.requestHeaderAllowlist).toEqual([ + "X-Custom-Header", + ]); + }); + test("warns when --hooks-configuration has no warmup hook in camelCase, and still adds it", async () => { const { projectRoot, cleanup } = await initProject(); cleanups.push(cleanup); diff --git a/src/projectSchemas/runtime.test.ts b/src/projectSchemas/runtime.test.ts index d398203b97..9464ae7089 100644 --- a/src/projectSchemas/runtime.test.ts +++ b/src/projectSchemas/runtime.test.ts @@ -1,10 +1,12 @@ import { describe, expect, it } from "bun:test"; import { + A2A_REQUEST_HEADERS, ProjectRuntimeSchema, LifecycleConfigurationSchema, checkAllowlistHeader, isReservedBuildArgKey, isValidDockerfilePath, + withProtocolRequestHeaders, } from "./runtime"; const codeZipAgent = { name: "agent", @@ -49,6 +51,25 @@ describe("runtime custom validation", () => { expect(checkAllowlistHeader("x-amzn-trace-id")).not.toBeNull(); expect(checkAllowlistHeader("X-Amzn-Bedrock-AgentCore-Runtime-Custom-Tenant")).toBeNull(); }); + it("allowlists the A2A protocol headers for A2A runtimes only", () => { + expect(withProtocolRequestHeaders("A2A", undefined)).toEqual(["A2A-Version", "A2A-Extensions"]); + expect(withProtocolRequestHeaders("A2A", ["X-Tenant"])).toEqual([ + "X-Tenant", + "A2A-Version", + "A2A-Extensions", + ]); + // Headers the user already allowlisted are kept as written, not added twice. + expect(withProtocolRequestHeaders("A2A", ["a2a-version", "X-Tenant"])).toEqual([ + "a2a-version", + "X-Tenant", + "A2A-Extensions", + ]); + for (const protocol of ["HTTP", "MCP", "AGUI", undefined] as const) { + expect(withProtocolRequestHeaders(protocol, undefined)).toBeUndefined(); + expect(withProtocolRequestHeaders(protocol, ["X-Tenant"])).toEqual(["X-Tenant"]); + } + for (const header of A2A_REQUEST_HEADERS) expect(checkAllowlistHeader(header)).toBeNull(); + }); it("requires lifecycle idle timeout not to exceed maximum lifetime", () => { expect( LifecycleConfigurationSchema.safeParse({ diff --git a/src/projectSchemas/runtime.ts b/src/projectSchemas/runtime.ts index e8acad8d0c..23889f2567 100644 --- a/src/projectSchemas/runtime.ts +++ b/src/projectSchemas/runtime.ts @@ -4,6 +4,7 @@ import { NetworkModeSchema, type PlatformVersion, PlatformVersionSchema, + type ProtocolMode, ProtocolModeSchema, RuntimeVersionSchema as RuntimeVersionSchemaFromConstants, SECURITY_GROUP_ID_PATTERN, @@ -145,6 +146,28 @@ export const RequestHeaderAllowlistSchema = z }), ) .max(MAX_HEADER_ALLOWLIST_SIZE, `Maximum ${MAX_HEADER_ALLOWLIST_SIZE} headers allowed`); + +/** The request headers the A2A protocol defines: the protocol version and its extensions. */ +export const A2A_REQUEST_HEADERS = ["A2A-Version", "A2A-Extensions"] as const; + +/** + * AgentCore Runtime forwards only allowlisted request headers to the container, + * so an A2A runtime needs the A2A protocol headers in its allowlist to receive + * them. Returns the allowlist with them added (matched case-insensitively, after + * the headers the user set); other protocols keep theirs as is. + */ +export function withProtocolRequestHeaders( + protocol: ProtocolMode | undefined, + allowlist: string[] | undefined, +): string[] | undefined { + if (protocol !== "A2A") return allowlist; + const headers = allowlist ?? []; + const present = new Set(headers.map((header) => header.toLowerCase())); + return [ + ...headers, + ...A2A_REQUEST_HEADERS.filter((header) => !present.has(header.toLowerCase())), + ]; +} export const SessionStorageSchema = z.object({ mountPath: z .string()