@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.
Files changed (145) hide show
  1. package/browser/resources/attestor-browser.min.mjs +4512 -0
  2. package/lib/avs/abis/avsDirectoryABI.js +338 -341
  3. package/lib/avs/abis/delegationABI.js +1 -4
  4. package/lib/avs/abis/registryABI.js +719 -722
  5. package/lib/avs/client/create-claim-on-avs.js +129 -157
  6. package/lib/avs/config.js +18 -24
  7. package/lib/avs/contracts/ReclaimServiceManager.js +1 -0
  8. package/lib/avs/contracts/common.js +1 -0
  9. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1139 -1156
  10. package/lib/avs/contracts/factories/index.js +4 -4
  11. package/lib/avs/contracts/index.js +2 -6
  12. package/lib/avs/types/index.js +1 -0
  13. package/lib/avs/utils/contracts.js +30 -50
  14. package/lib/avs/utils/register.js +75 -70
  15. package/lib/avs/utils/tasks.js +38 -45
  16. package/lib/client/create-claim.js +402 -431
  17. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +46 -48
  18. package/lib/client/tunnels/make-rpc-tls-tunnel.js +125 -121
  19. package/lib/client/utils/attestor-pool.js +23 -22
  20. package/lib/client/utils/client-socket.js +86 -109
  21. package/lib/client/utils/message-handler.js +79 -89
  22. package/lib/config/index.js +40 -58
  23. package/lib/external-rpc/benchmark.js +61 -74
  24. package/lib/external-rpc/event-bus.js +12 -15
  25. package/lib/external-rpc/handle-incoming-msg.js +216 -225
  26. package/lib/external-rpc/jsc-polyfills/1.js +70 -68
  27. package/lib/external-rpc/jsc-polyfills/2.js +17 -12
  28. package/lib/external-rpc/jsc-polyfills/event.js +10 -15
  29. package/lib/external-rpc/jsc-polyfills/index.js +2 -2
  30. package/lib/external-rpc/jsc-polyfills/ws.js +77 -79
  31. package/lib/external-rpc/setup-browser.js +28 -28
  32. package/lib/external-rpc/setup-jsc.js +17 -17
  33. package/lib/external-rpc/types.js +1 -0
  34. package/lib/external-rpc/utils.js +89 -89
  35. package/lib/external-rpc/zk.js +55 -50
  36. package/lib/index.js +2 -6
  37. package/lib/mechain/abis/governanceABI.js +457 -460
  38. package/lib/mechain/abis/taskABI.js +502 -505
  39. package/lib/mechain/client/create-claim-on-mechain.js +24 -29
  40. package/lib/mechain/constants/index.js +3 -8
  41. package/lib/mechain/types/index.js +1 -0
  42. package/lib/proto/api.js +4200 -4087
  43. package/lib/proto/tee-bundle.js +1261 -1241
  44. package/lib/providers/http/index.js +616 -603
  45. package/lib/providers/http/patch-parse5-tree.js +27 -29
  46. package/lib/providers/http/utils.js +289 -248
  47. package/lib/providers/index.js +3 -6
  48. package/lib/server/create-server.js +89 -91
  49. package/lib/server/handlers/claimTeeBundle.js +231 -211
  50. package/lib/server/handlers/claimTunnel.js +66 -73
  51. package/lib/server/handlers/completeClaimOnChain.js +20 -25
  52. package/lib/server/handlers/createClaimOnChain.js +21 -27
  53. package/lib/server/handlers/createTaskOnMechain.js +40 -50
  54. package/lib/server/handlers/createTunnel.js +85 -90
  55. package/lib/server/handlers/disconnectTunnel.js +4 -7
  56. package/lib/server/handlers/fetchCertificateBytes.js +37 -53
  57. package/lib/server/handlers/index.js +21 -24
  58. package/lib/server/handlers/init.js +27 -28
  59. package/lib/server/handlers/toprf.js +13 -16
  60. package/lib/server/socket.js +97 -100
  61. package/lib/server/tunnels/make-tcp-tunnel.js +161 -186
  62. package/lib/server/utils/apm.js +32 -25
  63. package/lib/server/utils/assert-valid-claim-request.js +305 -334
  64. package/lib/server/utils/config-env.js +2 -2
  65. package/lib/server/utils/dns.js +12 -18
  66. package/lib/server/utils/gcp-attestation.js +233 -181
  67. package/lib/server/utils/generics.d.ts +1 -1
  68. package/lib/server/utils/generics.js +43 -37
  69. package/lib/server/utils/iso.js +253 -256
  70. package/lib/server/utils/keep-alive.js +36 -36
  71. package/lib/server/utils/nitro-attestation.js +295 -220
  72. package/lib/server/utils/oprf-raw.js +48 -55
  73. package/lib/server/utils/process-handshake.js +200 -218
  74. package/lib/server/utils/proxy-session.js +5 -5
  75. package/lib/server/utils/tee-oprf-mpc-verification.js +82 -78
  76. package/lib/server/utils/tee-oprf-verification.js +165 -142
  77. package/lib/server/utils/tee-transcript-reconstruction.js +176 -129
  78. package/lib/server/utils/tee-verification.js +397 -334
  79. package/lib/server/utils/validation.js +30 -37
  80. package/lib/types/bgp.js +1 -0
  81. package/lib/types/claims.js +1 -0
  82. package/lib/types/client.js +1 -0
  83. package/lib/types/general.js +1 -0
  84. package/lib/types/handlers.js +1 -0
  85. package/lib/types/providers.d.ts +3 -2
  86. package/lib/types/providers.gen.js +9 -15
  87. package/lib/types/providers.js +1 -0
  88. package/lib/types/rpc.js +1 -0
  89. package/lib/types/signatures.d.ts +1 -2
  90. package/lib/types/signatures.js +1 -0
  91. package/lib/types/tunnel.js +1 -0
  92. package/lib/types/zk.js +1 -0
  93. package/lib/utils/auth.js +54 -66
  94. package/lib/utils/b64-json.js +15 -15
  95. package/lib/utils/bgp-listener.js +107 -111
  96. package/lib/utils/claims.js +89 -80
  97. package/lib/utils/env.js +13 -17
  98. package/lib/utils/error.js +43 -47
  99. package/lib/utils/generics.js +284 -235
  100. package/lib/utils/http-parser.js +232 -187
  101. package/lib/utils/logger.js +80 -71
  102. package/lib/utils/prepare-packets.js +69 -67
  103. package/lib/utils/redactions.js +163 -121
  104. package/lib/utils/retries.js +22 -24
  105. package/lib/utils/signatures/eth.js +29 -28
  106. package/lib/utils/signatures/index.js +5 -10
  107. package/lib/utils/socket-base.js +84 -88
  108. package/lib/utils/tls.js +28 -28
  109. package/lib/utils/ws.js +19 -19
  110. package/lib/utils/zk.js +542 -582
  111. package/package.json +12 -5
  112. package/lib/external-rpc/global.d.js +0 -0
  113. package/lib/scripts/build-browser.d.ts +0 -1
  114. package/lib/scripts/build-jsc.d.ts +0 -1
  115. package/lib/scripts/build-lib.d.ts +0 -1
  116. package/lib/scripts/check-avs-registration.d.ts +0 -1
  117. package/lib/scripts/check-avs-registration.js +0 -28
  118. package/lib/scripts/fallbacks/crypto.d.ts +0 -1
  119. package/lib/scripts/fallbacks/crypto.js +0 -4
  120. package/lib/scripts/fallbacks/empty.d.ts +0 -3
  121. package/lib/scripts/fallbacks/empty.js +0 -4
  122. package/lib/scripts/fallbacks/re2.d.ts +0 -1
  123. package/lib/scripts/fallbacks/re2.js +0 -7
  124. package/lib/scripts/fallbacks/snarkjs.d.ts +0 -1
  125. package/lib/scripts/fallbacks/snarkjs.js +0 -10
  126. package/lib/scripts/fallbacks/stwo.d.ts +0 -6
  127. package/lib/scripts/fallbacks/stwo.js +0 -159
  128. package/lib/scripts/generate-provider-types.d.ts +0 -5
  129. package/lib/scripts/generate-provider-types.js +0 -101
  130. package/lib/scripts/generate-receipt.d.ts +0 -9
  131. package/lib/scripts/generate-receipt.js +0 -101
  132. package/lib/scripts/generate-toprf-keys.d.ts +0 -1
  133. package/lib/scripts/generate-toprf-keys.js +0 -24
  134. package/lib/scripts/jsc-cli-rpc.d.ts +0 -1
  135. package/lib/scripts/jsc-cli-rpc.js +0 -35
  136. package/lib/scripts/register-avs-operator.d.ts +0 -1
  137. package/lib/scripts/register-avs-operator.js +0 -3
  138. package/lib/scripts/start-server.d.ts +0 -1
  139. package/lib/scripts/start-server.js +0 -11
  140. package/lib/scripts/update-avs-metadata.d.ts +0 -1
  141. package/lib/scripts/update-avs-metadata.js +0 -20
  142. package/lib/scripts/utils.d.ts +0 -1
  143. package/lib/scripts/utils.js +0 -10
  144. package/lib/scripts/whitelist-operator.d.ts +0 -1
  145. package/lib/scripts/whitelist-operator.js +0 -16
