Skip to content

Commit 2dffdff

Browse files
authored
fix(server): apply cors before legacy auth (anomalyco#26092)
1 parent a4ab140 commit 2dffdff

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

packages/opencode/src/server/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ function createHono(opts: CorsOptions, selection: ServerBackend.Selection = Serv
107107
const backendAttributes = ServerBackend.attributes(selection)
108108
const app = new Hono()
109109
.onError(ErrorMiddleware)
110-
.use(AuthMiddleware)
110+
.use(CorsMiddleware(opts))
111111
.use(LoggerMiddleware(backendAttributes))
112+
.use(AuthMiddleware)
112113
.use(CompressionMiddleware)
113-
.use(CorsMiddleware(opts))
114114
.route("/global", GlobalRoutes())
115115

116116
const runtime = adapter.create(app)

packages/opencode/test/server/httpapi-cors.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ describe("HttpApi CORS", () => {
6363
}),
6464
)
6565

66+
it.live("adds CORS headers to legacy unauthorized responses", () =>
67+
Effect.gen(function* () {
68+
const response = yield* Effect.promise(async () =>
69+
Server.Legacy().app.request("/global/config", {
70+
headers: { origin: "https://app.opencode.ai" },
71+
}),
72+
)
73+
74+
expect(response.status).toBe(401)
75+
expect(response.headers.get("access-control-allow-origin")).toBe("https://app.opencode.ai")
76+
}),
77+
)
78+
6679
it.live("uses custom CORS origins passed to the server", () =>
6780
Effect.gen(function* () {
6881
const listener = yield* Effect.acquireRelease(

0 commit comments

Comments
 (0)