Skip to content

Commit

Permalink
Make csp directive policy configurable via env (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMuriuki authored May 6, 2022
1 parent 9a3384f commit b25df2e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ EXPRESS_KEYCLOAK_LOGOUT_URL=https://keycloak-stage.smartregister.org/auth/realms
EXPRESS_MAXIMUM_LOGS_FILE_SIZE=5242880 # 5MB
EXPRESS_MAXIMUM_LOG_FILES_NUMBER=5
EXPRESS_LOGS_FILE_PATH='/home/.express/reveal-express-server.log

# https://github.com/helmetjs/helmet#reference
EXPRESS_CONTENT_SECURITY_POLICY_CONFIG=`{"default-src":["'self'"]}`
13 changes: 2 additions & 11 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import {
EXPRESS_SESSION_NAME,
EXPRESS_SESSION_PATH,
EXPRESS_SESSION_SECRET,
EXPRESS_CONTENT_SECURITY_POLICY_CONFIG,
} from '../configs/envs';
import { SESSION_IS_EXPIRED, TOKEN_NOT_FOUND, TOKEN_REFRESH_FAILED } from '../constants';
import { getOriginFromUrl } from '../utils';

type Dictionary = { [key: string]: unknown };

Expand All @@ -62,16 +62,7 @@ app.use(
// might consider turning this off to allow individual front-ends set Content-Security-Policy on meta tags themselves if list grows long
// <meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com;" >
contentSecurityPolicy: {
directives: {
'script-src': ["'self'", 'https://cdnjs.cloudflare.com', "'unsafe-inline'"],
'img-src': ["'self'", 'https://github.com', 'https://*.githubusercontent.com'],
// allow connection from keycloak and opensrp server
'connect-src': [
"'self'",
...getOriginFromUrl(EXPRESS_OPENSRP_AUTHORIZATION_URL),
...getOriginFromUrl(EXPRESS_OPENSRP_USER_URL),
],
},
directives: EXPRESS_CONTENT_SECURITY_POLICY_CONFIG,
},
crossOriginEmbedderPolicy: false,
}),
Expand Down
2 changes: 2 additions & 0 deletions src/app/tests/index.errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import app from '../index';

const oauthCallbackUri = '/oauth/callback/OpenSRP/?code=Boi4Wz&state=openssh';

jest.mock('../../configs/envs');

const panic = (err: Error, done: jest.DoneCallback): void => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (err) {
Expand Down
4 changes: 4 additions & 0 deletions src/app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ describe('src/index.ts', () => {
request(app)
.get('/')
.expect(200)
.expect((res) => {
const csp = res.headers['content-security-policy'];
expect(csp).toContain(`default-src 'self';report-uri https://example.com;`);
})
.expect('Do you mind\n')
.catch((err: Error) => {
throw err;
Expand Down
1 change: 1 addition & 0 deletions src/configs/__mocks__/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ export const EXPRESS_MAXIMUM_LOG_FILES_NUMBER = 5;
export const EXPRESS_LOGS_FILE_PATH = './logs/default-error.log';

export const EXPRESS_COMBINED_LOGS_FILE_PATH = './logs/default-error-and-info.log';
export const EXPRESS_CONTENT_SECURITY_POLICY_CONFIG = { 'default-src': ["'self'"], reportUri: 'https://example.com' };
8 changes: 8 additions & 0 deletions src/configs/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,11 @@ export type EXPRESS_LOGS_FILE_PATH = typeof EXPRESS_LOGS_FILE_PATH;
export const EXPRESS_COMBINED_LOGS_FILE_PATH =
process.env.EXPRESS_COMBINED_LOGS_FILE_PATH || './logs/default-error-and-info.log';
export type EXPRESS_COMBINED_LOGS_FILE_PATH = typeof EXPRESS_COMBINED_LOGS_FILE_PATH;

const defaultCsp = JSON.stringify({
'default-src': ['none'],
});
export const EXPRESS_CONTENT_SECURITY_POLICY_CONFIG = JSON.parse(
process.env.EXPRESS_CONTENT_SECURITY_POLICY_CONFIG || defaultCsp,
);
export type EXPRESS_CONTENT_SECURITY_POLICY_CONFIG = typeof EXPRESS_CONTENT_SECURITY_POLICY_CONFIG;
15 changes: 0 additions & 15 deletions src/utils/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/utils/tests/index.test.ts

This file was deleted.

0 comments on commit b25df2e

Please sign in to comment.