Skip to content

Commit

Permalink
Add report to header config (#30)
Browse files Browse the repository at this point in the history
* Add report to header config

* Support report only mode on csp

* Expose all headers as configs

* Remove reportOnly from directives

* Add reportOnly config to CSP fixture config

* Update sample and mock envs

* Remove report-only fixture
  • Loading branch information
peterMuriuki authored Jun 29, 2022
1 parent 7374620 commit 80fd0e6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ EXPRESS_CONTENT_SECURITY_POLICY_CONFIG=`{"default-src":["'self'"]}`
EXPRESS_REDIS_STAND_ALONE_URL=redis://username:[email protected]:6379/4

EXPRESS_REDIS_SENTINEL_CONFIG='{"name":"master","sentinelUsername":"u_name","sentinelPassword":"pass","db":4,"sentinels":[{"host":"127.0.0.1","port":6379},{"host":"127.0.0.1","port":6379}]}'

# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/report-to. `Map<string, stringifiedJson>`.
EXPRESS_RESPONSE_HEADERS='{"Report-To":"{ \"group\": \"csp-endpoint\", \"max_age\": 10886400, \"endpoints\": [{ \"url\": \"https://example.com/endpoint\" }] }", "Access-Control-Allow-Headers": "GET"}'
14 changes: 14 additions & 0 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
EXPRESS_REDIS_STAND_ALONE_URL,
EXPRESS_REDIS_SENTINEL_CONFIG,
EXPRESS_CONTENT_SECURITY_POLICY_CONFIG,
EXPRESS_RESPONSE_HEADERS,
} from '../configs/envs';
import { SESSION_IS_EXPIRED, TOKEN_NOT_FOUND, TOKEN_REFRESH_FAILED } from '../constants';

Expand All @@ -60,6 +61,7 @@ const app = express();

app.use(compression()); // Compress all routes
// helps mitigate cross-site scripting attacks and other known vulnerabilities

app.use(
helmet({
// override default contentSecurityPolicy directive like script-src to include cloudflare cdn and github static content
Expand Down Expand Up @@ -136,6 +138,18 @@ if (app.get('env') === 'production') {

app.use(cookieParser());
app.use(session(sess));
// apply other headers to reponse
app.use((_, res, next) => {
const customHeaders = Object.entries(EXPRESS_RESPONSE_HEADERS);
if (customHeaders.length > 0) {
customHeaders.forEach(([key, value]) => {
if (typeof value === 'string' && value !== '') {
res.header(key, value);
}
});
}
next();
});

class HttpException extends Error {
public statusCode: number;
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 @@ -120,7 +120,11 @@ describe('src/index.ts', () => {
.expect(200)
.expect((res) => {
const csp = res.headers['content-security-policy'];
const reportTo = res.headers['report-to'];
expect(csp).toContain(`default-src 'self';report-uri https://example.com;`);
expect(reportTo).toEqual(
'{ "group": "csp-endpoint", "max_age": 10886400, "endpoints": [{ "url": "https://example.com/csp-reports" }] }, { "group": "hpkp-endpoint", "max_age": 10886400, "endpoints": [{ "url": "https://example.com/hpkp-reports" }] }',
);
})
.expect('Do you mind\n')
.catch((err: Error) => {
Expand Down
10 changes: 9 additions & 1 deletion src/configs/__mocks__/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ 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' };
export const EXPRESS_CONTENT_SECURITY_POLICY_CONFIG = {
'default-src': ["'self'"],
reportUri: 'https://example.com',
};

export const EXPRESS_RESPONSE_HEADERS = {
'Report-To':
'{ "group": "csp-endpoint", "max_age": 10886400, "endpoints": [{ "url": "https://example.com/csp-reports" }] }, { "group": "hpkp-endpoint", "max_age": 10886400, "endpoints": [{ "url": "https://example.com/hpkp-reports" }] }',
};
4 changes: 4 additions & 0 deletions src/configs/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type EXPRESS_COMBINED_LOGS_FILE_PATH = typeof EXPRESS_COMBINED_LOGS_FILE_
const defaultCsp = JSON.stringify({
'default-src': ['none'],
});

export const EXPRESS_CONTENT_SECURITY_POLICY_CONFIG = JSON.parse(
process.env.EXPRESS_CONTENT_SECURITY_POLICY_CONFIG || defaultCsp,
);
Expand All @@ -112,3 +113,6 @@ export const { EXPRESS_REDIS_STAND_ALONE_URL } = process.env;

// see https://github.com/luin/ioredis#sentinel
export const EXPRESS_REDIS_SENTINEL_CONFIG = JSON.parse(process.env.EXPRESS_REDIS_SENTINEL_CONFIG || '{}');

export const EXPRESS_RESPONSE_HEADERS = JSON.parse(process.env.EXPRESS_RESPONSE_HEADERS || '{}');
export type EXPRESS_RESPONSE_HEADERS = typeof EXPRESS_RESPONSE_HEADERS;

0 comments on commit 80fd0e6

Please sign in to comment.