@reclaimprotocol/attestor-core 5.0.1-beta.21 → 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.
Files changed (132) hide show
  1. package/browser/resources/attestor-browser.min.mjs +9 -9
  2. package/lib/avs/abis/avsDirectoryABI.js +340 -0
  3. package/lib/avs/abis/delegationABI.js +1 -0
  4. package/lib/avs/abis/registryABI.js +725 -0
  5. package/lib/avs/client/create-claim-on-avs.js +140 -0
  6. package/lib/avs/config.js +20 -0
  7. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1166 -0
  8. package/lib/avs/contracts/factories/index.js +4 -0
  9. package/lib/avs/contracts/index.js +2 -0
  10. package/lib/avs/utils/contracts.js +33 -0
  11. package/lib/avs/utils/register.js +79 -0
  12. package/lib/avs/utils/tasks.js +41 -0
  13. package/lib/client/create-claim.js +432 -0
  14. package/lib/client/index.js +3 -0
  15. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +51 -0
  16. package/lib/client/tunnels/make-rpc-tls-tunnel.js +131 -0
  17. package/lib/client/utils/attestor-pool.js +25 -0
  18. package/lib/client/utils/client-socket.js +97 -0
  19. package/lib/client/utils/message-handler.js +87 -0
  20. package/lib/config/index.js +44 -0
  21. package/lib/external-rpc/benchmark.js +69 -0
  22. package/lib/external-rpc/event-bus.js +14 -0
  23. package/lib/external-rpc/handle-incoming-msg.js +232 -0
  24. package/lib/external-rpc/index.js +3 -10399
  25. package/lib/external-rpc/jsc-polyfills/1.js +82 -0
  26. package/lib/external-rpc/jsc-polyfills/2.js +20 -0
  27. package/lib/external-rpc/jsc-polyfills/event.js +14 -0
  28. package/lib/external-rpc/jsc-polyfills/index.js +2 -0
  29. package/lib/external-rpc/jsc-polyfills/ws.js +81 -0
  30. package/lib/external-rpc/setup-browser.js +33 -0
  31. package/lib/external-rpc/setup-jsc.js +22 -0
  32. package/lib/external-rpc/types.js +1 -0
  33. package/lib/external-rpc/utils.js +100 -0
  34. package/lib/external-rpc/zk.js +63 -0
  35. package/lib/index.js +9 -8326
  36. package/lib/mechain/abis/governanceABI.js +458 -0
  37. package/lib/mechain/abis/taskABI.js +509 -0
  38. package/lib/mechain/client/create-claim-on-mechain.js +28 -0
  39. package/lib/mechain/client/index.js +1 -0
  40. package/lib/mechain/constants/index.js +3 -0
  41. package/lib/mechain/index.js +2 -0
  42. package/lib/proto/api.js +4363 -0
  43. package/lib/proto/tee-bundle.js +1316 -0
  44. package/lib/providers/http/index.js +653 -0
  45. package/lib/providers/http/patch-parse5-tree.js +32 -0
  46. package/lib/providers/http/utils.js +324 -0
  47. package/lib/providers/index.js +4 -0
  48. package/lib/server/create-server.js +103 -0
  49. package/lib/server/handlers/claimTeeBundle.js +252 -0
  50. package/lib/server/handlers/claimTunnel.js +73 -0
  51. package/lib/server/handlers/completeClaimOnChain.js +24 -0
  52. package/lib/server/handlers/createClaimOnChain.js +26 -0
  53. package/lib/server/handlers/createTaskOnMechain.js +47 -0
  54. package/lib/server/handlers/createTunnel.js +93 -0
  55. package/lib/server/handlers/disconnectTunnel.js +5 -0
  56. package/lib/server/handlers/fetchCertificateBytes.js +41 -0
  57. package/lib/server/handlers/index.js +22 -0
  58. package/lib/server/handlers/init.js +32 -0
  59. package/lib/server/handlers/toprf.js +16 -0
  60. package/lib/server/index.js +4 -0
  61. package/lib/server/socket.js +109 -0
  62. package/lib/server/tunnels/make-tcp-tunnel.js +177 -0
  63. package/lib/server/utils/apm.js +36 -0
  64. package/lib/server/utils/assert-valid-claim-request.js +325 -0
  65. package/lib/server/utils/config-env.js +4 -0
  66. package/lib/server/utils/dns.js +18 -0
  67. package/lib/server/utils/gcp-attestation.js +289 -0
  68. package/lib/server/utils/generics.d.ts +1 -1
  69. package/lib/server/utils/generics.js +51 -0
  70. package/lib/server/utils/iso.js +256 -0
  71. package/lib/server/utils/keep-alive.js +38 -0
  72. package/lib/server/utils/nitro-attestation.js +324 -0
  73. package/lib/server/utils/oprf-raw.js +54 -0
  74. package/lib/server/utils/process-handshake.js +215 -0
  75. package/lib/server/utils/proxy-session.js +6 -0
  76. package/lib/server/utils/tee-oprf-mpc-verification.js +90 -0
  77. package/lib/server/utils/tee-oprf-verification.js +174 -0
  78. package/lib/server/utils/tee-transcript-reconstruction.js +187 -0
  79. package/lib/server/utils/tee-verification.js +421 -0
  80. package/lib/server/utils/validation.js +38 -0
  81. package/lib/types/bgp.js +1 -0
  82. package/lib/types/claims.js +1 -0
  83. package/lib/types/client.js +1 -0
  84. package/lib/types/general.js +1 -0
  85. package/lib/types/handlers.js +1 -0
  86. package/lib/types/index.js +10 -0
  87. package/lib/types/providers.d.ts +3 -2
  88. package/lib/types/providers.gen.js +10 -0
  89. package/lib/types/providers.js +1 -0
  90. package/lib/types/rpc.js +1 -0
  91. package/lib/types/signatures.d.ts +1 -2
  92. package/lib/types/signatures.js +1 -0
  93. package/lib/types/tunnel.js +1 -0
  94. package/lib/types/zk.js +1 -0
  95. package/lib/utils/auth.js +59 -0
  96. package/lib/utils/b64-json.js +17 -0
  97. package/lib/utils/bgp-listener.js +119 -0
  98. package/lib/utils/claims.js +98 -0
  99. package/lib/utils/env.js +15 -0
  100. package/lib/utils/error.js +50 -0
  101. package/lib/utils/generics.js +317 -0
  102. package/lib/utils/http-parser.js +246 -0
  103. package/lib/utils/index.js +13 -0
  104. package/lib/utils/logger.js +91 -0
  105. package/lib/utils/prepare-packets.js +71 -0
  106. package/lib/utils/redactions.js +177 -0
  107. package/lib/utils/retries.js +24 -0
  108. package/lib/utils/signatures/eth.js +32 -0
  109. package/lib/utils/signatures/index.js +7 -0
  110. package/lib/utils/socket-base.js +92 -0
  111. package/lib/utils/tls.js +58 -0
  112. package/lib/utils/ws.js +22 -0
  113. package/lib/utils/zk.js +585 -0
  114. package/package.json +5 -3
  115. package/lib/scripts/check-avs-registration.d.ts +0 -1
  116. package/lib/scripts/fallbacks/crypto.d.ts +0 -1
  117. package/lib/scripts/fallbacks/empty.d.ts +0 -3
  118. package/lib/scripts/fallbacks/re2.d.ts +0 -1
  119. package/lib/scripts/fallbacks/snarkjs.d.ts +0 -1
  120. package/lib/scripts/fallbacks/stwo.d.ts +0 -6
  121. package/lib/scripts/generate-provider-types.d.ts +0 -5
  122. package/lib/scripts/generate-receipt.d.ts +0 -9
  123. package/lib/scripts/jsc-cli-rpc.d.ts +0 -1
  124. package/lib/scripts/register-avs-operator.d.ts +0 -1
  125. package/lib/scripts/start-server.d.ts +0 -1
  126. package/lib/scripts/update-avs-metadata.d.ts +0 -1
  127. package/lib/scripts/utils.d.ts +0 -1
  128. package/lib/scripts/whitelist-operator.d.ts +0 -1
  129. /package/lib/{scripts/build-browser.d.ts → avs/contracts/ReclaimServiceManager.js} +0 -0
  130. /package/lib/{scripts/build-jsc.d.ts → avs/contracts/common.js} +0 -0
  131. /package/lib/{scripts/build-lib.d.ts → avs/types/index.js} +0 -0
  132. /package/lib/{scripts/generate-toprf-keys.d.ts → mechain/types/index.js} +0 -0
