fix: block staging backend on production white-label hosts (#1903)

* fix: block staging backend on production brands

* fix: clarify production domain classification

* fix: alert on forbidden white-label backend
This commit is contained in:
Mattsson
2026-08-25 19:55:53 +02:00
committed by GitHub
parent 436cbf5304
commit a83baede72
6 changed files with 234 additions and 1 deletions
+1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,55 @@
import { describe, expect, it } from 'vitest'
import { usesForbiddenWhiteLabelBackend } from '../production-white-label-backend'
const STAGING_URL = 'https://metjnjrhvujscngnpzdv.supabase.co'
const PRODUCTION_URL = 'https://pwxtzglxptnnvjrpixpg.supabase.co'
describe('production white-label backend guard', () => {
it.each([
'acount.accounted.se',
'arbore.accounted.se',
'elma.accounted.se',
'm360.accounted.se',
'redovisningskompaniet.accounted.se',
'willem.accounted.se',
'ziffr.accounted.se',
])('blocks %s when it uses the staging project', hostname => {
expect(usesForbiddenWhiteLabelBackend(hostname, STAGING_URL)).toBe(true)
})
it('normalizes case and a trailing dot before the exact host checks', () => {
expect(
usesForbiddenWhiteLabelBackend(
'ACOUNT.ACCOUNTED.SE.',
'https://METJNJRHVUJSCNGNPZDV.SUPABASE.CO./rest/v1',
),
).toBe(true)
})
it.each([
'app.accounted.se',
'accounted.se',
'preview.vercel.app',
'acount.accounted.se.attacker.test',
'notacount.accounted.se',
])('does not extend the production classification to %s', hostname => {
expect(usesForbiddenWhiteLabelBackend(hostname, STAGING_URL)).toBe(false)
})
it('allows a customer production host to use a different backend', () => {
expect(
usesForbiddenWhiteLabelBackend('acount.accounted.se', PRODUCTION_URL),
).toBe(false)
})
it.each([
undefined,
'',
'not a URL',
'https://metjnjrhvujscngnpzdv.supabase.co.attacker.test',
])('does not mistake an unrecognized backend for the staging project', url => {
expect(usesForbiddenWhiteLabelBackend('acount.accounted.se', url)).toBe(
false,
)
})
})
@@ -0,0 +1,44 @@
// This is an owner-approved production classification, not an auth callback
// allowlist. Do not derive it from NEXT_PUBLIC_WHITELABEL_DOMAINS, which can
// also contain demo, pilot, or self-hosted domains.
const CUSTOMER_PRODUCTION_WHITE_LABEL_HOSTS = new Set([
'acount.accounted.se',
'arbore.accounted.se',
'elma.accounted.se',
'm360.accounted.se',
'redovisningskompaniet.accounted.se',
'willem.accounted.se',
'ziffr.accounted.se',
])
const FORBIDDEN_STAGING_SUPABASE_HOST =
'metjnjrhvujscngnpzdv.supabase.co'
function normalizeHostname(hostname: string): string {
return hostname.trim().toLowerCase().replace(/\.$/, '')
}
function parseBackendHostname(supabaseUrl: string | undefined): string | null {
if (!supabaseUrl) return null
try {
return normalizeHostname(new URL(supabaseUrl).hostname)
} catch {
return null
}
}
/**
* Block Accounted's customer-facing white-label hosts from using the staging
* Supabase project. The request host and backend host are exact matches: this
* is an environment safety boundary, not suffix-based domain authorization.
*/
export function usesForbiddenWhiteLabelBackend(
requestHostname: string,
supabaseUrl: string | undefined,
): boolean {
const hostname = normalizeHostname(requestHostname)
if (!CUSTOMER_PRODUCTION_WHITE_LABEL_HOSTS.has(hostname)) return false
return parseBackendHostname(supabaseUrl) === FORBIDDEN_STAGING_SUPABASE_HOST
}
+104
View File
@@ -0,0 +1,104 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { unstable_doesMiddlewareMatch } from 'next/experimental/testing/server'
import { NextRequest, NextResponse } from 'next/server'
const updateSessionMock = vi.hoisted(() =>
vi.fn(async () => new NextResponse(null, { status: 204 })),
)
const loggerErrorMock = vi.hoisted(() => vi.fn())
vi.mock('@/lib/supabase/middleware', () => ({
updateSession: updateSessionMock,
}))
vi.mock('@/lib/logger', () => ({
createLogger: () => ({ error: loggerErrorMock }),
}))
import { config, proxy } from './proxy'
const STAGING_URL = 'https://metjnjrhvujscngnpzdv.supabase.co'
const PRODUCTION_URL = 'https://pwxtzglxptnnvjrpixpg.supabase.co'
describe('production white-label proxy guard', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', STAGING_URL)
})
afterEach(() => {
vi.unstubAllEnvs()
})
it.each(['/login', '/auth/callback', '/api/health'])(
'covers the application request path %s',
pathname => {
expect(
unstable_doesMiddlewareMatch({
config,
nextConfig: {},
url: `https://acount.accounted.se${pathname}`,
}),
).toBe(true)
},
)
it('returns a non-cacheable empty 503 before session handling', async () => {
const response = await proxy(
new NextRequest('https://acount.accounted.se/login'),
)
expect(response.status).toBe(503)
expect(response.headers.get('cache-control')).toBe('no-store')
expect(await response.text()).toBe('')
expect(updateSessionMock).not.toHaveBeenCalled()
expect(loggerErrorMock).toHaveBeenCalledOnce()
expect(loggerErrorMock).toHaveBeenCalledWith(
'Blocked production white-label host from staging backend',
{
alert: true,
operation: 'white_label_backend_guard',
requestHostname: 'acount.accounted.se',
backendClassification: 'staging',
},
)
})
it('preserves canonical app behavior with the same backend', async () => {
const request = new NextRequest('https://app.accounted.se/login')
expect((await proxy(request)).status).toBe(204)
expect(updateSessionMock).toHaveBeenCalledOnce()
expect(updateSessionMock).toHaveBeenCalledWith(request)
})
it('does not treat the callback allowlist as a production classification', async () => {
vi.stubEnv('NEXT_PUBLIC_WHITELABEL_DOMAINS', 'internal-demo.accounted.test')
const request = new NextRequest('https://internal-demo.accounted.test/login')
expect((await proxy(request)).status).toBe(204)
expect(updateSessionMock).toHaveBeenCalledOnce()
})
it('allows the production host after it moves to a different backend', async () => {
vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', PRODUCTION_URL)
const request = new NextRequest('https://acount.accounted.se/login')
expect((await proxy(request)).status).toBe(204)
expect(updateSessionMock).toHaveBeenCalledOnce()
})
it('uses the request URL host and ignores x-forwarded-host', async () => {
const spoofedForwardedHost = new NextRequest(
'https://preview.vercel.app/login',
{ headers: { 'x-forwarded-host': 'acount.accounted.se' } },
)
expect((await proxy(spoofedForwardedHost)).status).toBe(204)
const productionHost = new NextRequest(
'https://acount.accounted.se/login',
{ headers: { 'x-forwarded-host': 'preview.vercel.app' } },
)
expect((await proxy(productionHost)).status).toBe(503)
expect(updateSessionMock).toHaveBeenCalledTimes(1)
})
})
+26 -1
View File
@@ -1,7 +1,32 @@
import { type NextRequest } from 'next/server'
import { NextResponse, type NextRequest } from 'next/server'
import { usesForbiddenWhiteLabelBackend } from '@/lib/domains/production-white-label-backend'
import { createLogger } from '@/lib/logger'
import { updateSession } from '@/lib/supabase/middleware'
const log = createLogger('proxy')
export async function proxy(request: NextRequest) {
if (
usesForbiddenWhiteLabelBackend(
request.nextUrl.hostname,
process.env.NEXT_PUBLIC_SUPABASE_URL,
)
) {
log.error('Blocked production white-label host from staging backend', {
alert: true,
operation: 'white_label_backend_guard',
requestHostname: request.nextUrl.hostname,
backendClassification: 'staging',
})
return new NextResponse(null, {
status: 503,
headers: {
'Cache-Control': 'no-store',
},
})
}
return await updateSession(request)
}