1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-09 23:40:06 +00:00

chore: helmet config cleanup

This commit is contained in:
Mihajlo Medjedovic
2022-05-06 11:40:12 +00:00
parent dd3acce393
commit 3ad327b85f
5 changed files with 65 additions and 16 deletions

View File

@@ -0,0 +1,33 @@
import path from 'path'
import fs from 'fs'
export const getEnvCSPDirectives = (
HELMET_CSP_CONFIG_PATH: string | undefined
) => {
let cspConfigJson = {
'script-src': ["'self'", "'unsafe-inline'"]
}
if (
typeof HELMET_CSP_CONFIG_PATH === 'string' &&
HELMET_CSP_CONFIG_PATH.length > 0
) {
const cspConfigPath = path.join(process.cwd(), HELMET_CSP_CONFIG_PATH)
try {
let file = fs.readFileSync(cspConfigPath).toString()
try {
cspConfigJson = JSON.parse(file)
} catch (e) {
console.error(
'Parsing Content Security Policy JSON config failed. Make sure it is valid json'
)
}
} catch (e) {
console.error('Error reading HELMET CSP config file', e)
}
}
return cspConfigJson
}