Skip to content

Commit c17c9c6

Browse files
committed
fix(web): resolve CSP/CORS issues and enable LAN access
- Add 'unsafe-eval' to script-src to support WASM modules (Fixes anomalyco#17604, anomalyco#15327) - Add LAN IP ranges and .local domains to connect-src and CORS origin check - Ensure CORS headers are present in error responses - Move logging to the top for better preflight diagnostics
1 parent e14e874 commit c17c9c6

1 file changed

Lines changed: 81 additions & 53 deletions

File tree

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

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,35 @@ export namespace Server {
5656
export const Default = lazy(() => createApp({}))
5757

5858
export const createApp = (opts: { cors?: string[] }): Hono => {
59+
const match = (input?: string) => {
60+
if (!input) return
61+
if (input.startsWith("http://localhost:") || input === "http://localhost") return input
62+
if (input.startsWith("http://127.0.0.1:") || input === "http://127.0.0.1") return input
63+
64+
const host = input.split("://")[1]?.split(":")[0]
65+
if (host) {
66+
if (host.startsWith("192.168.")) return input
67+
if (host.startsWith("10.")) return input
68+
if (/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(host)) return input
69+
if (host.endsWith(".local")) return input
70+
}
71+
72+
if (
73+
input === "tauri://localhost" ||
74+
input === "http://tauri.localhost" ||
75+
input === "https://tauri.localhost"
76+
)
77+
return input
78+
79+
if (/^https?:\/\/([a-z0-9-]+\.)*opencode\.ai$/.test(input)) return input
80+
if (opts?.cors?.includes(input)) return input
81+
}
82+
5983
const app = new Hono()
6084
return app
61-
.onError((err, c) => {
62-
log.error("failed", {
63-
error: err,
64-
})
65-
if (err instanceof NamedError) {
66-
let status: ContentfulStatusCode
67-
if (err instanceof NotFoundError) status = 404
68-
else if (err instanceof Provider.ModelNotFoundError) status = 400
69-
else if (err.name.startsWith("Worktree")) status = 400
70-
else status = 500
71-
return c.json(err.toObject(), { status })
72-
}
73-
if (err instanceof HTTPException) return err.getResponse()
74-
const message = err instanceof Error && err.stack ? err.stack : err.toString()
75-
return c.json(new NamedError.Unknown({ message }).toObject(), {
76-
status: 500,
77-
})
78-
})
79-
.use((c, next) => {
80-
// Allow CORS preflight requests to succeed without auth.
81-
// Browser clients sending Authorization headers will preflight with OPTIONS.
82-
if (c.req.method === "OPTIONS") return next()
83-
const password = Flag.OPENCODE_SERVER_PASSWORD
84-
if (!password) return next()
85-
const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
86-
return basicAuth({ username, password })(c, next)
87-
})
8885
.use(async (c, next) => {
89-
const skipLogging = c.req.path === "/log"
90-
if (!skipLogging) {
86+
const skip = c.req.path === "/log"
87+
if (!skip) {
9188
log.info("request", {
9289
method: c.req.method,
9390
path: c.req.path,
@@ -98,36 +95,67 @@ export namespace Server {
9895
path: c.req.path,
9996
})
10097
await next()
101-
if (!skipLogging) {
98+
if (!skip) {
10299
timer.stop()
103100
}
104101
})
105-
.use(
106-
cors({
107-
origin(input) {
108-
if (!input) return
109-
110-
if (input.startsWith("http://localhost:")) return input
111-
if (input.startsWith("http://127.0.0.1:")) return input
112-
if (
113-
input === "tauri://localhost" ||
114-
input === "http://tauri.localhost" ||
115-
input === "https://tauri.localhost"
116-
)
117-
return input
102+
.onError((err, c) => {
103+
log.error("failed", {
104+
err,
105+
})
118106

119-
// *.opencode.ai (https only, adjust if needed)
120-
if (/^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/.test(input)) {
121-
return input
122-
}
123-
if (opts?.cors?.includes(input)) {
124-
return input
125-
}
107+
const ok = match(c.req.header("origin"))
108+
const headers = (res: Response) => {
109+
if (!ok) return res
110+
res.headers.set("Access-Control-Allow-Origin", ok)
111+
res.headers.set("Access-Control-Allow-Credentials", "true")
112+
res.headers.set("Access-Control-Allow-Methods", "GET, HEAD, PUT, POST, DELETE, PATCH")
113+
res.headers.set(
114+
"Access-Control-Allow-Headers",
115+
"Content-Type, Authorization, x-opencode-directory, x-opencode-workspace, Last-Event-ID",
116+
)
117+
return res
118+
}
126119

127-
return
128-
},
120+
if (err instanceof NamedError) {
121+
const status = (() => {
122+
if (err instanceof NotFoundError) return 404
123+
if (err instanceof Provider.ModelNotFoundError) return 400
124+
if (err.name.startsWith("Worktree")) return 400
125+
return 500
126+
})() as ContentfulStatusCode
127+
return headers(c.json(err.toObject(), { status }))
128+
}
129+
if (err instanceof HTTPException) return headers(err.getResponse())
130+
const msg = err instanceof Error && err.stack ? err.stack : err.toString()
131+
return headers(
132+
c.json(new NamedError.Unknown({ message: msg }).toObject(), {
133+
status: 500,
134+
}),
135+
)
136+
})
137+
.use(
138+
cors({
139+
origin: match,
140+
credentials: true,
141+
allowHeaders: [
142+
"Content-Type",
143+
"Authorization",
144+
"x-opencode-directory",
145+
"x-opencode-workspace",
146+
"Last-Event-ID",
147+
],
129148
}),
130149
)
150+
.use((c, next) => {
151+
// Allow CORS preflight requests to succeed without auth.
152+
// Browser clients sending Authorization headers will preflight with OPTIONS.
153+
if (c.req.method === "OPTIONS") return next()
154+
const password = Flag.OPENCODE_SERVER_PASSWORD
155+
if (!password) return next()
156+
const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
157+
return basicAuth({ username, password })(c, next)
158+
})
131159
.route("/global", GlobalRoutes())
132160
.put(
133161
"/auth/:providerID",
@@ -567,7 +595,7 @@ export namespace Server {
567595
})
568596
response.headers.set(
569597
"Content-Security-Policy",
570-
"default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; media-src 'self' data:; connect-src 'self' data:",
598+
"default-src 'self'; script-src 'self' 'wasm-unsafe-eval' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; media-src 'self' data:; connect-src 'self' data: http://localhost:* http://127.0.0.1:* http://192.168.*:* http://10.*:* http://172.16.*:* http://172.17.*:* http://172.18.*:* http://172.19.*:* http://172.20.*:* http://172.21.*:* http://172.22.*:* http://172.23.*:* http://172.24.*:* http://172.25.*:* http://172.26.*:* http://172.27.*:* http://172.28.*:* http://172.29.*:* http://172.30.*:* http://172.31.*:* *.local:*",
571599
)
572600
return response
573601
})

0 commit comments

Comments
 (0)