Skip to content
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
24 changes: 14 additions & 10 deletions middleware/corsValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

class CORSValidator {
static validate(config) {
static validate (config) {
const errors = []
const warnings = []

Expand Down Expand Up @@ -37,8 +37,13 @@
// Warn about localhost in production
if (process.env.NODE_ENV === 'production') {
config.origins.forEach((origin, idx) => {
if (origin.includes('localhost') || origin === 'http://localhost:3000') {
warnings.push(`WARNING: Localhost origin in production at index ${idx}`)
if (
origin.includes('localhost') ||

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production
origin === 'http://localhost:3000'

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production
) {
warnings.push(
`WARNING: Localhost origin in production at index ${idx}`
)
}
})
}
Expand All @@ -58,14 +63,16 @@
}
}

static createMiddleware(config) {
static createMiddleware (config) {
const validation = this.validate(config)

if (!validation.valid) {
throw new Error(`CORS Configuration Error: ${validation.errors.join(', ')}`)
throw new Error(
`CORS Configuration Error: ${validation.errors.join(', ')}`
)
}

validation.warnings.forEach(w => {
validation.warnings.forEach((w) => {
global.logger?.warn(w)
})

Expand Down Expand Up @@ -108,10 +115,7 @@
res.setHeader('Access-Control-Allow-Credentials', 'true')
}

res.setHeader(
'Access-Control-Max-Age',
validation.config.maxAge
)
res.setHeader('Access-Control-Max-Age', validation.config.maxAge)

Check notice on line 118 in middleware/corsValidator.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

middleware/corsValidator.js#L118

Missing semicolon.

// Handle preflight
if (req.method === 'OPTIONS') {
Expand Down
Loading