-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathsanitize-headers.js
61 lines (52 loc) · 1.4 KB
/
sanitize-headers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
// See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-read-only-headers
const readOnlyHeaders = [
'accept-encoding',
'content-length',
'if-modified-since',
'if-none-Match',
'if-range',
'if-unmodified-since',
'range',
'transfer-encoding',
'via'
];
// See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-blacklisted-headers
const blacklistedHeaders = [
'connection',
'expect',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'proxy-connection',
'trailer',
'upgrade',
'x-accel-buffering',
'x-accel-charset',
'x-accel-limit-rate',
'x-accel-redirect',
'x-cache',
'x-forwarded-proto',
'x-real-ip',
]
const omittedHeaders = [...readOnlyHeaders, ...blacklistedHeaders]
module.exports = function sanitizeHeaders(headers) {
return Object.keys(headers).reduce((memo, key) => {
const value = headers[key];
const normalizedKey = key.toLowerCase();
if (omittedHeaders.includes(normalizedKey)) {
return memo;
}
if (memo[normalizedKey] === undefined) {
memo[normalizedKey] = []
}
const valueArray = Array.isArray(value) ? value : [value]
valueArray.forEach(valueElement => {
memo[normalizedKey].push({
key: key,
value: valueElement
});
});
return memo;
}, {});
};