Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add report to header config #30

Merged
merged 7 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
machariamuguku marked this conversation as resolved.
Show resolved Hide resolved
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;