Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow localhost for CORS #306

Merged
merged 1 commit into from
Aug 25, 2023
Merged
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
20 changes: 16 additions & 4 deletions app/server-utils/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@ export const jsonCORS = <T>(data: T) =>
},
})

export const CORSOptions = (request: Request) => {
const allowedOrigins = (request: Request) => {
const origin = request.headers.get('origin') || ''
const isOriginAllowed = ALLOW_ORIGINS == '*' || ALLOW_ORIGINS.split(',').includes(origin)
const allowed = isOriginAllowed ? origin : ''
const allowedOrigins = ALLOW_ORIGINS.split(',')

// always allow localhost
try {
if (['localhost', '127.0.0.1'].includes(new URL(origin).hostname)) {
return origin
}
} catch (e) {
// ignore errors
}

return ALLOW_ORIGINS == '*' || allowedOrigins.includes(origin) ? origin : ''
}

export const CORSOptions = (request: Request) => {
return json(
{},
{
headers: {
'Access-Control-Allow-Origin': allowed,
'Access-Control-Allow-Origin': allowedOrigins(request),
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, allow-control-allow-origin',
},
Expand Down