From de127128f7fd79a30eccd9e40ce7c42e00d9f5a0 Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Thu, 5 Nov 2026 18:30:39 +0100 Subject: [PATCH] Refuse cross-origin writes in the bot dashboard proxy --- app/botproxy/route.ts | 8 +++++++- lib/botDashboardProxy.ts | 24 ++++++++++++++++++++++++ tests/botProxyOrigin.test.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/botProxyOrigin.test.ts diff --git a/app/botproxy/route.ts b/app/botproxy/route.ts index 26111d3..b168f95 100644 --- a/app/botproxy/route.ts +++ b/app/botproxy/route.ts @@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server' import { verifyHandoff, signProxySession, verifyProxySession, proxySecret, PROXY_COOKIE, PROXY_SECRET_HEADER, PROXY_USER_HEADER, PROXY_HOST, returnUrl, - PROXY_SESSION_MAX_AGE_S, + PROXY_SESSION_MAX_AGE_S, isAllowedProxyOrigin, } from '@/lib/botDashboardProxy' // Node runtime: we fetch a loopback address and stream arbitrary responses, both @@ -71,6 +71,12 @@ async function handle(req: NextRequest): Promise { return res } + // A write from another origin never reaches the bot, which trusts the proxy + // and skips its own CSRF check for proxied requests. + if (!isAllowedProxyOrigin(req.method, req.headers)) { + return new NextResponse('Cross-origin request refused.', { status: 403 }) + } + // ── 3. Forward to the loopback bot dashboard ──────────────────────────────── const target = `http://127.0.0.1:${session.botPort}${path}${search}` diff --git a/lib/botDashboardProxy.ts b/lib/botDashboardProxy.ts index 3c85de4..9de4d66 100644 --- a/lib/botDashboardProxy.ts +++ b/lib/botDashboardProxy.ts @@ -125,3 +125,27 @@ export const signProxySession = (c: Omit) => makeToken('bot export const verifyProxySession = (t?: string | null) => readToken('bot-proxysession', t) export const PROXY_SESSION_MAX_AGE_S = Math.floor(PROXY_SESSION_TTL_MS / 1000) + +/** Methods that only read; everything else changes something in the bot. */ +const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']) + +/** + * Whether a request to the proxy host may be forwarded, as far as its origin goes. + * + * The bot trusts the proxy and skips its own origin and CSRF checks for proxied + * requests, so this is the only place where a cross-site write can be stopped. + * The session cookie is SameSite=Lax, and every *.msk-scripts.de host counts as + * the same site, so a form on another subdomain could otherwise send a POST + * without a body (for example /api/bot/stop) with the customer's session. + * + * Reading requests pass. A writing request needs an Origin of exactly + * https://PROXY_HOST. Browsers send Origin on every POST, PUT, PATCH and DELETE; + * if it is missing, Sec-Fetch-Site: same-origin is accepted instead, and a + * request with neither is refused. + */ +export function isAllowedProxyOrigin(method: string, headers: Headers, host: string = PROXY_HOST): boolean { + if (SAFE_METHODS.has(method.toUpperCase())) return true + const origin = headers.get('origin') + if (origin !== null) return origin.toLowerCase() === `https://${host}` + return headers.get('sec-fetch-site') === 'same-origin' +} diff --git a/tests/botProxyOrigin.test.ts b/tests/botProxyOrigin.test.ts new file mode 100644 index 0000000..1487cdf --- /dev/null +++ b/tests/botProxyOrigin.test.ts @@ -0,0 +1,30 @@ +import { describe, it, expect } from 'vitest' + +import { isAllowedProxyOrigin, PROXY_HOST } from '@/lib/botDashboardProxy' + +// The bot trusts the proxy and skips its own CSRF check for proxied requests, +// so a write from another origin has to be stopped here. +describe('bot dashboard proxy origin check', () => { + const h = (init: Record) => new Headers(init) + const own = `https://${PROXY_HOST}` + + it('lets reading requests through without any origin', () => { + for (const m of ['GET', 'HEAD', 'OPTIONS', 'get']) expect(isAllowedProxyOrigin(m, h({}))).toBe(true) + }) + + it('accepts a write from the proxy host itself', () => { + for (const m of ['POST', 'PUT', 'PATCH', 'DELETE']) expect(isAllowedProxyOrigin(m, h({ origin: own }))).toBe(true) + }) + + it('refuses a write from another subdomain, another site or a lookalike', () => { + for (const origin of ['https://www.msk-scripts.de', 'https://evil.example', `http://${PROXY_HOST}`, `${own}.evil.example`, 'null']) { + expect(isAllowedProxyOrigin('POST', h({ origin }))).toBe(false) + } + }) + + it('without Origin, only Sec-Fetch-Site same-origin passes', () => { + expect(isAllowedProxyOrigin('POST', h({ 'sec-fetch-site': 'same-origin' }))).toBe(true) + expect(isAllowedProxyOrigin('POST', h({ 'sec-fetch-site': 'same-site' }))).toBe(false) + expect(isAllowedProxyOrigin('POST', h({}))).toBe(false) + }) +})