backend-app-api: accept camelCase csp directives

Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
Vincenzo Scamporlino
2024-05-10 14:46:48 +02:00
parent a940cd9f11
commit 2deb523f3d
2 changed files with 5 additions and 3 deletions
@@ -47,7 +47,7 @@ describe('readHelmetOptions', () => {
csp: {
key: ['value'],
'img-src': false,
'script-src-attr': ['custom'],
scriptSrcAttr: ['custom'],
},
});
expect(readHelmetOptions(config)).toEqual({
@@ -18,6 +18,7 @@ import { Config } from '@backstage/config';
import helmet from 'helmet';
import { HelmetOptions } from 'helmet';
import { ContentSecurityPolicyOptions } from 'helmet/dist/types/middlewares/content-security-policy';
import kebabCase from 'lodash/kebabCase';
/**
* Attempts to read Helmet options from the backend configuration object.
@@ -97,10 +98,11 @@ export function applyCspDirectives(
if (directives) {
for (const [key, value] of Object.entries(directives)) {
const kebabCaseKey = kebabCase(key);
if (value === false) {
delete result[key];
delete result[kebabCaseKey];
} else {
result[key] = value;
result[kebabCaseKey] = value;
}
}
}