Skip to content

Commit

Permalink
Address remaining feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Feb 3, 2021
1 parent 2283752 commit 5d87516
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
8 changes: 0 additions & 8 deletions src/core/server/http/integration_tests/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ describe('request logging', () => {
},
},
},
root: {
appenders: ['test-console', 'default'],
level: 'warn',
},
loggers: [
{
context: 'http.server.response',
Expand Down Expand Up @@ -107,10 +103,6 @@ describe('request logging', () => {
},
},
},
root: {
appenders: ['test-console', 'default'],
level: 'warn',
},
loggers: [
{
context: 'http.server.response',
Expand Down
25 changes: 25 additions & 0 deletions src/core/server/http/logging/get_response_log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ describe('getEcsResponseLog', () => {
}
`);
});

test('does not mutate original headers', () => {
const reqHeaders = { authorization: 'a', cookie: 'b', 'user-agent': 'hi' };
const resHeaders = { headers: { 'content-length': 123, 'set-cookie': 'c' } };
const req = createMockHapiRequest({
headers: reqHeaders,
response: { headers: resHeaders },
});
getEcsResponseLog(req, logger);
expect(reqHeaders).toMatchInlineSnapshot(`
Object {
"authorization": "a",
"cookie": "b",
"user-agent": "hi",
}
`);
expect(resHeaders).toMatchInlineSnapshot(`
Object {
"headers": Object {
"content-length": 123,
"set-cookie": "c",
},
}
`);
});
});

describe('ecs', () => {
Expand Down
20 changes: 8 additions & 12 deletions src/core/server/http/logging/get_response_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ const REDACTED_HEADER_TEXT = '[REDACTED]';

// We are excluding sensitive headers by default, until we have a log filtering mechanism.
function redactSensitiveHeaders(
headers: Record<string, string | string[]>
headers?: Record<string, string | string[]>
): Record<string, string | string[]> {
return (
headers &&
Object.keys(headers).reduce(
(acc, key) => ({
// Create a shallow copy to prevent mutating the original headers
...acc,
[key]: FORBIDDEN_HEADERS.includes(key) ? REDACTED_HEADER_TEXT : headers[key],
}),
{} as Record<string, string | string[]>
)
);
const result = {} as Record<string, string | string[]>;
if (headers) {
for (const key of Object.keys(headers)) {
result[key] = FORBIDDEN_HEADERS.includes(key) ? REDACTED_HEADER_TEXT : headers[key];
}
}
return result;
}

/**
Expand Down

0 comments on commit 5d87516

Please sign in to comment.