@heroku/heroku-fetch 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -7,6 +7,30 @@
|
|
|
7
7
|
import { debugAuth, debugRequest, debugResponse } from '../debug-loggers.js';
|
|
8
8
|
import { handleErrorResponse } from './http-error-handler.js';
|
|
9
9
|
import { handle2FAChallenge, is2FAError } from './two-factor-authentication-handler.js';
|
|
10
|
+
/**
|
|
11
|
+
* Headers whose values must never appear in debug output. Compared
|
|
12
|
+
* case-insensitively. Mirrors the redaction in http-call so tokens
|
|
13
|
+
* aren't leaked when `debug` is enabled.
|
|
14
|
+
*/
|
|
15
|
+
const SENSITIVE_HEADERS = new Set([
|
|
16
|
+
'authorization',
|
|
17
|
+
'cookie',
|
|
18
|
+
'heroku-two-factor-code',
|
|
19
|
+
'proxy-authorization',
|
|
20
|
+
'x-addon-sso',
|
|
21
|
+
]);
|
|
22
|
+
/**
|
|
23
|
+
* Build a plain object of a request's headers with sensitive values
|
|
24
|
+
* replaced by `[REDACTED]`, safe for debug logging.
|
|
25
|
+
*/
|
|
26
|
+
function redactHeaders(headers) {
|
|
27
|
+
const result = {};
|
|
28
|
+
// eslint-disable-next-line unicorn/no-array-for-each
|
|
29
|
+
headers.forEach((value, key) => {
|
|
30
|
+
result[key] = SENSITIVE_HEADERS.has(key.toLowerCase()) ? '[REDACTED]' : value;
|
|
31
|
+
});
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
10
34
|
/**
|
|
11
35
|
* Create a beforeRequest hook that adds authentication and custom headers
|
|
12
36
|
*/
|
|
@@ -24,7 +48,16 @@ export function createBeforeRequestHook(getToken, defaultAccept, customHeaders,
|
|
|
24
48
|
request.headers.set(key, value);
|
|
25
49
|
}
|
|
26
50
|
}
|
|
27
|
-
// Add authorization header
|
|
51
|
+
// Add authorization header.
|
|
52
|
+
//
|
|
53
|
+
// We deliberately don't strip this on cross-origin redirects: the
|
|
54
|
+
// underlying fetch implementation does it for us. Per the WHATWG
|
|
55
|
+
// Fetch spec, `Authorization` (along with `Cookie`,
|
|
56
|
+
// `Proxy-Authorization`, and `Host`) is dropped before following a
|
|
57
|
+
// redirect to a different origin. Note this only covers those
|
|
58
|
+
// spec'd headers — if we ever add a custom sensitive header (e.g.
|
|
59
|
+
// `x-addon-sso`), fetch will NOT strip it and we'd need to handle
|
|
60
|
+
// that ourselves.
|
|
28
61
|
const token = await getToken();
|
|
29
62
|
if (token) {
|
|
30
63
|
request.headers.set('Authorization', `Bearer ${token}`);
|
|
@@ -32,12 +65,7 @@ export function createBeforeRequestHook(getToken, defaultAccept, customHeaders,
|
|
|
32
65
|
}
|
|
33
66
|
debugRequest('%s %s', request.method, request.url);
|
|
34
67
|
if (debug) {
|
|
35
|
-
|
|
36
|
-
// eslint-disable-next-line unicorn/no-array-for-each
|
|
37
|
-
request.headers.forEach((value, key) => {
|
|
38
|
-
headers[key] = value;
|
|
39
|
-
});
|
|
40
|
-
debugRequest('Headers: %O', headers);
|
|
68
|
+
debugRequest('Headers: %O', redactHeaders(request.headers));
|
|
41
69
|
}
|
|
42
70
|
};
|
|
43
71
|
}
|