@decionis/agentsafe 0.2.0 → 0.2.2

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 (117) hide show
  1. package/README.md +10 -1
  2. package/dist/Cli.js +16 -7
  3. package/dist/Cli.js.map +1 -1
  4. package/dist/Index.d.ts +5 -1
  5. package/dist/Index.d.ts.map +1 -1
  6. package/dist/Index.js +5 -1
  7. package/dist/Index.js.map +1 -1
  8. package/dist/cli/CliProcess.d.ts +2 -0
  9. package/dist/cli/CliProcess.d.ts.map +1 -1
  10. package/dist/cli/CliProcess.js +9 -1
  11. package/dist/cli/CliProcess.js.map +1 -1
  12. package/dist/cli/Commands.d.ts +1 -1
  13. package/dist/cli/Commands.d.ts.map +1 -1
  14. package/dist/cli/Commands.js +5 -0
  15. package/dist/cli/Commands.js.map +1 -1
  16. package/dist/cli/Credentials.d.ts.map +1 -1
  17. package/dist/cli/Credentials.js +8 -1
  18. package/dist/cli/Credentials.js.map +1 -1
  19. package/dist/cli/Help.d.ts.map +1 -1
  20. package/dist/cli/Help.js +4 -1
  21. package/dist/cli/Help.js.map +1 -1
  22. package/dist/cli/Intercept.d.ts +66 -0
  23. package/dist/cli/Intercept.d.ts.map +1 -0
  24. package/dist/cli/Intercept.js +374 -0
  25. package/dist/cli/Intercept.js.map +1 -0
  26. package/dist/cli/Login.d.ts +7 -2
  27. package/dist/cli/Login.d.ts.map +1 -1
  28. package/dist/cli/Login.js +106 -3
  29. package/dist/cli/Login.js.map +1 -1
  30. package/dist/cli/Status.d.ts.map +1 -1
  31. package/dist/cli/Status.js +18 -1
  32. package/dist/cli/Status.js.map +1 -1
  33. package/dist/cli/TestCommand.d.ts +33 -2
  34. package/dist/cli/TestCommand.d.ts.map +1 -1
  35. package/dist/cli/TestCommand.js +130 -4
  36. package/dist/cli/TestCommand.js.map +1 -1
  37. package/dist/cli/VerifyIntent.d.ts +28 -0
  38. package/dist/cli/VerifyIntent.d.ts.map +1 -0
  39. package/dist/cli/VerifyIntent.js +136 -0
  40. package/dist/cli/VerifyIntent.js.map +1 -0
  41. package/dist/egress/TransparentDial.d.ts +18 -0
  42. package/dist/egress/TransparentDial.d.ts.map +1 -0
  43. package/dist/egress/TransparentDial.js +33 -0
  44. package/dist/egress/TransparentDial.js.map +1 -0
  45. package/dist/gateway/Activation.d.ts +10 -3
  46. package/dist/gateway/Activation.d.ts.map +1 -1
  47. package/dist/gateway/Activation.js +17 -2
  48. package/dist/gateway/Activation.js.map +1 -1
  49. package/dist/gateway/BoundaryTest.d.ts +54 -0
  50. package/dist/gateway/BoundaryTest.d.ts.map +1 -1
  51. package/dist/gateway/BoundaryTest.js +19 -7
  52. package/dist/gateway/BoundaryTest.js.map +1 -1
  53. package/dist/gateway/Gateway.d.ts +39 -0
  54. package/dist/gateway/Gateway.d.ts.map +1 -1
  55. package/dist/gateway/Gateway.js +62 -4
  56. package/dist/gateway/Gateway.js.map +1 -1
  57. package/dist/gateway/GatewayConfig.d.ts +7 -0
  58. package/dist/gateway/GatewayConfig.d.ts.map +1 -1
  59. package/dist/gateway/GatewayConfig.js +12 -7
  60. package/dist/gateway/GatewayConfig.js.map +1 -1
  61. package/dist/gateway/GatewayReport.d.ts +2 -1
  62. package/dist/gateway/GatewayReport.d.ts.map +1 -1
  63. package/dist/gateway/GatewayReport.js +3 -0
  64. package/dist/gateway/GatewayReport.js.map +1 -1
  65. package/dist/gateway/HostedBoundaryTest.d.ts +118 -0
  66. package/dist/gateway/HostedBoundaryTest.d.ts.map +1 -0
  67. package/dist/gateway/HostedBoundaryTest.js +235 -0
  68. package/dist/gateway/HostedBoundaryTest.js.map +1 -0
  69. package/dist/gateway/InstallSurface.d.ts +26 -0
  70. package/dist/gateway/InstallSurface.d.ts.map +1 -0
  71. package/dist/gateway/InstallSurface.js +53 -0
  72. package/dist/gateway/InstallSurface.js.map +1 -0
  73. package/dist/gateway/ShadowLedger.d.ts +74 -0
  74. package/dist/gateway/ShadowLedger.d.ts.map +1 -0
  75. package/dist/gateway/ShadowLedger.js +112 -0
  76. package/dist/gateway/ShadowLedger.js.map +1 -0
  77. package/dist/http/GatewayHttpServer.d.ts +7 -0
  78. package/dist/http/GatewayHttpServer.d.ts.map +1 -1
  79. package/dist/http/GatewayHttpServer.js +8 -0
  80. package/dist/http/GatewayHttpServer.js.map +1 -1
  81. package/dist/http/InterceptGovernor.d.ts +71 -0
  82. package/dist/http/InterceptGovernor.d.ts.map +1 -0
  83. package/dist/http/InterceptGovernor.js +128 -0
  84. package/dist/http/InterceptGovernor.js.map +1 -0
  85. package/dist/http/InterceptServer.d.ts +124 -0
  86. package/dist/http/InterceptServer.d.ts.map +1 -0
  87. package/dist/http/InterceptServer.js +303 -0
  88. package/dist/http/InterceptServer.js.map +1 -0
  89. package/dist/intercept/ClientHello.d.ts +37 -0
  90. package/dist/intercept/ClientHello.d.ts.map +1 -0
  91. package/dist/intercept/ClientHello.js +216 -0
  92. package/dist/intercept/ClientHello.js.map +1 -0
  93. package/dist/intercept/Der.d.ts +59 -0
  94. package/dist/intercept/Der.d.ts.map +1 -0
  95. package/dist/intercept/Der.js +140 -0
  96. package/dist/intercept/Der.js.map +1 -0
  97. package/dist/intercept/Destination.d.ts +35 -0
  98. package/dist/intercept/Destination.d.ts.map +1 -0
  99. package/dist/intercept/Destination.js +66 -0
  100. package/dist/intercept/Destination.js.map +1 -0
  101. package/dist/intercept/InterceptLedger.d.ts +103 -0
  102. package/dist/intercept/InterceptLedger.d.ts.map +1 -0
  103. package/dist/intercept/InterceptLedger.js +101 -0
  104. package/dist/intercept/InterceptLedger.js.map +1 -0
  105. package/dist/intercept/LeafIssuer.d.ts +40 -0
  106. package/dist/intercept/LeafIssuer.d.ts.map +1 -0
  107. package/dist/intercept/LeafIssuer.js +124 -0
  108. package/dist/intercept/LeafIssuer.js.map +1 -0
  109. package/dist/intercept/RequestHead.d.ts +48 -0
  110. package/dist/intercept/RequestHead.d.ts.map +1 -0
  111. package/dist/intercept/RequestHead.js +163 -0
  112. package/dist/intercept/RequestHead.js.map +1 -0
  113. package/dist/verify/VerifyIntentVector.d.ts +25 -0
  114. package/dist/verify/VerifyIntentVector.d.ts.map +1 -0
  115. package/dist/verify/VerifyIntentVector.js +188 -0
  116. package/dist/verify/VerifyIntentVector.js.map +1 -0
  117. package/package.json +2 -2
