Skip to content

Commit 21604ac

Browse files
committed
fix(server): set Content-Type on basic-auth 401 responses
hono's basicAuth middleware returns its 401 response without setting a Content-Type header. Bun's HTTP serializer fills the missing slot with `application/octet-stream`, which Chromium briefly treats as a download-attachment trigger before settling on the basic-auth dialog. The race produces 2-3 dialog flashes before the password field accepts input, and Playwright surfaces it as `Error: Download is starting` on the navigation call. Subresources (favicons, JS bundle, CSS, manifest) each hit basic-auth independently and each get the same octet-stream 401, compounding the loop. Wrap the basicAuth call in AuthMiddleware so a 401 response with no Content-Type gets `text/plain; charset=utf-8` added before being re-thrown. Authenticated requests are unaffected. Verified by: - `curl -i http://localhost:40960/` now returns `content-type: text/plain; charset=utf-8` on the 401. - `bun typecheck` and `bun test` clean from packages/opencode. Refs upstream issue anomalyco#18325
1 parent cf903b1 commit 21604ac

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

‎packages/opencode/src/server/middleware.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const ErrorMiddleware: ErrorHandler = (err, c) => {
3636
})
3737
}
3838

39-
export const AuthMiddleware: MiddlewareHandler = (c, next) => {
39+
export const AuthMiddleware: MiddlewareHandler = async (c, next) => {
4040
// Allow CORS preflight requests to succeed without auth.
4141
// Browser clients sending Authorization headers will preflight with OPTIONS.
4242
if (c.req.method === "OPTIONS") return next()
@@ -46,7 +46,25 @@ export const AuthMiddleware: MiddlewareHandler = (c, next) => {
4646

4747
if (c.req.query("auth_token")) c.req.raw.headers.set("authorization", `Basic ${c.req.query("auth_token")}`)
4848

49-
return basicAuth({ username, password })(c, next)
49+
// hono's basicAuth builds the 401 response without a Content-Type. Bun's
50+
// HTTP layer then defaults to `application/octet-stream`, which Chromium
51+
// briefly treats as a download attachment before settling on the basic-auth
52+
// dialog. The race produces 2-3 dialog flashes before the password field
53+
// accepts input, and Playwright surfaces it as `Error: Download is starting`
54+
// on the navigation call. Wrap the call so the 401 carries an explicit
55+
// text/plain Content-Type. Refs upstream issue anomalyco/opencode#18325.
56+
try {
57+
return await basicAuth({ username, password })(c, next)
58+
} catch (err) {
59+
if (err instanceof HTTPException && err.res && err.res.status === 401 && !err.res.headers.get("content-type")) {
60+
const headers = new Headers(err.res.headers)
61+
headers.set("content-type", "text/plain; charset=utf-8")
62+
throw new HTTPException(401, {
63+
res: new Response(err.res.body, { status: 401, headers }),
64+
})
65+
}
66+
throw err
67+
}
5068
}
5169

5270
export const LoggerMiddleware: MiddlewareHandler = async (c, next) => {

0 commit comments

Comments
 (0)