@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
|
@@ -16,15 +16,43 @@ export type InterceptRefusal = DestinationRefusal
|
|
|
16
16
|
/** The destination is this interceptor's own listener: a loop, refused. */
|
|
17
17
|
| "HOST_IS_INTERCEPTOR"
|
|
18
18
|
/** The interceptor holds as many connections as it will. */
|
|
19
|
-
| "TOO_MANY_CONNECTIONS"
|
|
20
|
-
|
|
19
|
+
| "TOO_MANY_CONNECTIONS"
|
|
20
|
+
/** The operator governs a list of destinations and refuses the rest; this was the rest. */
|
|
21
|
+
| "UNLISTED_DESTINATION"
|
|
22
|
+
/** A governed connection could not be taken: no authority to mint with, a failed handshake, no gateway. */
|
|
23
|
+
| "GOVERN_UNAVAILABLE";
|
|
24
|
+
/** A destination whose connections were spliced through: what the hop itself measured. */
|
|
25
|
+
export interface SplicedCounts {
|
|
21
26
|
readonly protocol: InterceptProtocol;
|
|
27
|
+
readonly governed: false;
|
|
22
28
|
readonly connections: number;
|
|
23
29
|
readonly bytes_to_destination: number;
|
|
24
30
|
readonly bytes_from_destination: number;
|
|
25
31
|
/** HTTP methods seen in the clear; empty for TLS, whose requests are not read. */
|
|
26
32
|
readonly methods: Readonly<Record<string, number>>;
|
|
27
33
|
}
|
|
34
|
+
/** What the gateway for a governed destination did with the requests it was handed. */
|
|
35
|
+
export interface GovernedGatewayCounts {
|
|
36
|
+
readonly mode: "SHADOW" | "ENFORCEMENT";
|
|
37
|
+
/** The gateway's own counts by name: `governed`, `interceptions`, `allows`, `blocks`, `escalations`, `shadow`, `passthrough`, ... */
|
|
38
|
+
readonly requests: Readonly<Record<string, number>>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A destination whose connections the gateway took: the hop counted the
|
|
42
|
+
* connections and read the first request's method where it was in the clear,
|
|
43
|
+
* and the requests themselves are the gateway's account, not a byte count.
|
|
44
|
+
*/
|
|
45
|
+
export interface GovernedCounts {
|
|
46
|
+
readonly protocol: InterceptProtocol;
|
|
47
|
+
readonly governed: true;
|
|
48
|
+
readonly connections: number;
|
|
49
|
+
readonly methods: Readonly<Record<string, number>>;
|
|
50
|
+
/** Null until the report is written, or when no request reached the gateway. */
|
|
51
|
+
readonly gateway: GovernedGatewayCounts | null;
|
|
52
|
+
}
|
|
53
|
+
export type DestinationCounts = SplicedCounts | GovernedCounts;
|
|
54
|
+
/** The gateway counts for a governed destination at report time, or null when none was created. */
|
|
55
|
+
export type GovernedCountsSource = (host: string, protocol: InterceptProtocol) => GovernedGatewayCounts | null;
|
|
28
56
|
export interface InterceptSummary {
|
|
29
57
|
readonly since: string | null;
|
|
30
58
|
readonly until: string | null;
|
|
@@ -44,13 +72,14 @@ export interface InterceptReport {
|
|
|
44
72
|
export declare class InterceptLedger {
|
|
45
73
|
private since;
|
|
46
74
|
private until;
|
|
47
|
-
private
|
|
75
|
+
private count;
|
|
48
76
|
private spliced;
|
|
49
77
|
private readonly refusals;
|
|
50
78
|
private readonly destinations;
|
|
51
79
|
/** A connection whose destination was read; returns the key `bytes` takes. */
|
|
52
|
-
record(host: string, port: number, protocol: InterceptProtocol, method: string | null, at: string): string;
|
|
53
|
-
|
|
80
|
+
record(host: string, port: number, protocol: InterceptProtocol, method: string | null, at: string, governed?: boolean): string;
|
|
81
|
+
private fresh;
|
|
82
|
+
/** Bytes that crossed for a spliced connection, by the key `record` returned; a governed destination has none to count. */
|
|
54
83
|
bytes(key: string, toDestination: number, fromDestination: number): void;
|
|
55
84
|
/** A recorded connection whose destination answered: the splice is up. */
|
|
56
85
|
placed(): void;
|
|
@@ -60,8 +89,15 @@ export declare class InterceptLedger {
|
|
|
60
89
|
* here; one refused after `record` has.
|
|
61
90
|
*/
|
|
62
91
|
refuse(reason: InterceptRefusal, at: string, countConnection: boolean): void;
|
|
63
|
-
|
|
64
|
-
|
|
92
|
+
/**
|
|
93
|
+
* The summary so far. A governed destination's `gateway` is filled from
|
|
94
|
+
* `governed`, the gateway's account of the requests it was handed, when one
|
|
95
|
+
* is given; the key is `host:port`, and the port names the protocol.
|
|
96
|
+
*/
|
|
97
|
+
summary(governed?: GovernedCountsSource): InterceptSummary;
|
|
98
|
+
report(at: string, governed?: GovernedCountsSource): InterceptReport;
|
|
99
|
+
/** How many connections have been counted so far, placed or refused. */
|
|
100
|
+
get connections(): number;
|
|
65
101
|
private touch;
|
|
66
102
|
}
|
|
67
103
|
//# sourceMappingURL=InterceptLedger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InterceptLedger.d.ts","sourceRoot":"","sources":["../../src/intercept/InterceptLedger.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE9E,MAAM,MAAM,gBAAgB,GACxB,kBAAkB;AACpB,+DAA+D;GAC7D,cAAc;AAChB,sDAAsD;GACpD,sBAAsB;AACxB,2EAA2E;GACzE,qBAAqB;AACvB,4DAA4D;GAC1D,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"InterceptLedger.d.ts","sourceRoot":"","sources":["../../src/intercept/InterceptLedger.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE9E,MAAM,MAAM,gBAAgB,GACxB,kBAAkB;AACpB,+DAA+D;GAC7D,cAAc;AAChB,sDAAsD;GACpD,sBAAsB;AACxB,2EAA2E;GACzE,qBAAqB;AACvB,4DAA4D;GAC1D,sBAAsB;AACxB,2FAA2F;GACzF,sBAAsB;AACxB,2GAA2G;GACzG,oBAAoB,CAAC;AAEzB,0FAA0F;AAC1F,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IACrC,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,sBAAsB,EAAE,MAAM,CAAC;IACxC,k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
|
@@ -4,32 +4,38 @@ const OTHER = "other";
|
|
|
4
4
|
export class InterceptLedger {
|
|
5
5
|
since = null;
|
|
6
6
|
until = null;
|
|
7
|
-
|
|
7
|
+
count = 0;
|
|
8
8
|
spliced = 0;
|
|
9
9
|
refusals = new Map();
|
|
10
10
|
destinations = new Map();
|
|
11
11
|
/** A connection whose destination was read; returns the key `bytes` takes. */
|
|
12
|
-
record(host, port, protocol, method, at) {
|
|
12
|
+
record(host, port, protocol, method, at, governed = false) {
|
|
13
13
|
this.touch(at);
|
|
14
14
|
const wanted = `${host}:${port}`;
|
|
15
15
|
const key = this.destinations.has(wanted) || this.destinations.size < MAX_DESTINATIONS ? wanted : OTHER;
|
|
16
|
-
const counts = this.destinations.get(key) ??
|
|
17
|
-
protocol,
|
|
18
|
-
connections: 0,
|
|
19
|
-
bytes_to_destination: 0,
|
|
20
|
-
bytes_from_destination: 0,
|
|
21
|
-
methods: {},
|
|
22
|
-
};
|
|
16
|
+
const counts = this.destinations.get(key) ?? this.fresh(protocol, governed);
|
|
23
17
|
const methods = method === null
|
|
24
18
|
? counts.methods
|
|
25
19
|
: { ...counts.methods, [method]: (counts.methods[method] ?? 0) + 1 };
|
|
26
20
|
this.destinations.set(key, { ...counts, connections: counts.connections + 1, methods });
|
|
27
21
|
return key;
|
|
28
22
|
}
|
|
29
|
-
|
|
23
|
+
fresh(protocol, governed) {
|
|
24
|
+
return governed
|
|
25
|
+
? { protocol, governed: true, connections: 0, methods: {}, gateway: null }
|
|
26
|
+
: {
|
|
27
|
+
protocol,
|
|
28
|
+
governed: false,
|
|
29
|
+
connections: 0,
|
|
30
|
+
bytes_to_destination: 0,
|
|
31
|
+
bytes_from_destination: 0,
|
|
32
|
+
methods: {},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Bytes that crossed for a spliced connection, by the key `record` returned; a governed destination has none to count. */
|
|
30
36
|
bytes(key, toDestination, fromDestination) {
|
|
31
37
|
const counts = this.destinations.get(key);
|
|
32
|
-
if (counts === undefined)
|
|
38
|
+
if (counts === undefined || counts.governed)
|
|
33
39
|
return;
|
|
34
40
|
this.destinations.set(key, {
|
|
35
41
|
...counts,
|
|
@@ -53,28 +59,43 @@ export class InterceptLedger {
|
|
|
53
59
|
this.until = at;
|
|
54
60
|
this.refusals.set(reason, (this.refusals.get(reason) ?? 0) + 1);
|
|
55
61
|
}
|
|
56
|
-
|
|
62
|
+
/**
|
|
63
|
+
* The summary so far. A governed destination's `gateway` is filled from
|
|
64
|
+
* `governed`, the gateway's account of the requests it was handed, when one
|
|
65
|
+
* is given; the key is `host:port`, and the port names the protocol.
|
|
66
|
+
*/
|
|
67
|
+
summary(governed = () => null) {
|
|
68
|
+
const destinations = [...this.destinations.entries()].sort().map(([key, counts]) => {
|
|
69
|
+
if (!counts.governed)
|
|
70
|
+
return [key, counts];
|
|
71
|
+
const host = key.slice(0, key.lastIndexOf(":"));
|
|
72
|
+
return [key, { ...counts, gateway: governed(host, counts.protocol) }];
|
|
73
|
+
});
|
|
57
74
|
return {
|
|
58
75
|
since: this.since,
|
|
59
76
|
until: this.until,
|
|
60
|
-
connections: this.
|
|
77
|
+
connections: this.count,
|
|
61
78
|
placed: this.spliced,
|
|
62
79
|
refused: Object.fromEntries([...this.refusals.entries()].sort()),
|
|
63
|
-
destinations: Object.fromEntries(
|
|
80
|
+
destinations: Object.fromEntries(destinations),
|
|
64
81
|
};
|
|
65
82
|
}
|
|
66
|
-
report(at) {
|
|
83
|
+
report(at, governed) {
|
|
67
84
|
return {
|
|
68
85
|
event: "INTERCEPT_REPORT",
|
|
69
86
|
at,
|
|
70
|
-
intercept: this.summary(),
|
|
71
|
-
next: "Every destination listed is somewhere this workload acts.
|
|
87
|
+
intercept: this.summary(governed),
|
|
88
|
+
next: "Every destination listed is somewhere this workload acts. Name the ones to govern in AGENTSAFE_INTERCEPT_GOVERN, with the authority the workload trusts, and this same hop asks Decionis before each consequential request reaches them.",
|
|
72
89
|
};
|
|
73
90
|
}
|
|
91
|
+
/** How many connections have been counted so far, placed or refused. */
|
|
92
|
+
get connections() {
|
|
93
|
+
return this.count;
|
|
94
|
+
}
|
|
74
95
|
touch(at) {
|
|
75
96
|
this.since ??= at;
|
|
76
97
|
this.until = at;
|
|
77
|
-
this.
|
|
98
|
+
this.count += 1;
|
|
78
99
|
}
|
|
79
100
|
}
|
|
80
101
|
//# sourceMappingURL=InterceptLedger.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InterceptLedger.js","sourceRoot":"","sources":["../../src/intercept/InterceptLedger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"InterceptLedger.js","sourceRoot":"","sources":["../../src/intercept/InterceptLedger.ts"],"names":[],"mappings":"AAoFA,6EAA6E;AAC7E,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAC/B,MAAM,KAAK,GAAG,OAAO,CAAC;AAEtB,MAAM,OAAO,eAAe;IAClB,KAAK,GAAkB,IAAI,CAAC;IAC5B,KAAK,GAAkB,IAAI,CAAC;IAC5B,KAAK,GAAG,CAAC,CAAC;IACV,OAAO,GAAG,CAAC,CAAC;IACH,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IACrC,YAAY,GAAG,IAAI,GAAG,EAA6B,CAAC;IAErE,8EAA8E;IACvE,MAAM,CACX,IAAY,EACZ,IAAY,EACZ,QAA2B,EAC3B,MAAqB,EACrB,EAAU,EACV,QAAQ,GAAG,KAAK;QAEhB,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACf,MAAM,MAAM,GAAG,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC;QACjC,MAAM,GAAG,GACP,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,GAAG,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;QAC9F,MAAM,MAAM,GAAsB,IAAI,CAAC,YAAY,CAAC,GA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,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;YAChE,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC;SAC/C,CAAC;IACJ,CAAC;IAEM,MAAM,CAAC,EAAU,EAAE,QAA+B;QACvD,OAAO;YACL,KAAK,EAAE,kBAAkB;YACzB,EAAE;YACF,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;YACjC,IAAI,EAAE,0OAA0O;SACjP,CAAC;IACJ,CAAC;IAED,wEAAwE;IACxE,IAAW,WAAW;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAEO,KAAK,CAAC,EAAU;QACtB,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC;IAClB,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare class LeafIssuerError extends Error {
|
|
2
|
+
readonly code: "CA_CERTIFICATE_INVALID" | "CA_KEY_INVALID" | "CA_KEY_MISMATCH" | "CA_NOT_AN_AUTHORITY";
|
|
3
|
+
constructor(code: "CA_CERTIFICATE_INVALID" | "CA_KEY_INVALID" | "CA_KEY_MISMATCH" | "CA_NOT_AN_AUTHORITY");
|
|
4
|
+
}
|
|
5
|
+
export interface OperatorAuthority {
|
|
6
|
+
/** The authority's certificate, PEM. Sent to clients as the chain, so an intermediate works too. */
|
|
7
|
+
readonly certificatePem: string;
|
|
8
|
+
/** The authority's private key, PEM (PKCS#8 or traditional), EC or RSA. */
|
|
9
|
+
readonly keyPem: string;
|
|
10
|
+
}
|
|
11
|
+
/** A minted leaf: what a TLS server presents for one host, and until when. */
|
|
12
|
+
export interface Leaf {
|
|
13
|
+
/** The leaf certificate alone, PEM. */
|
|
14
|
+
readonly certificatePem: string;
|
|
15
|
+
/** The leaf followed by the authority's certificate: the chain a client is sent. */
|
|
16
|
+
readonly chainPem: string;
|
|
17
|
+
/** The leaf's private key, PKCS#8 PEM, this process's alone. */
|
|
18
|
+
readonly keyPem: string;
|
|
19
|
+
readonly notAfter: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Mints and caches one leaf certificate per server name, signed by the
|
|
23
|
+
* operator's authority. The TLS context that presents a leaf is the
|
|
24
|
+
* listener's to build, under `src/http`, where sockets are opened.
|
|
25
|
+
*/
|
|
26
|
+
export declare class LeafIssuer {
|
|
27
|
+
private readonly clock;
|
|
28
|
+
private readonly signer;
|
|
29
|
+
private readonly signatureAlgorithm;
|
|
30
|
+
private readonly issuerName;
|
|
31
|
+
private readonly authorityPem;
|
|
32
|
+
private readonly leafKeyPem;
|
|
33
|
+
private readonly leafPublicKeyDer;
|
|
34
|
+
private readonly leaves;
|
|
35
|
+
constructor(authority: OperatorAuthority, clock?: () => number);
|
|
36
|
+
/** The leaf for `host`: minted now, or kept from before while it has more than two hours left. */
|
|
37
|
+
leafFor(host: string): Leaf;
|
|
38
|
+
private certificate;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=LeafIssuer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LeafIssuer.d.ts","sourceRoot":"","sources":["../../src/intercept/LeafIssuer.ts"],"names":[],"mappings":"AA0CA,qBAAa,eAAgB,SAAQ,KAAK;aAEtB,IAAI,EAClB,wBAAwB,GAAG,gBAAgB,GAAG,iBAAiB,GAAG,qBAAqB;gBADzE,IAAI,EAClB,wBAAwB,GAAG,gBAAgB,GAAG,iBAAiB,GAAG,qBAAqB;CAK5F;AAmBD,MAAM,WAAW,iBAAiB;IAChC,oGAAoG;IACpG,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,2EAA2E;IAC3E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,8EAA8E;AAC9E,MAAM,WAAW,IAAI;IACnB,uCAAuC;IACvC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,oFAAoF;IACpF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,gEAAgE;IAChE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED;;;;GAIG;AACH,qBAAa,UAAU;IAWnB,OAAO,CAAC,QAAQ,CAAC,KAAK;IAVxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAY;IACnC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAC5C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;IAC1C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2B;gBAGhD,SAAS,EAAE,iBAAiB,EACX,KAAK,GAAE,MAAM,MAAyB;IAgCzD,kGAAkG;IAC3F,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAoBlC,OAAO,CAAC,WAAW;CAwBpB"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The certificates the governing interceptor presents to a workload: one leaf
|
|
3
|
+
* per server name, minted from the operator's own certificate authority, so
|
|
4
|
+
* that a workload whose runtime trusts that authority completes its TLS
|
|
5
|
+
* handshake with the interceptor and never learns the difference. This is the
|
|
6
|
+
* price of governing HTTPS transparently, stated in ADR 0005, and this module
|
|
7
|
+
* is where it is paid: the authority's private key is read from a file the
|
|
8
|
+
* operator mounts, the leaf's key is generated once per process and never
|
|
9
|
+
* written anywhere, and each leaf names exactly one host, for a day.
|
|
10
|
+
*
|
|
11
|
+
* The shape is one X.509 v3 certificate with the extensions a TLS client
|
|
12
|
+
* reads (basic constraints, key usage, extended key usage for server
|
|
13
|
+
* authentication, one subject alternative name), written with the DER
|
|
14
|
+
* functions in `Der.ts` and signed with the authority's key: ECDSA with
|
|
15
|
+
* SHA-256 for an EC key, sha256WithRSAEncryption for an RSA key. The leaf's
|
|
16
|
+
* issuer is the authority certificate's subject, taken byte for byte from the
|
|
17
|
+
* certificate, so a verifier that compares names as bytes chains them.
|
|
18
|
+
*/
|
|
19
|
+
import { createPrivateKey, generateKeyPairSync, randomBytes, sign, X509Certificate, } from "node:crypto";
|
|
20
|
+
import { asn1Null, bitString, explicit, extension, implicit, integer, oid, pem, sequence, set, subjectNameOf, utcTime, utf8String, } from "./Der.js";
|
|
21
|
+
export class LeafIssuerError extends Error {
|
|
22
|
+
code;
|
|
23
|
+
constructor(code) {
|
|
24
|
+
super(code);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.name = "LeafIssuerError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const ECDSA_WITH_SHA256 = "1.2.840.10045.4.3.2";
|
|
30
|
+
const SHA256_WITH_RSA = "1.2.840.113549.1.1.11";
|
|
31
|
+
const COMMON_NAME = "2.5.4.3";
|
|
32
|
+
const BASIC_CONSTRAINTS = "2.5.29.19";
|
|
33
|
+
const KEY_USAGE = "2.5.29.15";
|
|
34
|
+
const SUBJECT_ALT_NAME = "2.5.29.17";
|
|
35
|
+
const EXTENDED_KEY_USAGE = "2.5.29.37";
|
|
36
|
+
const SERVER_AUTH = "1.3.6.1.5.5.7.3.1";
|
|
37
|
+
const HOUR_MS = 60 * 60 * 1_000;
|
|
38
|
+
/** A leaf is valid for a day, backdated an hour for clocks that disagree. */
|
|
39
|
+
const LEAF_LIFETIME_MS = 24 * HOUR_MS;
|
|
40
|
+
const LEAF_BACKDATE_MS = HOUR_MS;
|
|
41
|
+
/** A cached leaf is reissued when no more than this remains, so no client sees one expire. */
|
|
42
|
+
const REISSUE_MARGIN_MS = 2 * HOUR_MS;
|
|
43
|
+
/** The most leaves kept at once; a governed set is a handful of hosts, and this is the ceiling. */
|
|
44
|
+
const MAX_CACHED_LEAVES = 1_000;
|
|
45
|
+
/**
|
|
46
|
+
* Mints and caches one leaf certificate per server name, signed by the
|
|
47
|
+
* operator's authority. The TLS context that presents a leaf is the
|
|
48
|
+
* listener's to build, under `src/http`, where sockets are opened.
|
|
49
|
+
*/
|
|
50
|
+
export class LeafIssuer {
|
|
51
|
+
clock;
|
|
52
|
+
signer;
|
|
53
|
+
signatureAlgorithm;
|
|
54
|
+
issuerName;
|
|
55
|
+
authorityPem;
|
|
56
|
+
leafKeyPem;
|
|
57
|
+
leafPublicKeyDer;
|
|
58
|
+
leaves = new Map();
|
|
59
|
+
constructor(authority, clock = () => Date.now()) {
|
|
60
|
+
this.clock = clock;
|
|
61
|
+
let certificate;
|
|
62
|
+
try {
|
|
63
|
+
certificate = new X509Certificate(authority.certificatePem);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new LeafIssuerError("CA_CERTIFICATE_INVALID");
|
|
67
|
+
}
|
|
68
|
+
if (!certificate.ca)
|
|
69
|
+
throw new LeafIssuerError("CA_NOT_AN_AUTHORITY");
|
|
70
|
+
try {
|
|
71
|
+
this.signer = createPrivateKey(authority.keyPem);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
throw new LeafIssuerError("CA_KEY_INVALID");
|
|
75
|
+
}
|
|
76
|
+
if (!certificate.checkPrivateKey(this.signer))
|
|
77
|
+
throw new LeafIssuerError("CA_KEY_MISMATCH");
|
|
78
|
+
switch (this.signer.asymmetricKeyType) {
|
|
79
|
+
case "ec":
|
|
80
|
+
this.signatureAlgorithm = sequence(oid(ECDSA_WITH_SHA256));
|
|
81
|
+
break;
|
|
82
|
+
case "rsa":
|
|
83
|
+
this.signatureAlgorithm = sequence(oid(SHA256_WITH_RSA), asn1Null);
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
throw new LeafIssuerError("CA_KEY_INVALID");
|
|
87
|
+
}
|
|
88
|
+
this.issuerName = subjectNameOf(certificate.raw);
|
|
89
|
+
this.authorityPem = certificate.toString();
|
|
90
|
+
const keys = generateKeyPairSync("ec", { namedCurve: "P-256" });
|
|
91
|
+
this.leafKeyPem = keys.privateKey.export({ type: "pkcs8", format: "pem" });
|
|
92
|
+
this.leafPublicKeyDer = keys.publicKey.export({ type: "spki", format: "der" });
|
|
93
|
+
}
|
|
94
|
+
/** The leaf for `host`: minted now, or kept from before while it has more than two hours left. */
|
|
95
|
+
leafFor(host) {
|
|
96
|
+
const now = this.clock();
|
|
97
|
+
const cached = this.leaves.get(host);
|
|
98
|
+
if (cached !== undefined && cached.notAfter - now > REISSUE_MARGIN_MS)
|
|
99
|
+
return cached;
|
|
100
|
+
if (this.leaves.size >= MAX_CACHED_LEAVES && cached === undefined)
|
|
101
|
+
this.leaves.clear();
|
|
102
|
+
const notAfter = now + LEAF_LIFETIME_MS;
|
|
103
|
+
const certificatePem = pem("CERTIFICATE", this.certificate(host, now - LEAF_BACKDATE_MS, notAfter));
|
|
104
|
+
const leaf = {
|
|
105
|
+
certificatePem,
|
|
106
|
+
chainPem: `${certificatePem}${this.authorityPem}`,
|
|
107
|
+
keyPem: this.leafKeyPem,
|
|
108
|
+
notAfter,
|
|
109
|
+
};
|
|
110
|
+
this.leaves.set(host, leaf);
|
|
111
|
+
return leaf;
|
|
112
|
+
}
|
|
113
|
+
certificate(host, notBefore, notAfter) {
|
|
114
|
+
// A positive serial in minimal form: the first byte is never zero and never has its top bit set.
|
|
115
|
+
const serial = randomBytes(16);
|
|
116
|
+
serial.writeUInt8((serial.readUInt8(0) & 0x7f) | 0x01, 0);
|
|
117
|
+
const subject = sequence(set(sequence(oid(COMMON_NAME), utf8String(host))));
|
|
118
|
+
const extensions = sequence(extension(BASIC_CONSTRAINTS, true, sequence()), extension(KEY_USAGE, true, bitString(Buffer.from([0x80]), 7)), extension(EXTENDED_KEY_USAGE, false, sequence(oid(SERVER_AUTH))), extension(SUBJECT_ALT_NAME, false, sequence(implicit(2, Buffer.from(host)))));
|
|
119
|
+
const tbs = sequence(explicit(0, integer(Buffer.from([2]))), integer(serial), this.signatureAlgorithm, this.issuerName, sequence(utcTime(new Date(notBefore)), utcTime(new Date(notAfter))), subject, this.leafPublicKeyDer, explicit(3, extensions));
|
|
120
|
+
const signature = sign("sha256", tbs, this.signer);
|
|
121
|
+
return sequence(tbs, this.signatureAlgorithm, bitString(signature));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=LeafIssuer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LeafIssuer.js","sourceRoot":"","sources":["../../src/intercept/LeafIssuer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,IAAI,EACJ,eAAe,GAEhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,OAAO,EACP,GAAG,EACH,GAAG,EACH,QAAQ,EACR,GAAG,EACH,aAAa,EACb,OAAO,EACP,UAAU,GACX,MAAM,UAAU,CAAC;AAElB,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAEtB;IADlB,YACkB,IACyE;QAEzF,KAAK,CAAC,IAAI,CAAC,CAAC;QAHI,SAAI,GAAJ,IAAI,CACqE;QAGzF,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;AAChD,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAChD,MAAM,WAAW,GAAG,SAAS,CAAC;AAC9B,MAAM,iBAAiB,GAAG,WAAW,CAAC;AACtC,MAAM,SAAS,GAAG,WAAW,CAAC;AAC9B,MAAM,gBAAgB,GAAG,WAAW,CAAC;AACrC,MAAM,kBAAkB,GAAG,WAAW,CAAC;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E,QAAQ,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAC7E,CAAC;QACF,MAAM,GAAG,GAAG,QAAQ,CAClB,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EACtC,OAAO,CAAC,MAAM,CAAC,EACf,IAAI,CAAC,kBAAkB,EACvB,IAAI,CAAC,UAAU,EACf,QAAQ,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EACnE,OAAO,EACP,IAAI,CAAC,gBAAgB,EACrB,QAAQ,CAAC,CAAC,EAAE,UAAU,CAAC,CACxB,CAAC;QACF,MAAM,SAAS,GAAG,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACnD,OAAO,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC;IACtE,CAAC;CACF"}
|
|
@@ -42,7 +42,7 @@ interface Authority {
|
|
|
42
42
|
readonly host: string;
|
|
43
43
|
readonly port: number | null;
|
|
44
44
|
}
|
|
45
|
-
/** `host[:port]`, with the host a name, an IPv4 address or a bracketed IPv6 address. */
|
|
45
|
+
/** `host[:port]`, with the host a name, an IPv4 address or a bracketed IPv6 address, lowercased. */
|
|
46
46
|
export declare function parseAuthority(value: string): Authority | null;
|
|
47
47
|
export {};
|
|
48
48
|
//# sourceMappingURL=RequestHead.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestHead.d.ts","sourceRoot":"","sources":["../../src/intercept/RequestHead.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,uDAAuD;AACvD,MAAM,MAAM,kB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C;IACxB,mCAAmC;IACnC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6DAA6D;IAC7D,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEN,MAAM,MAAM,kBAAkB,GAC1B,sBAAsB,GACtB,cAAc,GACd,eAAe,GACf,cAAc,GACd,gBAAgB,GAChB,4BAA4B,GAC5B,gBAAgB,GAChB,eAAe,CAAC;AAEpB,iFAAiF;AACjF,eAAO,MAAM,sBAAsB,QAAS,CAAC;AA8B7C,4FAA4F;AAC5F,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,kBAAkB,CAsDrE;AAED,UAAU,SAAS;IACjB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,oGAAoG;AACpG,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAY9D"}
|
|
@@ -13,134 +13,146 @@
|
|
|
13
13
|
/** The most head bytes read before a request is refused as too long to place. */
|
|
14
14
|
export const MAX_REQUEST_HEAD_BYTES = 16_384;
|
|
15
15
|
const HEAD_END = "\r\n\r\n";
|
|
16
|
+
const LINE_END = "\r\n";
|
|
16
17
|
const TOKEN = /^[!#$%&'*+\-.^`|~\w]+$/;
|
|
18
|
+
/** Method, target and version: the target is origin-form or absolute-form, checked after. */
|
|
17
19
|
const REQUEST_LINE = /^([!#$%&'*+\-.^`|~\w]+) (\S+) HTTP\/1\.[01]$/;
|
|
20
|
+
/** Absolute-form target: the scheme this interceptor speaks, an authority, and a path that may be absent. */
|
|
21
|
+
const ABSOLUTE_TARGET = /^http:\/\/([^/?#]+)(\/\S*)?$/i;
|
|
22
|
+
/** A method token followed by a space, or by the end of what has arrived so far. */
|
|
23
|
+
const REQUEST_LINE_START = /^[A-Z][!#$%&'*+\-.^`|~\w]{0,19}(?: |$)/;
|
|
24
|
+
/** RFC 1123 host name: labels of letters, digits and hyphens, at most 253 characters. */
|
|
18
25
|
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])?)*$/;
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
26
|
+
/** A bracketed IPv6 literal, then an optional port. */
|
|
27
|
+
const IPV6_AUTHORITY = /^(\[[0-9a-f:.]+\])(?::(\d{1,5}))?$/;
|
|
28
|
+
/** A name or IPv4 address, then an optional port. */
|
|
29
|
+
const NAMED_AUTHORITY = /^([a-z0-9.-]+)(?::(\d{1,5}))?$/;
|
|
30
|
+
/** One decimal octet, no leading zero. */
|
|
31
|
+
const OCTET = /^(?:0|[1-9]\d{0,2})$/;
|
|
32
|
+
/** What can only be meant as an address. */
|
|
33
|
+
const DIGITS_AND_DOTS = /^[\d.]+$/;
|
|
34
|
+
const NEED_MORE = { kind: "NEED_MORE" };
|
|
35
|
+
const NOT_HTTP = { kind: "NOT_HTTP" };
|
|
36
|
+
const malformed = (reason) => ({
|
|
37
|
+
kind: "MALFORMED",
|
|
38
|
+
reason,
|
|
39
|
+
});
|
|
22
40
|
/** Reads a client's first bytes as an HTTP/1 request head, or says why they are not one. */
|
|
23
41
|
export function readRequestHead(bytes) {
|
|
24
|
-
|
|
42
|
+
// Only as much as a head may be, plus the terminator that would end it.
|
|
43
|
+
const window = Math.min(bytes.length, MAX_REQUEST_HEAD_BYTES + HEAD_END.length);
|
|
44
|
+
const text = latin1(bytes.subarray(0, window));
|
|
25
45
|
const end = text.indexOf(HEAD_END);
|
|
26
46
|
if (end === -1) {
|
|
27
|
-
if (!
|
|
28
|
-
return
|
|
29
|
-
return bytes.length > MAX_REQUEST_HEAD_BYTES
|
|
30
|
-
? { kind: "MALFORMED", reason: "HEAD_TOO_LONG" }
|
|
31
|
-
: { kind: "NEED_MORE" };
|
|
47
|
+
if (!REQUEST_LINE_START.test(text))
|
|
48
|
+
return NOT_HTTP;
|
|
49
|
+
return bytes.length > MAX_REQUEST_HEAD_BYTES ? malformed("HEAD_TOO_LONG") : NEED_MORE;
|
|
32
50
|
}
|
|
33
|
-
const
|
|
34
|
-
const
|
|
51
|
+
const head = text.slice(0, end);
|
|
52
|
+
const firstLineEnd = head.indexOf(LINE_END);
|
|
53
|
+
const requestLine = firstLineEnd === -1 ? head : head.slice(0, firstLineEnd);
|
|
54
|
+
const headerLines = firstLineEnd === -1 ? [] : head.slice(firstLineEnd + LINE_END.length).split(LINE_END);
|
|
35
55
|
const match = REQUEST_LINE.exec(requestLine);
|
|
36
56
|
if (match === null) {
|
|
37
|
-
return
|
|
38
|
-
? { kind: "MALFORMED", reason: "REQUEST_LINE_INVALID" }
|
|
39
|
-
: { kind: "NOT_HTTP" };
|
|
57
|
+
return REQUEST_LINE_START.test(text) ? malformed("REQUEST_LINE_INVALID") : NOT_HTTP;
|
|
40
58
|
}
|
|
41
|
-
|
|
42
|
-
const rawTarget = match[2] ?? "";
|
|
43
|
-
if (METHODS_WITHOUT_ORIGIN_FORM.has(method))
|
|
44
|
-
return { kind: "MALFORMED", reason: "TARGET_INVALID" };
|
|
59
|
+
// Stryker disable next-line all: both groups are matched whenever the line is; the fallbacks satisfy the type.
|
|
60
|
+
const [method, rawTarget] = [match[1] ?? "", match[2] ?? ""];
|
|
45
61
|
let hostHeader = null;
|
|
46
|
-
for (const line of
|
|
62
|
+
for (const line of headerLines) {
|
|
47
63
|
const header = readHeaderLine(line);
|
|
48
64
|
if (header === null)
|
|
49
|
-
return
|
|
65
|
+
return malformed("HEADER_INVALID");
|
|
50
66
|
if (header.name !== "host")
|
|
51
67
|
continue;
|
|
52
68
|
if (hostHeader !== null)
|
|
53
|
-
return
|
|
69
|
+
return malformed("HOST_REPEATED");
|
|
54
70
|
hostHeader = header.value;
|
|
55
71
|
}
|
|
56
72
|
let target = rawTarget;
|
|
57
73
|
let authority = null;
|
|
58
74
|
if (!rawTarget.startsWith("/")) {
|
|
59
|
-
const absolute =
|
|
75
|
+
const absolute = ABSOLUTE_TARGET.exec(rawTarget);
|
|
60
76
|
if (absolute === null)
|
|
61
|
-
return
|
|
77
|
+
return malformed("TARGET_INVALID");
|
|
78
|
+
// Stryker disable next-line all: the authority group is matched whenever the target is; the fallback satisfies the type.
|
|
62
79
|
authority = parseAuthority(absolute[1] ?? "");
|
|
63
80
|
if (authority === null)
|
|
64
|
-
return
|
|
81
|
+
return malformed("TARGET_INVALID");
|
|
65
82
|
target = absolute[2] ?? "/";
|
|
66
83
|
}
|
|
67
84
|
if (hostHeader === null) {
|
|
68
85
|
if (authority === null)
|
|
69
|
-
return
|
|
86
|
+
return malformed("HOST_MISSING");
|
|
70
87
|
}
|
|
71
88
|
else {
|
|
72
89
|
const fromHeader = parseAuthority(hostHeader);
|
|
73
90
|
if (fromHeader === null)
|
|
74
|
-
return
|
|
91
|
+
return malformed("HOST_INVALID");
|
|
75
92
|
if (authority !== null &&
|
|
76
93
|
(authority.host !== fromHeader.host || authority.port !== fromHeader.port)) {
|
|
77
|
-
return
|
|
94
|
+
return malformed("TARGET_DISAGREES_WITH_HOST");
|
|
78
95
|
}
|
|
79
96
|
authority ??= fromHeader;
|
|
80
97
|
}
|
|
81
98
|
return { kind: "REQUEST", method, target, host: authority.host, port: authority.port };
|
|
82
99
|
}
|
|
83
|
-
/** `host[:port]`, with the host a name, an IPv4 address or a bracketed IPv6 address. */
|
|
100
|
+
/** `host[:port]`, with the host a name, an IPv4 address or a bracketed IPv6 address, lowercased. */
|
|
84
101
|
export function parseAuthority(value) {
|
|
85
|
-
const text = value.
|
|
86
|
-
|
|
102
|
+
const text = value.toLowerCase();
|
|
103
|
+
const match = IPV6_AUTHORITY.exec(text) ?? NAMED_AUTHORITY.exec(text);
|
|
104
|
+
if (match === null)
|
|
87
105
|
return null;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (
|
|
91
|
-
const close = text.indexOf("]");
|
|
92
|
-
if (close === -1)
|
|
93
|
-
return null;
|
|
94
|
-
host = text.slice(0, close + 1);
|
|
95
|
-
rest = text.slice(close + 1);
|
|
96
|
-
if (!IPV6_LITERAL.test(host))
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
const colon = text.indexOf(":");
|
|
101
|
-
host = colon === -1 ? text : text.slice(0, colon);
|
|
102
|
-
rest = colon === -1 ? "" : text.slice(colon);
|
|
103
|
-
if (!HOST_NAME.test(host) && !IPV4.test(host))
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
if (rest === "")
|
|
107
|
-
return { host, port: null };
|
|
108
|
-
if (!/^:\d{1,5}$/.test(rest))
|
|
106
|
+
// Stryker disable next-line all: the host group is matched whenever the authority is; the fallback satisfies the type.
|
|
107
|
+
const host = match[1] ?? "";
|
|
108
|
+
if (!host.startsWith("[") && !isHost(host))
|
|
109
109
|
return null;
|
|
110
|
-
const
|
|
110
|
+
const digits = match[2];
|
|
111
|
+
if (digits === undefined)
|
|
112
|
+
return { host, port: null };
|
|
113
|
+
const port = Number(digits);
|
|
111
114
|
if (port < 1 || port > 65_535)
|
|
112
115
|
return null;
|
|
113
116
|
return { host, port };
|
|
114
117
|
}
|
|
115
118
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
+
* A host name, or an IPv4 address. Digits and dots alone are read as an
|
|
120
|
+
* address and must be a valid one: `256.1.1.1` is a name to the grammar and an
|
|
121
|
+
* address to a reader, which is exactly the disagreement this parser refuses.
|
|
119
122
|
*/
|
|
120
|
-
function
|
|
121
|
-
|
|
123
|
+
function isHost(host) {
|
|
124
|
+
if (DIGITS_AND_DOTS.test(host))
|
|
125
|
+
return isIpv4(host);
|
|
126
|
+
return HOST_NAME.test(host);
|
|
127
|
+
}
|
|
128
|
+
/** Four decimal octets, each without a leading zero and at most 255. */
|
|
129
|
+
function isIpv4(text) {
|
|
130
|
+
const octets = text.split(".");
|
|
131
|
+
return octets.length === 4 && octets.every((octet) => OCTET.test(octet) && Number(octet) <= 255);
|
|
122
132
|
}
|
|
123
133
|
/**
|
|
124
134
|
* One header line as `name: value`, the name a token lowercased and the value
|
|
125
135
|
* without its optional leading and trailing space or tab. A line with a bare
|
|
126
|
-
* CR or LF,
|
|
136
|
+
* CR or LF, no colon, or a name that is not a token is not a header.
|
|
127
137
|
*/
|
|
128
138
|
function readHeaderLine(line) {
|
|
129
139
|
if (line.includes("\n") || line.includes("\r"))
|
|
130
140
|
return null;
|
|
131
141
|
const colon = line.indexOf(":");
|
|
132
|
-
if (colon
|
|
142
|
+
if (colon === -1)
|
|
133
143
|
return null;
|
|
134
144
|
const name = line.slice(0, colon);
|
|
135
145
|
if (!TOKEN.test(name))
|
|
136
146
|
return null;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
147
|
+
return { name: name.toLowerCase(), value: trimBlanks(line.slice(colon + 1)) };
|
|
148
|
+
}
|
|
149
|
+
/** Without the optional leading and trailing spaces and tabs; nothing else is whitespace to HTTP. */
|
|
150
|
+
function trimBlanks(value) {
|
|
151
|
+
// Both searches find the same absence in a blank value, and the slice from
|
|
152
|
+
// one past the other's end is empty.
|
|
153
|
+
const start = value.search(/[^ \t]/);
|
|
154
|
+
const end = value.search(/[^ \t][ \t]*$/);
|
|
155
|
+
return value.slice(start, end + 1);
|
|
144
156
|
}
|
|
145
157
|
function latin1(bytes) {
|
|
146
158
|
let text = "";
|