fix(cors): reflect origin for WebView routes using Bearer auth

Routes prefixed with /api/opencode-proxy/ and /api/internal/ authenticate
via Bearer/CF-Access headers rather than cookies. These routes are now
allowed to reflect any origin in the CORS response since they don't rely
on cookie-based trust.

Also added CF-Access-Client-Id and CF-Access-Client-Secret to the allowed
headers list.
This commit is contained in:
Chris Scott committed 2026-07-14 09:33:47 -04:00
1 parent 5d9a159c07
commit 7c7e9f3c77
1 file changed
+12 -2
+12 -2
View File
@@ -78,15 +78,25 @@ const DB_PATH = getDatabasePath()
const app = new Hono()
/**
* Route prefixes reachable from custom WebViews whose origin is not a
* trusted web origin (e.g. "null" for file://). These routes authenticate via
* Bearer/CF-Access headers rather than cookies, so any origin may be reflected.
*/
const REFLECT_ANY_ORIGIN_PREFIXES = ['/api/opencode-proxy/', '/api/internal/']
app.use('/*', cors({
origin: (origin) => {
origin: (origin, c) => {
if (origin && REFLECT_ANY_ORIGIN_PREFIXES.some(prefix => c.req.path.startsWith(prefix))) {
return origin
}
const trustedOrigins = ENV.AUTH.TRUSTED_ORIGINS.split(',').map(o => o.trim())
if (!origin) return trustedOrigins[0]
if (trustedOrigins.includes(origin)) return origin
return trustedOrigins[0]
},
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization'],
allowHeaders: ['Content-Type', 'Authorization', 'CF-Access-Client-Id', 'CF-Access-Client-Secret'],
credentials: true,
}))