Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/botproxy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +71,12 @@ async function handle(req: NextRequest): Promise<NextResponse> {
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}`

Expand Down
24 changes: 24 additions & 0 deletions lib/botDashboardProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,27 @@ export const signProxySession = (c: Omit<ProxyClaims, 'exp'>) => 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'
}
30 changes: 30 additions & 0 deletions tests/botProxyOrigin.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>) => 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)
})
})
Loading