@sasjs/adapter 6.1.5 → 6.1.6
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.
- package/auth/getAccessTokenForSasjs.js +1 -2
- package/auth/getAccessTokenForViya.js +1 -2
- package/auth/getTokenRequestErrorPrefix.d.ts +50 -9
- package/auth/getTokenRequestErrorPrefix.js +85 -16
- package/auth/redactCredentials.d.ts +42 -0
- package/auth/redactCredentials.js +187 -0
- package/auth/refreshTokensForSasjs.js +1 -2
- package/auth/refreshTokensForViya.js +1 -2
- package/dist/minified_sas9.js +105 -11
- package/dist/sasjs-adapter.js +391 -36
- package/package.json +1 -1
- package/request/RequestClient.d.ts +11 -0
- package/request/RequestClient.js +33 -11
- package/request/responseReceived.d.ts +36 -0
- package/request/responseReceived.js +63 -0
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.getAccessTokenForSasjs = getAccessTokenForSasjs;
|
|
13
|
-
const error_1 = require("@sasjs/utils/error");
|
|
14
13
|
const getTokenRequestErrorPrefix_1 = require("./getTokenRequestErrorPrefix");
|
|
15
14
|
const types_1 = require("@sasjs/utils/types");
|
|
16
15
|
const apiBasePath_1 = require("../apiBasePath");
|
|
@@ -38,7 +37,7 @@ function getAccessTokenForSasjs(requestClient_1, clientId_1, authCode_1) {
|
|
|
38
37
|
};
|
|
39
38
|
})
|
|
40
39
|
.catch((err) => {
|
|
41
|
-
throw (0,
|
|
40
|
+
throw (0, getTokenRequestErrorPrefix_1.tokenRequestErrorMessage)(err, 'fetching access token', 'getAccessTokenForSasjs', types_1.ServerType.Sasjs, url, data, undefined, clientId);
|
|
42
41
|
});
|
|
43
42
|
});
|
|
44
43
|
}
|
|
@@ -11,7 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.getAccessTokenForViya = getAccessTokenForViya;
|
|
13
13
|
const types_1 = require("@sasjs/utils/types");
|
|
14
|
-
const error_1 = require("@sasjs/utils/error");
|
|
15
14
|
const errors_1 = require("../types/errors");
|
|
16
15
|
const getTokenRequestErrorPrefix_1 = require("./getTokenRequestErrorPrefix");
|
|
17
16
|
/**
|
|
@@ -46,7 +45,7 @@ function getAccessTokenForViya(requestClient, clientId, clientSecret, authCode)
|
|
|
46
45
|
.catch((err) => {
|
|
47
46
|
if (err instanceof errors_1.CertificateError)
|
|
48
47
|
throw err;
|
|
49
|
-
throw (0,
|
|
48
|
+
throw (0, getTokenRequestErrorPrefix_1.tokenRequestErrorMessage)(err, 'fetching access token', 'getAccessTokenForViya', types_1.ServerType.SasViya, url, dataJson, headers, clientId, clientSecret);
|
|
50
49
|
});
|
|
51
50
|
return authResponse;
|
|
52
51
|
});
|
|
@@ -7,25 +7,35 @@ type Operation = 'fetching access token' | 'refreshing tokens';
|
|
|
7
7
|
* @param funcName - name of the function sent the request.
|
|
8
8
|
* @param server - server type (SASVIYA or SASJS).
|
|
9
9
|
* @param url - endpoint used to send the request.
|
|
10
|
-
* @param data - request payload.
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @param
|
|
14
|
-
*
|
|
10
|
+
* @param data - request payload. Object and form-encoded payloads are redacted
|
|
11
|
+
* field by field, at any depth; any other text body is reported as a shape
|
|
12
|
+
* rather than printed.
|
|
13
|
+
* @param headers - request headers. An authorization value keeps its scheme and
|
|
14
|
+
* loses its credential.
|
|
15
|
+
* @param clientId - client ID to authenticate with. Printed: it names which
|
|
16
|
+
* client failed.
|
|
17
|
+
* @param clientSecret - client secret to authenticate with. Replaced, never
|
|
18
|
+
* printed; the report states that one was supplied.
|
|
19
|
+
* @param responseReceived - whether the request came back with a response
|
|
20
|
+
* (default `true`). When it did not, the title says so rather than presenting
|
|
21
|
+
* the transport's own message as an answer from the server.
|
|
22
|
+
* @returns - string containing request information, with every credential
|
|
23
|
+
* named rather than printed. A failure is pasted into tickets and kept in CI
|
|
24
|
+
* logs, so the report never carries a value that could be used as one. Example:
|
|
15
25
|
* Error while fetching access token from /SASLogon/oauth/token
|
|
16
26
|
* Thrown by the @sasjs/adapter getAccessTokenForViya function.
|
|
17
27
|
* Payload:
|
|
18
28
|
* {
|
|
19
29
|
* "grant_type": "authorization_code",
|
|
20
|
-
* "code": "
|
|
30
|
+
* "code": "<redacted>"
|
|
21
31
|
* }
|
|
22
32
|
* Headers:
|
|
23
33
|
* {
|
|
24
|
-
* "Authorization": "Basic
|
|
34
|
+
* "Authorization": "Basic <redacted>",
|
|
25
35
|
* "Accept": "application/json"
|
|
26
36
|
* }
|
|
27
37
|
* ClientId: exampleClientId
|
|
28
|
-
* ClientSecret:
|
|
38
|
+
* ClientSecret: <redacted>
|
|
29
39
|
*
|
|
30
40
|
* Response from Viya is below.
|
|
31
41
|
* Auth error: {
|
|
@@ -33,9 +43,40 @@ type Operation = 'fetching access token' | 'refreshing tokens';
|
|
|
33
43
|
* "error_description": "No scopes were granted"
|
|
34
44
|
* }
|
|
35
45
|
*/
|
|
36
|
-
export declare const getTokenRequestErrorPrefix: (operation: Operation, funcName: string, server: Server, url: string, data?: {}, headers?: {}, clientId?: string, clientSecret?: string) => string;
|
|
46
|
+
export declare const getTokenRequestErrorPrefix: (operation: Operation, funcName: string, server: Server, url: string, data?: {}, headers?: {}, clientId?: string, clientSecret?: string, responseReceived?: boolean) => string;
|
|
47
|
+
/**
|
|
48
|
+
* The message for a failed token request: how the request was made, then what
|
|
49
|
+
* came back.
|
|
50
|
+
*
|
|
51
|
+
* Every caller holds the error, so this is where "did the server answer?" is
|
|
52
|
+
* decided - once. Threading a boolean through the optional parameters of
|
|
53
|
+
* `getTokenRequestErrorPrefix` instead leaves the short call sites padding
|
|
54
|
+
* arguments they do not have, which is how a later caller gets it wrong.
|
|
55
|
+
*
|
|
56
|
+
* @param error - the rejection from the request client.
|
|
57
|
+
* @param operation - string describing operation ('fetching access token' or 'refreshing tokens').
|
|
58
|
+
* @param funcName - name of the function that sent the request.
|
|
59
|
+
* @param server - server type (SASVIYA or SASJS).
|
|
60
|
+
* @param url - endpoint used to send the request.
|
|
61
|
+
* @param data - request payload. Object and form-encoded payloads are redacted
|
|
62
|
+
* field by field, at any depth; any other text body is reported as a shape
|
|
63
|
+
* rather than printed.
|
|
64
|
+
* @param headers - request headers. An authorization value keeps its scheme and
|
|
65
|
+
* loses its credential.
|
|
66
|
+
* @param clientId - client ID to authenticate with. Printed: it names which
|
|
67
|
+
* client failed.
|
|
68
|
+
* @param clientSecret - client secret to authenticate with. Replaced, never
|
|
69
|
+
* printed; the report states that one was supplied.
|
|
70
|
+
* @returns - the error, with the prefix in front of its message.
|
|
71
|
+
*/
|
|
72
|
+
export declare const tokenRequestErrorMessage: (error: unknown, operation: Operation, funcName: string, server: Server, url: string, data?: {}, headers?: {}, clientId?: string, clientSecret?: string) => string | import("@sasjs/utils/types").Error;
|
|
37
73
|
/**
|
|
38
74
|
* Parse error prefix to get response payload.
|
|
75
|
+
*
|
|
76
|
+
* A prefix built for a request that never reached the server carries the
|
|
77
|
+
* transport's own message where the payload would be, so the split is made on
|
|
78
|
+
* whichever title the prefix holds rather than assuming one.
|
|
79
|
+
*
|
|
39
80
|
* @param prefix - error prefix generated by getTokenRequestErrorPrefix function.
|
|
40
81
|
* @param server - server type (SASVIYA or SASJS).
|
|
41
82
|
* @returns - response payload.
|
|
@@ -1,34 +1,59 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokenRequestErrorPrefixResponse = exports.getTokenRequestErrorPrefix = void 0;
|
|
3
|
+
exports.getTokenRequestErrorPrefixResponse = exports.tokenRequestErrorMessage = exports.getTokenRequestErrorPrefix = void 0;
|
|
4
4
|
const types_1 = require("@sasjs/utils/types");
|
|
5
|
+
const error_1 = require("@sasjs/utils/error");
|
|
6
|
+
const responseReceived_1 = require("../request/responseReceived");
|
|
7
|
+
const redactCredentials_1 = require("./redactCredentials");
|
|
5
8
|
const getServerName = (server) => server === types_1.ServerType.SasViya ? 'Viya' : 'Sasjs';
|
|
6
|
-
|
|
9
|
+
/**
|
|
10
|
+
* What follows the request details: the server's answer, or the statement that
|
|
11
|
+
* there was none.
|
|
12
|
+
*
|
|
13
|
+
* A transport failure - a wrong URL, a closed port, a VPN that is down, a
|
|
14
|
+
* certificate that does not verify - carries no response, so calling its text
|
|
15
|
+
* "Response from Viya" claims something the server never said. That is
|
|
16
|
+
* sasjs/adapter#895: the reader was told the target answered with
|
|
17
|
+
* `connect ETIMEDOUT`.
|
|
18
|
+
*/
|
|
19
|
+
const getResponseTitle = (server, responseReceived) => responseReceived
|
|
20
|
+
? `Response from ${getServerName(server)} is below.`
|
|
21
|
+
: `No response from ${getServerName(server)} - the request did not reach it.`;
|
|
7
22
|
/**
|
|
8
23
|
* Forms error prefix for requests related to token operations.
|
|
9
24
|
* @param operation - string describing operation ('fetching access token' or 'refreshing tokens').
|
|
10
25
|
* @param funcName - name of the function sent the request.
|
|
11
26
|
* @param server - server type (SASVIYA or SASJS).
|
|
12
27
|
* @param url - endpoint used to send the request.
|
|
13
|
-
* @param data - request payload.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @param
|
|
17
|
-
*
|
|
28
|
+
* @param data - request payload. Object and form-encoded payloads are redacted
|
|
29
|
+
* field by field, at any depth; any other text body is reported as a shape
|
|
30
|
+
* rather than printed.
|
|
31
|
+
* @param headers - request headers. An authorization value keeps its scheme and
|
|
32
|
+
* loses its credential.
|
|
33
|
+
* @param clientId - client ID to authenticate with. Printed: it names which
|
|
34
|
+
* client failed.
|
|
35
|
+
* @param clientSecret - client secret to authenticate with. Replaced, never
|
|
36
|
+
* printed; the report states that one was supplied.
|
|
37
|
+
* @param responseReceived - whether the request came back with a response
|
|
38
|
+
* (default `true`). When it did not, the title says so rather than presenting
|
|
39
|
+
* the transport's own message as an answer from the server.
|
|
40
|
+
* @returns - string containing request information, with every credential
|
|
41
|
+
* named rather than printed. A failure is pasted into tickets and kept in CI
|
|
42
|
+
* logs, so the report never carries a value that could be used as one. Example:
|
|
18
43
|
* Error while fetching access token from /SASLogon/oauth/token
|
|
19
44
|
* Thrown by the @sasjs/adapter getAccessTokenForViya function.
|
|
20
45
|
* Payload:
|
|
21
46
|
* {
|
|
22
47
|
* "grant_type": "authorization_code",
|
|
23
|
-
* "code": "
|
|
48
|
+
* "code": "<redacted>"
|
|
24
49
|
* }
|
|
25
50
|
* Headers:
|
|
26
51
|
* {
|
|
27
|
-
* "Authorization": "Basic
|
|
52
|
+
* "Authorization": "Basic <redacted>",
|
|
28
53
|
* "Accept": "application/json"
|
|
29
54
|
* }
|
|
30
55
|
* ClientId: exampleClientId
|
|
31
|
-
* ClientSecret:
|
|
56
|
+
* ClientSecret: <redacted>
|
|
32
57
|
*
|
|
33
58
|
* Response from Viya is below.
|
|
34
59
|
* Auth error: {
|
|
@@ -36,35 +61,79 @@ const getResponseTitle = (server) => `Response from ${getServerName(server)} is
|
|
|
36
61
|
* "error_description": "No scopes were granted"
|
|
37
62
|
* }
|
|
38
63
|
*/
|
|
39
|
-
const getTokenRequestErrorPrefix = (operation, funcName, server, url, data, headers, clientId, clientSecret) => {
|
|
64
|
+
const getTokenRequestErrorPrefix = (operation, funcName, server, url, data, headers, clientId, clientSecret, responseReceived = true) => {
|
|
40
65
|
const stringify = (obj) => JSON.stringify(obj, null, 2);
|
|
41
66
|
const lines = [
|
|
42
67
|
`Error while ${operation} from ${url}`,
|
|
43
68
|
`Thrown by the @sasjs/adapter ${funcName} function.`
|
|
44
69
|
];
|
|
70
|
+
// The request carries the credentials that minted the token: the header is the
|
|
71
|
+
// client pair base64-encoded together, the body a refresh token or an
|
|
72
|
+
// authorization code. A failure is pasted into tickets, so the values are
|
|
73
|
+
// named and not printed (#895).
|
|
45
74
|
if (data) {
|
|
75
|
+
const payload = (0, redactCredentials_1.redactPayload)(data);
|
|
46
76
|
lines.push('Payload:');
|
|
47
|
-
lines.push(stringify(
|
|
77
|
+
lines.push(typeof payload === 'string' ? payload : stringify(payload));
|
|
48
78
|
}
|
|
49
79
|
if (headers) {
|
|
50
80
|
lines.push('Headers:');
|
|
51
|
-
lines.push(stringify(headers));
|
|
81
|
+
lines.push(stringify((0, redactCredentials_1.redactHeaders)(headers)));
|
|
52
82
|
}
|
|
83
|
+
// The client id names which client failed and is not a credential; the secret
|
|
84
|
+
// beside it is, so it is stated as supplied rather than printed.
|
|
53
85
|
if (clientId)
|
|
54
86
|
lines.push(`ClientId: ${clientId}`);
|
|
55
87
|
if (clientSecret)
|
|
56
|
-
lines.push(`ClientSecret: ${
|
|
88
|
+
lines.push(`ClientSecret: ${redactCredentials_1.REDACTED}`);
|
|
57
89
|
lines.push('');
|
|
58
|
-
lines.push(`${getResponseTitle(server)}`);
|
|
90
|
+
lines.push(`${getResponseTitle(server, responseReceived)}`);
|
|
59
91
|
lines.push('');
|
|
60
92
|
return lines.join(`\n`);
|
|
61
93
|
};
|
|
62
94
|
exports.getTokenRequestErrorPrefix = getTokenRequestErrorPrefix;
|
|
95
|
+
/**
|
|
96
|
+
* The message for a failed token request: how the request was made, then what
|
|
97
|
+
* came back.
|
|
98
|
+
*
|
|
99
|
+
* Every caller holds the error, so this is where "did the server answer?" is
|
|
100
|
+
* decided - once. Threading a boolean through the optional parameters of
|
|
101
|
+
* `getTokenRequestErrorPrefix` instead leaves the short call sites padding
|
|
102
|
+
* arguments they do not have, which is how a later caller gets it wrong.
|
|
103
|
+
*
|
|
104
|
+
* @param error - the rejection from the request client.
|
|
105
|
+
* @param operation - string describing operation ('fetching access token' or 'refreshing tokens').
|
|
106
|
+
* @param funcName - name of the function that sent the request.
|
|
107
|
+
* @param server - server type (SASVIYA or SASJS).
|
|
108
|
+
* @param url - endpoint used to send the request.
|
|
109
|
+
* @param data - request payload. Object and form-encoded payloads are redacted
|
|
110
|
+
* field by field, at any depth; any other text body is reported as a shape
|
|
111
|
+
* rather than printed.
|
|
112
|
+
* @param headers - request headers. An authorization value keeps its scheme and
|
|
113
|
+
* loses its credential.
|
|
114
|
+
* @param clientId - client ID to authenticate with. Printed: it names which
|
|
115
|
+
* client failed.
|
|
116
|
+
* @param clientSecret - client secret to authenticate with. Replaced, never
|
|
117
|
+
* printed; the report states that one was supplied.
|
|
118
|
+
* @returns - the error, with the prefix in front of its message.
|
|
119
|
+
*/
|
|
120
|
+
const tokenRequestErrorMessage = (error, operation, funcName, server, url, data, headers, clientId, clientSecret) => (0, error_1.prefixMessage)(error, (0, exports.getTokenRequestErrorPrefix)(operation, funcName, server, url, data, headers, clientId, clientSecret, (0, responseReceived_1.responseWasReceived)(error)));
|
|
121
|
+
exports.tokenRequestErrorMessage = tokenRequestErrorMessage;
|
|
63
122
|
/**
|
|
64
123
|
* Parse error prefix to get response payload.
|
|
124
|
+
*
|
|
125
|
+
* A prefix built for a request that never reached the server carries the
|
|
126
|
+
* transport's own message where the payload would be, so the split is made on
|
|
127
|
+
* whichever title the prefix holds rather than assuming one.
|
|
128
|
+
*
|
|
65
129
|
* @param prefix - error prefix generated by getTokenRequestErrorPrefix function.
|
|
66
130
|
* @param server - server type (SASVIYA or SASJS).
|
|
67
131
|
* @returns - response payload.
|
|
68
132
|
*/
|
|
69
|
-
const getTokenRequestErrorPrefixResponse = (prefix, server) =>
|
|
133
|
+
const getTokenRequestErrorPrefixResponse = (prefix, server) => {
|
|
134
|
+
const receivedTitle = `${getResponseTitle(server, true)}\n`;
|
|
135
|
+
if (prefix.includes(receivedTitle))
|
|
136
|
+
return prefix.split(receivedTitle).pop();
|
|
137
|
+
return prefix.split(`${getResponseTitle(server, false)}\n`).pop();
|
|
138
|
+
};
|
|
70
139
|
exports.getTokenRequestErrorPrefixResponse = getTokenRequestErrorPrefixResponse;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a failed token request may say about the request it sent.
|
|
3
|
+
*
|
|
4
|
+
* A token failure is read by a person, pasted into a ticket and captured in CI
|
|
5
|
+
* logs, and the request behind it carries the credentials that minted it: an
|
|
6
|
+
* `Authorization` header holding the client id and secret base64-encoded
|
|
7
|
+
* together, a body holding a refresh token, and the authorization code itself.
|
|
8
|
+
* Printing those turns one error report into a credential disclosure, so this
|
|
9
|
+
* module names the values that are secret and replaces each with a marker.
|
|
10
|
+
*
|
|
11
|
+
* The marker is a fixed string, not an empty one, so a reader can tell "this was
|
|
12
|
+
* supplied" from "this was not". Keys are matched case-insensitively.
|
|
13
|
+
*/
|
|
14
|
+
export declare const REDACTED = "<redacted>";
|
|
15
|
+
/** Whether a parameter name carries a credential. */
|
|
16
|
+
export declare const isSecretKey: (key: string) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* An `Authorization` value with its scheme kept and its credential removed.
|
|
19
|
+
*
|
|
20
|
+
* The scheme stays because it is the part a reader diagnoses with: `Basic`
|
|
21
|
+
* means a client pair, `Bearer` means a token, and the two fail in different
|
|
22
|
+
* ways.
|
|
23
|
+
*/
|
|
24
|
+
export declare const redactAuthorizationValue: (value: unknown) => string;
|
|
25
|
+
/** Request headers, with the credential-bearing ones redacted. */
|
|
26
|
+
export declare const redactHeaders: (headers: {}) => {};
|
|
27
|
+
/**
|
|
28
|
+
* A request payload, with the credential-bearing values redacted.
|
|
29
|
+
*
|
|
30
|
+
* The shapes that reach this function, each handled on its own terms: a plain
|
|
31
|
+
* object or array is walked, so a secret one level down is redacted like one at the
|
|
32
|
+
* top; a byte view - a Buffer or any typed array - is reported as a shape, because
|
|
33
|
+
* walking it emits a key per byte; `URLSearchParams` (the form the OAuth code
|
|
34
|
+
* exchange is sent as) is rebuilt pair by pair; a string that parses as
|
|
35
|
+
* `key=value&key=value` is rebuilt the same way; and multipart form data (the
|
|
36
|
+
* refresh) is reported as its shape rather than its fields, because its parts are a
|
|
37
|
+
* stream the request still needs.
|
|
38
|
+
*
|
|
39
|
+
* `redactValue` carries the byte-view and cycle guards, so they hold at every depth
|
|
40
|
+
* rather than only here.
|
|
41
|
+
*/
|
|
42
|
+
export declare const redactPayload: (data: {} | string | undefined) => unknown;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.redactPayload = exports.redactHeaders = exports.redactAuthorizationValue = exports.isSecretKey = exports.REDACTED = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* What a failed token request may say about the request it sent.
|
|
6
|
+
*
|
|
7
|
+
* A token failure is read by a person, pasted into a ticket and captured in CI
|
|
8
|
+
* logs, and the request behind it carries the credentials that minted it: an
|
|
9
|
+
* `Authorization` header holding the client id and secret base64-encoded
|
|
10
|
+
* together, a body holding a refresh token, and the authorization code itself.
|
|
11
|
+
* Printing those turns one error report into a credential disclosure, so this
|
|
12
|
+
* module names the values that are secret and replaces each with a marker.
|
|
13
|
+
*
|
|
14
|
+
* The marker is a fixed string, not an empty one, so a reader can tell "this was
|
|
15
|
+
* supplied" from "this was not". Keys are matched case-insensitively.
|
|
16
|
+
*/
|
|
17
|
+
exports.REDACTED = '<redacted>';
|
|
18
|
+
/**
|
|
19
|
+
* The parameter names that carry a credential, in any of the spellings the
|
|
20
|
+
* estate uses: OAuth's snake case, the adapter's camel case, and the headers a
|
|
21
|
+
* server sets alongside a session.
|
|
22
|
+
*/
|
|
23
|
+
const SECRET_KEYS = [
|
|
24
|
+
'authorization',
|
|
25
|
+
'token',
|
|
26
|
+
'cookie',
|
|
27
|
+
'x-csrf-token',
|
|
28
|
+
'x-xsrf-token',
|
|
29
|
+
'client_secret',
|
|
30
|
+
'clientsecret',
|
|
31
|
+
'secret',
|
|
32
|
+
'password',
|
|
33
|
+
'passwd',
|
|
34
|
+
'refresh_token',
|
|
35
|
+
'refreshtoken',
|
|
36
|
+
'access_token',
|
|
37
|
+
'accesstoken',
|
|
38
|
+
'id_token',
|
|
39
|
+
'idtoken',
|
|
40
|
+
'code'
|
|
41
|
+
];
|
|
42
|
+
/** A parameter name with the spellings that vary (case, hyphen, underscore) removed. */
|
|
43
|
+
const normalise = (key) => key.toLowerCase().replace(/[\s_-]/g, '');
|
|
44
|
+
/**
|
|
45
|
+
* What a form-encoded decoder is allowed to read as a parameter name.
|
|
46
|
+
*
|
|
47
|
+
* Deliberately narrow: a name carrying `{`, `[`, `"`, `%`, `&`, whitespace or a
|
|
48
|
+
* second `=` is not a parameter of a form-encoded body, it is text this module
|
|
49
|
+
* cannot read - and a body it cannot read is reported as a shape, never printed.
|
|
50
|
+
*/
|
|
51
|
+
const PARAMETER_NAME = /^[A-Za-z0-9_.~[\]]+$/;
|
|
52
|
+
/** Whether a parameter name carries a credential. */
|
|
53
|
+
const isSecretKey = (key) => SECRET_KEYS.map(normalise).includes(normalise(key));
|
|
54
|
+
exports.isSecretKey = isSecretKey;
|
|
55
|
+
/**
|
|
56
|
+
* An `Authorization` value with its scheme kept and its credential removed.
|
|
57
|
+
*
|
|
58
|
+
* The scheme stays because it is the part a reader diagnoses with: `Basic`
|
|
59
|
+
* means a client pair, `Bearer` means a token, and the two fail in different
|
|
60
|
+
* ways.
|
|
61
|
+
*/
|
|
62
|
+
const redactAuthorizationValue = (value) => {
|
|
63
|
+
var _a;
|
|
64
|
+
const scheme = (_a = /^(basic|bearer|negotiate|digest)\b/i.exec(String(value))) === null || _a === void 0 ? void 0 : _a[0];
|
|
65
|
+
return scheme ? `${scheme} ${exports.REDACTED}` : exports.REDACTED;
|
|
66
|
+
};
|
|
67
|
+
exports.redactAuthorizationValue = redactAuthorizationValue;
|
|
68
|
+
/** Request headers, with the credential-bearing ones redacted. */
|
|
69
|
+
const redactHeaders = (headers) => Object.fromEntries(Object.entries(headers).map(([key, value]) => [
|
|
70
|
+
key,
|
|
71
|
+
(0, exports.isSecretKey)(key) ? (0, exports.redactAuthorizationValue)(value) : value
|
|
72
|
+
]));
|
|
73
|
+
exports.redactHeaders = redactHeaders;
|
|
74
|
+
const MAX_DEPTH = 20;
|
|
75
|
+
/**
|
|
76
|
+
* A value, with secret-named keys redacted at any depth.
|
|
77
|
+
*
|
|
78
|
+
* Two guards belong here because this is where the walk happens, and both are
|
|
79
|
+
* about a payload the reporter cannot print:
|
|
80
|
+
*
|
|
81
|
+
* - a byte view - a Buffer, a typed array, an ArrayBuffer - is a body, and walking
|
|
82
|
+
* one as an object emits a numbered key per byte (`{"0":103,"1":114,...}`), the
|
|
83
|
+
* body reproduced as clearly as the text it came from;
|
|
84
|
+
* - a payload that refers to itself, or nests past `MAX_DEPTH`, would replace the
|
|
85
|
+
* token failure with `RangeError: Maximum call stack size exceeded`. Both are
|
|
86
|
+
* reported as a shape instead.
|
|
87
|
+
*
|
|
88
|
+
* `seen` holds the objects on the CURRENT path, not every object ever visited, so a
|
|
89
|
+
* body that shares a sub-object between two keys is walked twice rather than
|
|
90
|
+
* reported as circular - it is not.
|
|
91
|
+
*/
|
|
92
|
+
const redactValue = (value, seen = new WeakSet(), depth = 0) => {
|
|
93
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer)
|
|
94
|
+
return 'binary body - values redacted';
|
|
95
|
+
if (!value || typeof value !== 'object')
|
|
96
|
+
return value;
|
|
97
|
+
if (seen.has(value))
|
|
98
|
+
return 'circular body - values redacted';
|
|
99
|
+
if (depth >= MAX_DEPTH)
|
|
100
|
+
return 'nested too deep - values redacted';
|
|
101
|
+
seen.add(value);
|
|
102
|
+
try {
|
|
103
|
+
if (Array.isArray(value))
|
|
104
|
+
return value.map((inner) => redactValue(inner, seen, depth + 1));
|
|
105
|
+
return Object.fromEntries(Object.entries(value).map(([key, inner]) => [
|
|
106
|
+
key,
|
|
107
|
+
(0, exports.isSecretKey)(key) ? exports.REDACTED : redactValue(inner, seen, depth + 1)
|
|
108
|
+
]));
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
seen.delete(value);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* A body that arrived as text, which a caller is free to pass: the parameter is
|
|
116
|
+
* typed to admit one.
|
|
117
|
+
*
|
|
118
|
+
* Form-encoded text is rebuilt pair by pair so the parameters that are not
|
|
119
|
+
* secrets stay readable, and anything else is reported as a shape rather than
|
|
120
|
+
* echoed, because a body this module cannot read is a body it cannot redact.
|
|
121
|
+
*
|
|
122
|
+
* A JSON body is never form-encoded, whatever it contains. Base64 carries `=`
|
|
123
|
+
* padding, so `{"client_secret":"YWJjWg=="}` splits into a key that is the
|
|
124
|
+
* secret's own text and a value of `"}"` - the key is not a secret *name*, so no
|
|
125
|
+
* key check catches it, and the secret is printed as though it were a parameter
|
|
126
|
+
* name. Structured text therefore takes the shape branch on its opening
|
|
127
|
+
* character, before any splitting.
|
|
128
|
+
*/
|
|
129
|
+
const redactText = (text) => {
|
|
130
|
+
const trimmed = text.trim();
|
|
131
|
+
if (!trimmed)
|
|
132
|
+
return trimmed;
|
|
133
|
+
if (/^[{[]/.test(trimmed))
|
|
134
|
+
return 'text body - values redacted';
|
|
135
|
+
// A linear test, not a regular expression: the obvious pattern for "pairs
|
|
136
|
+
// joined by &" nests quantifiers and backtracks exponentially on a string like
|
|
137
|
+
// `!=&!=&!=&...` (CodeQL js/redos, found on this branch).
|
|
138
|
+
const pairs = trimmed.split('&');
|
|
139
|
+
const isFormEncoded = !/\s/.test(trimmed) &&
|
|
140
|
+
pairs.every((pair) => pair.indexOf('=') > 0) &&
|
|
141
|
+
pairs.every((pair) => PARAMETER_NAME.test(pair.slice(0, pair.indexOf('='))));
|
|
142
|
+
if (isFormEncoded) {
|
|
143
|
+
const redacted = {};
|
|
144
|
+
for (const pair of pairs) {
|
|
145
|
+
const [key, ...rest] = pair.split('=');
|
|
146
|
+
const value = rest.join('=');
|
|
147
|
+
redacted[key] = (0, exports.isSecretKey)(key) ? exports.REDACTED : value;
|
|
148
|
+
}
|
|
149
|
+
return redacted;
|
|
150
|
+
}
|
|
151
|
+
// not form-encoded: a JSON string, a SAS log, an HTML page. It is not parsed
|
|
152
|
+
// here because a half-parsed body is worse than a stated shape.
|
|
153
|
+
return 'text body - values redacted';
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* A request payload, with the credential-bearing values redacted.
|
|
157
|
+
*
|
|
158
|
+
* The shapes that reach this function, each handled on its own terms: a plain
|
|
159
|
+
* object or array is walked, so a secret one level down is redacted like one at the
|
|
160
|
+
* top; a byte view - a Buffer or any typed array - is reported as a shape, because
|
|
161
|
+
* walking it emits a key per byte; `URLSearchParams` (the form the OAuth code
|
|
162
|
+
* exchange is sent as) is rebuilt pair by pair; a string that parses as
|
|
163
|
+
* `key=value&key=value` is rebuilt the same way; and multipart form data (the
|
|
164
|
+
* refresh) is reported as its shape rather than its fields, because its parts are a
|
|
165
|
+
* stream the request still needs.
|
|
166
|
+
*
|
|
167
|
+
* `redactValue` carries the byte-view and cycle guards, so they hold at every depth
|
|
168
|
+
* rather than only here.
|
|
169
|
+
*/
|
|
170
|
+
const redactPayload = (data) => {
|
|
171
|
+
if (!data)
|
|
172
|
+
return data;
|
|
173
|
+
if (typeof data === 'string')
|
|
174
|
+
return redactText(data);
|
|
175
|
+
const candidate = data;
|
|
176
|
+
if (typeof candidate.getBoundary === 'function')
|
|
177
|
+
return 'multipart form data - fields and values redacted';
|
|
178
|
+
if (typeof candidate.entries === 'function' &&
|
|
179
|
+
typeof candidate.get === 'function') {
|
|
180
|
+
const redacted = {};
|
|
181
|
+
for (const [key, value] of candidate.entries())
|
|
182
|
+
redacted[key] = (0, exports.isSecretKey)(key) ? exports.REDACTED : value;
|
|
183
|
+
return redacted;
|
|
184
|
+
}
|
|
185
|
+
return redactValue(candidate);
|
|
186
|
+
};
|
|
187
|
+
exports.redactPayload = redactPayload;
|
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.refreshTokensForSasjs = refreshTokensForSasjs;
|
|
13
|
-
const error_1 = require("@sasjs/utils/error");
|
|
14
13
|
const getTokenRequestErrorPrefix_1 = require("./getTokenRequestErrorPrefix");
|
|
15
14
|
const types_1 = require("@sasjs/utils/types");
|
|
16
15
|
const apiBasePath_1 = require("../apiBasePath");
|
|
@@ -36,7 +35,7 @@ function refreshTokensForSasjs(requestClient_1, refreshToken_1) {
|
|
|
36
35
|
};
|
|
37
36
|
})
|
|
38
37
|
.catch((err) => {
|
|
39
|
-
throw (0,
|
|
38
|
+
throw (0, getTokenRequestErrorPrefix_1.tokenRequestErrorMessage)(err, 'refreshing tokens', 'refreshTokensForSasjs', types_1.ServerType.Sasjs, url);
|
|
40
39
|
});
|
|
41
40
|
return authResponse;
|
|
42
41
|
});
|
|
@@ -14,7 +14,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.refreshTokensForViya = refreshTokensForViya;
|
|
16
16
|
const types_1 = require("@sasjs/utils/types");
|
|
17
|
-
const error_1 = require("@sasjs/utils/error");
|
|
18
17
|
const form_data_1 = __importDefault(require("form-data"));
|
|
19
18
|
const utils_1 = require("../utils");
|
|
20
19
|
const getTokenRequestErrorPrefix_1 = require("./getTokenRequestErrorPrefix");
|
|
@@ -45,7 +44,7 @@ function refreshTokensForViya(requestClient, clientId, clientSecret, refreshToke
|
|
|
45
44
|
.post(url, formData, undefined, 'multipart/form-data; boundary=' + formData._boundary, headers)
|
|
46
45
|
.then((res) => res.result)
|
|
47
46
|
.catch((err) => {
|
|
48
|
-
throw (0,
|
|
47
|
+
throw (0, getTokenRequestErrorPrefix_1.tokenRequestErrorMessage)(err, 'refreshing tokens', 'refreshTokensForViya', types_1.ServerType.SasViya, url, formData, headers, clientId, clientSecret);
|
|
49
48
|
});
|
|
50
49
|
return authResponse;
|
|
51
50
|
});
|