From 7c7e9f3c776fae9a676d4e101d1db1cbefb5f822 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Tue, 14 Jul 2026 09:33:47 -0400 Subject: [PATCH] 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. --- backend/src/index.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index 6e17feea..82fedfd2 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -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, }))