@stigmer/outbound 3.18.1-dev.20260919070736
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/LICENSE +190 -0
- package/README.md +22 -0
- package/egress/address.d.ts +23 -0
- package/egress/address.d.ts.map +1 -0
- package/egress/address.js +148 -0
- package/egress/address.js.map +1 -0
- package/egress/check.d.ts +50 -0
- package/egress/check.d.ts.map +1 -0
- package/egress/check.js +127 -0
- package/egress/check.js.map +1 -0
- package/egress/fetch.d.ts +52 -0
- package/egress/fetch.d.ts.map +1 -0
- package/egress/fetch.js +59 -0
- package/egress/fetch.js.map +1 -0
- package/egress/index.d.ts +10 -0
- package/egress/index.d.ts.map +1 -0
- package/egress/index.js +10 -0
- package/egress/index.js.map +1 -0
- package/egress/node-lookup.d.ts +4 -0
- package/egress/node-lookup.d.ts.map +1 -0
- package/egress/node-lookup.js +20 -0
- package/egress/node-lookup.js.map +1 -0
- package/index.d.ts +11 -0
- package/index.d.ts.map +1 -0
- package/index.js +11 -0
- package/index.js.map +1 -0
- package/mcp-oauth/challenge.d.ts +25 -0
- package/mcp-oauth/challenge.d.ts.map +1 -0
- package/mcp-oauth/challenge.js +33 -0
- package/mcp-oauth/challenge.js.map +1 -0
- package/mcp-oauth/index.d.ts +11 -0
- package/mcp-oauth/index.d.ts.map +1 -0
- package/mcp-oauth/index.js +11 -0
- package/mcp-oauth/index.js.map +1 -0
- package/mcp-oauth/metadata.d.ts +86 -0
- package/mcp-oauth/metadata.d.ts.map +1 -0
- package/mcp-oauth/metadata.js +113 -0
- package/mcp-oauth/metadata.js.map +1 -0
- package/mcp-oauth/probe.d.ts +68 -0
- package/mcp-oauth/probe.d.ts.map +1 -0
- package/mcp-oauth/probe.js +35 -0
- package/mcp-oauth/probe.js.map +1 -0
- package/mcp-oauth/request.d.ts +44 -0
- package/mcp-oauth/request.d.ts.map +1 -0
- package/mcp-oauth/request.js +71 -0
- package/mcp-oauth/request.js.map +1 -0
- package/package.json +42 -0
- package/src/__tests__/address.test.ts +88 -0
- package/src/__tests__/challenge.test.ts +41 -0
- package/src/__tests__/check.test.ts +98 -0
- package/src/__tests__/fetch.test.ts +161 -0
- package/src/__tests__/metadata.test.ts +171 -0
- package/src/__tests__/probe.test.ts +98 -0
- package/src/egress/address.ts +168 -0
- package/src/egress/check.ts +163 -0
- package/src/egress/fetch.ts +104 -0
- package/src/egress/index.ts +17 -0
- package/src/egress/node-lookup.ts +22 -0
- package/src/index.ts +10 -0
- package/src/mcp-oauth/challenge.ts +33 -0
- package/src/mcp-oauth/index.ts +25 -0
- package/src/mcp-oauth/metadata.ts +185 -0
- package/src/mcp-oauth/probe.ts +91 -0
- package/src/mcp-oauth/request.ts +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Stigmer Contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# @stigmer/outbound
|
|
2
|
+
|
|
3
|
+
The rules a Stigmer process follows when it dials a URL a user supplied, shared by the control plane (`@stigmer/server`), the runner (`@stigmer/runner`) and the catalogue audit (`plugins/scripts/audit`).
|
|
4
|
+
|
|
5
|
+
Two entry points:
|
|
6
|
+
|
|
7
|
+
- `@stigmer/outbound/egress`: which addresses a process refuses to dial (`blockedReason`, two postures, `strict` for managed cloud processes and `relaxed` for a machine that belongs to the user), the check that applies a policy to a URL after resolving every address it has (`checkEgress`), and the fetch that applies it to every redirect hop (`guardedFetch`). A process composes one guarded fetch at its root and hands it to every module that dials; the modules hold no `fetch` of their own.
|
|
8
|
+
- `@stigmer/outbound/mcp-oauth`: how an MCP endpoint's authentication is read without a credential (`isOAuthChallenge`, the complete `initializeRequest`, `probeEndpointAuth`) and how its login server is found (`resolveAuthorizationServers` over RFC 9728, `readAuthorizationServerMetadata` over RFC 8414 with the issuer's path and OpenID's document).
|
|
9
|
+
|
|
10
|
+
## Why one library
|
|
11
|
+
|
|
12
|
+
Each rule here was first written where it was first needed: the address classification in the runner's `web_fetch` guard, the OAuth-challenge rule in the runner's failed-tool-call path, the complete handshake and the metadata walk in the catalogue audit. The control plane then needed all of them at once, to complete a URL-only server's `auth` at save time and to reach login servers on another origin, and a third copy would have let the storefront call OAuth what the runner called a bad token. So the rules moved here and every consumer imports them; the consumers keep only what is theirs (the runner its posture from its mode and its user-facing sentence, the control plane its edition driver point and its pinned validation copy, the audit its evidence and retry).
|
|
13
|
+
|
|
14
|
+
## Shape
|
|
15
|
+
|
|
16
|
+
Pure over injected `fetch` and `lookup`; the one Node-bound module is `egress/node-lookup.ts`. Every function that can fail on the network returns a value (`EgressCheck`, `EndpointAuthOutcome`, `MetadataRead`) rather than throwing, so a caller renders its own sentence; `EgressError` wraps a refusal for the guarded fetch. `OutboundFetch` is the fetch shape consumers take (`(url: string | URL, init?) => Promise<Response>`); the global `fetch` satisfies it.
|
|
17
|
+
|
|
18
|
+
Published in lockstep with the other `backend/libs/ts` libraries by `scripts/publish-libs.mjs`; the server and runner link it with `file:` and pin the release version at stamp time.
|
|
19
|
+
|
|
20
|
+
## Verify
|
|
21
|
+
|
|
22
|
+
`npm run typecheck && npm test` here. The consumers' suites when a rule changes: `make test-server`, `make test-runner`, `make test-plugins-static`.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The two egress postures; the caller derives one from where it runs. */
|
|
2
|
+
export type EgressPosture = "strict" | "relaxed";
|
|
3
|
+
/**
|
|
4
|
+
* An egress policy: the synchronous address judgement a process dials
|
|
5
|
+
* under. `blockedReason` answers for every string (an unparseable one is
|
|
6
|
+
* refused as "unrecognized"), never throws, and does no I/O. A policy that
|
|
7
|
+
* would need a network or a hostname to answer is a different seam.
|
|
8
|
+
*/
|
|
9
|
+
export interface EgressPolicy {
|
|
10
|
+
/** The posture's name, for logs and error copy. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** A human-readable range name when the address is refused, null when allowed. */
|
|
13
|
+
blockedReason(address: string): string | null;
|
|
14
|
+
}
|
|
15
|
+
/** The policy for a posture: the two shapes every consumer composes. */
|
|
16
|
+
export declare function egressPolicyForPosture(posture: EgressPosture): EgressPolicy;
|
|
17
|
+
/**
|
|
18
|
+
* Classify an IP address against the posture's refused ranges.
|
|
19
|
+
*
|
|
20
|
+
* @returns a human-readable range name when refused, or null when allowed.
|
|
21
|
+
*/
|
|
22
|
+
export declare function blockedReason(address: string, posture: EgressPosture): string | null;
|
|
23
|
+
//# sourceMappingURL=address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/egress/address.ts"],"names":[],"mappings":"AAkCA,0EAA0E;AAC1E,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,mDAAmD;IACnD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,kFAAkF;IAClF,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CAC/C;AAED,wEAAwE;AACxE,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,aAAa,GAAG,YAAY,CAK3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,GAAG,IAAI,CAoBpF"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address classification for outbound egress: which IP addresses a Stigmer
|
|
3
|
+
* process refuses to dial, under which posture, and why.
|
|
4
|
+
*
|
|
5
|
+
* This is the one home of the rule. It began as the runner's `web_fetch`
|
|
6
|
+
* SSRF boundary (backend/services/runner/src/tools/url-guard.ts, which now
|
|
7
|
+
* delegates here) and is shared because the control plane dials
|
|
8
|
+
* user-supplied URLs too: an MCP endpoint at save time and its login server
|
|
9
|
+
* on Sign in. A rule with two copies drifts; a copy that drifted on the
|
|
10
|
+
* control plane would be the copy guarding the wider network.
|
|
11
|
+
*
|
|
12
|
+
* Two postures, because locality differs, not trust:
|
|
13
|
+
*
|
|
14
|
+
* - "strict" (managed cloud processes): loopback, RFC 1918 private,
|
|
15
|
+
* link-local, unspecified, and their IPv6 equivalents are all refused.
|
|
16
|
+
* - "relaxed" (self-hosted and local processes): the machine belongs to
|
|
17
|
+
* the user, and a server beside the process (http://localhost:3000/mcp,
|
|
18
|
+
* the proto's own example) is the normal case. Only the link-local range,
|
|
19
|
+
* which carries the cloud metadata endpoint 169.254.169.254, stays
|
|
20
|
+
* refused, as costless defence in depth.
|
|
21
|
+
*
|
|
22
|
+
* Who chooses the posture is the caller's business: the runner keys it on
|
|
23
|
+
* its mode with an operator override; the control plane takes it from an
|
|
24
|
+
* edition driver point. This module knows addresses and nothing else.
|
|
25
|
+
*
|
|
26
|
+
* IPv4-mapped IPv6 (::ffff:a.b.c.d in dotted or hex form) is classified by
|
|
27
|
+
* the embedded IPv4, so the mapping cannot smuggle a refused address past
|
|
28
|
+
* the check. Anything that is not a well-formed address fails closed.
|
|
29
|
+
*
|
|
30
|
+
* Proven by __tests__/address.test.ts, the runner's classification cases
|
|
31
|
+
* moved here byte for byte.
|
|
32
|
+
*/
|
|
33
|
+
import { isIP } from "node:net";
|
|
34
|
+
/** The policy for a posture: the two shapes every consumer composes. */
|
|
35
|
+
export function egressPolicyForPosture(posture) {
|
|
36
|
+
return {
|
|
37
|
+
name: posture,
|
|
38
|
+
blockedReason: (address) => blockedReason(address, posture),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Classify an IP address against the posture's refused ranges.
|
|
43
|
+
*
|
|
44
|
+
* @returns a human-readable range name when refused, or null when allowed.
|
|
45
|
+
*/
|
|
46
|
+
export function blockedReason(address, posture) {
|
|
47
|
+
const family = isIP(address);
|
|
48
|
+
if (family === 4) {
|
|
49
|
+
return blockedReasonV4(address, posture);
|
|
50
|
+
}
|
|
51
|
+
if (family === 6) {
|
|
52
|
+
const groups = expandV6(address);
|
|
53
|
+
if (!groups)
|
|
54
|
+
return "unrecognized";
|
|
55
|
+
const embedded = extractMappedV4(groups);
|
|
56
|
+
if (embedded) {
|
|
57
|
+
return blockedReasonV4(embedded, posture);
|
|
58
|
+
}
|
|
59
|
+
return blockedReasonV6(groups, posture);
|
|
60
|
+
}
|
|
61
|
+
// Unparseable: fail closed; only real addresses get sockets.
|
|
62
|
+
return "unrecognized";
|
|
63
|
+
}
|
|
64
|
+
function blockedReasonV4(address, posture) {
|
|
65
|
+
const octets = address.split(".").map(Number);
|
|
66
|
+
if (octets.length !== 4 || octets.some((o) => Number.isNaN(o) || o < 0 || o > 255)) {
|
|
67
|
+
return "unrecognized";
|
|
68
|
+
}
|
|
69
|
+
const a = octets[0];
|
|
70
|
+
const b = octets[1];
|
|
71
|
+
// Link-local (169.254.0.0/16) hosts the cloud metadata service at
|
|
72
|
+
// 169.254.169.254: refused under BOTH postures.
|
|
73
|
+
if (a === 169 && b === 254)
|
|
74
|
+
return "link-local (cloud metadata)";
|
|
75
|
+
if (posture === "relaxed")
|
|
76
|
+
return null;
|
|
77
|
+
if (a === 127)
|
|
78
|
+
return "loopback";
|
|
79
|
+
if (a === 0)
|
|
80
|
+
return "unspecified";
|
|
81
|
+
if (a === 10)
|
|
82
|
+
return "private (RFC 1918)";
|
|
83
|
+
if (a === 172 && b !== undefined && b >= 16 && b <= 31)
|
|
84
|
+
return "private (RFC 1918)";
|
|
85
|
+
if (a === 192 && b === 168)
|
|
86
|
+
return "private (RFC 1918)";
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function blockedReasonV6(groups, posture) {
|
|
90
|
+
const first = groups[0] ?? 0;
|
|
91
|
+
// fe80::/10, the IPv6 link-local range, the v6 sibling of the metadata
|
|
92
|
+
// range: refused under BOTH postures for symmetry with v4.
|
|
93
|
+
if ((first & 0xffc0) === 0xfe80)
|
|
94
|
+
return "link-local (cloud metadata)";
|
|
95
|
+
if (posture === "relaxed")
|
|
96
|
+
return null;
|
|
97
|
+
const allZero = groups.every((g) => g === 0);
|
|
98
|
+
if (allZero)
|
|
99
|
+
return "unspecified";
|
|
100
|
+
if (groups.slice(0, 7).every((g) => g === 0) && groups[7] === 1)
|
|
101
|
+
return "loopback";
|
|
102
|
+
// fc00::/7: unique local (private) addresses.
|
|
103
|
+
if ((first & 0xfe00) === 0xfc00)
|
|
104
|
+
return "private (unique local)";
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Expand an IPv6 address (already validated by isIP) into its 8 groups.
|
|
109
|
+
* Handles `::` compression, a trailing dotted-IPv4 tail, and zone suffixes.
|
|
110
|
+
*/
|
|
111
|
+
function expandV6(address) {
|
|
112
|
+
let text = address.toLowerCase().split("%")[0] ?? "";
|
|
113
|
+
// Convert a dotted-IPv4 tail (for example ::ffff:127.0.0.1) into two hex groups.
|
|
114
|
+
const v4Tail = /(\d+\.\d+\.\d+\.\d+)$/.exec(text);
|
|
115
|
+
const tail4 = v4Tail?.[1];
|
|
116
|
+
if (tail4 !== undefined) {
|
|
117
|
+
const octets = tail4.split(".").map(Number);
|
|
118
|
+
if (octets.length !== 4 || octets.some((o) => o > 255))
|
|
119
|
+
return null;
|
|
120
|
+
const [o0 = 0, o1 = 0, o2 = 0, o3 = 0] = octets;
|
|
121
|
+
const hex = `${((o0 << 8) | o1).toString(16)}:${((o2 << 8) | o3).toString(16)}`;
|
|
122
|
+
text = text.slice(0, -tail4.length) + hex;
|
|
123
|
+
}
|
|
124
|
+
const halves = text.split("::");
|
|
125
|
+
if (halves.length > 2)
|
|
126
|
+
return null;
|
|
127
|
+
const parseHalf = (half) => (half === "" ? [] : half.split(":").map((g) => parseInt(g, 16)));
|
|
128
|
+
const head = parseHalf(halves[0] ?? "");
|
|
129
|
+
const tail = halves.length === 2 ? parseHalf(halves[1] ?? "") : [];
|
|
130
|
+
const fill = 8 - head.length - tail.length;
|
|
131
|
+
if (halves.length === 2 ? fill < 0 : head.length !== 8)
|
|
132
|
+
return null;
|
|
133
|
+
const groups = [...head, ...(halves.length === 2 ? Array(fill).fill(0) : []), ...tail];
|
|
134
|
+
if (groups.length !== 8 || groups.some((g) => Number.isNaN(g) || g < 0 || g > 0xffff)) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return groups;
|
|
138
|
+
}
|
|
139
|
+
/** Extract the IPv4 payload from IPv4-mapped groups (::ffff:0:0/96), if any. */
|
|
140
|
+
function extractMappedV4(groups) {
|
|
141
|
+
const isMapped = groups.slice(0, 5).every((g) => g === 0) && groups[5] === 0xffff;
|
|
142
|
+
if (!isMapped)
|
|
143
|
+
return null;
|
|
144
|
+
const hi = groups[6] ?? 0;
|
|
145
|
+
const lo = groups[7] ?? 0;
|
|
146
|
+
return `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=address.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.js","sourceRoot":"","sources":["../../src/egress/address.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAkBhC,wEAAwE;AACxE,MAAM,UAAU,sBAAsB,CAAC,OAAsB;IAC3D,OAAO;QACL,IAAI,EAAE,OAAO;QACb,aAAa,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC;KAC5D,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,OAAe,EAAE,OAAsB;IACnE,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAE7B,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;QACjB,OAAO,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;QACjB,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM;YAAE,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AM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC1B,OAAO,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { EgressPolicy } from "./address.js";
|
|
2
|
+
/** Resolve a hostname to every address it has; rejects when it has none. */
|
|
3
|
+
export type LookupFn = (hostname: string) => Promise<readonly string[]>;
|
|
4
|
+
/** Why a URL was refused; one shape per cause, each carrying what a sentence needs. */
|
|
5
|
+
export type EgressRefusal = {
|
|
6
|
+
readonly kind: "invalid-url";
|
|
7
|
+
readonly url: string;
|
|
8
|
+
} | {
|
|
9
|
+
readonly kind: "unsupported-scheme";
|
|
10
|
+
readonly url: URL;
|
|
11
|
+
readonly scheme: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: "unresolvable";
|
|
14
|
+
readonly url: URL;
|
|
15
|
+
readonly hostname: string;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: "blocked";
|
|
18
|
+
readonly url: URL;
|
|
19
|
+
readonly hostname: string;
|
|
20
|
+
readonly address: string;
|
|
21
|
+
readonly reason: string;
|
|
22
|
+
readonly policy: string;
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: "too-many-redirects";
|
|
25
|
+
readonly url: URL;
|
|
26
|
+
readonly hops: number;
|
|
27
|
+
};
|
|
28
|
+
export type EgressCheck = {
|
|
29
|
+
readonly ok: true;
|
|
30
|
+
readonly url: URL;
|
|
31
|
+
readonly addresses: readonly string[];
|
|
32
|
+
} | {
|
|
33
|
+
readonly ok: false;
|
|
34
|
+
readonly refusal: EgressRefusal;
|
|
35
|
+
};
|
|
36
|
+
export interface EgressCheckOptions {
|
|
37
|
+
readonly lookup: LookupFn;
|
|
38
|
+
/** Bounds the resolution; an abort propagates as the signal's reason. */
|
|
39
|
+
readonly signal?: AbortSignal | null | undefined;
|
|
40
|
+
}
|
|
41
|
+
/** One sentence per refusal, safe to show a user or a model. */
|
|
42
|
+
export declare function describeRefusal(refusal: EgressRefusal): string;
|
|
43
|
+
/** A refusal as a throwable, for callers that want one. */
|
|
44
|
+
export declare class EgressError extends Error {
|
|
45
|
+
readonly refusal: EgressRefusal;
|
|
46
|
+
constructor(refusal: EgressRefusal);
|
|
47
|
+
}
|
|
48
|
+
/** Judge a URL under a policy: scheme, then every resolved address. */
|
|
49
|
+
export declare function checkEgress(rawUrl: string | URL, policy: EgressPolicy, options: EgressCheckOptions): Promise<EgressCheck>;
|
|
50
|
+
//# sourceMappingURL=check.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../../src/egress/check.ts"],"names":[],"mappings":"AA6BA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,4EAA4E;AAC5E,MAAM,MAAM,QAAQ,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,CAAC;AAExE,uFAAuF;AACvF,MAAM,MAAM,aAAa,GACrB;IAAE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,QAAQ,CAAC,IAAI,EAAE,oBAAoB,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACnF;IAAE,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC/E;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,QAAQ,CAAC,QAAQ,EAAE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The answer is a value, not a throw, so a caller can render its own
|
|
5
|
+
* sentence (the runner tells a model "this runner does not fetch from", the
|
|
6
|
+
* control plane tells a user what the endpoint resolved to) while the
|
|
7
|
+
* judgement itself has one home. `EgressError` wraps a refusal for the
|
|
8
|
+
* callers that want a throw (the guarded fetch).
|
|
9
|
+
*
|
|
10
|
+
* The check resolves the hostname and judges EVERY address it resolves to:
|
|
11
|
+
* a name with one public and one private record must be refused, or the
|
|
12
|
+
* private record becomes the bypass. Literal IP hosts are judged as they
|
|
13
|
+
* are, without a lookup. Resolution is injected (`LookupFn`) so tests never
|
|
14
|
+
* touch DNS, and it races the caller's `signal`: `dns.lookup` has no
|
|
15
|
+
* timeout of its own, and a save that waits on a hanging resolver would be
|
|
16
|
+
* a save that hangs (the control plane's probe runs under one deadline
|
|
17
|
+
* that covers resolution, for exactly this reason). An aborted signal
|
|
18
|
+
* propagates as the signal's reason, the way `fetch` itself behaves.
|
|
19
|
+
*
|
|
20
|
+
* Accepted limitation, inherited from the runner's guard: a DNS-rebinding
|
|
21
|
+
* window remains between our lookup and the socket connect. Node's fetch
|
|
22
|
+
* (undici) offers no lookup pinning without replacing the dispatcher, and
|
|
23
|
+
* the strict posture's range refusals make the rebinding payoff (an
|
|
24
|
+
* internal address) unreachable anyway.
|
|
25
|
+
*
|
|
26
|
+
* Proven by __tests__/check.test.ts.
|
|
27
|
+
*/
|
|
28
|
+
import { isIP } from "node:net";
|
|
29
|
+
/** One sentence per refusal, safe to show a user or a model. */
|
|
30
|
+
export function describeRefusal(refusal) {
|
|
31
|
+
switch (refusal.kind) {
|
|
32
|
+
case "invalid-url":
|
|
33
|
+
return `Invalid URL: ${refusal.url}`;
|
|
34
|
+
case "unsupported-scheme":
|
|
35
|
+
return `Unsupported URL scheme "${refusal.scheme}": only http and https are allowed.`;
|
|
36
|
+
case "unresolvable":
|
|
37
|
+
return `Could not resolve hostname: ${refusal.hostname}`;
|
|
38
|
+
case "blocked":
|
|
39
|
+
return `Refusing to reach ${refusal.hostname}: it resolves to ${refusal.address}, a ${refusal.reason} address the ${refusal.policy} egress policy does not dial.`;
|
|
40
|
+
case "too-many-redirects":
|
|
41
|
+
return `Refusing to follow more than ${refusal.hops} redirects from ${refusal.url.href}.`;
|
|
42
|
+
default: {
|
|
43
|
+
const exhaustive = refusal;
|
|
44
|
+
return exhaustive;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** A refusal as a throwable, for callers that want one. */
|
|
49
|
+
export class EgressError extends Error {
|
|
50
|
+
refusal;
|
|
51
|
+
constructor(refusal) {
|
|
52
|
+
super(describeRefusal(refusal));
|
|
53
|
+
this.refusal = refusal;
|
|
54
|
+
this.name = "EgressError";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Judge a URL under a policy: scheme, then every resolved address. */
|
|
58
|
+
export async function checkEgress(rawUrl, policy, options) {
|
|
59
|
+
let url;
|
|
60
|
+
try {
|
|
61
|
+
url = new URL(rawUrl);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return { ok: false, refusal: { kind: "invalid-url", url: String(rawUrl) } };
|
|
65
|
+
}
|
|
66
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
67
|
+
return { ok: false, refusal: { kind: "unsupported-scheme", url, scheme: url.protocol.replace(/:$/, "") } };
|
|
68
|
+
}
|
|
69
|
+
const addresses = await resolveAddresses(url.hostname, options);
|
|
70
|
+
if (addresses === undefined) {
|
|
71
|
+
return { ok: false, refusal: { kind: "unresolvable", url, hostname: url.hostname } };
|
|
72
|
+
}
|
|
73
|
+
for (const address of addresses) {
|
|
74
|
+
const reason = policy.blockedReason(address);
|
|
75
|
+
if (reason !== null) {
|
|
76
|
+
return { ok: false, refusal: { kind: "blocked", url, hostname: url.hostname, address, reason, policy: policy.name } };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { ok: true, url, addresses };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Every address a hostname has. Literal IPs pass through (a bracketed IPv6
|
|
83
|
+
* host arrives from URL.hostname still bracketed). `undefined` when the name
|
|
84
|
+
* does not resolve; an aborted signal throws its reason.
|
|
85
|
+
*/
|
|
86
|
+
async function resolveAddresses(hostname, options) {
|
|
87
|
+
const literal = hostname.replace(/^\[|\]$/g, "");
|
|
88
|
+
if (isIP(literal) !== 0) {
|
|
89
|
+
return [literal];
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
const records = await raceSignal(options.lookup(hostname), options.signal);
|
|
93
|
+
return records.length === 0 ? undefined : records;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (options.signal?.aborted === true)
|
|
97
|
+
throw error;
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function raceSignal(promise, signal) {
|
|
102
|
+
if (signal === null || signal === undefined)
|
|
103
|
+
return promise;
|
|
104
|
+
if (signal.aborted) {
|
|
105
|
+
// The resolver was already asked; its eventual answer is nobody's now.
|
|
106
|
+
promise.catch(() => undefined);
|
|
107
|
+
return Promise.reject(abortReason(signal));
|
|
108
|
+
}
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
const onAbort = () => {
|
|
111
|
+
promise.catch(() => undefined);
|
|
112
|
+
reject(abortReason(signal));
|
|
113
|
+
};
|
|
114
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
115
|
+
promise.then((value) => {
|
|
116
|
+
signal.removeEventListener("abort", onAbort);
|
|
117
|
+
resolve(value);
|
|
118
|
+
}, (error) => {
|
|
119
|
+
signal.removeEventListener("abort", onAbort);
|
|
120
|
+
reject(error);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function abortReason(signal) {
|
|
125
|
+
return signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=check.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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AAC,CAAC;QACF,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1D,OAAO,CAAC,IAAI,CACV,CAAC,KAAK,EAAE,EAAE;YACR,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC7C,OAAO,CAAC,KAAK,CAAC,CAAC;QACjB,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;YACjB,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC7C,MAAM,CAAC,KAAK,CAAC,CAAC;QAChB,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,WAAW,CAAC,MAAmB;IACtC,OAAO,MAAM,CAAC,MAAM,IAAI,IAAI,YAAY,CAAC,4BAA4B,EAAE,YAAY,CAAC,CAAC;AACvF,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The guarded fetch: a `fetch`-shaped function that judges every URL it is
|
|
3
|
+
* about to dial, including every redirect hop, under one egress policy.
|
|
4
|
+
*
|
|
5
|
+
* A process composes exactly one of these at its root and hands it to
|
|
6
|
+
* every module that dials a user-supplied URL, so an unguarded call cannot
|
|
7
|
+
* be written by omission: the module holds no `fetch` of its own, only the
|
|
8
|
+
* `OutboundFetch` it was given. Tests hand a fake in the same shape.
|
|
9
|
+
*
|
|
10
|
+
* Redirects are the reason this is a wrapper and not a pre-check. The
|
|
11
|
+
* platform's own `fetch` would follow a redirect to an internal address
|
|
12
|
+
* after the first hop passed, so redirects are always requested `manual`
|
|
13
|
+
* from the underlying fetch and followed here, each hop judged like the
|
|
14
|
+
* first, up to `maxRedirects` (3, enough for a scheme upgrade and a
|
|
15
|
+
* trailing-slash canonicalisation with one to spare). A caller that asked
|
|
16
|
+
* for `redirect: "manual"` gets the 3xx back untouched (the OAuth preflight
|
|
17
|
+
* wants exactly that); `redirect: "error"` is honoured by throwing the same
|
|
18
|
+
* TypeError `fetch` throws. On a cross-origin hop the `Authorization`
|
|
19
|
+
* header is dropped, as the fetch standard does, so a credential meant for
|
|
20
|
+
* one host never travels to another. A 303, or a 301/302 answering a
|
|
21
|
+
* non-GET, becomes a GET without a body; 307 and 308 keep method and body.
|
|
22
|
+
*
|
|
23
|
+
* `OutboundFetch` takes a string or a URL, never a `Request`: a Request
|
|
24
|
+
* carries its own headers and body that a wrapper would have to merge, and
|
|
25
|
+
* no caller here builds one. The global `fetch` is assignable to it, so a
|
|
26
|
+
* test or a script can pass `fetch` where the guard is not the point.
|
|
27
|
+
*
|
|
28
|
+
* Proven by __tests__/fetch.test.ts.
|
|
29
|
+
*/
|
|
30
|
+
import type { EgressPolicy } from "./address.js";
|
|
31
|
+
import { type LookupFn } from "./check.js";
|
|
32
|
+
/** The fetch shape every outbound-dialling module takes; the global `fetch` satisfies it. */
|
|
33
|
+
export type OutboundFetch = (url: string | URL, init?: RequestInit) => Promise<Response>;
|
|
34
|
+
export interface GuardedFetchOptions {
|
|
35
|
+
readonly fetchImpl: OutboundFetch;
|
|
36
|
+
readonly lookup: LookupFn;
|
|
37
|
+
/** Hops followed before refusing; default 3. */
|
|
38
|
+
readonly maxRedirects?: number;
|
|
39
|
+
}
|
|
40
|
+
/** The default hop budget: a scheme upgrade and a canonicalisation, with one to spare. */
|
|
41
|
+
export declare const DEFAULT_MAX_REDIRECTS = 3;
|
|
42
|
+
/**
|
|
43
|
+
* An `OutboundFetch` in the global `fetch`'s shape, for a seam typed
|
|
44
|
+
* `typeof fetch`. A `Request` input is refused rather than flattened: this
|
|
45
|
+
* library's callers pass URLs, and a wrapper that silently dropped a
|
|
46
|
+
* Request's body or headers would be a bug that looked like a network
|
|
47
|
+
* failure.
|
|
48
|
+
*/
|
|
49
|
+
export declare function asFetch(outbound: OutboundFetch): typeof fetch;
|
|
50
|
+
/** Compose a fetch that judges every URL and every hop under `policy`. */
|
|
51
|
+
export declare function guardedFetch(policy: EgressPolicy, options: GuardedFetchOptions): OutboundFetch;
|
|
52
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/egress/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAA4B,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAErE,6FAA6F;AAC7F,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEzF,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC;IAClC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1B,gDAAgD;IAChD,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,0FAA0F;AAC1F,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAIvC;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,KAAK,CAK7D;AAED,0EAA0E;AAC1E,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,mBAAmB,GAAG,aAAa,CAyC9F"}
|