This commit is contained in:
2026-10-27 17:18:38 +01:00
parent 7dd9f4b14d
commit 39b2d72808

View File

@@ -67,9 +67,20 @@ function parseCmc(value: string | undefined): number | undefined | null {
return Number.isFinite(parsed) && parsed >= 0 ? parsed : null
}
// Dev servers move to another port when 3000 is taken, and localhost/127.0.0.1
// are the same machine. CORS_ORIGIN lists any additional production origins.
const LOCAL_ORIGIN = /^http:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/
const configuredOrigins = process.env.CORS_ORIGIN?.split(',')
.map(origin => origin.trim())
.filter(Boolean)
function isAllowedOrigin(origin: string | undefined): boolean {
if (!origin || LOCAL_ORIGIN.test(origin)) return true
return configuredOrigins?.includes(origin) ?? false
}
await app.register(cors, {
origin: process.env.CORS_ORIGIN?.split(',').map(origin => origin.trim())
?? ['http://localhost:3000'],
origin: (origin, callback) => callback(null, isAllowedOrigin(origin)),
// @fastify/cors defaults to GET,HEAD,POST; decks and preferences are saved with PUT.
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
})
@@ -416,9 +427,9 @@ app.get<{ Params: { id: string }; Querystring: { token?: string } }>(
getMatchView(request.params.id, token)
const origin = request.headers.origin
const allowed = process.env.CORS_ORIGIN?.split(',').map(entry => entry.trim())
?? ['http://localhost:3000']
const allowOrigin = origin && allowed.includes(origin) ? origin : allowed[0]
const allowOrigin = origin && isAllowedOrigin(origin)
? origin
: configuredOrigins?.[0] ?? 'http://localhost:3000'
reply.hijack()
reply.raw.writeHead(200, {
@@ -427,6 +438,7 @@ app.get<{ Params: { id: string }; Querystring: { token?: string } }>(
Connection: 'keep-alive',
'X-Accel-Buffering': 'no',
'Access-Control-Allow-Origin': allowOrigin,
Vary: 'Origin',
})
reply.raw.write(': connected\n\n')