@@ -0,0 +1,103 @@
1
+ /**
2
+ * What the interceptor saw, kept by the interceptor itself: for every
3
+ * connection it placed, the destination and the protocol, counted, with the
4
+ * bytes that crossed each way and the HTTP methods where the protocol showed
5
+ * them; for every connection it refused, the reason. It is the report an
6
+ * operator reads to learn what a workload actually reaches before any of it
7
+ * is governed, and it is counts and names only: host names and ports, never
8
+ * a path, a header, a body or an address of the caller.
9
+ */
10
+ import type { DestinationRefusal, InterceptProtocol } from "./Destination.js";
11
+ export type InterceptRefusal = DestinationRefusal
12
+ /** The client sent nothing decisive within the peek window. */
13
+ | "PEEK_TIMEOUT"
14
+ /** The destination could not be reached from here. */
15
+ | "UPSTREAM_UNREACHABLE"
16
+ /** The destination is this interceptor's own listener: a loop, refused. */
17
+ | "HOST_IS_INTERCEPTOR"
18
+ /** The interceptor holds as many connections as it will. */
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 {
26
+ readonly protocol: InterceptProtocol;
27
+ readonly governed: false;
28
+ readonly connections: number;
29
+ readonly bytes_to_destination: number;
30
+ readonly bytes_from_destination: number;
31
+ /** HTTP methods seen in the clear; empty for TLS, whose requests are not read. */
32
+ readonly methods: Readonly<Record<string, number>>;
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;
56
+ export interface InterceptSummary {
57
+ readonly since: string | null;
58
+ readonly until: string | null;
59
+ readonly connections: number;
60
+ readonly placed: number;
61
+ readonly refused: Readonly<Record<string, number>>;
62
+ /** By `host:port`, the destinations this workload reached. */
63
+ readonly destinations: Readonly<Record<string, DestinationCounts>>;
64
+ }
65
+ export interface InterceptReport {
66
+ readonly event: "INTERCEPT_REPORT";
67
+ readonly at: string;
68
+ readonly intercept: InterceptSummary;
69
+ /** What an operator does with the list, in their own terms. */
70
+ readonly next: string;
71
+ }
72
+ export declare class InterceptLedger {
73
+ private since;
74
+ private until;
75
+ private count;
76
+ private spliced;
77
+ private readonly refusals;
78
+ private readonly destinations;
79
+ /** A connection whose destination was read; returns the key `bytes` takes. */
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. */
83
+ bytes(key: string, toDestination: number, fromDestination: number): void;
84
+ /** A recorded connection whose destination answered: the splice is up. */
85
+ placed(): void;
86
+ /**
87
+ * A connection that was not placed, by the reason. A connection refused
88
+ * before its destination was read has not been counted yet and is counted
89
+ * here; one refused after `record` has.
90
+ */
91
+ refuse(reason: InterceptRefusal, at: string, countConnection: boolean): void;
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;
101
+ private touch;
102
+ }
103
+ //# sourceMappingURL=InterceptLedger.d.ts.map
@@ -0,0 +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;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,kFAAkF;IAClF,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACpD;AAED,uFAAuF;AACvF,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAA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IACpB,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC;IACrC,+DAA+D;IAC/D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAMD,qBAAa,eAAe;IAC1B,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA6B;IACtD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAwC;IAErE,8EAA8E;IACvE,MAAM,CACX,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,iBAAiB,EAC3B,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,EAAE,EAAE,MAAM,EACV,QAAQ,UAAQ,GACf,MAAM;IAcT,OAAO,CAAC,KAAK;IAab,2HAA2H;IACpH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,IAAI;IAU/E,0EAA0E;IACnE,MAAM,IAAI,IAAI;IAIrB;;;;OAIG;IACI,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM,EAAE,eAAe,EAAE,OAAO,GAAG,IAAI;IAMnF;;;;OAIG;IACI,OAAO,CAAC,QAAQ,GAAE,oBAAiC,GAAG,gBAAgB;IAgBtE,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,oBAAoB,GAAG,eAAe;IAS3E,wEAAwE;IACxE,IAAW,WAAW,IAAI,MAAM,CAE/B;IAED,OAAO,CAAC,KAAK;CAKd"}
@@ -0,0 +1,101 @@
1
+ /** The most destinations kept apart; the rest are counted under one name. */
2
+ const MAX_DESTINATIONS = 1_000;
3
+ const OTHER = "other";
4
+ export class InterceptLedger {
5
+ since = null;
6
+ until = null;
7
+ count = 0;
8
+ spliced = 0;
9
+ refusals = new Map();
10
+ destinations = new Map();
11
+ /** A connection whose destination was read; returns the key `bytes` takes. */
12
+ record(host, port, protocol, method, at, governed = false) {
13
+ this.touch(at);
14
+ const wanted = `${host}:${port}`;
15
+ const key = this.destinations.has(wanted) || this.destinations.size < MAX_DESTINATIONS ? wanted : OTHER;
16
+ const counts = this.destinations.get(key) ?? this.fresh(protocol, governed);
17
+ const methods = method === null
18
+ ? counts.methods
19
+ : { ...counts.methods, [method]: (counts.methods[method] ?? 0) + 1 };
20
+ this.destinations.set(key, { ...counts, connections: counts.connections + 1, methods });
21
+ return key;
22
+ }
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. */
36
+ bytes(key, toDestination, fromDestination) {
37
+ const counts = this.destinations.get(key);
38
+ if (counts === undefined || counts.governed)
39
+ return;
40
+ this.destinations.set(key, {
41
+ ...counts,
42
+ bytes_to_destination: counts.bytes_to_destination + toDestination,
43
+ bytes_from_destination: counts.bytes_from_destination + fromDestination,
44
+ });
45
+ }
46
+ /** A recorded connection whose destination answered: the splice is up. */
47
+ placed() {
48
+ this.spliced += 1;
49
+ }
50
+ /**
51
+ * A connection that was not placed, by the reason. A connection refused
52
+ * before its destination was read has not been counted yet and is counted
53
+ * here; one refused after `record` has.
54
+ */
55
+ refuse(reason, at, countConnection) {
56
+ if (countConnection)
57
+ this.touch(at);
58
+ else
59
+ this.until = at;
60
+ this.refusals.set(reason, (this.refusals.get(reason) ?? 0) + 1);
61
+ }
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
+ });
74
+ return {
75
+ since: this.since,
76
+ until: this.until,
77
+ connections: this.count,
78
+ placed: this.spliced,
79
+ refused: Object.fromEntries([...this.refusals.entries()].sort()),
80
+ destinations: Object.fromEntries(destinations),
81
+ };
82
+ }
83
+ report(at, governed) {
84
+ return {
85
+ event: "INTERCEPT_REPORT",
86
+ at,
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.",
89
+ };
90
+ }
91
+ /** How many connections have been counted so far, placed or refused. */
92
+ get connections() {
93
+ return this.count;
94
+ }
95
+ touch(at) {
96
+ this.since ??= at;
97
+ this.until = at;
98
+ this.count += 1;
99
+ }
100
+ }
101
+ //# sourceMappingURL=InterceptLedger.js.map
@@ -0,0 +1 @@
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,KAA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O,CAAC,GAAG,EAAE,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAU,CAAC;QACjF,CAAC,CAAC,CAAC;QACH,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,WAAW,EAAE,IAAI,CAAC,KAAK;YACvB,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,OAAO,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,CA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AAC,iBAAiB,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAC9C,SAAS,CAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAC7D,SAAS,CAAC,kBAAkB,EAAE,KAAK,EAAE,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,EAChE,SAAS,CAAC,gBAAgB,EAAE,KAAK,EAAE,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"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The head of a plaintext HTTP/1 request, read for where it was going: the
3
+ * `Host` header, or the authority of an absolute-form target, which a
4
+ * transparent interceptor has no other way to learn once the destination
5
+ * address has been rewritten to it. It reads the request line and the header
6
+ * names and values only as far as `Host`, keeps nothing, and refuses a head
7
+ * that two parsers could read differently (a repeated `Host`, a target that
8
+ * disagrees with it, a line the grammar does not allow) rather than choose.
9
+ *
10
+ * RFC 9112 sections 3 (request line), 3.2 (request target) and 5 (field
11
+ * syntax); RFC 9110 section 7.2 (Host).
12
+ */
13
+ /** The reading of a client's first bytes as HTTP/1. */
14
+ export type RequestHeadReading =
15
+ /** The bytes are an HTTP request whose head has not fully arrived. */
16
+ {
17
+ readonly kind: "NEED_MORE";
18
+ }
19
+ /** The bytes are not an HTTP/1 request. */
20
+ | {
21
+ readonly kind: "NOT_HTTP";
22
+ }
23
+ /** An HTTP/1 request whose destination cannot be placed with certainty. */
24
+ | {
25
+ readonly kind: "MALFORMED";
26
+ readonly reason: RequestHeadRefusal;
27
+ } | {
28
+ readonly kind: "REQUEST";
29
+ readonly method: string;
30
+ /** The path and query, as sent. */
31
+ readonly target: string;
32
+ readonly host: string;
33
+ /** The port the authority named; null when it named none. */
34
+ readonly port: number | null;
35
+ };
36
+ export type RequestHeadRefusal = "REQUEST_LINE_INVALID" | "HOST_MISSING" | "HOST_REPEATED" | "HOST_INVALID" | "TARGET_INVALID" | "TARGET_DISAGREES_WITH_HOST" | "HEADER_INVALID" | "HEAD_TOO_LONG";
37
+ /** The most head bytes read before a request is refused as too long to place. */
38
+ export declare const MAX_REQUEST_HEAD_BYTES = 16384;
39
+ /** Reads a client's first bytes as an HTTP/1 request head, or says why they are not one. */
40
+ export declare function readRequestHead(bytes: Uint8Array): RequestHeadReading;
41
+ interface Authority {
42
+ readonly host: string;
43
+ readonly port: number | null;
44
+ }
45
+ /** `host[:port]`, with the host a name, an IPv4 address or a bracketed IPv6 address, lowercased. */
46
+ export declare function parseAuthority(value: string): Authority | null;
47
+ export {};
48
+ //# sourceMappingURL=RequestHead.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RequestHead.d.ts","sourceRoot":"","sources":["../../src/intercept/RequestHead.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,uDAAuD;AACvD,MAAM,MAAM,kBAAkB;AAC5B,sEAAsE;AACpE;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE;AAChC,2CAA2C;GACzC;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAAE;AAC/B,2EAA2E;GACzE;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,kBAAkB,CAAA;CAAE,GACnE;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;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"}
@@ -0,0 +1,163 @@
1
+ /**
2
+ * The head of a plaintext HTTP/1 request, read for where it was going: the
3
+ * `Host` header, or the authority of an absolute-form target, which a
4
+ * transparent interceptor has no other way to learn once the destination
5
+ * address has been rewritten to it. It reads the request line and the header
6
+ * names and values only as far as `Host`, keeps nothing, and refuses a head
7
+ * that two parsers could read differently (a repeated `Host`, a target that
8
+ * disagrees with it, a line the grammar does not allow) rather than choose.
9
+ *
10
+ * RFC 9112 sections 3 (request line), 3.2 (request target) and 5 (field
11
+ * syntax); RFC 9110 section 7.2 (Host).
12
+ */
13
+ /** The most head bytes read before a request is refused as too long to place. */
14
+ export const MAX_REQUEST_HEAD_BYTES = 16_384;
15
+ const HEAD_END = "\r\n\r\n";
16
+ const LINE_END = "\r\n";
17
+ const TOKEN = /^[!#$%&'*+\-.^`|~\w]+$/;
18
+ /** Method, target and version: the target is origin-form or absolute-form, checked after. */
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. */
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])?)*$/;
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
+ });
40
+ /** Reads a client's first bytes as an HTTP/1 request head, or says why they are not one. */
41
+ export function readRequestHead(bytes) {
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));
45
+ const end = text.indexOf(HEAD_END);
46
+ if (end === -1) {
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;
50
+ }
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);
55
+ const match = REQUEST_LINE.exec(requestLine);
56
+ if (match === null) {
57
+ return REQUEST_LINE_START.test(text) ? malformed("REQUEST_LINE_INVALID") : NOT_HTTP;
58
+ }
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] ?? ""];
61
+ let hostHeader = null;
62
+ for (const line of headerLines) {
63
+ const header = readHeaderLine(line);
64
+ if (header === null)
65
+ return malformed("HEADER_INVALID");
66
+ if (header.name !== "host")
67
+ continue;
68
+ if (hostHeader !== null)
69
+ return malformed("HOST_REPEATED");
70
+ hostHeader = header.value;
71
+ }
72
+ let target = rawTarget;
73
+ let authority = null;
74
+ if (!rawTarget.startsWith("/")) {
75
+ const absolute = ABSOLUTE_TARGET.exec(rawTarget);
76
+ if (absolute === null)
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.
79
+ authority = parseAuthority(absolute[1] ?? "");
80
+ if (authority === null)
81
+ return malformed("TARGET_INVALID");
82
+ target = absolute[2] ?? "/";
83
+ }
84
+ if (hostHeader === null) {
85
+ if (authority === null)
86
+ return malformed("HOST_MISSING");
87
+ }
88
+ else {
89
+ const fromHeader = parseAuthority(hostHeader);
90
+ if (fromHeader === null)
91
+ return malformed("HOST_INVALID");
92
+ if (authority !== null &&
93
+ (authority.host !== fromHeader.host || authority.port !== fromHeader.port)) {
94
+ return malformed("TARGET_DISAGREES_WITH_HOST");
95
+ }
96
+ authority ??= fromHeader;
97
+ }
98
+ return { kind: "REQUEST", method, target, host: authority.host, port: authority.port };
99
+ }
100
+ /** `host[:port]`, with the host a name, an IPv4 address or a bracketed IPv6 address, lowercased. */
101
+ export function parseAuthority(value) {
102
+ const text = value.toLowerCase();
103
+ const match = IPV6_AUTHORITY.exec(text) ?? NAMED_AUTHORITY.exec(text);
104
+ if (match === null)
105
+ return null;
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
+ return null;
110
+ const digits = match[2];
111
+ if (digits === undefined)
112
+ return { host, port: null };
113
+ const port = Number(digits);
114
+ if (port < 1 || port > 65_535)
115
+ return null;
116
+ return { host, port };
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.
122
+ */
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);
132
+ }
133
+ /**
134
+ * One header line as `name: value`, the name a token lowercased and the value
135
+ * without its optional leading and trailing space or tab. A line with a bare
136
+ * CR or LF, no colon, or a name that is not a token is not a header.
137
+ */
138
+ function readHeaderLine(line) {
139
+ if (line.includes("\n") || line.includes("\r"))
140
+ return null;
141
+ const colon = line.indexOf(":");
142
+ if (colon === -1)
143
+ return null;
144
+ const name = line.slice(0, colon);
145
+ if (!TOKEN.test(name))
146
+ return null;
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);
156
+ }
157
+ function latin1(bytes) {
158
+ let text = "";
159
+ for (const byte of bytes)
160
+ text += String.fromCharCode(byte);
161
+ return text;
162
+ }
163
+ //# sourceMappingURL=RequestHead.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RequestHead.js","sourceRoot":"","sources":["../../src/intercept/RequestHead.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA8BH,iFAAiF;AACjF,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAE7C,MAAM,QAAQ,GAAG,UAAU,CAAC;AAC5B,MAAM,QAAQ,GAAG,MAAM,CAAC;AACxB,MAAM,KAAK,GAAG,wBAAwB,CAAC;AACvC,6FAA6F;AAC7F,MAAM,YAAY,GAAG,8CAA8C,CAAC;AACpE,6GAA6G;AAC7G,MAAM,eAAe,GAAG,+BAA+B,CAAC;AACxD,oFAAoF;AACpF,MAAM,kBAAkB,GAAG,wCAAwC,CAAC;AACpE,yFAAyF;AACzF,MAAM,SAAS,GACb,gGAAgG,CAAC;AACnG,uDAAuD;AACvD,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAC5D,qDAAqD;AACrD,MAAM,eAAe,GAAG,gCAAgC,CAAC;AACzD,0CAA0C;AAC1C,MAAM,KAAK,GAAG,sBAAsB,CAAC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