@decionis/agentsafe 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/Credentials.d.ts.map +1 -1
- package/dist/cli/Credentials.js +8 -1
- package/dist/cli/Credentials.js.map +1 -1
- package/dist/cli/Help.d.ts.map +1 -1
- package/dist/cli/Help.js +3 -2
- package/dist/cli/Help.js.map +1 -1
- package/dist/cli/Intercept.d.ts +46 -10
- package/dist/cli/Intercept.d.ts.map +1 -1
- package/dist/cli/Intercept.js +255 -27
- package/dist/cli/Intercept.js.map +1 -1
- package/dist/cli/Login.d.ts +7 -2
- package/dist/cli/Login.d.ts.map +1 -1
- package/dist/cli/Login.js +106 -3
- package/dist/cli/Login.js.map +1 -1
- package/dist/cli/Status.d.ts.map +1 -1
- package/dist/cli/Status.js +9 -4
- package/dist/cli/Status.js.map +1 -1
- package/dist/cli/TestCommand.d.ts +24 -2
- package/dist/cli/TestCommand.d.ts.map +1 -1
- package/dist/cli/TestCommand.js +122 -1
- package/dist/cli/TestCommand.js.map +1 -1
- package/dist/egress/TransparentDial.d.ts +4 -3
- package/dist/egress/TransparentDial.d.ts.map +1 -1
- package/dist/egress/TransparentDial.js +4 -3
- package/dist/egress/TransparentDial.js.map +1 -1
- package/dist/gateway/BoundaryTest.d.ts +54 -0
- package/dist/gateway/BoundaryTest.d.ts.map +1 -1
- package/dist/gateway/BoundaryTest.js +19 -7
- package/dist/gateway/BoundaryTest.js.map +1 -1
- package/dist/gateway/Gateway.d.ts +23 -0
- package/dist/gateway/Gateway.d.ts.map +1 -1
- package/dist/gateway/Gateway.js +39 -10
- package/dist/gateway/Gateway.js.map +1 -1
- package/dist/gateway/HostedBoundaryTest.d.ts +118 -0
- package/dist/gateway/HostedBoundaryTest.d.ts.map +1 -0
- package/dist/gateway/HostedBoundaryTest.js +235 -0
- package/dist/gateway/HostedBoundaryTest.js.map +1 -0
- package/dist/gateway/ShadowLedger.d.ts +21 -2
- package/dist/gateway/ShadowLedger.d.ts.map +1 -1
- package/dist/gateway/ShadowLedger.js +42 -12
- package/dist/gateway/ShadowLedger.js.map +1 -1
- package/dist/http/GatewayHttpServer.d.ts +7 -0
- package/dist/http/GatewayHttpServer.d.ts.map +1 -1
- package/dist/http/GatewayHttpServer.js +8 -0
- package/dist/http/GatewayHttpServer.js.map +1 -1
- package/dist/http/InterceptGovernor.d.ts +71 -0
- package/dist/http/InterceptGovernor.d.ts.map +1 -0
- package/dist/http/InterceptGovernor.js +128 -0
- package/dist/http/InterceptGovernor.js.map +1 -0
- package/dist/http/InterceptServer.d.ts +31 -1
- package/dist/http/InterceptServer.d.ts.map +1 -1
- package/dist/http/InterceptServer.js +58 -7
- package/dist/http/InterceptServer.js.map +1 -1
- package/dist/intercept/ClientHello.d.ts.map +1 -1
- package/dist/intercept/ClientHello.js +76 -72
- package/dist/intercept/ClientHello.js.map +1 -1
- package/dist/intercept/Der.d.ts +59 -0
- package/dist/intercept/Der.d.ts.map +1 -0
- package/dist/intercept/Der.js +140 -0
- package/dist/intercept/Der.js.map +1 -0
- package/dist/intercept/Destination.d.ts +1 -1
- package/dist/intercept/Destination.d.ts.map +1 -1
- package/dist/intercept/Destination.js +28 -24
- package/dist/intercept/Destination.js.map +1 -1
- package/dist/intercept/InterceptLedger.d.ts +43 -7
- package/dist/intercept/InterceptLedger.d.ts.map +1 -1
- package/dist/intercept/InterceptLedger.js +39 -18
- package/dist/intercept/InterceptLedger.js.map +1 -1
- package/dist/intercept/LeafIssuer.d.ts +40 -0
- package/dist/intercept/LeafIssuer.d.ts.map +1 -0
- package/dist/intercept/LeafIssuer.js +124 -0
- package/dist/intercept/LeafIssuer.js.map +1 -0
- package/dist/intercept/RequestHead.d.ts +1 -1
- package/dist/intercept/RequestHead.d.ts.map +1 -1
- package/dist/intercept/RequestHead.js +78 -66
- package/dist/intercept/RequestHead.js.map +1 -1
- package/package.json +2 -2
|
@@ -12,72 +12,83 @@
|
|
|
12
12
|
* RFC 7301 (ALPN).
|
|
13
13
|
*/
|
|
14
14
|
const RECORD_HANDSHAKE = 0x16;
|
|
15
|
+
const RECORD_VERSION_MAJOR = 0x03;
|
|
15
16
|
const HANDSHAKE_CLIENT_HELLO = 0x01;
|
|
16
17
|
const RECORD_HEADER_LENGTH = 5;
|
|
17
18
|
const HANDSHAKE_HEADER_LENGTH = 4;
|
|
18
19
|
const EXTENSION_SERVER_NAME = 0x0000;
|
|
19
20
|
const EXTENSION_ALPN = 0x0010;
|
|
20
21
|
const SERVER_NAME_HOST_NAME = 0x00;
|
|
21
|
-
/** The largest record
|
|
22
|
-
const MAX_RECORD_LENGTH = 16_384
|
|
22
|
+
/** The largest plaintext record TLS allows (RFC 8446 section 5.1). */
|
|
23
|
+
const MAX_RECORD_LENGTH = 16_384;
|
|
23
24
|
/** The most handshake bytes read before a hello is called malformed; well beyond any real one. */
|
|
24
25
|
export const MAX_CLIENT_HELLO_BYTES = 65_536;
|
|
25
26
|
/** RFC 1123 host name: labels of letters, digits and hyphens, at most 253 characters. */
|
|
26
27
|
const HOST_NAME = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/;
|
|
28
|
+
/** An ALPN protocol name as this interceptor will report it: printable ASCII, nothing else. */
|
|
29
|
+
const ALPN_PROTOCOL = /^[\x21-\x7e]+$/;
|
|
30
|
+
/** Thrown when a field claims more bytes than remain; the reader answers MALFORMED. */
|
|
31
|
+
class Short extends RangeError {
|
|
32
|
+
}
|
|
27
33
|
class Cursor {
|
|
28
34
|
bytes;
|
|
35
|
+
view;
|
|
29
36
|
offset = 0;
|
|
30
37
|
constructor(bytes) {
|
|
31
38
|
this.bytes = bytes;
|
|
39
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
32
40
|
}
|
|
33
41
|
get remaining() {
|
|
34
42
|
return this.bytes.length - this.offset;
|
|
35
43
|
}
|
|
44
|
+
/** A read past the end throws from the view itself; the reader answers MALFORMED. */
|
|
36
45
|
u8() {
|
|
37
|
-
|
|
38
|
-
throw new RangeError("short");
|
|
39
|
-
const value = this.bytes[this.offset] ?? 0;
|
|
46
|
+
const value = this.view.getUint8(this.offset);
|
|
40
47
|
this.offset += 1;
|
|
41
48
|
return value;
|
|
42
49
|
}
|
|
43
50
|
u16() {
|
|
44
|
-
|
|
51
|
+
const value = this.view.getUint16(this.offset);
|
|
52
|
+
this.offset += 2;
|
|
53
|
+
return value;
|
|
45
54
|
}
|
|
46
55
|
u24() {
|
|
47
|
-
return (this.u8() << 16) |
|
|
56
|
+
return (this.u8() << 16) | this.u16();
|
|
48
57
|
}
|
|
58
|
+
/** Skipping past the end leaves nothing to read, and the next read throws. */
|
|
49
59
|
skip(length) {
|
|
50
|
-
if (this.remaining < length)
|
|
51
|
-
throw new RangeError("short");
|
|
52
60
|
this.offset += length;
|
|
53
61
|
}
|
|
62
|
+
/** Taking more than remains is refused here: a short slice would read as a shorter, valid field. */
|
|
54
63
|
take(length) {
|
|
55
64
|
if (this.remaining < length)
|
|
56
|
-
throw new
|
|
65
|
+
throw new Short();
|
|
57
66
|
const slice = this.bytes.subarray(this.offset, this.offset + length);
|
|
58
67
|
this.offset += length;
|
|
59
68
|
return slice;
|
|
60
69
|
}
|
|
61
70
|
}
|
|
62
71
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
72
|
+
* Walks the handshake records from the start of the bytes until at least
|
|
73
|
+
* `wanted` handshake bytes have been collected, or the bytes run out. A
|
|
74
|
+
* ClientHello may be fragmented across records, and a client may already have
|
|
75
|
+
* sent the first record of its next flight after it.
|
|
67
76
|
*/
|
|
68
|
-
function
|
|
77
|
+
function handshakeRecords(bytes, wanted) {
|
|
69
78
|
const fragments = [];
|
|
70
79
|
let total = 0;
|
|
71
80
|
let offset = 0;
|
|
72
|
-
|
|
73
|
-
let needed = 0;
|
|
74
|
-
while (needed === 0 || total < needed) {
|
|
81
|
+
while (total < wanted) {
|
|
75
82
|
if (bytes.length - offset < RECORD_HEADER_LENGTH)
|
|
76
83
|
return { kind: "NEED_MORE" };
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
84
|
+
const header = new DataView(bytes.buffer, bytes.byteOffset + offset, RECORD_HEADER_LENGTH);
|
|
85
|
+
const type = header.getUint8(0);
|
|
86
|
+
const major = header.getUint8(1);
|
|
87
|
+
const length = header.getUint16(3);
|
|
88
|
+
if (type !== RECORD_HANDSHAKE ||
|
|
89
|
+
major !== RECORD_VERSION_MAJOR ||
|
|
90
|
+
length === 0 ||
|
|
91
|
+
length > MAX_RECORD_LENGTH) {
|
|
81
92
|
// Not a handshake record where one was due: not TLS at all before any
|
|
82
93
|
// record, and a broken hello once one has begun.
|
|
83
94
|
return fragments.length === 0 ? { kind: "NOT_TLS" } : { kind: "MALFORMED" };
|
|
@@ -89,20 +100,10 @@ function handshakeBytes(bytes) {
|
|
|
89
100
|
fragments.push(bytes.subarray(start, end));
|
|
90
101
|
total += length;
|
|
91
102
|
offset = end;
|
|
92
|
-
if (needed === 0 && total >= HANDSHAKE_HEADER_LENGTH) {
|
|
93
|
-
const head = concat(fragments, HANDSHAKE_HEADER_LENGTH);
|
|
94
|
-
needed =
|
|
95
|
-
HANDSHAKE_HEADER_LENGTH + (((head[1] ?? 0) << 16) | ((head[2] ?? 0) << 8) | (head[3] ?? 0));
|
|
96
|
-
// A hello that announces itself larger than any real one is refused now,
|
|
97
|
-
// not read for as long as the client cares to send.
|
|
98
|
-
if (needed > MAX_CLIENT_HELLO_BYTES)
|
|
99
|
-
return { kind: "MALFORMED" };
|
|
100
|
-
}
|
|
101
103
|
}
|
|
102
|
-
|
|
103
|
-
// client's next message, which are not this parser's to read.
|
|
104
|
-
return { data: concat(fragments, needed) };
|
|
104
|
+
return { fragments };
|
|
105
105
|
}
|
|
106
|
+
/** The first `length` bytes of the fragments, in order: only the hello, never what follows it. */
|
|
106
107
|
function concat(fragments, length) {
|
|
107
108
|
const out = new Uint8Array(length);
|
|
108
109
|
let offset = 0;
|
|
@@ -110,11 +111,27 @@ function concat(fragments, length) {
|
|
|
110
111
|
const slice = fragment.subarray(0, Math.max(0, Math.min(fragment.length, length - offset)));
|
|
111
112
|
out.set(slice, offset);
|
|
112
113
|
offset += slice.length;
|
|
113
|
-
if (offset >= length)
|
|
114
|
-
break;
|
|
115
114
|
}
|
|
116
115
|
return out;
|
|
117
116
|
}
|
|
117
|
+
/** The handshake message bytes of the ClientHello, exactly as long as its header says. */
|
|
118
|
+
function handshakeBytes(bytes) {
|
|
119
|
+
const head = handshakeRecords(bytes, HANDSHAKE_HEADER_LENGTH);
|
|
120
|
+
if ("kind" in head)
|
|
121
|
+
return head;
|
|
122
|
+
const header = new Cursor(concat(head.fragments, HANDSHAKE_HEADER_LENGTH));
|
|
123
|
+
if (header.u8() !== HANDSHAKE_CLIENT_HELLO)
|
|
124
|
+
return { kind: "MALFORMED" };
|
|
125
|
+
const needed = HANDSHAKE_HEADER_LENGTH + header.u24();
|
|
126
|
+
// A hello that announces itself larger than any real one is refused now,
|
|
127
|
+
// not read for as long as the client cares to send.
|
|
128
|
+
if (needed > MAX_CLIENT_HELLO_BYTES)
|
|
129
|
+
return { kind: "MALFORMED" };
|
|
130
|
+
const whole = handshakeRecords(bytes, needed);
|
|
131
|
+
if ("kind" in whole)
|
|
132
|
+
return whole;
|
|
133
|
+
return { data: concat(whole.fragments, needed) };
|
|
134
|
+
}
|
|
118
135
|
/** Reads a client's first bytes as a TLS ClientHello, or says why they are not one. */
|
|
119
136
|
export function readClientHello(bytes) {
|
|
120
137
|
const records = handshakeBytes(bytes);
|
|
@@ -122,11 +139,7 @@ export function readClientHello(bytes) {
|
|
|
122
139
|
return records;
|
|
123
140
|
const cursor = new Cursor(records.data);
|
|
124
141
|
try {
|
|
125
|
-
|
|
126
|
-
return { kind: "MALFORMED" };
|
|
127
|
-
const length = cursor.u24();
|
|
128
|
-
if (length !== cursor.remaining)
|
|
129
|
-
return { kind: "MALFORMED" };
|
|
142
|
+
cursor.skip(HANDSHAKE_HEADER_LENGTH);
|
|
130
143
|
cursor.u16(); // legacy_version
|
|
131
144
|
cursor.skip(32); // random
|
|
132
145
|
cursor.skip(cursor.u8()); // legacy_session_id
|
|
@@ -137,76 +150,67 @@ export function readClientHello(bytes) {
|
|
|
137
150
|
const extensionsLength = cursor.u16();
|
|
138
151
|
if (extensionsLength !== cursor.remaining)
|
|
139
152
|
return { kind: "MALFORMED" };
|
|
153
|
+
// Each extension may appear once (RFC 8446 section 4.2); a repeat is a
|
|
154
|
+
// hello two parsers could read differently, and is refused.
|
|
140
155
|
let serverName = null;
|
|
141
|
-
let alpn =
|
|
142
|
-
let sawServerName = false;
|
|
143
|
-
let sawAlpn = false;
|
|
156
|
+
let alpn = null;
|
|
144
157
|
while (cursor.remaining > 0) {
|
|
145
158
|
const type = cursor.u16();
|
|
146
159
|
const body = new Cursor(cursor.take(cursor.u16()));
|
|
147
160
|
if (type === EXTENSION_SERVER_NAME) {
|
|
148
|
-
if (
|
|
161
|
+
if (serverName !== null)
|
|
149
162
|
return { kind: "MALFORMED" };
|
|
150
|
-
sawServerName = true;
|
|
151
163
|
serverName = readServerName(body);
|
|
152
164
|
if (serverName === null)
|
|
153
165
|
return { kind: "MALFORMED" };
|
|
154
166
|
}
|
|
155
167
|
else if (type === EXTENSION_ALPN) {
|
|
156
|
-
if (
|
|
168
|
+
if (alpn !== null)
|
|
157
169
|
return { kind: "MALFORMED" };
|
|
158
|
-
sawAlpn = true;
|
|
159
170
|
alpn = readAlpn(body);
|
|
160
171
|
}
|
|
161
172
|
}
|
|
162
|
-
return { kind: "CLIENT_HELLO", serverName, alpn };
|
|
173
|
+
return { kind: "CLIENT_HELLO", serverName, alpn: alpn ?? [] };
|
|
163
174
|
}
|
|
164
175
|
catch {
|
|
176
|
+
// A field that ran past the bytes, or a length that disagreed with them:
|
|
177
|
+
// a hello this parser cannot place, whatever the cause.
|
|
165
178
|
return { kind: "MALFORMED" };
|
|
166
179
|
}
|
|
167
180
|
}
|
|
168
181
|
/** The one host_name entry the extension carries; null for anything else. */
|
|
169
182
|
function readServerName(body) {
|
|
170
|
-
|
|
171
|
-
if (listLength !== body.remaining)
|
|
183
|
+
if (body.u16() !== body.remaining)
|
|
172
184
|
return null;
|
|
173
185
|
let host = null;
|
|
174
186
|
while (body.remaining > 0) {
|
|
175
187
|
const nameType = body.u8();
|
|
176
|
-
const name = body.take(body.u16());
|
|
188
|
+
const name = latin1(body.take(body.u16())).toLowerCase();
|
|
177
189
|
if (nameType !== SERVER_NAME_HOST_NAME)
|
|
178
190
|
continue;
|
|
179
|
-
if (host !== null)
|
|
191
|
+
if (host !== null || !HOST_NAME.test(name))
|
|
180
192
|
return null;
|
|
181
|
-
|
|
182
|
-
if (text === null || !HOST_NAME.test(text))
|
|
183
|
-
return null;
|
|
184
|
-
host = text;
|
|
193
|
+
host = name;
|
|
185
194
|
}
|
|
186
195
|
return host;
|
|
187
196
|
}
|
|
197
|
+
/** The protocol names in the order the client prefers them; a name outside printable ASCII is a refusal. */
|
|
188
198
|
function readAlpn(body) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
throw new RangeError("alpn");
|
|
199
|
+
if (body.u16() !== body.remaining)
|
|
200
|
+
throw new Short();
|
|
192
201
|
const protocols = [];
|
|
193
202
|
while (body.remaining > 0) {
|
|
194
|
-
const name = body.take(body.u8());
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
protocols.push(text);
|
|
203
|
+
const name = latin1(body.take(body.u8()));
|
|
204
|
+
if (!ALPN_PROTOCOL.test(name))
|
|
205
|
+
throw new Short();
|
|
206
|
+
protocols.push(name);
|
|
199
207
|
}
|
|
200
208
|
return protocols;
|
|
201
209
|
}
|
|
202
|
-
|
|
203
|
-
function asciiLowercase(bytes) {
|
|
210
|
+
function latin1(bytes) {
|
|
204
211
|
let text = "";
|
|
205
|
-
for (const byte of bytes)
|
|
206
|
-
if (byte < 0x21 || byte > 0x7e)
|
|
207
|
-
return null;
|
|
212
|
+
for (const byte of bytes)
|
|
208
213
|
text += String.fromCharCode(byte);
|
|
209
|
-
|
|
210
|
-
return text.toLowerCase();
|
|
214
|
+
return text;
|
|
211
215
|
}
|
|
212
216
|
//# sourceMappingURL=ClientHello.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientHello.js","sourceRoot":"","sources":["../../src/intercept/ClientHello.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAiBH,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,sBAAsB,GAAG,IAAI,CAAC;AACpC,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAC/B,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAClC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AACrC,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,
|
|
1
|
+
{"version":3,"file":"ClientHello.js","sourceRoot":"","sources":["../../src/intercept/ClientHello.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAiBH,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,MAAM,sBAAsB,GAAG,IAAI,CAAC;AACpC,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAC/B,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAClC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AACrC,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,sEAAsE;AACtE,MAAM,iBAAiB,GAAG,MAAM,CAAC;AACjC,kGAAkG;AAClG,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAC7C,yFAAyF;AACzF,MAAM,SAAS,GACb,gGA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0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The DER the leaf issuer writes and reads: the handful of ASN.1 shapes an
|
|
3
|
+
* X.509 certificate is made of, encoded minimally, and a walker over a DER
|
|
4
|
+
* SEQUENCE's content. Minimal encoding is not a nicety here. A verifier in a
|
|
5
|
+
* workload's runtime may be strict (Go's, Java's and the Rust web-PKI
|
|
6
|
+
* libraries all refuse a length that is not in its shortest form), so a leaf
|
|
7
|
+
* that OpenSSL would read and they would not is a leaf that governs Node and
|
|
8
|
+
* fails Go. Each function encodes one shape and nothing else; the certificate
|
|
9
|
+
* itself is composed in `LeafIssuer`.
|
|
10
|
+
*/
|
|
11
|
+
/** DER that cannot be walked: a length past the end of its container. */
|
|
12
|
+
export declare class MalformedDer extends RangeError {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
/** The length octets for `size` content bytes, in the shortest form DER allows. */
|
|
16
|
+
export declare function lengthOctets(size: number): Buffer;
|
|
17
|
+
/** One element: tag, length, content. */
|
|
18
|
+
export declare function element(tag: number, content: Buffer): Buffer;
|
|
19
|
+
export declare const sequence: (...parts: Buffer[]) => Buffer;
|
|
20
|
+
export declare const set: (...parts: Buffer[]) => Buffer;
|
|
21
|
+
export declare const octetString: (bytes: Buffer) => Buffer;
|
|
22
|
+
export declare const bitString: (bytes: Buffer, unusedBits?: number) => Buffer;
|
|
23
|
+
export declare const utf8String: (text: string) => Buffer;
|
|
24
|
+
export declare const ia5String: (text: string) => Buffer;
|
|
25
|
+
export declare const booleanTrue: Buffer<ArrayBuffer>;
|
|
26
|
+
export declare const asn1Null: Buffer<ArrayBuffer>;
|
|
27
|
+
/** A context-specific, constructed, explicitly tagged element: `[index]`. */
|
|
28
|
+
export declare const explicit: (index: number, content: Buffer) => Buffer;
|
|
29
|
+
/** A context-specific, primitive, implicitly tagged element, such as a GeneralName's dNSName. */
|
|
30
|
+
export declare const implicit: (index: number, content: Buffer) => Buffer;
|
|
31
|
+
/**
|
|
32
|
+
* A non-negative INTEGER from its big-endian magnitude: a leading zero is
|
|
33
|
+
* added when the top bit is set, so the value is not read as negative.
|
|
34
|
+
*/
|
|
35
|
+
export declare function integer(magnitude: Buffer): Buffer;
|
|
36
|
+
/** An OBJECT IDENTIFIER from its dotted form. */
|
|
37
|
+
export declare function oid(dotted: string): Buffer;
|
|
38
|
+
/** A UTCTime, `YYMMDDHHMMSSZ`, which X.509 uses for dates before 2050. */
|
|
39
|
+
export declare function utcTime(date: Date): Buffer;
|
|
40
|
+
/** An X.509 Extension: identifier, `critical` only when true (DER omits the default), value. */
|
|
41
|
+
export declare function extension(identifier: string, critical: boolean, value: Buffer): Buffer;
|
|
42
|
+
/** The PEM of `bytes` under `label`, base64 in lines of 64. */
|
|
43
|
+
export declare function pem(label: string, bytes: Buffer): string;
|
|
44
|
+
/** One element as read: its tag, the bytes of the whole element, and of its content. */
|
|
45
|
+
export interface Element {
|
|
46
|
+
readonly tag: number;
|
|
47
|
+
readonly whole: Buffer;
|
|
48
|
+
readonly content: Buffer;
|
|
49
|
+
}
|
|
50
|
+
/** The elements of a SEQUENCE's (or SET's) content, in order. */
|
|
51
|
+
export declare function children(content: Buffer): Element[];
|
|
52
|
+
/**
|
|
53
|
+
* The subject Name of a certificate, as encoded: the element after the
|
|
54
|
+
* validity in its TBSCertificate, whether or not the version is present. These
|
|
55
|
+
* are the bytes a leaf's issuer field must repeat exactly for a strict verifier
|
|
56
|
+
* to chain them; OpenSSL compares names canonically, others compare bytes.
|
|
57
|
+
*/
|
|
58
|
+
export declare function subjectNameOf(certificate: Buffer): Buffer;
|
|
59
|
+
//# sourceMappingURL=Der.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Der.d.ts","sourceRoot":"","sources":["../../src/intercept/Der.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,yEAAyE;AACzE,qBAAa,YAAa,SAAQ,UAAU;gBACvB,OAAO,EAAE,MAAM;CAInC;AAED,mFAAmF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,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAOxD;AAED,wFAAwF;AACxF,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,iEAAiE;AACjE,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,EAAE,CA6BnD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAWzD"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The DER the leaf issuer writes and reads: the handful of ASN.1 shapes an
|
|
3
|
+
* X.509 certificate is made of, encoded minimally, and a walker over a DER
|
|
4
|
+
* SEQUENCE's content. Minimal encoding is not a nicety here. A verifier in a
|
|
5
|
+
* workload's runtime may be strict (Go's, Java's and the Rust web-PKI
|
|
6
|
+
* libraries all refuse a length that is not in its shortest form), so a leaf
|
|
7
|
+
* that OpenSSL would read and they would not is a leaf that governs Node and
|
|
8
|
+
* fails Go. Each function encodes one shape and nothing else; the certificate
|
|
9
|
+
* itself is composed in `LeafIssuer`.
|
|
10
|
+
*/
|
|
11
|
+
/** DER that cannot be walked: a length past the end of its container. */
|
|
12
|
+
export class MalformedDer extends RangeError {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "MalformedDer";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** The length octets for `size` content bytes, in the shortest form DER allows. */
|
|
19
|
+
export function lengthOctets(size) {
|
|
20
|
+
if (size < 0x80)
|
|
21
|
+
return Buffer.from([size]);
|
|
22
|
+
if (size < 0x100)
|
|
23
|
+
return Buffer.from([0x81, size]);
|
|
24
|
+
if (size < 0x10000)
|
|
25
|
+
return Buffer.from([0x82, size >> 8, size & 0xff]);
|
|
26
|
+
return Buffer.from([0x83, size >> 16, (size >> 8) & 0xff, size & 0xff]);
|
|
27
|
+
}
|
|
28
|
+
/** One element: tag, length, content. */
|
|
29
|
+
export function element(tag, content) {
|
|
30
|
+
return Buffer.concat([Buffer.from([tag]), lengthOctets(content.length), content]);
|
|
31
|
+
}
|
|
32
|
+
export const sequence = (...parts) => element(0x30, Buffer.concat(parts));
|
|
33
|
+
export const set = (...parts) => element(0x31, Buffer.concat(parts));
|
|
34
|
+
export const octetString = (bytes) => element(0x04, bytes);
|
|
35
|
+
export const bitString = (bytes, unusedBits = 0) => element(0x03, Buffer.concat([Buffer.from([unusedBits]), bytes]));
|
|
36
|
+
export const utf8String = (text) => element(0x0c, Buffer.from(text));
|
|
37
|
+
export const ia5String = (text) => element(0x16, Buffer.from(text));
|
|
38
|
+
// Written out rather than composed, so that nothing runs while the module loads.
|
|
39
|
+
export const booleanTrue = Buffer.from([0x01, 0x01, 0xff]);
|
|
40
|
+
export const asn1Null = Buffer.from([0x05, 0x00]);
|
|
41
|
+
/** A context-specific, constructed, explicitly tagged element: `[index]`. */
|
|
42
|
+
export const explicit = (index, content) => element(0xa0 | index, content);
|
|
43
|
+
/** A context-specific, primitive, implicitly tagged element, such as a GeneralName's dNSName. */
|
|
44
|
+
export const implicit = (index, content) => element(0x80 | index, content);
|
|
45
|
+
/**
|
|
46
|
+
* A non-negative INTEGER from its big-endian magnitude: a leading zero is
|
|
47
|
+
* added when the top bit is set, so the value is not read as negative.
|
|
48
|
+
*/
|
|
49
|
+
export function integer(magnitude) {
|
|
50
|
+
const first = magnitude.readUInt8(0);
|
|
51
|
+
return element(0x02, (first & 0x80) === 0 ? magnitude : Buffer.concat([Buffer.from([0]), magnitude]));
|
|
52
|
+
}
|
|
53
|
+
/** An OBJECT IDENTIFIER from its dotted form. */
|
|
54
|
+
export function oid(dotted) {
|
|
55
|
+
const [first = 0, second = 0, ...rest] = dotted.split(".").map(Number);
|
|
56
|
+
const bytes = [first * 40 + second];
|
|
57
|
+
for (const part of rest) {
|
|
58
|
+
const encoded = [part & 0x7f];
|
|
59
|
+
let remaining = part >> 7;
|
|
60
|
+
while (remaining > 0) {
|
|
61
|
+
encoded.unshift((remaining & 0x7f) | 0x80);
|
|
62
|
+
remaining >>= 7;
|
|
63
|
+
}
|
|
64
|
+
bytes.push(...encoded);
|
|
65
|
+
}
|
|
66
|
+
return element(0x06, Buffer.from(bytes));
|
|
67
|
+
}
|
|
68
|
+
/** A UTCTime, `YYMMDDHHMMSSZ`, which X.509 uses for dates before 2050. */
|
|
69
|
+
export function utcTime(date) {
|
|
70
|
+
const text = date.toISOString().replace(/[-:T]/g, "").slice(2, 14);
|
|
71
|
+
return element(0x17, Buffer.from(`${text}Z`));
|
|
72
|
+
}
|
|
73
|
+
/** An X.509 Extension: identifier, `critical` only when true (DER omits the default), value. */
|
|
74
|
+
export function extension(identifier, critical, value) {
|
|
75
|
+
return sequence(oid(identifier), ...(critical ? [booleanTrue] : []), octetString(value));
|
|
76
|
+
}
|
|
77
|
+
/** The PEM of `bytes` under `label`, base64 in lines of 64. */
|
|
78
|
+
export function pem(label, bytes) {
|
|
79
|
+
const base64 = bytes.toString("base64");
|
|
80
|
+
const lines = [];
|
|
81
|
+
for (let offset = 0; offset < base64.length; offset += 64) {
|
|
82
|
+
lines.push(base64.slice(offset, offset + 64));
|
|
83
|
+
}
|
|
84
|
+
return `-----BEGIN ${label}-----\n${lines.join("\n")}\n-----END ${label}-----\n`;
|
|
85
|
+
}
|
|
86
|
+
/** The elements of a SEQUENCE's (or SET's) content, in order. */
|
|
87
|
+
export function children(content) {
|
|
88
|
+
const found = [];
|
|
89
|
+
let offset = 0;
|
|
90
|
+
while (offset < content.length) {
|
|
91
|
+
if (offset + 2 > content.length)
|
|
92
|
+
throw new MalformedDer(`element header at ${offset} is cut`);
|
|
93
|
+
const tag = content.readUInt8(offset);
|
|
94
|
+
let size = content.readUInt8(offset + 1);
|
|
95
|
+
let headerLength = 2;
|
|
96
|
+
if (size & 0x80) {
|
|
97
|
+
const count = size & 0x7f;
|
|
98
|
+
if (offset + 2 + count > content.length) {
|
|
99
|
+
throw new MalformedDer(`length octets at ${offset} are cut`);
|
|
100
|
+
}
|
|
101
|
+
size = 0;
|
|
102
|
+
for (let index = 0; index < count; index += 1) {
|
|
103
|
+
size = size * 0x100 + content.readUInt8(offset + 2 + index);
|
|
104
|
+
}
|
|
105
|
+
headerLength = 2 + count;
|
|
106
|
+
}
|
|
107
|
+
const end = offset + headerLength + size;
|
|
108
|
+
if (end > content.length)
|
|
109
|
+
throw new MalformedDer(`element at ${offset} runs past the end`);
|
|
110
|
+
found.push({
|
|
111
|
+
tag,
|
|
112
|
+
whole: content.subarray(offset, end),
|
|
113
|
+
content: content.subarray(offset + headerLength, end),
|
|
114
|
+
});
|
|
115
|
+
offset = end;
|
|
116
|
+
}
|
|
117
|
+
return found;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The subject Name of a certificate, as encoded: the element after the
|
|
121
|
+
* validity in its TBSCertificate, whether or not the version is present. These
|
|
122
|
+
* are the bytes a leaf's issuer field must repeat exactly for a strict verifier
|
|
123
|
+
* to chain them; OpenSSL compares names canonically, others compare bytes.
|
|
124
|
+
*/
|
|
125
|
+
export function subjectNameOf(certificate) {
|
|
126
|
+
const [outer] = children(certificate);
|
|
127
|
+
if (outer === undefined || outer.tag !== 0x30)
|
|
128
|
+
throw new MalformedDer("not a Certificate");
|
|
129
|
+
const [tbs] = children(outer.content);
|
|
130
|
+
if (tbs === undefined || tbs.tag !== 0x30)
|
|
131
|
+
throw new MalformedDer("not a TBSCertificate");
|
|
132
|
+
const fields = children(tbs.content);
|
|
133
|
+
const versionless = fields[0]?.tag === 0xa0 ? fields.slice(1) : fields;
|
|
134
|
+
// serial, signature algorithm, issuer, validity, subject
|
|
135
|
+
const subject = versionless[4];
|
|
136
|
+
if (subject === undefined || subject.tag !== 0x30)
|
|
137
|
+
throw new MalformedDer("no subject Name");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G,YAAY,EAAE,GAAG,CAAC;SACtD,CAAC,CAAC;QACH,MAAM,GAAG,GAAG,CAAC;IACf,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB;IAC/C,MAAM,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;IACtC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,GAAG,KAAK,IAAI;QAAE,MAAM,IAAI,YAAY,CAAC,mBAAmB,CAAC,CAAC;IAC3F,MAAM,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACtC,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI;QAAE,MAAM,IAAI,YAAY,CAAC,sBAAsB,CAAC,CAAC;IAC1F,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrC,MAAM,WAAW,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACvE,yDAAyD;IACzD,MAAM,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;IAC/B,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,GAAG,KAAK,IAAI;QAAE,MAAM,IAAI,YAAY,CAAC,iBAAiB,CAAC,CAAC;IAC7F,OAAO,OAAO,CAAC,KAAK,CAAC;AACvB,CAAC"}
|
|
@@ -24,7 +24,7 @@ export type DestinationRefusal =
|
|
|
24
24
|
"DESTINATION_UNKNOWN"
|
|
25
25
|
/** TLS or HTTP whose head does not follow its grammar. */
|
|
26
26
|
| "MALFORMED";
|
|
27
|
-
/** The most bytes read while waiting for a head; a hello
|
|
27
|
+
/** The most bytes read while waiting for a head; a hello that needs more at this size is refused. */
|
|
28
28
|
export declare const MAX_PEEK_BYTES = 65536;
|
|
29
29
|
/**
|
|
30
30
|
* Reads the destination from a connection's first bytes. `fallbackPort` is
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Destination.d.ts","sourceRoot":"","sources":["../../src/intercept/Destination.ts"],"names":[],"mappings":"AASA,OAAO,EAAmB,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE5E,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,MAAM,CAAC;AAE/C,MAAM,MAAM,kBAAkB;AAC5B,uEAAuE;AACrE;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE,GAC9B;IACE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uFAAuF;IACvF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,kBAAkB,CAAC;IACpC,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAC5C,QAAQ,CAAC,MAAM,CAAC,EAAE,kBAAkB,CAAC;CACtC,CAAC;AAEN,MAAM,MAAM,kBAAkB;AAC5B,4FAA4F;AAC1F,qBAAqB;AACvB,0DAA0D;GACxD,WAAW,CAAC;AAEhB,
|
|
1
|
+
{"version":3,"file":"Destination.d.ts","sourceRoot":"","sources":["../../src/intercept/Destination.ts"],"names":[],"mappings":"AASA,OAAO,EAAmB,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE5E,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,MAAM,CAAC;AAE/C,MAAM,MAAM,kBAAkB;AAC5B,uEAAuE;AACrE;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE,GAC9B;IACE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uFAAuF;IACvF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,kBAAkB,CAAC;IACpC,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAC5C,QAAQ,CAAC,MAAM,CAAC,EAAE,kBAAkB,CAAC;CACtC,CAAC;AAEN,MAAM,MAAM,kBAAkB;AAC5B,4FAA4F;AAC1F,qBAAqB;AACvB,0DAA0D;GACxD,WAAW,CAAC;AAEhB,qGAAqG;AACrG,eAAO,MAAM,cAAc,QAAS,CAAC;AAErC;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,GAAG,kBAAkB,CAuB3F"}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { readClientHello } from "./ClientHello.js";
|
|
10
10
|
import { readRequestHead } from "./RequestHead.js";
|
|
11
|
-
/** The most bytes read while waiting for a head; a hello
|
|
11
|
+
/** The most bytes read while waiting for a head; a hello that needs more at this size is refused. */
|
|
12
12
|
export const MAX_PEEK_BYTES = 65_536;
|
|
13
13
|
/**
|
|
14
14
|
* Reads the destination from a connection's first bytes. `fallbackPort` is
|
|
@@ -17,9 +17,35 @@ export const MAX_PEEK_BYTES = 65_536;
|
|
|
17
17
|
*/
|
|
18
18
|
export function readDestination(bytes, fallbackPort) {
|
|
19
19
|
const hello = readClientHello(bytes);
|
|
20
|
+
if (hello.kind !== "NOT_TLS")
|
|
21
|
+
return fromHello(hello, bytes.length, fallbackPort);
|
|
22
|
+
const head = readRequestHead(bytes);
|
|
23
|
+
switch (head.kind) {
|
|
24
|
+
case "NEED_MORE":
|
|
25
|
+
// The head reader bounds itself at its own maximum, well under the
|
|
26
|
+
// peek bound, so more is always worth waiting for here.
|
|
27
|
+
return { kind: "NEED_MORE" };
|
|
28
|
+
case "MALFORMED":
|
|
29
|
+
return { kind: "REFUSED", reason: "MALFORMED", protocol: "HTTP", detail: head.reason };
|
|
30
|
+
case "REQUEST":
|
|
31
|
+
return {
|
|
32
|
+
kind: "DESTINATION",
|
|
33
|
+
protocol: "HTTP",
|
|
34
|
+
host: head.host,
|
|
35
|
+
port: head.port ?? fallbackPort,
|
|
36
|
+
method: head.method,
|
|
37
|
+
target: head.target,
|
|
38
|
+
};
|
|
39
|
+
case "NOT_HTTP":
|
|
40
|
+
return { kind: "REFUSED", reason: "DESTINATION_UNKNOWN", protocol: null };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** A TLS reading as a destination: the server name on the port the connection was addressed to. */
|
|
44
|
+
function fromHello(hello, received, fallbackPort) {
|
|
20
45
|
switch (hello.kind) {
|
|
21
46
|
case "NEED_MORE":
|
|
22
|
-
|
|
47
|
+
// A hello still incomplete at the peek bound is not worth waiting for.
|
|
48
|
+
return received >= MAX_PEEK_BYTES
|
|
23
49
|
? { kind: "REFUSED", reason: "MALFORMED", protocol: "TLS" }
|
|
24
50
|
: { kind: "NEED_MORE" };
|
|
25
51
|
case "MALFORMED":
|
|
@@ -35,28 +61,6 @@ export function readDestination(bytes, fallbackPort) {
|
|
|
35
61
|
port: fallbackPort,
|
|
36
62
|
alpn: hello.alpn,
|
|
37
63
|
};
|
|
38
|
-
case "NOT_TLS":
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
const head = readRequestHead(bytes);
|
|
42
|
-
switch (head.kind) {
|
|
43
|
-
case "NEED_MORE":
|
|
44
|
-
return bytes.length >= MAX_PEEK_BYTES
|
|
45
|
-
? { kind: "REFUSED", reason: "MALFORMED", protocol: "HTTP", detail: "HEAD_TOO_LONG" }
|
|
46
|
-
: { kind: "NEED_MORE" };
|
|
47
|
-
case "MALFORMED":
|
|
48
|
-
return { kind: "REFUSED", reason: "MALFORMED", protocol: "HTTP", detail: head.reason };
|
|
49
|
-
case "REQUEST":
|
|
50
|
-
return {
|
|
51
|
-
kind: "DESTINATION",
|
|
52
|
-
protocol: "HTTP",
|
|
53
|
-
host: head.host,
|
|
54
|
-
port: head.port ?? fallbackPort,
|
|
55
|
-
method: head.method,
|
|
56
|
-
target: head.target,
|
|
57
|
-
};
|
|
58
|
-
case "NOT_HTTP":
|
|
59
|
-
return { kind: "REFUSED", reason: "DESTINATION_UNKNOWN", protocol: null };
|
|
60
64
|
}
|
|
61
65
|
}
|
|
62
66
|
//# sourceMappingURL=Destination.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Destination.js","sourceRoot":"","sources":["../../src/intercept/Destination.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"Destination.js","sourceRoot":"","sources":["../../src/intercept/Destination.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,eAAe,EAA2B,MAAM,kBAAkB,CAAC;AAC5E,OAAO,EAAE,eAAe,EAA2B,MAAM,kBAAkB,CAAC;AA8B5E,qGAAqG;AACrG,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC;AAErC;;;;GAIG;AACH,MAAM,UAAU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