@@ -1,34 +1,32 @@
1
- import { Element, Node } from "domhandler";
2
- Element.prototype.toString = function() {
3
- throw new Error("Element.toString() is not supported");
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)
4
6
  };
5
- Object.defineProperty(Node.prototype, "nodeName", {
6
- get: function() {
7
- return this.name;
8
- }
7
+ Object.defineProperty(Node.prototype, 'nodeName', {
8
+ get: function () {
9
+ return this.name;
10
+ },
9
11
  });
10
- Object.defineProperty(Node.prototype, "localName", {
11
- get: function() {
12
- return this.name;
13
- }
12
+ Object.defineProperty(Node.prototype, 'localName', {
13
+ get: function () {
14
+ return this.name;
15
+ },
14
16
  });
15
- const origAttributes = Object.getOwnPropertyDescriptor(
16
- Element.prototype,
17
- "attributes"
18
- )?.get;
17
+ const origAttributes = Object.getOwnPropertyDescriptor(Element.prototype, 'attributes')?.get;
19
18
  if (origAttributes) {
20
- Object.defineProperty(Element.prototype, "attributes", {
21
- get: function(...args) {
22
- const attrs = origAttributes.call(this, ...args);
23
- attrs.item = (idx) => {
24
- const el = attrs[idx];
25
- return { ...el, nodeType: 2, localName: el.name };
26
- };
27
- return attrs;
28
- }
29
- });
30
- } else {
31
- console.warn(
32
- "[WARN] Unable to patch DOM: Element.attributes property descriptor not found"
33
- );
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');
34
32
  }
@@ -1,283 +1,324 @@
1
- import "../../providers/http/patch-parse5-tree.js";
2
- import { concatenateUint8Arrays } from "@reclaimprotocol/tls";
3
- import {
4
- ArrayExpression,
5
- ExpressionStatement,
6
- ObjectExpression,
7
- parseScript,
8
- Property,
9
- Syntax
10
- } from "esprima-next";
11
- import { JSONPath } from "jsonpath-plus";
12
- import { parse } from "parse5";
13
- import { adapter as htmlAdapter } from "parse5-htmlparser2-tree-adapter";
14
- import RE2 from "re2";
15
- import xpath from "xpath";
1
+ // noinspection ExceptionCaughtLocallyJS
2
+ import "./patch-parse5-tree.js";
3
+ import { concatenateUint8Arrays } from '@reclaimprotocol/tls';
4
+ import { ArrayExpression, ExpressionStatement, ObjectExpression, parseScript, Property, Syntax } from 'esprima-next';
5
+ import { JSONPath } from 'jsonpath-plus';
6
+ import { parse } from 'parse5';
7
+ import { adapter as htmlAdapter, } from 'parse5-htmlparser2-tree-adapter';
8
+ import RE2 from 're2';
9
+ import xpath from 'xpath';
16
10
  import { getHttpRequestDataFromTranscript, isApplicationData, makeHttpResponseParser, REDACTION_CHAR_CODE } from "../../utils/index.js";
17
- function extractHTMLElement(html, xpathExpression, contentsOnly) {
18
- const { start, end } = extractHTMLElementIndex(html, xpathExpression, contentsOnly);
19
- return html.slice(start, end);
11
+ /**
12
+ * Returns only first extracted element
13
+ * @param html
14
+ * @param xpathExpression
15
+ * @param contentsOnly
16
+ */
17
+ export function extractHTMLElement(html, xpathExpression, contentsOnly) {
18
+ const { start, end } = extractHTMLElementIndex(html, xpathExpression, contentsOnly);
19
+ return html.slice(start, end);
20
20
  }
21
- function extractHTMLElements(html, xpathExpression, contentsOnly) {
22
- const indexes = extractHTMLElementsIndexes(html, xpathExpression, contentsOnly);
23
- const res = [];
24
- for (const { start, end } of indexes) {
25
- res.push(html.slice(start, end));
26
- }
27
- return res;
21
+ /**
22
+ * Returns all extracted elements
23
+ * @param html
24
+ * @param xpathExpression
25
+ * @param contentsOnly
26
+ */
27
+ export function extractHTMLElements(html, xpathExpression, contentsOnly) {
28
+ const indexes = extractHTMLElementsIndexes(html, xpathExpression, contentsOnly);
29
+ const res = [];
30
+ for (const { start, end } of indexes) {
31
+ res.push(html.slice(start, end));
32
+ }
33
+ return res;
28
34
  }
29
- function extractHTMLElementIndex(html, xpathExpression, contentsOnly) {
30
- return extractHTMLElementsIndexes(html, xpathExpression, contentsOnly)[0];
35
+ /**
36
+ * returns a single index of extracted element
37
+ * @param html
38
+ * @param xpathExpression
39
+ * @param contentsOnly
40
+ */
41
+ export function extractHTMLElementIndex(html, xpathExpression, contentsOnly) {
42
+ return extractHTMLElementsIndexes(html, xpathExpression, contentsOnly)[0];
31
43
  }
32
- function extractHTMLElementsIndexes(html, xpathExpression, contentsOnly) {
33
- return extractHTMLElementIndexesParse5(html, xpathExpression, contentsOnly);
44
+ /**
45
+ * Returns indexes of all extracted elements
46
+ * @param html
47
+ * @param xpathExpression
48
+ * @param contentsOnly indices of the start and end of the element's contents only,
49
+ * not the whole tag
50
+ */
51
+ export function extractHTMLElementsIndexes(html, xpathExpression, contentsOnly) {
52
+ return extractHTMLElementIndexesParse5(html, xpathExpression, contentsOnly);
34
53
  }
35
54
  function extractHTMLElementIndexesParse5(html, xpathExpression, contentsOnly) {
36
- const domLight = parse(
37
- html,
38
- { treeAdapter: htmlAdapter, sourceCodeLocationInfo: true }
39
- );
40
- domLight["name"] = "root";
41
- const parsedPath = xpath.parse(xpathExpression);
42
- const nodes = parsedPath.select({
43
- node: domLight,
44
- allowAnyNamespaceForNoPrefix: true
45
- });
46
- if (!nodes.length) {
47
- throw new Error(`Failed to find XPath: "${xpathExpression}"`);
48
- }
49
- return nodes.map((node) => getNodeRange(node, contentsOnly));
55
+ const domLight = parse(html, { treeAdapter: htmlAdapter, sourceCodeLocationInfo: true });
56
+ // lets xpath identify this as a node
57
+ domLight['name'] = 'root';
58
+ const parsedPath = xpath.parse(xpathExpression);
59
+ const nodes = parsedPath.select({
60
+ node: domLight,
61
+ allowAnyNamespaceForNoPrefix: true,
62
+ });
63
+ if (!nodes.length) {
64
+ throw new Error(`Failed to find XPath: "${xpathExpression}"`);
65
+ }
66
+ return nodes.map(node => getNodeRange(node, contentsOnly));
50
67
  }
51
68
  function getNodeRange(node, contentsOnly) {
52
- if (!contentsOnly) {
53
- return { start: node.startIndex, end: node.endIndex };
54
- }
55
- if (!("firstChild" in node) || !node.firstChild) {
56
- throw new Error(`Node "${node["name"]}" has no children`);
57
- }
58
- return {
59
- start: node.firstChild.startIndex,
60
- end: node.lastChild.endIndex
61
- };
69
+ if (!contentsOnly) {
70
+ return { start: node.startIndex, end: node.endIndex };
71
+ }
72
+ if (!('firstChild' in node) || !node.firstChild) {
73
+ throw new Error(`Node "${node['name']}" has no children`);
74
+ }
75
+ return {
76
+ start: node.firstChild.startIndex,
77
+ end: node.lastChild.endIndex
78
+ };
62
79
  }
63
- function extractJSONValueIndex(json, jsonPath) {
64
- return extractJSONValueIndexes(json, jsonPath)[0];
80
+ export function extractJSONValueIndex(json, jsonPath) {
81
+ return extractJSONValueIndexes(json, jsonPath)[0];
65
82
  }
66
- function extractJSONValueIndexes(json, jsonPath) {
67
- const pointers = JSONPath({
68
- path: jsonPath,
69
- json: JSON.parse(json),
70
- wrap: false,
71
- resultType: "pointer",
72
- eval: "safe",
73
- // @ts-ignore
74
- ignoreEvalErrors: true
75
- });
76
- if (!pointers) {
77
- throw new Error("jsonPath not found");
78
- }
79
- const tree = parseScript("(" + json + ")", { range: true });
80
- if (tree.body[0] instanceof ExpressionStatement && (tree.body[0].expression instanceof ObjectExpression || tree.body[0].expression instanceof ArrayExpression)) {
81
- const traversePointers = Array.isArray(pointers) ? pointers : [pointers];
82
- const res = [];
83
- for (const pointer of traversePointers) {
84
- const index = traverse(tree.body[0].expression, "", [pointer]);
85
- if (index) {
86
- res.push({
87
- start: index.start - 1,
88
- //account for '('
89
- end: index.end - 1
90
- });
91
- }
83
+ export function extractJSONValueIndexes(json, jsonPath) {
84
+ const pointers = JSONPath({
85
+ path: jsonPath,
86
+ json: JSON.parse(json),
87
+ wrap: false,
88
+ resultType: 'pointer',
89
+ eval: 'safe',
90
+ // @ts-ignore
91
+ ignoreEvalErrors: true
92
+ });
93
+ if (!pointers) {
94
+ throw new Error('jsonPath not found');
92
95
  }
93
- return res;
94
- }
95
- throw new Error("jsonPath not found");
96
+ //wrap in parentheses for esprima to parse
97
+ const tree = parseScript('(' + json + ')', { range: true });
98
+ if (tree.body[0] instanceof ExpressionStatement
99
+ && (tree.body[0].expression instanceof ObjectExpression || tree.body[0].expression instanceof ArrayExpression)) {
100
+ const traversePointers = Array.isArray(pointers) ? pointers : [pointers];
101
+ const res = [];
102
+ for (const pointer of traversePointers) {
103
+ const index = traverse(tree.body[0].expression, '', [pointer]);
104
+ if (index) {
105
+ res.push({
106
+ start: index.start - 1, //account for '('
107
+ end: index.end - 1,
108
+ });
109
+ }
110
+ }
111
+ return res;
112
+ }
113
+ throw new Error('jsonPath not found');
96
114
  }
115
+ /**
116
+ * recursively go through AST tree and build a JSON path while it's not equal to the one we search for
117
+ * @param o - esprima expression for root object
118
+ * @param path - path that is being built
119
+ * @param pointers - JSON pointers to compare to
120
+ */
97
121
  function traverse(o, path, pointers) {
98
- if (o instanceof ObjectExpression) {
99
- for (const p of o.properties) {
100
- if (!(p instanceof Property)) {
101
- continue;
102
- }
103
- const localPath = p.key.type === Syntax.Literal ? path + "/" + p.key.value : path;
104
- if (pointers.includes(localPath) && "range" in p && Array.isArray(p.range)) {
105
- return {
106
- start: p.range[0],
107
- end: p.range[1]
108
- };
109
- }
110
- if (p.value instanceof ObjectExpression || p.value instanceof ArrayExpression) {
111
- const res = traverse(p.value, localPath, pointers);
112
- if (res) {
113
- return res;
122
+ if (o instanceof ObjectExpression) {
123
+ for (const p of o.properties) {
124
+ if (!(p instanceof Property)) {
125
+ continue;
126
+ }
127
+ const localPath = p.key.type === Syntax.Literal
128
+ ? path + '/' + p.key.value
129
+ : path;
130
+ if (pointers.includes(localPath) && 'range' in p && Array.isArray(p.range)) {
131
+ return {
132
+ start: p.range[0],
133
+ end: p.range[1],
134
+ };
135
+ }
136
+ if (p.value instanceof ObjectExpression
137
+ || p.value instanceof ArrayExpression) {
138
+ const res = traverse(p.value, localPath, pointers);
139
+ if (res) {
140
+ return res;
141
+ }
142
+ }
114
143
  }
115
- }
116
144
  }
117
- }
118
- if (o instanceof ArrayExpression) {
119
- for (let i = 0; i < o.elements.length; i++) {
120
- const element = o.elements[i];
121
- if (!element) {
122
- continue;
123
- }
124
- const localPath = path + "/" + i;
125
- if (pointers.includes(localPath) && "range" in element && Array.isArray(element.range)) {
126
- return {
127
- start: element.range[0],
128
- end: element.range[1]
129
- };
130
- }
131
- if (element instanceof ObjectExpression) {
132
- const res = traverse(element, localPath, pointers);
133
- if (res) {
134
- return res;
135
- }
136
- }
137
- if (element instanceof ArrayExpression) {
138
- const res = traverse(element, localPath, pointers);
139
- if (res) {
140
- return res;
145
+ if (o instanceof ArrayExpression) {
146
+ for (let i = 0; i < o.elements.length; i++) {
147
+ const element = o.elements[i];
148
+ if (!element) {
149
+ continue;
150
+ }
151
+ const localPath = path + '/' + i;
152
+ if (pointers.includes(localPath) &&
153
+ 'range' in element &&
154
+ Array.isArray(element.range)) {
155
+ return {
156
+ start: element.range[0],
157
+ end: element.range[1],
158
+ };
159
+ }
160
+ if (element instanceof ObjectExpression) {
161
+ const res = traverse(element, localPath, pointers);
162
+ if (res) {
163
+ return res;
164
+ }
165
+ }
166
+ if (element instanceof ArrayExpression) {
167
+ const res = traverse(element, localPath, pointers);
168
+ if (res) {
169
+ return res;
170
+ }
171
+ }
141
172
  }
142
- }
143
173
  }
144
- }
145
- return null;
174
+ return null;
146
175
  }
147
- function buildHeaders(input) {
148
- const headers = [];
149
- for (const [key, value] of Object.entries(input || {})) {
150
- headers.push(`${key}: ${value}`);
151
- }
152
- return headers;
176
+ export function buildHeaders(input) {
177
+ const headers = [];
178
+ for (const [key, value] of Object.entries(input || {})) {
179
+ headers.push(`${key}: ${value}`);
180
+ }
181
+ return headers;
153
182
  }
154
- function convertResponsePosToAbsolutePos(pos, bodyStartIdx, chunks) {
155
- if (chunks?.length) {
156
- let chunkBodyStart = 0;
157
- for (const chunk of chunks) {
158
- const chunkSize = chunk.toIndex - chunk.fromIndex;
159
- if (pos >= chunkBodyStart && pos <= chunkBodyStart + chunkSize) {
160
- return pos - chunkBodyStart + chunk.fromIndex;
161
- }
162
- chunkBodyStart += chunkSize;
183
+ /**
184
+ * Converts position in HTTP response body to an absolute position in TLS transcript considering chunked encoding
185
+ * @param pos
186
+ * @param bodyStartIdx
187
+ * @param chunks
188
+ */
189
+ export function convertResponsePosToAbsolutePos(pos, bodyStartIdx, chunks) {
190
+ if (chunks?.length) {
191
+ let chunkBodyStart = 0;
192
+ for (const chunk of chunks) {
193
+ const chunkSize = chunk.toIndex - chunk.fromIndex;
194
+ if (pos >= chunkBodyStart && pos <= (chunkBodyStart + chunkSize)) {
195
+ return pos - chunkBodyStart + chunk.fromIndex;
196
+ }
197
+ chunkBodyStart += chunkSize;
198
+ }
199
+ throw new Error('position out of range');
163
200
  }
164
- throw new Error("position out of range");
165
- }
166
- return bodyStartIdx + pos;
201
+ return bodyStartIdx + pos;
167
202
  }
168
- function getRedactionsForChunkHeaders(from, to, chunks) {
169
- const res = [];
170
- if (!chunks?.length) {
171
- return res;
172
- }
173
- for (let i = 1; i < chunks?.length; i++) {
174
- const chunk = chunks[i];
175
- if (chunk.fromIndex > from && chunk.fromIndex < to) {
176
- res.push({
177
- fromIndex: chunks[i - 1].toIndex,
178
- toIndex: chunk.fromIndex
179
- });
203
+ /**
204
+ * If this reveal spans the boundary of two chunks, we'll
205
+ *
206
+ */
207
+ export function getRedactionsForChunkHeaders(from, to, chunks) {
208
+ const res = [];
209
+ if (!chunks?.length) {
210
+ return res;
180
211
  }
181
- }
182
- return res;
212
+ for (let i = 1; i < chunks?.length; i++) {
213
+ const chunk = chunks[i];
214
+ if (chunk.fromIndex > from && chunk.fromIndex < to) {
215
+ res.push({
216
+ fromIndex: chunks[i - 1].toIndex,
217
+ toIndex: chunk.fromIndex,
218
+ });
219
+ }
220
+ }
221
+ return res;
183
222
  }
184
- function parseHttpResponse(buff) {
185
- const parser = makeHttpResponseParser();
186
- parser.onChunk(buff);
187
- parser.streamEnded();
188
- return parser.res;
223
+ export function parseHttpResponse(buff) {
224
+ const parser = makeHttpResponseParser();
225
+ parser.onChunk(buff);
226
+ parser.streamEnded();
227
+ return parser.res;
189
228
  }
190
- function makeRegex(str) {
191
- return RE2(str, "sgiu");
229
+ export function makeRegex(str) {
230
+ return RE2(str, 'sgiu');
192
231
  }
193
- const TEMPLATE_START_CHARCODE = "{".charCodeAt(0);
194
- const TEMPLATE_END_CHARCODE = "}".charCodeAt(0);
195
- function matchRedactedStrings(templateString, redactedString) {
196
- if (templateString.length === 0 && redactedString?.length === 0) {
197
- return true;
198
- }
199
- if (!redactedString) {
200
- return false;
201
- }
202
- let ts = -1;
203
- let rs = -1;
204
- while (ts < templateString.length && rs < redactedString.length) {
205
- let ct = getTChar();
206
- let cr = getRChar();
207
- if (ct !== cr) {
208
- if (ct === TEMPLATE_START_CHARCODE && cr === REDACTION_CHAR_CODE) {
209
- if (getTChar() !== TEMPLATE_START_CHARCODE) {
210
- return false;
232
+ const TEMPLATE_START_CHARCODE = '{'.charCodeAt(0);
233
+ const TEMPLATE_END_CHARCODE = '}'.charCodeAt(0);
234
+ /**
235
+ * Try to match strings that contain templates like {{param}}
236
+ * against redacted string that has *** instead of that param
237
+ */
238
+ export function matchRedactedStrings(templateString, redactedString) {
239
+ if (templateString.length === 0 && redactedString?.length === 0) {
240
+ return true;
241
+ }
242
+ if (!redactedString) {
243
+ return false;
244
+ }
245
+ let ts = -1;
246
+ let rs = -1;
247
+ while (ts < templateString.length && rs < redactedString.length) {
248
+ let ct = getTChar();
249
+ let cr = getRChar();
250
+ if (ct !== cr) {
251
+ // only valid if param contains "{" & redacted contains "*"
252
+ if (ct === TEMPLATE_START_CHARCODE && cr === REDACTION_CHAR_CODE) {
253
+ //check that the char after first "{" is also "{"
254
+ if (getTChar() !== TEMPLATE_START_CHARCODE) {
255
+ return false;
256
+ }
257
+ //look for first closing bracket
258
+ while (((ct = getTChar()) !== TEMPLATE_END_CHARCODE) && ct !== -1) {
259
+ }
260
+ //look for second closing bracket
261
+ while (((ct = getTChar()) !== TEMPLATE_END_CHARCODE) && ct !== -1) {
262
+ }
263
+ if (ct === -1) {
264
+ return false;
265
+ }
266
+ //find the end of redaction
267
+ while (((cr = getRChar()) === REDACTION_CHAR_CODE) && cr !== -1) {
268
+ }
269
+ if (cr === -1) {
270
+ //if there's nothing after template too then both ended at the end of strings
271
+ return getTChar() === -1;
272
+ }
273
+ //rewind redacted string position back 1 char because we read one extra
274
+ rs--;
275
+ }
276
+ else {
277
+ return false;
278
+ }
211
279
  }
212
- while ((ct = getTChar()) !== TEMPLATE_END_CHARCODE && ct !== -1) {
280
+ }
281
+ function getTChar() {
282
+ ts++;
283
+ if (ts < templateString.length) {
284
+ return templateString[ts];
213
285
  }
214
- while ((ct = getTChar()) !== TEMPLATE_END_CHARCODE && ct !== -1) {
286
+ else {
287
+ return -1;
215
288
  }
216
- if (ct === -1) {
217
- return false;
289
+ }
290
+ function getRChar() {
291
+ if (!redactedString) {
292
+ return -1;
218
293
  }
219
- while ((cr = getRChar()) === REDACTION_CHAR_CODE && cr !== -1) {
294
+ rs++;
295
+ if (rs < redactedString.length) {
296
+ return redactedString[rs];
220
297
  }
221
- if (cr === -1) {
222
- return getTChar() === -1;
298
+ else {
299
+ return -1;
223
300
  }
224
- rs--;
225
- } else {
226
- return false;
227
- }
228
- }
229
- }
230
- function getTChar() {
231
- ts++;
232
- if (ts < templateString.length) {
233
- return templateString[ts];
234
- } else {
235
- return -1;
236
- }
237
- }
238
- function getRChar() {
239
- if (!redactedString) {
240
- return -1;
241
301
  }
242
- rs++;
243
- if (rs < redactedString.length) {
244
- return redactedString[rs];
245
- } else {
246
- return -1;
247
- }
248
- }
249
- return ts === templateString.length && rs === redactedString.length;
302
+ return ts === templateString.length && rs === redactedString.length;
250
303
  }
251
- function generateRequstAndResponseFromTranscript(transcript, tlsVersion) {
252
- const allPackets = transcript;
253
- const packets = [];
254
- for (const b of allPackets) {
255
- if (b.message.type !== "ciphertext" || !isApplicationData(b.message, tlsVersion)) {
256
- continue;
304
+ export function generateRequstAndResponseFromTranscript(transcript, tlsVersion) {
305
+ const allPackets = transcript;
306
+ const packets = [];
307
+ for (const b of allPackets) {
308
+ if (b.message.type !== 'ciphertext'
309
+ || !isApplicationData(b.message, tlsVersion)) {
310
+ continue;
311
+ }
312
+ const plaintext = tlsVersion === 'TLS1_3'
313
+ ? b.message.plaintext.slice(0, -1)
314
+ : b.message.plaintext;
315
+ packets.push({
316
+ message: plaintext,
317
+ sender: b.sender
318
+ });
257
319
  }
258
- const plaintext = tlsVersion === "TLS1_3" ? b.message.plaintext.slice(0, -1) : b.message.plaintext;
259
- packets.push({
260
- message: plaintext,
261
- sender: b.sender
262
- });
263
- }
264
- const req = getHttpRequestDataFromTranscript(packets);
265
- const responsePackets = concatenateUint8Arrays(packets.filter((p) => p.sender === "server").map((p) => p.message).filter((b) => !b.every((b2) => b2 === REDACTION_CHAR_CODE)));
266
- const res = parseHttpResponse(responsePackets);
267
- return { req, res };
320
+ const req = getHttpRequestDataFromTranscript(packets);
321
+ const responsePackets = concatenateUint8Arrays(packets.filter(p => p.sender === 'server').map(p => p.message).filter(b => !b.every(b => b === REDACTION_CHAR_CODE)));
322
+ const res = parseHttpResponse(responsePackets);
323
+ return { req, res };
268
324
  }
269
- export {
270
- buildHeaders,
271
- convertResponsePosToAbsolutePos,
272
- extractHTMLElement,
273
- extractHTMLElementIndex,
274
- extractHTMLElements,
275
- extractHTMLElementsIndexes,
276
- extractJSONValueIndex,
277
- extractJSONValueIndexes,
278
- generateRequstAndResponseFromTranscript,
279
- getRedactionsForChunkHeaders,
280
- makeRegex,
281
- matchRedactedStrings,
282
- parseHttpResponse
283
- };
@@ -1,7 +1,4 @@
1
- import http from "../providers/http/index.js";
2
- const providers = {
3
- http
4
- };
5
- export {
6
- providers
1
+ import http from "./http/index.js";
2
+ export const providers = {
3
+ http,
7
4
  };