Skip to content

Commit

Permalink
Merge pull request #24723 from backstage/fix-csp-camelCase
Browse files Browse the repository at this point in the history
Added support for camerCase csp in app-config
  • Loading branch information
Rugvip committed May 13, 2024
2 parents 7ae8d97 + 32e329e commit e54d3b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .changeset/olive-pants-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@backstage/backend-app-api': patch
---

Added support for camel case CSP directives in app-config. For example:

```yaml
backend:
csp:
upgradeInsecureRequests: false
```
5 changes: 3 additions & 2 deletions packages/backend-app-api/src/http/readHelmetOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ describe('readHelmetOptions', () => {
csp: {
key: ['value'],
'img-src': false,
'script-src-attr': ['custom'],
scriptSrcAttr: ['custom'],
'object-src': ['asd'],
},
});
expect(readHelmetOptions(config)).toEqual({
Expand All @@ -58,7 +59,7 @@ describe('readHelmetOptions', () => {
'base-uri': ["'self'"],
'font-src': ["'self'", 'https:', 'data:'],
'frame-ancestors': ["'self'"],
'object-src': ["'none'"],
'object-src': ['asd'],
'script-src': ["'self'", "'unsafe-eval'"],
'style-src': ["'self'", 'https:', "'unsafe-inline'"],
'script-src-attr': ['custom'],
Expand Down
6 changes: 4 additions & 2 deletions packages/backend-app-api/src/http/readHelmetOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit e54d3b9

Please sign in to comment.