The Open Framework for Building Agents
fix(eve): channels - simplify CORS opt-in (#370)
Andrew Barba0bcb68902498
diff --git a/.changeset/eve-channel-cors.md b/.changeset/eve-channel-cors.mdindex b5536c619..a6e5bc2b8 100644--- a/.changeset/eve-channel-cors.md+++ b/.changeset/eve-channel-cors.md@@ -2,4 +2,4 @@"eve": patch----The eve HTTP channel now enables permissive browser CORS by default, including preflight handling for session routes. Custom channels can opt into CORS with `defineChannel({ cors })`, and `eveChannel({ cors })` can disable or narrow the default policy.+HTTP channels can now opt into browser CORS with preflight handling. Use `defineChannel({ cors })` for custom channels or `eveChannel({ cors: true | options })` for the eve channel; omitted CORS remains disabled.diff --git a/docs/channels/eve.mdx b/docs/channels/eve.mdxindex 441464055..af9ebb3c4 100644--- a/docs/channels/eve.mdx+++ b/docs/channels/eve.mdx@@ -47,12 +47,12 @@ See [Sessions, runs & streaming](../concepts/sessions-runs-and-streaming) for th## CORS-The eve channel enables permissive browser CORS by default. Its session routes-respond to preflight requests and emit wildcard CORS headers, so browser-frontends can call the channel without extra deployment configuration. Route-auth still runs on the actual session requests.+The eve channel leaves CORS untouched by default. Pass `cors: true` to enable+permissive browser CORS with preflight handling, or pass an options object to+narrow origins, methods, and headers. Route auth still runs on the actual+session requests.-Disable or narrow CORS only when another layer owns it:+Enable or narrow CORS only when browser clients call the channel directly:```ts title="agent/channels/eve.ts"import { eveChannel } from "eve/channels/eve";diff --git a/packages/eve/src/public/channels/eve.test.ts b/packages/eve/src/public/channels/eve.test.tsindex 8a79d6e65..86aad1430 100644--- a/packages/eve/src/public/channels/eve.test.ts+++ b/packages/eve/src/public/channels/eve.test.ts@@ -162,12 +162,21 @@ function contextAccessorFor(ctx: ContextContainer): ContextAccessor {}describe("eveChannel — events", () => {- it("defaults to permissive CORS", () => {+ it("leaves CORS disabled by default", () => {const channel = eveChannel({ auth: none() });if (!isCompiledChannel(channel)) {throw new Error("Expected eveChannel() to return a compiled channel.");}+ expect(channel.cors).toBeUndefined();+ });++ it("accepts true for explicit permissive CORS", () => {+ const channel = eveChannel({ auth: none(), cors: true });+ if (!isCompiledChannel(channel)) {+ throw new Error("Expected eveChannel() to return a compiled channel.");+ }+expect(channel.cors).toEqual({});});@@ -208,6 +217,28 @@ describe("eveChannel — events", () => {});});+ it("passes wildcard values through inside CORS options", () => {+ const channel = eveChannel({+ auth: none(),+ cors: {+ allowedHeaders: "*",+ exposedHeaders: "*",+ methods: "*",+ origin: "*",+ },+ });+ if (!isCompiledChannel(channel)) {+ throw new Error("Expected eveChannel() to return a compiled channel.");+ }++ expect(channel.cors).toEqual({+ allowHeaders: "*",+ exposeHeaders: "*",+ methods: "*",+ origin: "*",+ });+ });+it("passes configured event handlers through with session context", async () => {const observed: string[] = [];const adapter = getEveAdapter({diff --git a/packages/eve/src/public/channels/eve.ts b/packages/eve/src/public/channels/eve.tsindex f33991f20..486cbef28 100644--- a/packages/eve/src/public/channels/eve.ts+++ b/packages/eve/src/public/channels/eve.ts@@ -66,10 +66,10 @@ export interface EveChannelCorsOptions {}/**- * Higher-level CORS policy for the default eve HTTP channel. Omit or pass `"*"`- * / `true` for fully permissive browser access; pass `false` to disable CORS.+ * Higher-level CORS policy for the default eve HTTP channel. Pass `true` for+ * fully permissive browser access, or pass an options object to narrow it.*/-export type EveChannelCors = "*" | boolean | EveChannelCorsOptions;+export type EveChannelCors = boolean | EveChannelCorsOptions;/** Low-level eve HTTP handle exposed to `eveChannel({ onMessage })`. */export interface EveHandle {@@ -122,8 +122,9 @@ export interface EveChannelInput {*/readonly uploadPolicy?: UploadPolicyInput;/**- * Browser CORS policy for the eve HTTP routes. Omit for fully permissive- * CORS (`"*"`). Pass `false` only when another layer owns CORS.+ * Browser CORS policy for the eve HTTP routes. Omit or pass `false` to leave+ * CORS untouched, pass `true` for fully permissive CORS, or pass an options+ * object to narrow the policy.*/readonly cors?: EveChannelCors;/**@@ -335,12 +336,12 @@ export function eveChannel(input: EveChannelInput): EveChannel {}function normalizeEveCors(cors: EveChannelCors | undefined): ChannelCors {- if (cors === undefined || cors === "*" || cors === true) {- return true;- }- if (cors === false) {+ if (cors === undefined || cors === false) {return false;}+ if (cors === true) {+ return true;+ }const result: {origin?: "*" | "null" | readonly string[];