Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Commit

Permalink
fix: ensure Access-Control-Allow-Origin has no trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Sep 9, 2021
1 parent 896157b commit 1decf6c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/server/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import EventEmitter from 'events'
import { backend } from './feat/backend'
import { startExpressServer } from './feat/express'
import { setUp as setUpEmails } from './feat/emails'
import { URL } from 'url'

const omnibus = new EventEmitter()

const app = backend({
omnibus,
cookieSecret: process.env.COOKIE_SECRET ?? v4(),
origin: process.env.CLIENT_URL || 'http://localhost:8080',
origin: new URL(process.env.CLIENT_URL || 'http://localhost:8080'),
})

startExpressServer(app)
Expand Down
5 changes: 3 additions & 2 deletions src/server/feat/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cors from 'cors'
import EventEmitter from 'events'
import express, { Express } from 'express'
import passport from 'passport'
import { URL } from 'url'
import { cookieAuthStrategy } from '../../authenticateRequest'
import login from '../../routes/login'
import getProfile from '../../routes/me'
Expand All @@ -22,7 +23,7 @@ export const backend = ({
origin,
}: {
omnibus: EventEmitter
origin: string
origin: URL
cookieSecret: string
}): Express => {
const app = express()
Expand All @@ -36,7 +37,7 @@ export const backend = ({

app.use(
cors({
origin,
origin: `${origin.protocol}//${origin.host}`,
credentials: true,
}),
)
Expand Down
13 changes: 10 additions & 3 deletions src/server/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import EventEmitter from 'events'
import { backend } from './feat/backend'
import { startExpressServer } from './feat/express'
import { setUp as setUpEmails } from './feat/emails'
import { URL } from 'url'

const omnibus = new EventEmitter()

Expand All @@ -21,9 +22,15 @@ if (cookieSecret === undefined || cookieSecret.length === 0) {
cookieSecret = v4()
}

const origin = process.env.ORIGIN
if (origin === undefined || !/^http/.test(origin)) {
console.error(`Must set ORIGIN!`)
let origin: URL
try {
origin = new URL(process.env.ORIGIN ?? '')
} catch (err) {
console.error(
`Must set ORIGIN, current value is not a URL: "${process.env.ORIGIN}": ${
(err as Error).message
}!`,
)
process.exit(1)
}

Expand Down

0 comments on commit 1decf6c

Please sign in to comment.