Things
This commit is contained in:
@@ -67,9 +67,20 @@ function parseCmc(value: string | undefined): number | undefined | null {
|
|||||||
return Number.isFinite(parsed) && parsed >= 0 ? parsed : 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, {
|
await app.register(cors, {
|
||||||
origin: process.env.CORS_ORIGIN?.split(',').map(origin => origin.trim())
|
origin: (origin, callback) => callback(null, isAllowedOrigin(origin)),
|
||||||
?? ['http://localhost:3000'],
|
|
||||||
// @fastify/cors defaults to GET,HEAD,POST; decks and preferences are saved with PUT.
|
// @fastify/cors defaults to GET,HEAD,POST; decks and preferences are saved with PUT.
|
||||||
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
|
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
|
||||||
})
|
})
|
||||||
@@ -416,9 +427,9 @@ app.get<{ Params: { id: string }; Querystring: { token?: string } }>(
|
|||||||
getMatchView(request.params.id, token)
|
getMatchView(request.params.id, token)
|
||||||
|
|
||||||
const origin = request.headers.origin
|
const origin = request.headers.origin
|
||||||
const allowed = process.env.CORS_ORIGIN?.split(',').map(entry => entry.trim())
|
const allowOrigin = origin && isAllowedOrigin(origin)
|
||||||
?? ['http://localhost:3000']
|
? origin
|
||||||
const allowOrigin = origin && allowed.includes(origin) ? origin : allowed[0]
|
: configuredOrigins?.[0] ?? 'http://localhost:3000'
|
||||||
|
|
||||||
reply.hijack()
|
reply.hijack()
|
||||||
reply.raw.writeHead(200, {
|
reply.raw.writeHead(200, {
|
||||||
@@ -427,6 +438,7 @@ app.get<{ Params: { id: string }; Querystring: { token?: string } }>(
|
|||||||
Connection: 'keep-alive',
|
Connection: 'keep-alive',
|
||||||
'X-Accel-Buffering': 'no',
|
'X-Accel-Buffering': 'no',
|
||||||
'Access-Control-Allow-Origin': allowOrigin,
|
'Access-Control-Allow-Origin': allowOrigin,
|
||||||
|
Vary: 'Origin',
|
||||||
})
|
})
|
||||||
reply.raw.write(': connected\n\n')
|
reply.raw.write(': connected\n\n')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user