@reclaimprotocol/attestor-core 5.0.1-beta.2 → 5.0.1-beta.22
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/browser/resources/attestor-browser.min.mjs +4512 -0
- package/lib/avs/abis/avsDirectoryABI.js +338 -341
- package/lib/avs/abis/delegationABI.js +1 -4
- package/lib/avs/abis/registryABI.js +719 -722
- package/lib/avs/client/create-claim-on-avs.js +129 -157
- package/lib/avs/config.js +18 -24
- package/lib/avs/contracts/ReclaimServiceManager.js +1 -0
- package/lib/avs/contracts/common.js +1 -0
- package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1139 -1156
- package/lib/avs/contracts/factories/index.js +4 -4
- package/lib/avs/contracts/index.js +2 -6
- package/lib/avs/types/index.js +1 -0
- package/lib/avs/utils/contracts.js +30 -50
- package/lib/avs/utils/register.js +75 -70
- package/lib/avs/utils/tasks.js +38 -45
- package/lib/client/create-claim.js +402 -431
- package/lib/client/tunnels/make-rpc-tcp-tunnel.js +46 -48
- package/lib/client/tunnels/make-rpc-tls-tunnel.js +125 -121
- package/lib/client/utils/attestor-pool.js +23 -22
- package/lib/client/utils/client-socket.js +86 -109
- package/lib/client/utils/message-handler.js +79 -89
- package/lib/config/index.js +40 -58
- package/lib/external-rpc/benchmark.js +61 -74
- package/lib/external-rpc/event-bus.js +12 -15
- package/lib/external-rpc/handle-incoming-msg.js +216 -225
- package/lib/external-rpc/jsc-polyfills/1.js +70 -68
- package/lib/external-rpc/jsc-polyfills/2.js +17 -12
- package/lib/external-rpc/jsc-polyfills/event.js +10 -15
- package/lib/external-rpc/jsc-polyfills/index.js +2 -2
- package/lib/external-rpc/jsc-polyfills/ws.js +77 -79
- package/lib/external-rpc/setup-browser.js +28 -28
- package/lib/external-rpc/setup-jsc.js +17 -17
- package/lib/external-rpc/types.js +1 -0
- package/lib/external-rpc/utils.js +89 -89
- package/lib/external-rpc/zk.js +55 -50
- package/lib/index.js +2 -6
- package/lib/mechain/abis/governanceABI.js +457 -460
- package/lib/mechain/abis/taskABI.js +502 -505
- package/lib/mechain/client/create-claim-on-mechain.js +24 -29
- package/lib/mechain/constants/index.js +3 -8
- package/lib/mechain/types/index.js +1 -0
- package/lib/proto/api.js +4200 -4087
- package/lib/proto/tee-bundle.js +1261 -1241
- package/lib/providers/http/index.js +616 -603
- package/lib/providers/http/patch-parse5-tree.js +27 -29
- package/lib/providers/http/utils.js +289 -248
- package/lib/providers/index.js +3 -6
- package/lib/server/create-server.js +89 -91
- package/lib/server/handlers/claimTeeBundle.js +231 -211
- package/lib/server/handlers/claimTunnel.js +66 -73
- package/lib/server/handlers/completeClaimOnChain.js +20 -25
- package/lib/server/handlers/createClaimOnChain.js +21 -27
- package/lib/server/handlers/createTaskOnMechain.js +40 -50
- package/lib/server/handlers/createTunnel.js +85 -90
- package/lib/server/handlers/disconnectTunnel.js +4 -7
- package/lib/server/handlers/fetchCertificateBytes.js +37 -53
- package/lib/server/handlers/index.js +21 -24
- package/lib/server/handlers/init.js +27 -28
- package/lib/server/handlers/toprf.js +13 -16
- package/lib/server/socket.js +97 -100
- package/lib/server/tunnels/make-tcp-tunnel.js +161 -186
- package/lib/server/utils/apm.js +32 -25
- package/lib/server/utils/assert-valid-claim-request.js +305 -334
- package/lib/server/utils/config-env.js +2 -2
- package/lib/server/utils/dns.js +12 -18
- package/lib/server/utils/gcp-attestation.js +233 -181
- package/lib/server/utils/generics.d.ts +1 -1
- package/lib/server/utils/generics.js +43 -37
- package/lib/server/utils/iso.js +253 -256
- package/lib/server/utils/keep-alive.js +36 -36
- package/lib/server/utils/nitro-attestation.js +295 -220
- package/lib/server/utils/oprf-raw.js +48 -55
- package/lib/server/utils/process-handshake.js +200 -218
- package/lib/server/utils/proxy-session.js +5 -5
- package/lib/server/utils/tee-oprf-mpc-verification.js +82 -78
- package/lib/server/utils/tee-oprf-verification.js +165 -142
- package/lib/server/utils/tee-transcript-reconstruction.js +176 -129
- package/lib/server/utils/tee-verification.js +397 -334
- package/lib/server/utils/validation.js +30 -37
- package/lib/types/bgp.js +1 -0
- package/lib/types/claims.js +1 -0
- package/lib/types/client.js +1 -0
- package/lib/types/general.js +1 -0
- package/lib/types/handlers.js +1 -0
- package/lib/types/providers.d.ts +3 -2
- package/lib/types/providers.gen.js +9 -15
- package/lib/types/providers.js +1 -0
- package/lib/types/rpc.js +1 -0
- package/lib/types/signatures.d.ts +1 -2
- package/lib/types/signatures.js +1 -0
- package/lib/types/tunnel.js +1 -0
- package/lib/types/zk.js +1 -0
- package/lib/utils/auth.js +54 -66
- package/lib/utils/b64-json.js +15 -15
- package/lib/utils/bgp-listener.js +107 -111
- package/lib/utils/claims.js +89 -80
- package/lib/utils/env.js +13 -17
- package/lib/utils/error.js +43 -47
- package/lib/utils/generics.js +284 -235
- package/lib/utils/http-parser.js +232 -187
- package/lib/utils/logger.js +80 -71
- package/lib/utils/prepare-packets.js +69 -67
- package/lib/utils/redactions.js +163 -121
- package/lib/utils/retries.js +22 -24
- package/lib/utils/signatures/eth.js +29 -28
- package/lib/utils/signatures/index.js +5 -10
- package/lib/utils/socket-base.js +84 -88
- package/lib/utils/tls.js +28 -28
- package/lib/utils/ws.js +19 -19
- package/lib/utils/zk.js +542 -582
- package/package.json +12 -5
- package/lib/external-rpc/global.d.js +0 -0
- package/lib/scripts/build-browser.d.ts +0 -1
- package/lib/scripts/build-jsc.d.ts +0 -1
- package/lib/scripts/build-lib.d.ts +0 -1
- package/lib/scripts/check-avs-registration.d.ts +0 -1
- package/lib/scripts/check-avs-registration.js +0 -28
- package/lib/scripts/fallbacks/crypto.d.ts +0 -1
- package/lib/scripts/fallbacks/crypto.js +0 -4
- package/lib/scripts/fallbacks/empty.d.ts +0 -3
- package/lib/scripts/fallbacks/empty.js +0 -4
- package/lib/scripts/fallbacks/re2.d.ts +0 -1
- package/lib/scripts/fallbacks/re2.js +0 -7
- package/lib/scripts/fallbacks/snarkjs.d.ts +0 -1
- package/lib/scripts/fallbacks/snarkjs.js +0 -10
- package/lib/scripts/fallbacks/stwo.d.ts +0 -6
- package/lib/scripts/fallbacks/stwo.js +0 -159
- package/lib/scripts/generate-provider-types.d.ts +0 -5
- package/lib/scripts/generate-provider-types.js +0 -101
- package/lib/scripts/generate-receipt.d.ts +0 -9
- package/lib/scripts/generate-receipt.js +0 -101
- package/lib/scripts/generate-toprf-keys.d.ts +0 -1
- package/lib/scripts/generate-toprf-keys.js +0 -24
- package/lib/scripts/jsc-cli-rpc.d.ts +0 -1
- package/lib/scripts/jsc-cli-rpc.js +0 -35
- package/lib/scripts/register-avs-operator.d.ts +0 -1
- package/lib/scripts/register-avs-operator.js +0 -3
- package/lib/scripts/start-server.d.ts +0 -1
- package/lib/scripts/start-server.js +0 -11
- package/lib/scripts/update-avs-metadata.d.ts +0 -1
- package/lib/scripts/update-avs-metadata.js +0 -20
- package/lib/scripts/utils.d.ts +0 -1
- package/lib/scripts/utils.js +0 -10
- package/lib/scripts/whitelist-operator.d.ts +0 -1
- package/lib/scripts/whitelist-operator.js +0 -16
package/lib/utils/http-parser.js
CHANGED
|
@@ -1,201 +1,246 @@
|
|
|
1
|
-
import { asciiToUint8Array, concatenateUint8Arrays } from
|
|
2
|
-
import { findIndexInUint8Array, uint8ArrayToStr } from "
|
|
3
|
-
import { REDACTION_CHAR_CODE } from "
|
|
4
|
-
const HTTP_HEADER_LINE_END = asciiToUint8Array(
|
|
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
|
-
|
|
1
|
+
import { asciiToUint8Array, concatenateUint8Arrays } from '@reclaimprotocol/tls';
|
|
2
|
+
import { findIndexInUint8Array, uint8ArrayToStr } from "./generics.js";
|
|
3
|
+
import { REDACTION_CHAR_CODE } from "./redactions.js";
|
|
4
|
+
const HTTP_HEADER_LINE_END = asciiToUint8Array('\r\n');
|
|
5
|
+
/**
|
|
6
|
+
* parses http/1.1 responses
|
|
7
|
+
*/
|
|
8
|
+
export function makeHttpResponseParser() {
|
|
9
|
+
/** the HTTP response data */
|
|
10
|
+
const res = {
|
|
11
|
+
statusCode: 0,
|
|
12
|
+
statusMessage: '',
|
|
13
|
+
headers: {},
|
|
14
|
+
body: new Uint8Array(),
|
|
15
|
+
complete: false,
|
|
16
|
+
headersComplete: false,
|
|
17
|
+
headerIndices: new Map(),
|
|
18
|
+
headerEndIdx: 0
|
|
19
|
+
};
|
|
20
|
+
let remainingBodyBytes = 0;
|
|
21
|
+
let isChunked = false;
|
|
22
|
+
let remaining = new Uint8Array();
|
|
23
|
+
let currentByteIdx = 0;
|
|
24
|
+
return {
|
|
25
|
+
res,
|
|
26
|
+
/**
|
|
27
|
+
* Parse the next chunk of data
|
|
28
|
+
* @param data the data to parse
|
|
29
|
+
*/
|
|
30
|
+
onChunk(data) {
|
|
31
|
+
// concatenate the remaining data from the last chunk
|
|
32
|
+
remaining = concatenateUint8Arrays([remaining, data]);
|
|
33
|
+
// if we don't have the headers yet, keep reading lines
|
|
34
|
+
// as each header is in a line
|
|
35
|
+
if (!res.headersComplete) {
|
|
36
|
+
for (let line = getLine(); typeof line !== 'undefined'; line = getLine()) {
|
|
37
|
+
// first line is the HTTP version, status code & message
|
|
38
|
+
if (!res.statusCode) {
|
|
39
|
+
const [, statusCode, statusMessage] = line.match(/HTTP\/\d\.\d (\d+) (.*)/) || [];
|
|
40
|
+
res.statusCode = Number(statusCode);
|
|
41
|
+
res.statusMessage = statusMessage;
|
|
42
|
+
res.statusLineEndIndex = currentByteIdx - HTTP_HEADER_LINE_END.length;
|
|
43
|
+
}
|
|
44
|
+
else if (line === '') { // empty line signifies end of headers
|
|
45
|
+
res.headersComplete = true;
|
|
46
|
+
res.headerEndIdx = currentByteIdx - 4;
|
|
47
|
+
// if the response is chunked, we need to process the body differently
|
|
48
|
+
if (res.headers['transfer-encoding']?.includes('chunked')) {
|
|
49
|
+
isChunked = true;
|
|
50
|
+
res.chunks = [];
|
|
51
|
+
break;
|
|
52
|
+
// if the response has a content-length, we know how many bytes to read
|
|
53
|
+
}
|
|
54
|
+
else if (res.headers['content-length']) {
|
|
55
|
+
remainingBodyBytes = Number(res.headers['content-length']);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
remainingBodyBytes = -1;
|
|
60
|
+
break;
|
|
61
|
+
// otherwise,
|
|
62
|
+
// no content-length, no chunked transfer encoding
|
|
63
|
+
// means wait till the stream ends
|
|
64
|
+
// https://stackoverflow.com/a/11376887
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (!res.complete) { // parse the header
|
|
68
|
+
const [key, value] = line.split(': ');
|
|
69
|
+
res.headers[key.toLowerCase()] = value;
|
|
70
|
+
res.headerIndices[key.toLowerCase()] = {
|
|
71
|
+
fromIndex: currentByteIdx - line.length - HTTP_HEADER_LINE_END.length,
|
|
72
|
+
toIndex: currentByteIdx - HTTP_HEADER_LINE_END.length
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
throw new Error('got more data after response was complete');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
48
79
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
80
|
+
if (res.headersComplete) {
|
|
81
|
+
if (remainingBodyBytes) {
|
|
82
|
+
readBody();
|
|
83
|
+
// if no more body bytes to read,
|
|
84
|
+
// and the response was not chunked we're done
|
|
85
|
+
if (!remainingBodyBytes && !isChunked) {
|
|
86
|
+
res.complete = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (res.headers['content-length'] === '0') {
|
|
90
|
+
res.complete = true;
|
|
91
|
+
}
|
|
92
|
+
if (isChunked) {
|
|
93
|
+
for (let line = getLine(); typeof line !== 'undefined'; line = getLine()) {
|
|
94
|
+
if (line === '') {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const chunkSize = Number.parseInt(line, 16);
|
|
98
|
+
// if chunk size is 0, we're done
|
|
99
|
+
if (!chunkSize) {
|
|
100
|
+
res.complete = true;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
res.chunks?.push({
|
|
104
|
+
fromIndex: currentByteIdx,
|
|
105
|
+
toIndex: currentByteIdx + chunkSize,
|
|
106
|
+
});
|
|
107
|
+
// otherwise read the chunk
|
|
108
|
+
remainingBodyBytes = chunkSize;
|
|
109
|
+
readBody();
|
|
110
|
+
// if we read all the data we had,
|
|
111
|
+
// but there's still data left,
|
|
112
|
+
// break the loop and wait for the next chunk
|
|
113
|
+
if (remainingBodyBytes) {
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
/**
|
|
121
|
+
* Call to prevent further parsing; indicating the end of the request
|
|
122
|
+
* Checks that the response is valid & complete, otherwise throws an error
|
|
123
|
+
*/
|
|
124
|
+
streamEnded() {
|
|
125
|
+
if (!res.headersComplete) {
|
|
126
|
+
throw new Error('stream ended before headers were complete');
|
|
75
127
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
res.complete = true;
|
|
79
|
-
continue;
|
|
128
|
+
if (remaining.length) {
|
|
129
|
+
throw new Error('stream ended before remaining data arrived');
|
|
80
130
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
toIndex: currentByteIdx + chunkSize
|
|
84
|
-
});
|
|
85
|
-
remainingBodyBytes = chunkSize;
|
|
86
|
-
readBody();
|
|
87
|
-
if (remainingBodyBytes) {
|
|
88
|
-
break;
|
|
131
|
+
if (remainingBodyBytes > 0) {
|
|
132
|
+
throw new Error('stream ended before all body bytes were received');
|
|
89
133
|
}
|
|
90
|
-
|
|
134
|
+
res.complete = true;
|
|
91
135
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
136
|
+
};
|
|
137
|
+
function readBody() {
|
|
138
|
+
if (res.complete) {
|
|
139
|
+
throw new Error('got more data after response was complete');
|
|
140
|
+
}
|
|
141
|
+
if (!res.bodyStartIndex) {
|
|
142
|
+
res.bodyStartIndex = currentByteIdx;
|
|
143
|
+
}
|
|
144
|
+
let bytesToCopy;
|
|
145
|
+
if (remainingBodyBytes === -1) {
|
|
146
|
+
// all bytes are body bytes
|
|
147
|
+
bytesToCopy = remaining.length;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
// take the number of bytes we need to read, or the number of bytes remaining
|
|
151
|
+
// and append to the bytes of the body
|
|
152
|
+
bytesToCopy = Math.min(remainingBodyBytes, remaining.length);
|
|
153
|
+
remainingBodyBytes -= bytesToCopy;
|
|
154
|
+
}
|
|
155
|
+
res.body = concatenateUint8Arrays([
|
|
156
|
+
res.body,
|
|
157
|
+
remaining.slice(0, bytesToCopy)
|
|
158
|
+
]);
|
|
159
|
+
remaining = remaining.slice(bytesToCopy);
|
|
160
|
+
currentByteIdx += bytesToCopy;
|
|
114
161
|
}
|
|
115
|
-
|
|
116
|
-
|
|
162
|
+
function getLine() {
|
|
163
|
+
// find end of line, if it exists
|
|
164
|
+
// otherwise return undefined
|
|
165
|
+
const idx = findIndexInUint8Array(remaining, HTTP_HEADER_LINE_END);
|
|
166
|
+
if (idx === -1) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
const line = uint8ArrayToStr(remaining.slice(0, idx));
|
|
170
|
+
remaining = remaining.slice(idx + HTTP_HEADER_LINE_END.length);
|
|
171
|
+
currentByteIdx += idx + HTTP_HEADER_LINE_END.length;
|
|
172
|
+
return line;
|
|
117
173
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Read the HTTP request from a TLS receipt transcript.
|
|
177
|
+
* @param receipt the transcript to read from or application messages if they were extracted beforehand
|
|
178
|
+
* @returns the parsed HTTP request
|
|
179
|
+
*/
|
|
180
|
+
export function getHttpRequestDataFromTranscript(receipt) {
|
|
181
|
+
const clientMsgs = receipt
|
|
182
|
+
.filter(s => s.sender === 'client');
|
|
183
|
+
// if the first message is redacted, we can't parse it
|
|
184
|
+
// as we don't know what the request was
|
|
185
|
+
if (clientMsgs[0].message[0] === REDACTION_CHAR_CODE) {
|
|
186
|
+
throw new Error('First client message request is redacted. Cannot parse');
|
|
124
187
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
188
|
+
const request = {
|
|
189
|
+
method: '',
|
|
190
|
+
url: '',
|
|
191
|
+
protocol: '',
|
|
192
|
+
headers: {}
|
|
193
|
+
};
|
|
194
|
+
let requestBuffer = concatenateUint8Arrays(clientMsgs.map(m => m.message));
|
|
195
|
+
// keep reading lines until we get to the end of the headers
|
|
196
|
+
for (let line = getLine(); typeof line !== 'undefined'; line = getLine()) {
|
|
197
|
+
if (line === '') {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (!request.method) {
|
|
201
|
+
const [, method, url, protocol] = line.match(/(\w+) (.*) (.*)/) || [];
|
|
202
|
+
request.method = method.toLowerCase();
|
|
203
|
+
request.url = url;
|
|
204
|
+
request.protocol = protocol;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
let keyIdx = line.indexOf(':');
|
|
208
|
+
if (keyIdx === -1) {
|
|
209
|
+
keyIdx = line.length - 1;
|
|
210
|
+
}
|
|
211
|
+
const key = line.slice(0, keyIdx)
|
|
212
|
+
.toLowerCase()
|
|
213
|
+
.trim();
|
|
214
|
+
const value = line.slice(keyIdx + 1)
|
|
215
|
+
.trim();
|
|
216
|
+
const oldValue = request.headers[key];
|
|
217
|
+
if (typeof oldValue === 'string') {
|
|
218
|
+
request.headers[key] = [oldValue, value];
|
|
219
|
+
}
|
|
220
|
+
else if (Array.isArray(oldValue)) {
|
|
221
|
+
oldValue.push(value);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
request.headers[key] = value;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
136
227
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return line;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
function getHttpRequestDataFromTranscript(receipt) {
|
|
144
|
-
const clientMsgs = receipt.filter((s) => s.sender === "client");
|
|
145
|
-
if (clientMsgs[0].message[0] === REDACTION_CHAR_CODE) {
|
|
146
|
-
throw new Error("First client message request is redacted. Cannot parse");
|
|
147
|
-
}
|
|
148
|
-
const request = {
|
|
149
|
-
method: "",
|
|
150
|
-
url: "",
|
|
151
|
-
protocol: "",
|
|
152
|
-
headers: {}
|
|
153
|
-
};
|
|
154
|
-
let requestBuffer = concatenateUint8Arrays(clientMsgs.map((m) => m.message));
|
|
155
|
-
for (let line = getLine(); typeof line !== "undefined"; line = getLine()) {
|
|
156
|
-
if (line === "") {
|
|
157
|
-
break;
|
|
228
|
+
//the rest is request body
|
|
229
|
+
if (requestBuffer.length) {
|
|
230
|
+
request.body = requestBuffer;
|
|
158
231
|
}
|
|
159
232
|
if (!request.method) {
|
|
160
|
-
|
|
161
|
-
request.method = method.toLowerCase();
|
|
162
|
-
request.url = url;
|
|
163
|
-
request.protocol = protocol;
|
|
164
|
-
} else {
|
|
165
|
-
let keyIdx = line.indexOf(":");
|
|
166
|
-
if (keyIdx === -1) {
|
|
167
|
-
keyIdx = line.length - 1;
|
|
168
|
-
}
|
|
169
|
-
const key = line.slice(0, keyIdx).toLowerCase().trim();
|
|
170
|
-
const value = line.slice(keyIdx + 1).trim();
|
|
171
|
-
const oldValue = request.headers[key];
|
|
172
|
-
if (typeof oldValue === "string") {
|
|
173
|
-
request.headers[key] = [oldValue, value];
|
|
174
|
-
} else if (Array.isArray(oldValue)) {
|
|
175
|
-
oldValue.push(value);
|
|
176
|
-
} else {
|
|
177
|
-
request.headers[key] = value;
|
|
178
|
-
}
|
|
233
|
+
throw new Error('Client request is incomplete');
|
|
179
234
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (idx === -1) {
|
|
191
|
-
return void 0;
|
|
235
|
+
return request;
|
|
236
|
+
function getLine() {
|
|
237
|
+
const idx = findIndexInUint8Array(requestBuffer, HTTP_HEADER_LINE_END);
|
|
238
|
+
if (idx === -1) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
const line = uint8ArrayToStr(requestBuffer.slice(0, idx));
|
|
242
|
+
requestBuffer = requestBuffer
|
|
243
|
+
.slice(idx + HTTP_HEADER_LINE_END.length);
|
|
244
|
+
return line;
|
|
192
245
|
}
|
|
193
|
-
const line = uint8ArrayToStr(requestBuffer.slice(0, idx));
|
|
194
|
-
requestBuffer = requestBuffer.slice(idx + HTTP_HEADER_LINE_END.length);
|
|
195
|
-
return line;
|
|
196
|
-
}
|
|
197
246
|
}
|
|
198
|
-
export {
|
|
199
|
-
getHttpRequestDataFromTranscript,
|
|
200
|
-
makeHttpResponseParser
|
|
201
|
-
};
|
package/lib/utils/logger.js
CHANGED
|
@@ -1,82 +1,91 @@
|
|
|
1
|
-
import { pino, stdTimeFunctions } from
|
|
2
|
-
import { getEnvVariable } from "
|
|
3
|
-
const PII_PROPERTIES = [
|
|
4
|
-
const redactedText =
|
|
5
|
-
const envLevel = getEnvVariable(
|
|
6
|
-
let logger = pino();
|
|
1
|
+
import { pino, stdTimeFunctions } from 'pino';
|
|
2
|
+
import { getEnvVariable } from "./env.js";
|
|
3
|
+
const PII_PROPERTIES = ['ownerPrivateKey', 'secretParams'];
|
|
4
|
+
const redactedText = '[REDACTED]';
|
|
5
|
+
const envLevel = getEnvVariable('LOG_LEVEL');
|
|
6
|
+
export let logger = pino();
|
|
7
7
|
makeLogger(false, envLevel);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
info: (log) => writeLog("info", log),
|
|
22
|
-
debug: (log) => writeLog("debug", log),
|
|
23
|
-
trace: (log) => writeLog("trace", log)
|
|
24
|
-
}
|
|
8
|
+
/**
|
|
9
|
+
* Creates a logger instance with optional redaction of PII.
|
|
10
|
+
* Replaces default logger
|
|
11
|
+
* See PII_PROPERTIES for the list of properties that will be redacted.
|
|
12
|
+
*
|
|
13
|
+
* @param redactPii - whether to redact PII from logs
|
|
14
|
+
* @param level - the log level to use
|
|
15
|
+
* @param onLog - a callback to call when a log is written
|
|
16
|
+
*/
|
|
17
|
+
export function makeLogger(redactPii, level, onLog) {
|
|
18
|
+
const opts = {
|
|
19
|
+
// Log human readable time stamps instead of epoch time
|
|
20
|
+
timestamp: stdTimeFunctions.isoTime,
|
|
25
21
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
22
|
+
if (redactPii) {
|
|
23
|
+
opts.formatters = { log: redact };
|
|
24
|
+
opts.serializers = { redact };
|
|
25
|
+
opts.browser = {
|
|
26
|
+
write: {
|
|
27
|
+
fatal: log => writeLog('fatal', log),
|
|
28
|
+
error: log => writeLog('error', log),
|
|
29
|
+
warn: log => writeLog('warn', log),
|
|
30
|
+
info: log => writeLog('info', log),
|
|
31
|
+
debug: log => writeLog('debug', log),
|
|
32
|
+
trace: log => writeLog('trace', log),
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const pLogger = pino(opts);
|
|
37
|
+
pLogger.level = level || 'info';
|
|
38
|
+
logger = pLogger;
|
|
39
|
+
return pLogger;
|
|
40
|
+
function writeLog(level, log) {
|
|
41
|
+
log = redact(log);
|
|
42
|
+
const { msg, ...obj } = log;
|
|
43
|
+
if (console[level]) {
|
|
44
|
+
console[level](obj, msg);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
console.log(obj, msg);
|
|
48
|
+
}
|
|
49
|
+
onLog?.(level, log);
|
|
38
50
|
}
|
|
39
|
-
onLog?.(level2, log);
|
|
40
|
-
}
|
|
41
51
|
}
|
|
42
52
|
function isObjectProperty(property) {
|
|
43
|
-
|
|
53
|
+
return (typeof property) === 'object'
|
|
54
|
+
&& !Array.isArray(property)
|
|
55
|
+
&& property !== null;
|
|
44
56
|
}
|
|
45
57
|
function getReplacer() {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
// Store references to previously visited objects
|
|
59
|
+
const references = new WeakSet();
|
|
60
|
+
return function (key, value) {
|
|
61
|
+
const isObject = (typeof value) === 'object' && value !== null;
|
|
62
|
+
if (isObject) {
|
|
63
|
+
if (references.has(value)) {
|
|
64
|
+
return '[CIRCULAR]';
|
|
65
|
+
}
|
|
66
|
+
references.add(value);
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
};
|
|
57
70
|
}
|
|
58
|
-
function redact(json) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
const redacted = JSON.parse(JSON.stringify(json, getReplacer()));
|
|
64
|
-
for (const prop in redacted) {
|
|
65
|
-
if (PII_PROPERTIES.includes(prop)) {
|
|
66
|
-
redacted[prop] = redactedText;
|
|
71
|
+
export function redact(json) {
|
|
72
|
+
const isObject = isObjectProperty(json);
|
|
73
|
+
if (!isObject && !Array.isArray(json)) {
|
|
74
|
+
return json;
|
|
67
75
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
const redacted = JSON.parse(JSON.stringify(json, getReplacer()));
|
|
77
|
+
for (const prop in redacted) {
|
|
78
|
+
if (PII_PROPERTIES.includes(prop)) {
|
|
79
|
+
redacted[prop] = redactedText;
|
|
80
|
+
}
|
|
81
|
+
if (Array.isArray(redacted[prop])) {
|
|
82
|
+
for (const [index, value] of redacted[prop].entries()) {
|
|
83
|
+
redacted[prop][index] = redact(value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else if (isObjectProperty(redacted[prop])) {
|
|
87
|
+
redacted[prop] = redact(redacted[prop]);
|
|
88
|
+
}
|
|
74
89
|
}
|
|
75
|
-
|
|
76
|
-
return redacted;
|
|
90
|
+
return redacted;
|
|
77
91
|
}
|
|
78
|
-
export {
|
|
79
|
-
logger,
|
|
80
|
-
makeLogger,
|
|
81
|
-
redact
|
|
82
|
-
};
|