1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Pluggable HTTP transport for the curl/wget/http builtins.
//!
//! Design decision (see `specs/http-transport.md`): bashkit owns HTTP
//! *policy* — URL allowlist, DNS/private-IP SSRF precheck, `before_http` /
//! `after_http` hooks, credential injection, bot-auth signing, and response
//! size caps — while an injected [`HttpTransport`] owns *connectivity*.
//! Embedding hosts implement the trait to route every request the sandbox
//! makes through their own outbound boundary (an egress service, a corporate
//! proxy, an audit/cache layer, or a test double). The shape mirrors
//! fetchkit's `HttpTransport` so a host can back both libraries with one
//! egress implementation.
//!
//! When no transport is injected, [`HttpClient`](super::HttpClient) uses its
//! built-in reqwest transport with connect-time private-IP filtering.
use IpAddr;
use Duration;
use ;
/// Outbound HTTP request handed to an [`HttpTransport`].
///
/// Built by [`HttpClient`](super::HttpClient) after every in-sandbox policy
/// step has already run: the URL allowlist check, the DNS/private-IP SSRF
/// precheck, `before_http` hooks (including credential injection), and
/// bot-auth signing. `headers` therefore already carries injected credential
/// headers and `Signature`/`Signature-Input`/`Signature-Agent` signing
/// headers — a transport only moves bytes, it does not re-run policy.
///
/// Intentionally no `Debug` derive: `headers` can carry `Authorization`
/// values and signing material (TM-LOG-001 — no accidental secret logging).
/// Typed failure returned by an [`HttpTransport`].
///
/// Variants map onto distinct curl/wget failure modes so host-side policy
/// decisions surface as the right script-visible exit codes:
///
/// | Variant | curl stderr prefix | curl exit code |
/// |---------|--------------------|----------------|
/// | [`Denied`](Self::Denied) | `access denied:` | 7 |
/// | [`Timeout`](Self::Timeout) | `operation timed out` | 28 |
/// | [`TooLarge`](Self::TooLarge) | `response too large:` | 63 |
/// | [`Transport`](Self::Transport) | message as-is | 1 |
/// Pluggable transport for all outbound HTTP made by sandboxed scripts.
///
/// Implement this to direct `curl`/`wget`/`http` traffic through a
/// host-owned path — an egress service, proxy, audit log, cache, or mock.
/// Inject it with `BashBuilder::http_transport` (or
/// [`HttpClient::set_transport`](super::HttpClient::set_transport)); the
/// same `Arc` can be shared across many `Bash` instances.
///
/// Every policy step runs in bashkit *before* `execute` is called — see
/// [`HttpTransportRequest`] — and the redirect loop in curl/wget issues one
/// `execute` call per hop, so redirect targets are re-validated and
/// re-signed like fetchkit's per-hop transport calls.
///
/// # SSRF responsibility (TM-NET-023)
///
/// **Custom transports DO NOT inherit the built-in reqwest transport's
/// connect-time private-IP filter.** bashkit's DNS precheck is best-effort:
/// there is a rebind window between the precheck and the moment the
/// transport opens its own socket. A transport that performs real network
/// I/O MUST either connect only to [`HttpTransportRequest::pinned_addrs`]
/// (when non-empty), re-resolve and re-apply private-IP filtering itself
/// (`bashkit::network::allowlist::is_private_ip` is the same classifier the
/// built-in transport uses), or constrain its egress at a lower layer (the
/// typical host-boundary case). Transports that only consult fixtures or
/// in-memory state have no exposure here.
///
/// # Example
///
/// ```
/// use bashkit::{HttpTransport, HttpTransportError, HttpTransportRequest, HttpResponse};
///
/// /// Routes sandbox HTTP through a host-owned egress boundary.
/// struct EgressTransport;
///
/// #[async_trait::async_trait]
/// impl HttpTransport for EgressTransport {
/// async fn execute(
/// &self,
/// request: HttpTransportRequest,
/// ) -> Result<HttpResponse, HttpTransportError> {
/// // Forward method/url/headers/body/timeout/pinned_addrs to the
/// // host egress client here; map its policy denials to `Denied`.
/// if request.url.starts_with("https://blocked.internal") {
/// return Err(HttpTransportError::Denied(request.url));
/// }
/// Ok(HttpResponse { status: 200, headers: vec![], body: b"ok".to_vec() })
/// }
/// }
/// ```