Skip to content

Commit

Permalink
Fix: CORS logging issue #1576
Browse files Browse the repository at this point in the history
  • Loading branch information
JacoKoster authored and tkurki committed Sep 9, 2023
1 parent 5fb76f7 commit 7d7c18a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/server-admin-ui/src/views/security/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class Settings extends Component {
by configuring allowed CORS origins below. The host
where this page is loaded from is automatically
included in the allowed CORS origins so that the Admin
UI continues to work.
UI continues to work. Changes to the Allowed CORS origins requires a server restart.
</Label>
</Col>
</FormGroup>{' '}
Expand Down
23 changes: 4 additions & 19 deletions src/cors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { IRouter } from 'express'
import { createDebug } from './debug'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { ACL, Device, SecurityConfig } from './security'
import { SecurityConfig } from './security'
import cors, { CorsOptions } from 'cors'

type OriginCallback = (err: Error | null, origin?: string) => void

export function setupCors(
app: IRouter,
{ allowedCorsOrigins }: SecurityConfig
Expand All @@ -18,23 +15,11 @@ export function setupCors(
.map((s: string) => s.trim().replace(/\/*$/, ''))
: []
corsDebug(`corsOrigins:${corsOrigins.toString()}`)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const corsOptions: CorsOptions = {
credentials: true
}
if (corsOrigins.length) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
corsOptions.origin = (origin: string | undefined, cb: OriginCallback) => {
if (origin === undefined || corsOrigins.indexOf(origin) >= 0) {
corsDebug(`${origin} OK`)
cb(null, origin)
} else {
const errorMsg = `${origin} not allowed`
corsDebug(errorMsg)
cb(new Error(errorMsg))
}
}
credentials: true,
origin: allowedCorsOrigins
}

app.use(cors(corsOptions))
}

Expand Down

0 comments on commit 7d7c18a

Please sign in to comment.