@@ -0,0 +1,653 @@
1
+ import { areUint8ArraysEqual, concatenateUint8Arrays, uint8ArrayToBinaryStr } from '@reclaimprotocol/tls';
2
+ import { encodeBase64 } from 'ethers';
3
+ import { DEFAULT_HTTPS_PORT, RECLAIM_USER_AGENT } from "../../config/index.js";
4
+ import { AttestorVersion } from "../../proto/api.js";
5
+ import { buildHeaders, convertResponsePosToAbsolutePos, extractHTMLElementsIndexes, extractJSONValueIndexes, getRedactionsForChunkHeaders, makeRegex, matchRedactedStrings, parseHttpResponse, } from "./utils.js";
6
+ import { isValidProxySessionId } from "../../server/utils/proxy-session.js";
7
+ import { findIndexInUint8Array, getHttpRequestDataFromTranscript, logger, REDACTION_CHAR_CODE, strToUint8Array, uint8ArrayToStr, } from "../../utils/index.js";
8
+ const OK_HTTP_HEADER = 'HTTP/1.1 200';
9
+ const dateHeaderRegex = '[dD]ate: ((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (?:[0-3][0-9]) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?:[0-9]{4}) (?:[01][0-9]|2[0-3])(?::[0-5][0-9]){2} GMT)';
10
+ const dateDiff = 1000 * 60 * 10; // allow 10 min difference
11
+ const HTTP_PROVIDER = {
12
+ hostPort: getHostPort,
13
+ writeRedactionMode(params) {
14
+ return ('writeRedactionMode' in params)
15
+ ? params.writeRedactionMode
16
+ : undefined;
17
+ },
18
+ geoLocation(params, secretParams) {
19
+ return ('geoLocation' in params)
20
+ ? getGeoLocation(params, secretParams)
21
+ : undefined;
22
+ },
23
+ proxySessionId(params, secretParams) {
24
+ return ('proxySessionId' in params)
25
+ ? getProxySessionId(params, secretParams)
26
+ : undefined;
27
+ },
28
+ additionalClientOptions(params) {
29
+ let defaultOptions = {
30
+ applicationLayerProtocols: ['http/1.1']
31
+ };
32
+ if ('additionalClientOptions' in params) {
33
+ defaultOptions = {
34
+ ...defaultOptions,
35
+ ...params.additionalClientOptions
36
+ };
37
+ }
38
+ return defaultOptions;
39
+ },
40
+ createRequest(secretParams, params, logger) {
41
+ if (!secretParams.cookieStr &&
42
+ !secretParams.authorisationHeader &&
43
+ !secretParams.headers) {
44
+ throw new Error('auth parameters are not set');
45
+ }
46
+ const pubHeaders = params.headers || {};
47
+ const secHeaders = { ...secretParams.headers };
48
+ if (secretParams.cookieStr) {
49
+ secHeaders['Cookie'] = secretParams.cookieStr;
50
+ }
51
+ if (secretParams.authorisationHeader) {
52
+ secHeaders['Authorization'] = secretParams.authorisationHeader;
53
+ }
54
+ const hasUserAgent = Object.keys(pubHeaders)
55
+ .some(k => k.toLowerCase() === 'user-agent') ||
56
+ Object.keys(secHeaders)
57
+ .some(k => k.toLowerCase() === 'user-agent');
58
+ if (!hasUserAgent) {
59
+ //only set user-agent if not set by provider
60
+ pubHeaders['User-Agent'] = RECLAIM_USER_AGENT;
61
+ }
62
+ const newParams = substituteParamValues(params, secretParams);
63
+ params = newParams.newParams;
64
+ const url = new URL(params.url);
65
+ const { pathname } = url;
66
+ const searchParams = params.url.includes('?') ? params.url.split('?')[1] : '';
67
+ logger.info({ url: params.url, path: pathname, query: searchParams.toString() });
68
+ const body = params.body instanceof Uint8Array
69
+ ? params.body
70
+ : strToUint8Array(params.body || '');
71
+ const contentLength = body.length;
72
+ const reqLine = `${params.method} ${pathname}${searchParams?.length ? '?' + searchParams : ''} HTTP/1.1`;
73
+ const secHeadersList = buildHeaders(secHeaders);
74
+ logger.info({ requestLine: reqLine });
75
+ const httpReqHeaderStr = [
76
+ reqLine,
77
+ `Host: ${getHostHeaderString(url)}`,
78
+ `Content-Length: ${contentLength}`,
79
+ 'Connection: close',
80
+ //no compression
81
+ 'Accept-Encoding: identity',
82
+ ...buildHeaders(pubHeaders),
83
+ ...secHeadersList,
84
+ '\r\n',
85
+ ].join('\r\n');
86
+ const headerStr = strToUint8Array(httpReqHeaderStr);
87
+ const data = concatenateUint8Arrays([headerStr, body]);
88
+ // hide all secret headers
89
+ const secHeadersStr = secHeadersList.join('\r\n');
90
+ const tokenStartIndex = findIndexInUint8Array(data, strToUint8Array(secHeadersStr));
91
+ const redactions = [
92
+ {
93
+ fromIndex: tokenStartIndex,
94
+ toIndex: tokenStartIndex + secHeadersStr.length,
95
+ }
96
+ ];
97
+ if (newParams.hiddenBodyParts?.length > 0) {
98
+ for (const hiddenBodyPart of newParams.hiddenBodyParts) {
99
+ if (hiddenBodyPart.length) {
100
+ redactions.push({
101
+ fromIndex: headerStr.length + hiddenBodyPart.index,
102
+ toIndex: headerStr.length + hiddenBodyPart.index + hiddenBodyPart.length,
103
+ });
104
+ }
105
+ }
106
+ }
107
+ if (newParams.hiddenURLParts?.length > 0) {
108
+ for (const hiddenURLPart of newParams.hiddenURLParts) {
109
+ if (hiddenURLPart.length) {
110
+ redactions.push({
111
+ fromIndex: hiddenURLPart.index,
112
+ toIndex: hiddenURLPart.index + hiddenURLPart.length,
113
+ });
114
+ }
115
+ }
116
+ }
117
+ redactions.sort((a, b) => a.toIndex - b.toIndex);
118
+ return {
119
+ data,
120
+ redactions: redactions,
121
+ };
122
+ },
123
+ getResponseRedactions({ response, params: rawParams, logger, ctx }) {
124
+ logger.debug({ response: encodeBase64(response), params: rawParams });
125
+ const res = parseHttpResponse(response);
126
+ if (!rawParams.responseRedactions?.length) {
127
+ return [];
128
+ }
129
+ if (((res.statusCode / 100) >> 0) !== 2) {
130
+ logger.error({ response: encodeBase64(response), params: rawParams });
131
+ throw new Error(`Expected status 2xx, got ${res.statusCode} (${res.statusMessage})`);
132
+ }
133
+ const newParams = substituteParamValues(rawParams, undefined, true);
134
+ const params = newParams.newParams;
135
+ const headerEndIndex = res.statusLineEndIndex;
136
+ const bodyStartIdx = res.bodyStartIndex ?? 0;
137
+ if (bodyStartIdx < 4) {
138
+ logger.error({ response: encodeBase64(response) });
139
+ throw new Error('Failed to find response body');
140
+ }
141
+ const reveals = [
142
+ { fromIndex: 0, toIndex: headerEndIndex }
143
+ ];
144
+ //reveal double CRLF which separates headers from body
145
+ if (shouldRevealCrlf(ctx)) {
146
+ const crlfs = response
147
+ .slice(res.headerEndIdx, res.headerEndIdx + 4);
148
+ if (!areUint8ArraysEqual(crlfs, strToUint8Array('\r\n\r\n'))) {
149
+ logger.error({ response: encodeBase64(response) });
150
+ throw new Error(`Failed to find header/body separator at index ${res.headerEndIdx}`);
151
+ }
152
+ }
153
+ reveals.push({ fromIndex: res.headerEndIdx, toIndex: res.headerEndIdx + 4 });
154
+ //reveal date header
155
+ if (res.headerIndices['date']) {
156
+ reveals.push(res.headerIndices['date']);
157
+ }
158
+ const body = uint8ArrayToBinaryStr(res.body);
159
+ const redactions = [];
160
+ for (const rs of params.responseRedactions || []) {
161
+ const processor = processRedactionRequest(body, rs, bodyStartIdx, res.chunks);
162
+ for (const { reveal, redactions: reds } of processor) {
163
+ reveals.push(reveal);
164
+ redactions.push(...reds);
165
+ }
166
+ }
167
+ reveals.sort((a, b) => a.toIndex - b.toIndex);
168
+ if (reveals.length > 1) {
169
+ let currentIndex = 0;
170
+ for (const r of reveals) {
171
+ if (currentIndex < r.fromIndex) {
172
+ redactions.push({ fromIndex: currentIndex, toIndex: r.fromIndex });
173
+ }
174
+ currentIndex = r.toIndex;
175
+ }
176
+ redactions.push({ fromIndex: currentIndex, toIndex: response.length });
177
+ }
178
+ for (const r of reveals) {
179
+ if (!r.hash) {
180
+ continue;
181
+ }
182
+ redactions.push(r);
183
+ }
184
+ redactions.sort((a, b) => a.toIndex - b.toIndex);
185
+ return redactions;
186
+ },
187
+ assertValidProviderReceipt({ receipt, params: paramsAny, logger, ctx }) {
188
+ logTranscript();
189
+ let extractedParams = {};
190
+ const secretParams = ('secretParams' in paramsAny)
191
+ ? paramsAny.secretParams
192
+ : undefined;
193
+ const newParams = substituteParamValues(paramsAny, secretParams, !secretParams);
194
+ const params = newParams.newParams;
195
+ extractedParams = { ...extractedParams, ...newParams.extractedValues };
196
+ const req = getHttpRequestDataFromTranscript(receipt);
197
+ if (req.method !== params.method.toLowerCase()) {
198
+ throw new Error(`Invalid method: ${req.method}`);
199
+ }
200
+ const url = new URL(params.url);
201
+ const { protocol, pathname } = url;
202
+ if (protocol !== 'https:') {
203
+ logger.error('params URL: %s', params.url);
204
+ throw new Error(`Expected protocol: https, found: ${protocol}`);
205
+ }
206
+ const searchParams = params.url.includes('?') ? params.url.split('?')[1] : '';
207
+ //brackets in URL path turn into %7B and %7D, so replace them back
208
+ const expectedPath = pathname.replaceAll('%7B', '{').replaceAll('%7D', '}') + (searchParams?.length ? '?' + searchParams : '');
209
+ if (!matchRedactedStrings(strToUint8Array(expectedPath), strToUint8Array(req.url))) {
210
+ logger.error('params URL: %s', params.url);
211
+ throw new Error(`Expected path: ${expectedPath}, found: ${req.url}`);
212
+ }
213
+ const expectedHostStr = getHostHeaderString(url);
214
+ if (req.headers.host !== expectedHostStr) {
215
+ throw new Error(`Expected host: ${expectedHostStr}, found: ${req.headers.host}`);
216
+ }
217
+ const connectionHeader = req.headers['connection'];
218
+ if (connectionHeader !== 'close') {
219
+ throw new Error(`Connection header must be "close", got "${connectionHeader}"`);
220
+ }
221
+ const serverBlocks = receipt
222
+ .filter(s => s.sender === 'server')
223
+ .map((r) => r.message)
224
+ // filter out fully redacted blocks
225
+ .filter(b => !b.every(b => b === REDACTION_CHAR_CODE));
226
+ const response = concatArrays(...serverBlocks);
227
+ let res;
228
+ res = uint8ArrayToStr(response);
229
+ const okRegex = makeRegex('^HTTP\\/1.1 2\\d{2}');
230
+ const matchRes = okRegex.exec(res);
231
+ if (!matchRes) {
232
+ const statusRegex = makeRegex('^HTTP\\/1.1 (\\d{3})');
233
+ const matchRes = statusRegex.exec(res);
234
+ if (matchRes && matchRes.length > 1) {
235
+ throw new Error(`Provider returned error ${matchRes[1]}`);
236
+ }
237
+ let lineEnd = res.indexOf('*');
238
+ if (lineEnd === -1) {
239
+ lineEnd = res.indexOf('\n');
240
+ }
241
+ if (lineEnd === -1) {
242
+ lineEnd = OK_HTTP_HEADER.length;
243
+ }
244
+ throw new Error(`Response did not start with \"HTTP/1.1 2XX\" got "${res.slice(0, lineEnd)}"`);
245
+ }
246
+ let bodyStart;
247
+ if (shouldRevealCrlf(ctx)) {
248
+ bodyStart = res.indexOf('\r\n\r\n', OK_HTTP_HEADER.length) + 4;
249
+ if (bodyStart < 4) {
250
+ throw new Error('Response body start not found');
251
+ }
252
+ }
253
+ else {
254
+ bodyStart = OK_HTTP_HEADER.length;
255
+ }
256
+ //validate server Date header if present
257
+ const dateHeader = makeRegex(dateHeaderRegex).exec(res);
258
+ if (dateHeader && dateHeader.length > 1) {
259
+ const serverDate = new Date(dateHeader[1]);
260
+ if ((Date.now() - serverDate.getTime()) > dateDiff) {
261
+ logger.info({ dateHeader: dateHeader[0], current: Date.now() }, 'date header is off');
262
+ // too many false positives
263
+ // throw new Error(
264
+ // `Server date is off by "${(Date.now() - serverDate.getTime()) / 1000} s"`
265
+ // )
266
+ }
267
+ }
268
+ const paramBody = params.body instanceof Uint8Array
269
+ ? params.body
270
+ : strToUint8Array(params.body || '');
271
+ if (paramBody.length > 0 && !matchRedactedStrings(paramBody, req.body)) {
272
+ throw new Error('request body mismatch');
273
+ }
274
+ //remove asterisks to account for chunks in the middle of revealed strings
275
+ if (!secretParams) {
276
+ res = res.slice(bodyStart).replace(/(\*){3,}/g, '');
277
+ }
278
+ for (const { type, value, invert } of params.responseMatches || []) {
279
+ const inv = Boolean(invert); // explicitly cast to boolean
280
+ switch (type) {
281
+ case 'regex':
282
+ const regexRes = makeRegex(value).exec(res);
283
+ const match = regexRes !== null;
284
+ if (match === inv) { // if both true or both false then fail
285
+ throw new Error('Invalid receipt.'
286
+ + ` Regex "${value}" ${invert ? 'matched' : 'didn\'t match'}`);
287
+ }
288
+ if (!match) {
289
+ continue;
290
+ }
291
+ const groups = regexRes?.groups;
292
+ for (const paramName in groups || []) {
293
+ if (paramName in extractedParams) {
294
+ throw new Error(`Duplicate parameter ${paramName}`);
295
+ }
296
+ const value = groups?.[paramName];
297
+ if (typeof value !== 'string') {
298
+ continue;
299
+ }
300
+ extractedParams[paramName] = value;
301
+ }
302
+ break;
303
+ case 'contains':
304
+ const includes = res.includes(value);
305
+ if (includes === inv) {
306
+ throw new Error(`Invalid receipt. Response ${invert ? 'contains' : 'does not contain'} "${value}"`);
307
+ }
308
+ break;
309
+ default:
310
+ throw new Error(`Invalid response match type ${type}`);
311
+ }
312
+ }
313
+ function concatArrays(...bufs) {
314
+ const totalSize = bufs.reduce((acc, e) => acc + e.length, 0);
315
+ const merged = new Uint8Array(totalSize);
316
+ let lenDone = 0;
317
+ for (const array of bufs) {
318
+ merged.set(array, lenDone);
319
+ lenDone += array.length;
320
+ }
321
+ return merged;
322
+ }
323
+ return { extractedParameters: extractedParams };
324
+ function logTranscript() {
325
+ const clientMsgs = receipt.filter(s => s.sender === 'client').map(m => m.message);
326
+ const serverMsgs = receipt.filter(s => s.sender === 'server').map(m => m.message);
327
+ const clientTranscript = encodeBase64(concatenateUint8Arrays(clientMsgs));
328
+ const serverTranscript = encodeBase64(concatenateUint8Arrays(serverMsgs));
329
+ logger.debug({ request: clientTranscript, response: serverTranscript, params: paramsAny });
330
+ }
331
+ },
332
+ };
333
+ // revealing CRLF is a breaking change -- and should only be done
334
+ // if the client's version supports it
335
+ function shouldRevealCrlf({ version }) {
336
+ return version >= AttestorVersion.ATTESTOR_VERSION_2_0_1;
337
+ }
338
+ function getHostPort(params, secretParams) {
339
+ const { host } = new URL(getURL(params, secretParams));
340
+ if (!host) {
341
+ throw new Error('url is incorrect');
342
+ }
343
+ return host;
344
+ }
345
+ /**
346
+ * Obtain the host header string from the URL.
347
+ * https://stackoverflow.com/a/3364396
348
+ */
349
+ function getHostHeaderString(url) {
350
+ const host = url.hostname;
351
+ const port = url.port;
352
+ return port && +port !== DEFAULT_HTTPS_PORT
353
+ ? `${host}:${port}`
354
+ : host;
355
+ }
356
+ const paramsRegex = /{{([^{}]+)}}/sgi;
357
+ function* processRedactionRequest(body, rs, bodyStartIdx, resChunks) {
358
+ let element = body;
359
+ let elementIdx = 0;
360
+ let elementLength = -1;
361
+ if (rs.xPath) {
362
+ const indexes = extractHTMLElementsIndexes(body, rs.xPath, !!rs.jsonPath);
363
+ for (const { start, end } of indexes) {
364
+ element = body.slice(start, end);
365
+ elementIdx = start;
366
+ elementLength = end - start;
367
+ if (rs.jsonPath) {
368
+ yield* processJsonPath();
369
+ }
370
+ else if (rs.regex) {
371
+ yield* processRegexp();
372
+ }
373
+ else {
374
+ yield* addRedaction();
375
+ }
376
+ }
377
+ }
378
+ else if (rs.jsonPath) {
379
+ yield* processJsonPath();
380
+ }
381
+ else if (rs.regex) {
382
+ yield* processRegexp();
383
+ }
384
+ else {
385
+ throw new Error('Expected either xPath, jsonPath or regex for redaction');
386
+ }
387
+ function* processJsonPath() {
388
+ const jsonPathIndexes = extractJSONValueIndexes(element, rs.jsonPath);
389
+ const eIndex = elementIdx;
390
+ for (const ji of jsonPathIndexes) {
391
+ const jStart = ji.start;
392
+ const jEnd = ji.end;
393
+ element = body.slice(eIndex + jStart, eIndex + jEnd);
394
+ elementIdx = eIndex + jStart;
395
+ elementLength = jEnd - jStart;
396
+ if (rs.regex) {
397
+ yield* processRegexp();
398
+ }
399
+ else {
400
+ yield* addRedaction();
401
+ }
402
+ }
403
+ }
404
+ function* processRegexp() {
405
+ logger.debug({
406
+ element: encodeBase64(strToUint8Array(element)),
407
+ body: encodeBase64(strToUint8Array(body))
408
+ });
409
+ const regexp = makeRegex(rs.regex);
410
+ const elem = element || body;
411
+ const match = regexp.exec(elem);
412
+ if (!match?.[0]) {
413
+ throw new Error(`regexp ${rs.regex} does not match found element '${encodeBase64(strToUint8Array(elem))}'`);
414
+ }
415
+ elementIdx += match.index;
416
+ elementLength = regexp.lastIndex - match.index;
417
+ element = match[0];
418
+ if (rs.hash && (!match.groups || Object.keys(match.groups).length > 1)) {
419
+ throw new Error('Exactly one named capture group is needed per hashed redaction');
420
+ }
421
+ // if there are groups in the regex,
422
+ // we'll only hash the group values
423
+ if (!rs.hash || !match.groups) {
424
+ yield* addRedaction();
425
+ return;
426
+ }
427
+ const fullStr = match[0];
428
+ const grp = Object.values(match.groups)[0];
429
+ const grpIdx = fullStr.indexOf(grp);
430
+ // don't hash the entire regex, we'll hash the group values
431
+ elementLength = grpIdx;
432
+ element = fullStr.slice(0, grpIdx);
433
+ yield* addRedaction(null);
434
+ elementIdx += grpIdx;
435
+ element = grp;
436
+ elementLength = grp.length;
437
+ const reveal = getReveal(elementIdx, elementLength, rs.hash);
438
+ const chunkReds = getRedactionsForChunkHeaders(reveal.fromIndex, reveal.toIndex, resChunks);
439
+ if (chunkReds.length) {
440
+ throw new Error('Hash redactions cannot be performed if '
441
+ + 'the redacted string is split between 2'
442
+ + ' or more HTTP chunks');
443
+ }
444
+ yield { reveal, redactions: chunkReds };
445
+ elementIdx += grp.length;
446
+ element = fullStr.slice(grpIdx + grp.length);
447
+ elementLength = element.length;
448
+ yield* addRedaction(null);
449
+ }
450
+ function* addRedaction(hash = rs.hash, _resChunks = resChunks) {
451
+ if (elementIdx < 0 || !elementLength) {
452
+ return;
453
+ }
454
+ const reveal = getReveal(elementIdx, elementLength, hash || undefined);
455
+ yield {
456
+ reveal,
457
+ redactions: getRedactionsForChunkHeaders(reveal.fromIndex, reveal.toIndex, _resChunks)
458
+ };
459
+ }
460
+ function getReveal(startIdx, len, hash) {
461
+ const from = convertResponsePosToAbsolutePos(startIdx, bodyStartIdx, resChunks);
462
+ const to = convertResponsePosToAbsolutePos(startIdx + len, bodyStartIdx, resChunks);
463
+ return { fromIndex: from, toIndex: to, hash };
464
+ }
465
+ }
466
+ export function substituteParamValues(currentParams, secretParams, ignoreMissingParams) {
467
+ const params = JSON.parse(JSON.stringify(currentParams));
468
+ let extractedValues = {};
469
+ const hiddenURLParts = [];
470
+ const urlParams = extractAndReplaceTemplateValues(params.url, ignoreMissingParams);
471
+ if (urlParams) {
472
+ params.url = urlParams.newParam;
473
+ extractedValues = { ...urlParams.extractedValues };
474
+ if (urlParams.hiddenParts.length) {
475
+ const host = getHostHeaderString(new URL(params.url));
476
+ const offset = `https://${host}`.length - currentParams.method.length - 1; //space between method and start of the path
477
+ for (const hiddenURLPart of urlParams.hiddenParts) {
478
+ hiddenURLParts.push({ index: hiddenURLPart.index - offset, length: hiddenURLPart.length });
479
+ }
480
+ }
481
+ }
482
+ let bodyParams;
483
+ let hiddenBodyParts = [];
484
+ if (params.body) {
485
+ const strBody = typeof params.body === 'string' ? params.body : uint8ArrayToStr(params.body);
486
+ bodyParams = extractAndReplaceTemplateValues(strBody, ignoreMissingParams);
487
+ if (bodyParams) {
488
+ params.body = bodyParams.newParam;
489
+ extractedValues = { ...extractedValues, ...bodyParams.extractedValues };
490
+ hiddenBodyParts = bodyParams.hiddenParts;
491
+ }
492
+ }
493
+ const geoParams = extractAndReplaceTemplateValues(params.geoLocation);
494
+ if (geoParams) {
495
+ params.geoLocation = geoParams.newParam;
496
+ extractedValues = { ...extractedValues, ...geoParams.extractedValues };
497
+ }
498
+ const proxySessionIdParams = extractAndReplaceTemplateValues(params.proxySessionId);
499
+ if (proxySessionIdParams) {
500
+ params.proxySessionId = proxySessionIdParams.newParam;
501
+ extractedValues = { ...extractedValues, ...proxySessionIdParams.extractedValues };
502
+ }
503
+ if (params.responseRedactions) {
504
+ for (const r of params.responseRedactions) {
505
+ if (r.regex) {
506
+ const regexParams = extractAndReplaceTemplateValues(r.regex);
507
+ r.regex = regexParams?.newParam;
508
+ }
509
+ if (r.xPath) {
510
+ const xpathParams = extractAndReplaceTemplateValues(r.xPath);
511
+ r.xPath = xpathParams?.newParam;
512
+ }
513
+ if (r.jsonPath) {
514
+ const jsonPathParams = extractAndReplaceTemplateValues(r.jsonPath);
515
+ r.jsonPath = jsonPathParams?.newParam;
516
+ }
517
+ }
518
+ }
519
+ if (params.responseMatches) {
520
+ for (const r of params.responseMatches) {
521
+ if (r.value !== '') {
522
+ const matchParam = extractAndReplaceTemplateValues(r.value);
523
+ r.value = matchParam?.newParam;
524
+ extractedValues = { ...extractedValues, ...matchParam?.extractedValues };
525
+ }
526
+ }
527
+ }
528
+ return {
529
+ newParams: params,
530
+ extractedValues: extractedValues,
531
+ hiddenBodyParts: hiddenBodyParts,
532
+ hiddenURLParts: hiddenURLParts
533
+ };
534
+ function extractAndReplaceTemplateValues(param, ignoreMissingParams) {
535
+ if (!param) {
536
+ return null;
537
+ }
538
+ //const paramNames: Set<string> = new Set()
539
+ const extractedValues = {};
540
+ const hiddenParts = [];
541
+ let totalOffset = 0;
542
+ param = param.replace(paramsRegex, (match, pn, offset) => {
543
+ if (params.paramValues && pn in params.paramValues) {
544
+ extractedValues[pn] = params.paramValues[pn];
545
+ totalOffset += params.paramValues[pn].length - match.length;
546
+ return params.paramValues[pn];
547
+ }
548
+ else if (secretParams) {
549
+ if (secretParams?.paramValues && pn in secretParams?.paramValues) {
550
+ hiddenParts.push({
551
+ index: offset + totalOffset,
552
+ length: secretParams.paramValues[pn].length,
553
+ });
554
+ totalOffset += secretParams.paramValues[pn].length - match.length;
555
+ return secretParams.paramValues[pn];
556
+ }
557
+ else {
558
+ throw new Error(`parameter's "${pn}" value not found in paramValues and secret parameter's paramValues`);
559
+ }
560
+ }
561
+ else {
562
+ if (!(!!ignoreMissingParams)) {
563
+ throw new Error(`parameter's "${pn}" value not found in paramValues`);
564
+ }
565
+ else {
566
+ return match;
567
+ }
568
+ }
569
+ });
570
+ return {
571
+ newParam: param,
572
+ extractedValues: extractedValues,
573
+ hiddenParts: hiddenParts
574
+ };
575
+ }
576
+ }
577
+ function getGeoLocation(v2Params, secretParams) {
578
+ if (v2Params?.geoLocation) {
579
+ const paramNames = new Set();
580
+ let geo = v2Params.geoLocation;
581
+ //extract param names
582
+ let match = null;
583
+ while (match = paramsRegex.exec(geo)) {
584
+ paramNames.add(match[1]);
585
+ }
586
+ for (const pn of paramNames) {
587
+ if (v2Params.paramValues && pn in v2Params.paramValues) {
588
+ geo = geo?.replaceAll(`{{${pn}}}`, v2Params.paramValues[pn].toString());
589
+ }
590
+ else if (secretParams?.paramValues && pn in secretParams.paramValues) {
591
+ geo = geo?.replaceAll(`{{${pn}}}`, secretParams.paramValues[pn].toString());
592
+ }
593
+ else {
594
+ throw new Error(`parameter "${pn}" value not found in templateParams`);
595
+ }
596
+ }
597
+ const geoRegex = /^[A-Za-z]{2}$/sgiu;
598
+ if (!geoRegex.test(geo)) {
599
+ throw new Error(`Geolocation ${geo} is invalid`);
600
+ }
601
+ return geo;
602
+ }
603
+ return undefined;
604
+ }
605
+ function getProxySessionId(v2Params, secretParams) {
606
+ if (v2Params?.proxySessionId) {
607
+ const paramNames = new Set();
608
+ let proxySessionIdValue = v2Params.proxySessionId;
609
+ //extract param names
610
+ let match = null;
611
+ while (match = paramsRegex.exec(proxySessionIdValue)) {
612
+ paramNames.add(match[1]);
613
+ }
614
+ for (const pn of paramNames) {
615
+ if (v2Params.paramValues && pn in v2Params.paramValues) {
616
+ proxySessionIdValue = proxySessionIdValue?.replaceAll(`{{${pn}}}`, v2Params.paramValues[pn].toString());
617
+ }
618
+ else if (secretParams?.paramValues && pn in secretParams.paramValues) {
619
+ proxySessionIdValue = proxySessionIdValue?.replaceAll(`{{${pn}}}`, secretParams.paramValues[pn].toString());
620
+ }
621
+ else {
622
+ throw new Error(`parameter "${pn}" value not found in templateParams`);
623
+ }
624
+ }
625
+ if (!isValidProxySessionId(proxySessionIdValue)) {
626
+ throw new Error(`proxySessionId ${proxySessionIdValue} is invalid`);
627
+ }
628
+ return proxySessionIdValue;
629
+ }
630
+ return undefined;
631
+ }
632
+ function getURL(v2Params, secretParams) {
633
+ let hostPort = v2Params?.url;
634
+ const paramNames = new Set();
635
+ //extract param names
636
+ let match = null;
637
+ while (match = paramsRegex.exec(hostPort)) {
638
+ paramNames.add(match[1]);
639
+ }
640
+ for (const pn of paramNames) {
641
+ if (v2Params.paramValues && pn in v2Params.paramValues) {
642
+ hostPort = hostPort?.replaceAll(`{{${pn}}}`, v2Params.paramValues[pn].toString());
643
+ }
644
+ else if (secretParams?.paramValues && pn in secretParams.paramValues) {
645
+ hostPort = hostPort?.replaceAll(`{{${pn}}}`, secretParams.paramValues[pn].toString());
646
+ }
647
+ else {
648
+ throw new Error(`parameter "${pn}" value not found in templateParams`);
649
+ }
650
+ }
651
+ return hostPort;
652
+ }
653
+ export default HTTP_PROVIDER;
@@ -0,0 +1,32 @@
1
+ // these patches are required to make "xpath" work with parse5
2
+ import { Element, Node } from 'domhandler';
3
+ Element.prototype.toString = function () {
4
+ throw new Error('Element.toString() is not supported');
5
+ // return ds(this)
6
+ };
7
+ Object.defineProperty(Node.prototype, 'nodeName', {
8
+ get: function () {
9
+ return this.name;
10
+ },
11
+ });
12
+ Object.defineProperty(Node.prototype, 'localName', {
13
+ get: function () {
14
+ return this.name;
15
+ },
16
+ });
17
+ const origAttributes = Object.getOwnPropertyDescriptor(Element.prototype, 'attributes')?.get;
18
+ if (origAttributes) {
19
+ Object.defineProperty(Element.prototype, 'attributes', {
20
+ get: function (...args) {
21
+ const attrs = origAttributes.call(this, ...args);
22
+ attrs.item = (idx) => {
23
+ const el = attrs[idx];
24
+ return { ...el, nodeType: 2, localName: el.name };
25
+ };
26
+ return attrs;
27
+ },
28
+ });
29
+ }
30
+ else {
31
+ console.warn('[WARN] Unable to patch DOM: Element.attributes property descriptor not found');
32
+ }