Skip to main content

dekopon_http_host/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(test, allow(clippy::unwrap_used))]
3#![cfg_attr(
4    test,
5    allow(
6        clippy::disallowed_methods,
7        clippy::disallowed_types,
8        reason = "tests spawn, join and drain freely; production sites carry their own expectation"
9    )
10)]
11pub mod asset;
12mod signed_download;
13mod stream;
14#[cfg(feature = "native-test")]
15pub mod test_transport;
16pub use signed_download::{Coverage, DownloadError, DownloadResponse, Window};
17pub use stream::{
18    CHUNK_BYTES, FilePart, Part, Representation, StreamedRequest, StreamedResponse, read_decoded,
19};
20
21use std::{
22    borrow::Cow,
23    collections::{BTreeSet, HashMap},
24    error::Error as _,
25    fmt,
26    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
27    sync::Arc,
28    time::Duration,
29};
30
31use base64::{Engine as _, engine::general_purpose::STANDARD};
32use dekopon_capability::{
33    ChatSlotValues, HttpConstraints, HttpConstraintsError, PathSegment, SecretUseGrant,
34};
35use dekopon_core::{Redacted, SecretBytes, SecretSinkKind};
36use reqwest::{
37    Method, Url,
38    header::{ACCEPT_ENCODING, AUTHORIZATION, HeaderMap, HeaderName, HeaderValue},
39    redirect,
40};
41use serde::{Deserialize, Serialize};
42use thiserror::Error;
43use tokio::time::{Instant, timeout};
44use tracing::Instrument as _;
45
46const DEFAULT_HTTPS_PORT: u16 = 443;
47const CHATGPT_ACCOUNT_HEADER: &str = "chatgpt-account-id";
48const MAX_ERROR_MESSAGE_BYTES: usize = 256;
49const MAX_RESOLVED_ADDRESSES: usize = 16;
50const REQUEST_ENCODING_OVERHEAD_BYTES: u64 = 128;
51
52pub const DEFAULT_MAX_REQUESTS: u32 = 32;
53pub const DEFAULT_MAX_REQUEST_BYTES: u64 = 1024 * 1024;
54pub const DEFAULT_MAX_RESPONSE_BYTES: u64 = 4 * 1024 * 1024;
55pub const DEFAULT_MAX_HEADERS: usize = 128;
56pub const DEFAULT_MAX_HEADER_BYTES: usize = 64 * 1024;
57pub const MAX_CREDENTIAL_BYTES: usize = 4096;
58pub const MIN_CREDENTIAL_BYTES: usize = 16;
59const _: () = assert!(MIN_CREDENTIAL_BYTES == 16);
60
61#[derive(Clone, Debug, Eq, PartialEq)]
62pub struct Header {
63    pub name: String,
64    pub value: Vec<u8>,
65}
66
67#[derive(Clone, Debug, Eq, PartialEq)]
68pub struct Request {
69    pub method: String,
70    pub uri: String,
71    pub headers: Vec<Header>,
72    pub body: Vec<u8>,
73}
74
75#[derive(Clone, Debug, Eq, PartialEq)]
76pub struct Response {
77    pub status: u16,
78    pub headers: Vec<Header>,
79    pub body: Vec<u8>,
80}
81
82#[derive(Clone, Copy, Debug, Eq, PartialEq)]
83pub enum ErrorCode {
84    InvalidMethod,
85    InvalidUri,
86    InvalidHeader,
87    RequestTooLarge,
88    Denied,
89    HostCallLimit,
90    Dns,
91    Connect,
92    Tls,
93    Timeout,
94    Protocol,
95    ResponseTooLarge,
96    Internal,
97}
98
99#[derive(Clone, Copy)]
100enum ResponseMetadata {
101    Ordinary,
102    SignedDownload,
103}
104
105#[derive(Clone, Copy, Debug, Eq, PartialEq)]
106pub enum PolicyViolation {
107    Denied,
108    RedirectRefused,
109    HostCallLimit,
110    InvalidHttpRequest,
111    ByteLimit(Option<ByteLimitExceeded>),
112}
113
114#[derive(Clone, Copy, Debug, Eq, PartialEq)]
115pub enum ByteLimitKey {
116    MaxRequestBytes,
117    MaxResponseBytes,
118}
119
120impl ByteLimitKey {
121    pub const fn as_str(self) -> &'static str {
122        match self {
123            Self::MaxRequestBytes => "maxRequestBytes",
124            Self::MaxResponseBytes => "maxResponseBytes",
125        }
126    }
127
128    const fn code(self) -> ErrorCode {
129        match self {
130            Self::MaxRequestBytes => ErrorCode::RequestTooLarge,
131            Self::MaxResponseBytes => ErrorCode::ResponseTooLarge,
132        }
133    }
134
135    const fn direction(self) -> &'static str {
136        match self {
137            Self::MaxRequestBytes => "request",
138            Self::MaxResponseBytes => "response",
139        }
140    }
141}
142
143#[derive(Clone, Copy, Debug, Eq, PartialEq)]
144pub struct ByteLimitExceeded {
145    pub key: ByteLimitKey,
146    pub limit: u64,
147    pub observed: u64,
148}
149
150impl fmt::Display for ByteLimitExceeded {
151    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
152        write!(
153            formatter,
154            "the grant allows {} bytes and the {} is at least {} bytes",
155            self.limit,
156            self.key.direction(),
157            self.observed
158        )
159    }
160}
161
162impl PolicyViolation {
163    pub const fn label(self) -> &'static str {
164        match self {
165            Self::Denied => "denied",
166            Self::RedirectRefused => "redirect-refused",
167            Self::HostCallLimit => "host-call-limit",
168            Self::InvalidHttpRequest => "invalid-http-request",
169            Self::ByteLimit(_) => "byte-limit",
170        }
171    }
172
173    fn from_error(error: &HttpError) -> Option<Self> {
174        match error.code {
175            ErrorCode::Denied => Some(Self::Denied),
176            ErrorCode::HostCallLimit => Some(Self::HostCallLimit),
177            ErrorCode::InvalidMethod | ErrorCode::InvalidUri | ErrorCode::InvalidHeader => {
178                Some(Self::InvalidHttpRequest)
179            }
180            ErrorCode::RequestTooLarge | ErrorCode::ResponseTooLarge => {
181                Some(Self::ByteLimit(error.byte_limit))
182            }
183            ErrorCode::Dns
184            | ErrorCode::Connect
185            | ErrorCode::Tls
186            | ErrorCode::Timeout
187            | ErrorCode::Protocol
188            | ErrorCode::Internal => None,
189        }
190    }
191}
192
193#[derive(Clone, Debug, Eq, PartialEq)]
194pub struct HttpError {
195    pub code: ErrorCode,
196    pub message: String,
197    pub byte_limit: Option<ByteLimitExceeded>,
198}
199
200impl fmt::Display for HttpError {
201    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
202        write!(formatter, "{:?}: {}", self.code, self.message)
203    }
204}
205
206impl std::error::Error for HttpError {}
207
208#[derive(Clone, Debug, Default, Eq, PartialEq)]
209pub struct PlaintextHosts {
210    hosts: Arc<BTreeSet<String>>,
211}
212
213impl PlaintextHosts {
214    pub fn new<I>(entries: I) -> Result<Self, PlaintextHostError>
215    where
216        I: IntoIterator,
217        I::Item: AsRef<str>,
218    {
219        let mut hosts = BTreeSet::new();
220        for entry in entries {
221            let entry = entry.as_ref().trim();
222            if entry.is_empty() {
223                return Err(PlaintextHostError::Empty);
224            }
225            let owned = || entry.to_owned();
226            if entry.contains("://") {
227                return Err(PlaintextHostError::Scheme { entry: owned() });
228            }
229            if entry.contains('*') {
230                return Err(PlaintextHostError::Wildcard { entry: owned() });
231            }
232            if entry.contains('/') {
233                return Err(PlaintextHostError::Path { entry: owned() });
234            }
235            if entry.contains(':') {
236                return Err(PlaintextHostError::Port { entry: owned() });
237            }
238            if !is_bare_hostname(entry) {
239                return Err(PlaintextHostError::InvalidHost { entry: owned() });
240            }
241            hosts.insert(entry.to_ascii_lowercase());
242        }
243        Ok(Self {
244            hosts: Arc::new(hosts),
245        })
246    }
247
248    #[must_use]
249    pub fn contains(&self, host: &str) -> bool {
250        self.hosts.contains(host.to_ascii_lowercase().as_str())
251    }
252
253    #[must_use]
254    pub fn is_empty(&self) -> bool {
255        self.hosts.is_empty()
256    }
257
258    pub fn iter(&self) -> impl ExactSizeIterator<Item = &str> {
259        self.hosts.iter().map(String::as_str)
260    }
261}
262
263#[derive(Clone, Debug, Eq, Error, PartialEq)]
264pub enum PlaintextHostError {
265    #[error("a plaintext host entry is empty")]
266    Empty,
267    #[error("plaintext host `{entry}` must be a bare hostname, without a scheme")]
268    Scheme { entry: String },
269    #[error("plaintext host `{entry}` must be a bare hostname, without a path")]
270    Path { entry: String },
271    #[error("plaintext host `{entry}` must carry no port: the port is irrelevant to this rule")]
272    Port { entry: String },
273    #[error("plaintext host `{entry}` must be an exact hostname: wildcards are not accepted")]
274    Wildcard { entry: String },
275    #[error("plaintext host `{entry}` is not a hostname")]
276    InvalidHost { entry: String },
277}
278
279fn is_bare_hostname(entry: &str) -> bool {
280    entry
281        .bytes()
282        .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'.')
283        && !entry.starts_with(['-', '.'])
284        && !entry.ends_with(['-', '.'])
285}
286
287#[derive(Clone, Debug, Eq, PartialEq)]
288pub struct NonPublicHttpsAuthority {
289    pub authority: String,
290}
291
292impl NonPublicHttpsAuthority {
293    pub fn new(authority: &str) -> Result<Self, &'static str> {
294        let url = Url::parse(&format!("https://{authority}"))
295            .map_err(|_error| "authority must be a DNS hostname and explicit port")?;
296        let host = url.host_str().ok_or("authority must have a hostname")?;
297        if url.username() != ""
298            || url.password().is_some()
299            || url.path() != "/"
300            || url.query().is_some()
301            || url.fragment().is_some()
302            || url.port().is_none()
303            || !is_bare_hostname(host)
304            || host.parse::<IpAddr>().is_ok()
305            || !authority
306                .eq_ignore_ascii_case(&format!("{host}:{}", url.port().unwrap_or_default()))
307        {
308            return Err("authority must be an exact DNS hostname and explicit port");
309        }
310        Ok(Self {
311            authority: authority.to_ascii_lowercase(),
312        })
313    }
314}
315
316#[derive(Clone, Debug, Default, Eq, PartialEq)]
317pub struct ReachableDestinationSettings {
318    pub plaintext_hosts: BTreeSet<String>,
319    pub non_public_https: BTreeSet<String>,
320}
321
322// Matches `plaintext_permitted` (bare host, any port) and `authority_matches` (bare allowed host is
323// HTTPS port 443), so a setting enters the projection whenever enforcement could consult it.
324#[must_use]
325pub fn reachable_destination_settings<'a>(
326    allowed: impl IntoIterator<Item = &'a str>,
327    plaintext_hosts: &PlaintextHosts,
328    non_public_https: &[NonPublicHttpsAuthority],
329) -> ReachableDestinationSettings {
330    let mut reachable = ReachableDestinationSettings::default();
331    for entry in allowed {
332        let entry = entry.trim().to_ascii_lowercase();
333        let (host, authority) = match entry.rsplit_once(':') {
334            Some((host, port)) if !host.ends_with(':') && port.parse::<u16>().is_ok() => {
335                (host.to_owned(), entry.clone())
336            }
337            Some(_) | None => (entry.clone(), format!("{entry}:{DEFAULT_HTTPS_PORT}")),
338        };
339        if plaintext_hosts.contains(&host) {
340            reachable.plaintext_hosts.insert(host);
341        }
342        if non_public_https
343            .iter()
344            .any(|configured| configured.authority == authority)
345        {
346            reachable.non_public_https.insert(authority);
347        }
348    }
349    reachable
350}
351
352#[derive(Clone, Debug, Eq, PartialEq)]
353pub struct LoopbackHttpsPin {
354    authority: String,
355    address: SocketAddr,
356    ca_pem: Vec<u8>,
357}
358
359impl LoopbackHttpsPin {
360    pub fn new(
361        authority: &str,
362        address: SocketAddr,
363        ca_pem: Vec<u8>,
364    ) -> Result<Self, &'static str> {
365        let target = NonPublicHttpsAuthority::new(authority)?;
366        if !address.ip().is_loopback()
367            || address.port().to_string() != authority.rsplit(':').next().unwrap_or("")
368            || ca_pem.is_empty()
369        {
370            return Err("test pin requires an exact loopback HTTPS authority and CA");
371        }
372        Ok(Self {
373            authority: target.authority,
374            address,
375            ca_pem,
376        })
377    }
378}
379
380#[derive(Clone, Debug, Eq, PartialEq)]
381pub struct HttpHostCeilings {
382    pub max_requests: u32,
383    pub max_request_bytes: u64,
384    pub max_response_bytes: u64,
385    pub max_headers: usize,
386    pub max_header_bytes: usize,
387    pub plaintext_hosts: PlaintextHosts,
388    pub extra_ca_bundles: Arc<Vec<Vec<u8>>>,
389    pub non_public_https: Arc<Vec<NonPublicHttpsAuthority>>,
390    pub loopback_https_pin: Option<LoopbackHttpsPin>,
391}
392
393impl Default for HttpHostCeilings {
394    fn default() -> Self {
395        Self {
396            max_requests: DEFAULT_MAX_REQUESTS,
397            max_request_bytes: DEFAULT_MAX_REQUEST_BYTES,
398            max_response_bytes: DEFAULT_MAX_RESPONSE_BYTES,
399            max_headers: DEFAULT_MAX_HEADERS,
400            max_header_bytes: DEFAULT_MAX_HEADER_BYTES,
401            plaintext_hosts: PlaintextHosts::default(),
402            extra_ca_bundles: Arc::new(Vec::new()),
403            non_public_https: Arc::new(Vec::new()),
404            loopback_https_pin: None,
405        }
406    }
407}
408
409#[derive(Clone, Debug, Eq, Error, PartialEq)]
410pub enum ConfigurationError {
411    #[error("HTTP host ceiling {field} must be greater than zero")]
412    ZeroCeiling { field: &'static str },
413    #[error("HTTP authorization is not exact: {source}")]
414    InvalidGrant {
415        #[source]
416        source: HttpConstraintsError,
417    },
418    #[error("HTTP authorization exceeds native host ceilings")]
419    GrantExceedsCeiling,
420    #[error("HTTP execution deadline must be greater than zero")]
421    ZeroTimeout,
422    #[error("HTTP execution deadline is too large")]
423    TimeoutOverflow,
424    #[error("secret-use grant is invalid")]
425    InvalidSecretGrant {
426        #[source]
427        source: dekopon_capability::SecretUseGrantError,
428    },
429    #[error("bound credential is invalid: {reason}")]
430    InvalidCredential { reason: &'static str },
431}
432
433#[derive(Clone)]
434pub struct BoundCredential {
435    header_value: Redacted<String>,
436    companion_header: Option<(HeaderName, Redacted<String>)>,
437    destinations: Vec<String>,
438    secret_binding: Option<SecretBindingIdentity>,
439    echo_values: Vec<SecretBytes>,
440}
441
442#[derive(Clone, Debug, Eq, PartialEq)]
443struct SecretBindingIdentity {
444    grant: SecretUseGrant,
445}
446
447impl fmt::Debug for BoundCredential {
448    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
449        formatter
450            .debug_struct("BoundCredential")
451            .field("header_value", &self.header_value)
452            .field(
453                "companion_header",
454                &self
455                    .companion_header
456                    .as_ref()
457                    .map(|(name, value)| (name, value)),
458            )
459            .field("destinations", &self.destinations)
460            .field("secret_binding", &self.secret_binding.is_some())
461            .field("echo_values", &self.echo_values)
462            .finish()
463    }
464}
465
466impl BoundCredential {
467    pub fn bearer(
468        scheme: &str,
469        secret: Redacted<String>,
470        destinations: Vec<String>,
471    ) -> Result<Self, ConfigurationError> {
472        let invalid = |reason| ConfigurationError::InvalidCredential { reason };
473        if scheme.is_empty() || !scheme.bytes().all(|byte| byte.is_ascii_graphic()) {
474            return Err(invalid("scheme must be a printable ASCII token"));
475        }
476        {
477            let value = secret.expose();
478            if value.len() < MIN_CREDENTIAL_BYTES {
479                return Err(invalid("secret is shorter than the 16-byte minimum"));
480            }
481            if value.len() > MAX_CREDENTIAL_BYTES {
482                return Err(invalid("secret exceeds the credential byte limit"));
483            }
484            if !value.bytes().all(|byte| byte.is_ascii_graphic()) {
485                return Err(invalid(
486                    "secret contains whitespace, control, or non-ASCII bytes",
487                ));
488            }
489        }
490        if destinations.is_empty() {
491            return Err(invalid("at least one destination is required"));
492        }
493        if !destinations
494            .iter()
495            .all(|destination| is_destination_scope(destination))
496        {
497            return Err(invalid(
498                "destinations must be host or host:port authorities",
499            ));
500        }
501        let echo_value = SecretBytes::new(secret.expose().as_bytes().to_vec());
502        let header_value = Redacted::new(format!("{scheme} {}", secret.expose()));
503        Ok(Self {
504            header_value,
505            companion_header: None,
506            destinations,
507            secret_binding: None,
508            echo_values: vec![echo_value],
509        })
510    }
511
512    pub fn with_chatgpt_account(mut self, account_id: &str) -> Result<Self, ConfigurationError> {
513        let invalid = |reason| ConfigurationError::InvalidCredential { reason };
514        if self
515            .secret_binding
516            .as_ref()
517            .is_none_or(|identity| identity.grant.sink != SecretSinkKind::HttpBearer)
518        {
519            return Err(invalid(
520                "a ChatGPT account accompanies only a secret-bound Bearer credential",
521            ));
522        }
523        if account_id.is_empty()
524            || account_id.len() > MAX_CREDENTIAL_BYTES
525            || !account_id.bytes().all(|byte| byte.is_ascii_graphic())
526        {
527            return Err(invalid(
528                "ChatGPT account identifier must be a printable ASCII token",
529            ));
530        }
531        self.companion_header = Some((
532            HeaderName::from_static(CHATGPT_ACCOUNT_HEADER),
533            Redacted::new(account_id.to_owned()),
534        ));
535        Ok(self)
536    }
537
538    pub fn secret_bearer(
539        secret: SecretBytes,
540        grant: &SecretUseGrant,
541    ) -> Result<Self, ConfigurationError> {
542        Self::secret_token(SecretSinkKind::HttpBearer, "Bearer", secret, grant)
543    }
544
545    pub fn secret_bot(
546        secret: SecretBytes,
547        grant: &SecretUseGrant,
548    ) -> Result<Self, ConfigurationError> {
549        Self::secret_token(SecretSinkKind::HttpBot, "Bot", secret, grant)
550    }
551
552    fn secret_token(
553        sink: SecretSinkKind,
554        scheme: &str,
555        secret: SecretBytes,
556        grant: &SecretUseGrant,
557    ) -> Result<Self, ConfigurationError> {
558        if grant.sink != sink || grant.basic_username.is_some() {
559            return Err(ConfigurationError::InvalidCredential {
560                reason: "token material does not match the secret-use sink",
561            });
562        }
563        #[allow(
564            clippy::map_err_ignore,
565            reason = "Utf8Error carries the secret-derived invalid byte offset and valid prefix \
566                      length; the fixed structural refusal must not expose either"
567        )]
568        let token = std::str::from_utf8(secret.expose()).map_err(|_| {
569            ConfigurationError::InvalidCredential {
570                reason: "token secret must be UTF-8",
571            }
572        })?;
573        if token.len() < MIN_CREDENTIAL_BYTES {
574            return Err(ConfigurationError::InvalidCredential {
575                reason: "token secret is shorter than the 16-byte minimum",
576            });
577        }
578        if token.len() > MAX_CREDENTIAL_BYTES
579            || token
580                .bytes()
581                .any(|byte| byte.is_ascii_control() || byte.is_ascii_whitespace())
582        {
583            return Err(ConfigurationError::InvalidCredential {
584                reason: "token secret must be bounded and free of whitespace or controls",
585            });
586        }
587        let rendered = format!("{scheme} {token}");
588        Ok(Self {
589            header_value: Redacted::new(rendered),
590            companion_header: None,
591            destinations: grant.allowed_hosts.clone(),
592            secret_binding: Some(SecretBindingIdentity {
593                grant: grant.clone(),
594            }),
595            echo_values: vec![secret],
596        })
597    }
598
599    pub fn secret_basic(
600        password: SecretBytes,
601        grant: &SecretUseGrant,
602    ) -> Result<Self, ConfigurationError> {
603        let Some(username) = grant.basic_username.as_deref() else {
604            return Err(ConfigurationError::InvalidCredential {
605                reason: "Basic secret use requires a fixed username",
606            });
607        };
608        if password.expose().len() < MIN_CREDENTIAL_BYTES {
609            return Err(ConfigurationError::InvalidCredential {
610                reason: "Basic secret is shorter than the 16-byte minimum",
611            });
612        }
613        if grant.sink != SecretSinkKind::HttpBasic
614            || username.is_empty()
615            || username.contains(':')
616            || username.bytes().any(|byte| byte.is_ascii_control())
617            || password.expose().len() > MAX_CREDENTIAL_BYTES
618            || password.expose().contains(&b'\n')
619            || password.expose().contains(&b'\r')
620        {
621            return Err(ConfigurationError::InvalidCredential {
622                reason: "Basic credential fields are structurally invalid",
623            });
624        }
625        let mut pair = Vec::with_capacity(username.len() + 1 + password.expose().len());
626        pair.extend_from_slice(username.as_bytes());
627        pair.push(b':');
628        pair.extend_from_slice(password.expose());
629        let encoded = STANDARD.encode(&pair);
630        let rendered = format!("Basic {encoded}");
631        Ok(Self {
632            header_value: Redacted::new(rendered),
633            companion_header: None,
634            destinations: grant.allowed_hosts.clone(),
635            secret_binding: Some(SecretBindingIdentity {
636                grant: grant.clone(),
637            }),
638            echo_values: vec![password, SecretBytes::new(encoded.into_bytes())],
639        })
640    }
641
642    #[must_use]
643    pub fn destinations(&self) -> &[String] {
644        &self.destinations
645    }
646
647    #[must_use]
648    pub fn covers(&self, allowed_host: &str) -> bool {
649        destinations_cover(&self.destinations, allowed_host)
650    }
651
652    fn matches(&self, host: &str, port: u16, scheme: &str) -> bool {
653        self.destinations
654            .iter()
655            .any(|destination| authority_matches(destination, host, port, scheme))
656    }
657
658    #[must_use]
659    pub fn matches_secret_grant(&self, grant: Option<&SecretUseGrant>) -> bool {
660        match (&self.secret_binding, grant) {
661            (None, None) => true,
662            (Some(identity), Some(grant)) => &identity.grant == grant,
663            _ => false,
664        }
665    }
666
667    fn scan_stream_chunk(
668        &self,
669        window: &mut Vec<u8>,
670        bytes: &[u8],
671        overlap: usize,
672    ) -> Option<usize> {
673        window.extend_from_slice(bytes);
674        if self
675            .echo_values
676            .iter()
677            .any(|secret| !secret.expose().is_empty() && contains_bytes(window, secret.expose()))
678        {
679            return None;
680        }
681        Some(window.len() - window.len().min(overlap))
682    }
683
684    fn echo_overlap(&self) -> usize {
685        self.echo_values
686            .iter()
687            .map(|secret| secret.expose().len().saturating_sub(1))
688            .max()
689            .unwrap_or(0)
690    }
691
692    fn echoes_credential(&self, response: &Response) -> bool {
693        self.echo_values.iter().any(|secret| {
694            let secret = secret.expose();
695            !secret.is_empty()
696                && (contains_bytes(&response.body, secret)
697                    || response.headers.iter().any(|header| {
698                        contains_bytes(header.name.as_bytes(), secret)
699                            || contains_bytes(&header.value, secret)
700                    }))
701        })
702    }
703
704    fn render(&self) -> Result<HeaderValue, HttpError> {
705        #[allow(
706            clippy::map_err_ignore,
707            reason = "`InvalidHeaderValue` reports only that parsing failed, and `bearer` already \
708                      restricted these bytes to ASCII graphic; nothing derived from a credential \
709                      value may be reported anyway"
710        )]
711        let mut value = HeaderValue::from_str(self.header_value.expose())
712            .map_err(|_| http_error(ErrorCode::Internal, "credential could not be rendered"))?;
713        value.set_sensitive(true);
714        Ok(value)
715    }
716
717    fn render_companion(&self) -> Option<Result<(HeaderName, HeaderValue), HttpError>> {
718        let (name, value) = self.companion_header.as_ref()?;
719        #[allow(
720            clippy::map_err_ignore,
721            reason = "`InvalidHeaderValue` reports only that parsing failed, and the constructor \
722                      already restricted these bytes to ASCII graphic; nothing derived from a \
723                      credential value may be reported anyway"
724        )]
725        let rendered = HeaderValue::from_str(value.expose())
726            .map_err(|_| http_error(ErrorCode::Internal, "credential could not be rendered"));
727        Some(rendered.map(|mut rendered| {
728            rendered.set_sensitive(true);
729            (name.clone(), rendered)
730        }))
731    }
732}
733
734#[must_use]
735pub fn destinations_cover(destinations: &[String], allowed_host: &str) -> bool {
736    let allowed = allowed_host.trim().to_ascii_lowercase();
737    destinations
738        .iter()
739        .any(|destination| destination.trim().to_ascii_lowercase() == allowed)
740}
741
742fn is_destination_scope(value: &str) -> bool {
743    !value.is_empty()
744        && value.trim() == value
745        && !value
746            .bytes()
747            .any(|byte| byte.is_ascii_control() || byte.is_ascii_whitespace())
748        && !value.contains(['/', '?', '#', '@', '*'])
749}
750
751#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
752#[serde(deny_unknown_fields, rename_all = "camelCase")]
753pub struct HttpCallEvidence {
754    pub method: String,
755    pub authority: String,
756    #[serde(skip_serializing_if = "Option::is_none")]
757    pub status: Option<u16>,
758    pub request_bytes: u64,
759    pub response_bytes: u64,
760    #[serde(default, skip_serializing_if = "Option::is_none")]
761    pub upstream_request_id: Option<String>,
762    #[serde(default, skip_serializing_if = "Option::is_none")]
763    pub vm_session: Option<dekopon_core::VmSession>,
764    #[serde(default, skip_serializing_if = "is_false")]
765    pub credential_injected: bool,
766}
767
768#[allow(clippy::trivially_copy_pass_by_ref)]
769fn is_false(value: &bool) -> bool {
770    !*value
771}
772
773fn contains_bytes(bytes: &[u8], value: &[u8]) -> bool {
774    !value.is_empty() && bytes.windows(value.len()).any(|window| window == value)
775}
776
777fn secret_raw_path_is_ambiguous(uri: &str) -> bool {
778    let Some((_, authority_and_path)) = uri.split_once("://") else {
779        return true;
780    };
781    if authority_and_path.contains('\\') {
782        return true;
783    }
784    let raw_path = authority_and_path
785        .find('/')
786        .map_or("/", |index| &authority_and_path[index..]);
787    let raw_path = raw_path.split_once('?').map_or(raw_path, |(path, _)| path);
788    let raw_path = raw_path.split_once('#').map_or(raw_path, |(path, _)| path);
789    raw_path.contains('%')
790        || raw_path.contains("//")
791        || raw_path
792            .split('/')
793            .skip(1)
794            .any(|segment| matches!(segment, "." | ".."))
795}
796
797#[derive(Clone, Debug, Eq, PartialEq)]
798pub struct HttpRequestFacts {
799    pub method: String,
800    pub host: String,
801    pub path: String,
802    pub query: String,
803    pub headers: Vec<(String, Vec<u8>)>,
804    pub body: Option<Vec<u8>>,
805    pub content_type: Option<String>,
806}
807
808pub type RequestAuthorizer = std::sync::Arc<
809    dyn Fn(HttpRequestFacts) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send>>
810        + Send
811        + Sync,
812>;
813
814pub struct BufferedHttpClient {
815    grant: Option<HttpConstraints>,
816    request_authorizer: Option<RequestAuthorizer>,
817    secret_grant: Option<SecretUseGrant>,
818    credential: Option<BoundCredential>,
819    slots: ChatSlotValues,
820    ceilings: HttpHostCeilings,
821    timeout: Duration,
822    deadline: Instant,
823    calls: u32,
824    secret_injections: u32,
825    attempted: bool,
826    policy_violation: Option<PolicyViolation>,
827    asset_over_budget: bool,
828    evidence: Vec<HttpCallEvidence>,
829    resolved: HashMap<String, Vec<SocketAddr>>,
830    pinned_client: Option<PinnedClient>,
831    #[cfg(feature = "native-test")]
832    test_transport: Option<test_transport::Transport>,
833    #[cfg(test)]
834    signed_download_destination: Option<signed_download::Destination>,
835}
836
837impl std::fmt::Debug for BufferedHttpClient {
838    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
839        f.debug_struct("BufferedHttpClient").finish_non_exhaustive()
840    }
841}
842
843#[derive(Debug)]
844struct PinnedClient {
845    host: String,
846    addresses: Vec<SocketAddr>,
847    client: reqwest::Client,
848}
849
850impl BufferedHttpClient {
851    pub fn disabled(
852        ceilings: HttpHostCeilings,
853        timeout: Duration,
854    ) -> Result<Self, ConfigurationError> {
855        validate_configuration(None, &ceilings, timeout)?;
856        Self::new(None, None, None, ceilings, timeout)
857    }
858
859    pub fn authorized(
860        grant: HttpConstraints,
861        ceilings: HttpHostCeilings,
862        timeout: Duration,
863    ) -> Result<Self, ConfigurationError> {
864        validate_configuration(Some(&grant), &ceilings, timeout)?;
865        Self::new(Some(grant), None, None, ceilings, timeout)
866    }
867
868    pub fn authorized_with_credential(
869        grant: HttpConstraints,
870        credential: Option<BoundCredential>,
871        ceilings: HttpHostCeilings,
872        timeout: Duration,
873    ) -> Result<Self, ConfigurationError> {
874        validate_configuration(Some(&grant), &ceilings, timeout)?;
875        if credential
876            .as_ref()
877            .is_some_and(|credential| !credential.matches_secret_grant(None))
878        {
879            return Err(ConfigurationError::InvalidCredential {
880                reason: "DRN-bound credential requires an authorization-bound secret grant",
881            });
882        }
883        Self::new(Some(grant), None, credential, ceilings, timeout)
884    }
885
886    pub fn authorized_with_secret_credential(
887        grant: HttpConstraints,
888        secret_grant: SecretUseGrant,
889        credential: BoundCredential,
890        ceilings: HttpHostCeilings,
891        timeout: Duration,
892    ) -> Result<Self, ConfigurationError> {
893        validate_configuration(Some(&grant), &ceilings, timeout)?;
894        secret_grant
895            .validate()
896            .map_err(|source| ConfigurationError::InvalidSecretGrant { source })?;
897        if !credential.matches_secret_grant(Some(&secret_grant)) {
898            return Err(ConfigurationError::InvalidCredential {
899                reason: "resolved credential does not match the authorization-bound secret grant",
900            });
901        }
902        Self::new(
903            Some(grant),
904            Some(secret_grant),
905            Some(credential),
906            ceilings,
907            timeout,
908        )
909    }
910
911    fn new(
912        grant: Option<HttpConstraints>,
913        secret_grant: Option<SecretUseGrant>,
914        credential: Option<BoundCredential>,
915        ceilings: HttpHostCeilings,
916        timeout: Duration,
917    ) -> Result<Self, ConfigurationError> {
918        let deadline = Instant::now()
919            .checked_add(timeout)
920            .ok_or(ConfigurationError::TimeoutOverflow)?;
921        Ok(Self {
922            grant,
923            request_authorizer: None,
924            secret_grant,
925            credential,
926            slots: ChatSlotValues::default(),
927            ceilings,
928            timeout,
929            deadline,
930            calls: 0,
931            secret_injections: 0,
932            attempted: false,
933            policy_violation: None,
934            asset_over_budget: false,
935            evidence: Vec::new(),
936            resolved: HashMap::new(),
937            pinned_client: None,
938            #[cfg(feature = "native-test")]
939            test_transport: None,
940            #[cfg(test)]
941            signed_download_destination: None,
942        })
943    }
944
945    #[must_use]
946    pub fn with_chat_slots(mut self, slots: ChatSlotValues) -> Self {
947        self.slots = slots;
948        self
949    }
950
951    #[cfg(feature = "native-test")]
952    pub fn with_test_transport(mut self, transport: test_transport::Transport) -> Self {
953        self.test_transport = Some(transport);
954        self
955    }
956
957    pub fn with_request_authorizer(mut self, authorizer: RequestAuthorizer) -> Self {
958        self.request_authorizer = Some(authorizer);
959        self
960    }
961
962    pub fn asset_over_budget(&self) -> bool {
963        self.asset_over_budget
964    }
965
966    pub fn attempted(&self) -> bool {
967        self.attempted
968    }
969
970    pub fn policy_violation(&self) -> Option<PolicyViolation> {
971        self.policy_violation
972    }
973
974    pub fn into_evidence(self) -> Vec<HttpCallEvidence> {
975        self.evidence
976    }
977
978    pub async fn send(&mut self, request: Request) -> Result<Response, HttpError> {
979        let span = tracing::info_span!(
980            "http.request",
981            "http.request.method" = tracing::field::Empty,
982            "server.address" = tracing::field::Empty,
983            "http.response.status_code" = tracing::field::Empty,
984            "dekopon.http.request.accounted_bytes" = tracing::field::Empty,
985            "dekopon.http.response.accounted_bytes" = tracing::field::Empty,
986            "error.code" = tracing::field::Empty,
987            "error.message" = tracing::field::Empty,
988            "dekopon.http.byte_limit.key" = tracing::field::Empty,
989            "dekopon.http.byte_limit.bytes" = tracing::field::Empty,
990            "url.full" = tracing::field::Empty,
991            outcome = tracing::field::Empty,
992        );
993        span.record("url.full", request.uri.as_str());
994
995        let evidence_index = self.evidence.len();
996        self.attempted = true;
997        // This must use instrument, not enter: holding an entered guard across these awaits would
998        // leave the span current on whatever thread happens to poll unrelated tasks.
999        let result = self.send_checked(request).instrument(span.clone()).await;
1000
1001        self.record_request(&span, evidence_index, &result);
1002        result
1003    }
1004
1005    fn record_request<T>(
1006        &mut self,
1007        span: &tracing::Span,
1008        evidence_index: usize,
1009        result: &Result<T, HttpError>,
1010    ) {
1011        let outcome = outcome_label(result);
1012        let failure = result.as_ref().err();
1013        let byte_limit = failure.and_then(|error| error.byte_limit);
1014        span.in_scope(|| {
1015            let evidence = self.evidence.get(evidence_index);
1016            if let Some(evidence) = evidence {
1017                span.record("http.request.method", evidence.method.as_str());
1018                span.record("server.address", evidence.authority.as_str());
1019                span.record(
1020                    "dekopon.http.request.accounted_bytes",
1021                    evidence.request_bytes,
1022                );
1023                span.record(
1024                    "dekopon.http.response.accounted_bytes",
1025                    evidence.response_bytes,
1026                );
1027                if let Some(status) = evidence.status {
1028                    span.record("http.response.status_code", status);
1029                }
1030            }
1031            if let Some(error) = failure {
1032                // Recording the error reason is safe only because every message this crate produces
1033                // is a static, pre-sanitized string; that property must be preserved.
1034                span.record("error.code", tracing::field::debug(error.code));
1035                span.record("error.message", error.message.as_str());
1036            }
1037            if let Some(exceeded) = byte_limit {
1038                span.record("dekopon.http.byte_limit.key", exceeded.key.as_str());
1039                span.record("dekopon.http.byte_limit.bytes", exceeded.limit);
1040            }
1041            span.record("outcome", outcome);
1042
1043            tracing::info!(
1044                target: "dekopon_http_host::audit",
1045                {
1046                    audit.event = "accounting.http.request",
1047                    "http.request.method" = evidence.map(|evidence| evidence.method.as_str()),
1048                    "server.address" = evidence.map(|evidence| evidence.authority.as_str()),
1049                    "http.response.status_code" = evidence.and_then(|evidence| evidence.status),
1050                    "dekopon.http.request.accounted_bytes" =
1051                        evidence.map(|evidence| evidence.request_bytes),
1052                    "dekopon.http.response.accounted_bytes" =
1053                        evidence.map(|evidence| evidence.response_bytes),
1054                    "error.code" = failure.map(|error| tracing::field::debug(error.code)),
1055                    "error.message" = failure.map(|error| error.message.as_str()),
1056                    "dekopon.http.byte_limit.key" = byte_limit.map(|exceeded| exceeded.key.as_str()),
1057                    "dekopon.http.byte_limit.bytes" = byte_limit.map(|exceeded| exceeded.limit),
1058                    outcome = outcome,
1059                },
1060                "accounting.http.request"
1061            );
1062        });
1063
1064        if let Some(error) = failure {
1065            self.policy_violation = PolicyViolation::from_error(error).or(self.policy_violation);
1066        }
1067    }
1068
1069    async fn authorize_request(
1070        &mut self,
1071        request: Request,
1072        streamed: Option<stream::RequestLengths>,
1073    ) -> Result<(PreparedRequest, HttpConstraints, usize), HttpError> {
1074        let Some(grant) = self.grant.clone() else {
1075            return Err(http_error(
1076                ErrorCode::Denied,
1077                "this invocation has no HTTP authorization",
1078            ));
1079        };
1080        if self.calls >= grant.max_requests {
1081            return Err(http_error(
1082                ErrorCode::HostCallLimit,
1083                "the authorized HTTP request limit is exhausted",
1084            ));
1085        }
1086        self.calls = self.calls.saturating_add(1);
1087        // Each request gets the whole timeout: a provider parked on its stdin has done no work.
1088        self.deadline = Instant::now()
1089            .checked_add(self.timeout)
1090            .ok_or_else(|| http_error(ErrorCode::Internal, "request deadline overflowed"))?;
1091
1092        if self.secret_grant.is_some() && secret_raw_path_is_ambiguous(&request.uri) {
1093            return Err(http_error(
1094                ErrorCode::Denied,
1095                "secret-bearing request path uses prohibited encoding or separators",
1096            ));
1097        }
1098        #[cfg(feature = "native-test")]
1099        let guest = request.clone();
1100        let mut prepared = self.prepare(request, &grant, streamed).await?;
1101        #[cfg(feature = "native-test")]
1102        if let Some(transport) = &self.test_transport {
1103            transport.guest(guest);
1104        }
1105        tracing::Span::current().record("url.full", prepared.url.as_str());
1106        // Credential injection happens strictly after every guest-facing check, and its binding is
1107        // narrower than the grant and fails closed: an allowed-but-unbound destination is refused
1108        // rather than sent unauthenticated.
1109        if let Some(secret) = &self.secret_grant {
1110            let path_allowed = secret
1111                .allowed_paths
1112                .iter()
1113                .any(|rule| rule.matches(prepared.url.path()));
1114            let method_allowed = secret
1115                .allowed_methods
1116                .iter()
1117                .any(|method| method == prepared.method.as_str());
1118            let port = prepared
1119                .url
1120                .port_or_known_default()
1121                .unwrap_or(DEFAULT_HTTPS_PORT);
1122            let host_allowed = secret.allowed_hosts.iter().any(|authority| {
1123                authority_matches(authority, &prepared.host, port, prepared.url.scheme())
1124            });
1125            if !path_allowed
1126                || !method_allowed
1127                || !host_allowed
1128                || (!secret.allow_query && prepared.url.query().is_some())
1129            {
1130                return Err(http_error(
1131                    ErrorCode::Denied,
1132                    "request is outside this secret's authorized HTTP scope",
1133                ));
1134            }
1135            if self.secret_injections >= secret.max_injections {
1136                return Err(http_error(
1137                    ErrorCode::HostCallLimit,
1138                    "the authorized secret injection limit is exhausted",
1139                ));
1140            }
1141        }
1142
1143        let credential_header = self.credential.as_ref().map(|credential| {
1144            let port = prepared
1145                .url
1146                .port_or_known_default()
1147                .unwrap_or(DEFAULT_HTTPS_PORT);
1148            if credential.matches(&prepared.host, port, prepared.url.scheme()) {
1149                credential.render()
1150            } else {
1151                Err(http_error(
1152                    ErrorCode::Denied,
1153                    "destination is outside this credential's binding",
1154                ))
1155            }
1156        });
1157
1158        // Evidence is pushed before the credential decision so a binding refusal still leaves an
1159        // accounted, status-less record rather than a silent gap.
1160        let evidence_index = self.evidence.len();
1161        self.evidence.push(HttpCallEvidence {
1162            method: prepared.method.as_str().to_owned(),
1163            authority: prepared.authority.clone(),
1164            status: None,
1165            request_bytes: prepared.request_bytes,
1166            response_bytes: 0,
1167            upstream_request_id: None,
1168            vm_session: None,
1169            credential_injected: false,
1170        });
1171        if let Some(header) = credential_header {
1172            prepared.headers.insert(AUTHORIZATION, header?);
1173            // The companion header is inserted only after accounting, alongside the bearer token,
1174            // so it never counts against the guest's byte grant.
1175            if let Some(companion) = self
1176                .credential
1177                .as_ref()
1178                .and_then(BoundCredential::render_companion)
1179            {
1180                let (name, value) = companion?;
1181                prepared.headers.insert(name, value);
1182            }
1183            self.evidence[evidence_index].credential_injected = true;
1184            if self.secret_grant.is_some() {
1185                self.secret_injections = self.secret_injections.saturating_add(1);
1186            }
1187        }
1188
1189        if grant.propagate_trace
1190            && let Some(context) = dekopon_telemetry::current_trace_context()
1191        {
1192            let value = format!(
1193                "00-{:032x}-{:016x}-{:02x}",
1194                u128::from_be_bytes(context.trace_id),
1195                u64::from_be_bytes(context.span_id),
1196                context.flags,
1197            );
1198            let header = HeaderValue::from_str(&value).map_err(|_error| {
1199                http_error(ErrorCode::Internal, "trace context could not be rendered")
1200            })?;
1201            prepared.headers.insert("traceparent", header);
1202        }
1203
1204        Ok((prepared, grant, evidence_index))
1205    }
1206
1207    async fn send_checked(&mut self, request: Request) -> Result<Response, HttpError> {
1208        self.send_with_metadata(request, ResponseMetadata::Ordinary)
1209            .await
1210    }
1211
1212    async fn send_with_metadata(
1213        &mut self,
1214        request: Request,
1215        metadata: ResponseMetadata,
1216    ) -> Result<Response, HttpError> {
1217        let (prepared, grant, evidence_index) = self.authorize_request(request, None).await?;
1218        let response = self
1219            .execute(prepared, &grant, evidence_index, metadata)
1220            .await?;
1221        if self
1222            .credential
1223            .as_ref()
1224            .is_some_and(|credential| credential.echoes_credential(&response))
1225        {
1226            return Err(stream::echoed());
1227        }
1228        Ok(response)
1229    }
1230
1231    #[expect(
1232        clippy::too_many_lines,
1233        reason = "reshaped by the unit that next rewrites this"
1234    )]
1235    async fn prepare(
1236        &mut self,
1237        request: Request,
1238        grant: &HttpConstraints,
1239        streamed: Option<stream::RequestLengths>,
1240    ) -> Result<PreparedRequest, HttpError> {
1241        #[allow(
1242            clippy::map_err_ignore,
1243            reason = "`http::method::InvalidMethod` is an opaque unit error whose whole content is \
1244                      \"invalid HTTP method\", which the replacement already states"
1245        )]
1246        let method = Method::from_bytes(request.method.as_bytes()).map_err(|_| {
1247            http_error(ErrorCode::InvalidMethod, "method is not a valid HTTP token")
1248        })?;
1249        if !request
1250            .method
1251            .bytes()
1252            .all(|byte| !byte.is_ascii_lowercase())
1253        {
1254            return Err(http_error(
1255                ErrorCode::Denied,
1256                "HTTP method must be uppercase",
1257            ));
1258        }
1259        if !grant
1260            .allowed_methods
1261            .iter()
1262            .any(|allowed| allowed == method.as_str())
1263        {
1264            return Err(http_error(
1265                ErrorCode::Denied,
1266                "HTTP method is not authorized for this invocation",
1267            ));
1268        }
1269        let bounded_body_bytes =
1270            streamed.map_or(request.body.len() as u64, |lengths| lengths.literal);
1271        let minimum_request_bytes =
1272            encoded_request_bytes(method.as_str(), &request.uri, 0, bounded_body_bytes)
1273                .ok_or_else(|| http_error(ErrorCode::RequestTooLarge, "request size overflowed"))?;
1274        if minimum_request_bytes > grant.max_request_bytes {
1275            return Err(byte_limit_error(
1276                ByteLimitKey::MaxRequestBytes,
1277                grant.max_request_bytes,
1278                minimum_request_bytes,
1279                "request exceeds the authorized byte limit",
1280            ));
1281        }
1282
1283        let mut url = Url::parse(&request.uri).map_err(|error| {
1284            http_error(
1285                ErrorCode::InvalidUri,
1286                format!("URI is not a valid absolute URL: {error}"),
1287            )
1288        })?;
1289        if url.username() != "" || url.password().is_some() || url.fragment().is_some() {
1290            return Err(http_error(
1291                ErrorCode::InvalidUri,
1292                "URI user information and fragments are prohibited",
1293            ));
1294        }
1295        // host_str() returns IPv6 addresses bracketed, but resolution, pinning, and credential
1296        // matching all require the bare unbracketed form.
1297        let host = unbracketed_host(
1298            url.host_str()
1299                .ok_or_else(|| http_error(ErrorCode::InvalidUri, "URI has no host"))?,
1300        )
1301        .to_ascii_lowercase();
1302        let port = url.port_or_known_default().ok_or_else(|| {
1303            http_error(
1304                ErrorCode::InvalidUri,
1305                "URI scheme has no recognized default port",
1306            )
1307        })?;
1308        let authority = canonical_authority(&host, port);
1309        if !grant
1310            .allowed_hosts
1311            .iter()
1312            .any(|allowed| authority_matches(allowed, &host, port, url.scheme()))
1313        {
1314            return Err(http_error(
1315                ErrorCode::Denied,
1316                "HTTP destination is not authorized for this invocation",
1317            ));
1318        }
1319
1320        match url.scheme() {
1321            "https" => {}
1322            "http" if grant.allow_plaintext_loopback => {}
1323            "http" => {
1324                return Err(http_error(
1325                    ErrorCode::Denied,
1326                    "plaintext HTTP is not authorized",
1327                ));
1328            }
1329            _ => {
1330                return Err(http_error(
1331                    ErrorCode::InvalidUri,
1332                    "only HTTP and HTTPS URLs are supported",
1333                ));
1334            }
1335        }
1336        apply_request_templates(grant, &self.slots, &method, &mut url)?;
1337        if !grant.request_templates.is_empty()
1338            && (method == Method::GET || method == Method::HEAD)
1339            && (bounded_body_bytes > 0 || streamed.is_some())
1340        {
1341            return Err(http_error(
1342                ErrorCode::Denied,
1343                "a request template's GET or HEAD rule takes no body",
1344            ));
1345        }
1346
1347        if request.headers.len() > self.ceilings.max_headers {
1348            return Err(http_error(
1349                ErrorCode::RequestTooLarge,
1350                "request has too many headers",
1351            ));
1352        }
1353        let mut headers = HeaderMap::new();
1354        let mut guest_headers = Vec::with_capacity(request.headers.len());
1355        let mut header_bytes = 0_u64;
1356        for header in request.headers {
1357            header_bytes = header_bytes
1358                .checked_add(header.name.len() as u64)
1359                .and_then(|size| size.checked_add(header.value.len() as u64))
1360                .and_then(|size| size.checked_add(4))
1361                .ok_or_else(|| {
1362                    http_error(ErrorCode::RequestTooLarge, "request header size overflowed")
1363                })?;
1364            if header_bytes > self.ceilings.max_header_bytes as u64 {
1365                return Err(http_error(
1366                    ErrorCode::RequestTooLarge,
1367                    "request headers exceed the host limit",
1368                ));
1369            }
1370            #[allow(
1371                clippy::map_err_ignore,
1372                reason = "`InvalidHeaderName` is an opaque unit error naming neither the offending \
1373                          byte nor its position"
1374            )]
1375            let name = HeaderName::from_bytes(header.name.as_bytes()).map_err(|_| {
1376                http_error(ErrorCode::InvalidHeader, "request header name is invalid")
1377            })?;
1378            if is_forbidden_request_header(&name) {
1379                return Err(http_error(
1380                    ErrorCode::InvalidHeader,
1381                    "request header is broker-owned or hop-by-hop",
1382                ));
1383            }
1384            #[allow(
1385                clippy::map_err_ignore,
1386                reason = "`InvalidHeaderValue` is an opaque unit error naming neither the \
1387                          offending byte nor its position"
1388            )]
1389            let value = HeaderValue::from_bytes(&header.value).map_err(|_| {
1390                http_error(ErrorCode::InvalidHeader, "request header value is invalid")
1391            })?;
1392            guest_headers.push((name.as_str().to_owned(), header.value));
1393            headers.append(name, value);
1394        }
1395
1396        let request_bytes = encoded_request_bytes(
1397            method.as_str(),
1398            url.as_str(),
1399            header_bytes,
1400            bounded_body_bytes,
1401        )
1402        .ok_or_else(|| http_error(ErrorCode::RequestTooLarge, "request size overflowed"))?;
1403        if request_bytes > grant.max_request_bytes {
1404            return Err(byte_limit_error(
1405                ByteLimitKey::MaxRequestBytes,
1406                grant.max_request_bytes,
1407                request_bytes,
1408                "request exceeds the authorized byte limit",
1409            ));
1410        }
1411
1412        if let Some(authorize) = &self.request_authorizer {
1413            let content_types = headers.get_all(reqwest::header::CONTENT_TYPE);
1414            let mut types = content_types.iter();
1415            let content_type = types.next().and_then(|value| {
1416                types
1417                    .next()
1418                    .is_none()
1419                    .then(|| value.to_str().ok())
1420                    .flatten()
1421            });
1422            let facts = HttpRequestFacts {
1423                method: method.as_str().to_owned(),
1424                host: host.clone(),
1425                path: url.path().to_owned(),
1426                query: url.query().unwrap_or_default().to_owned(),
1427                headers: guest_headers,
1428                body: (streamed.is_none()).then_some(request.body.clone()),
1429                content_type: content_type.map(str::to_owned),
1430            };
1431            if !authorize(facts).await {
1432                return Err(http_error(
1433                    ErrorCode::Denied,
1434                    "HTTP request denied by policy",
1435                ));
1436            }
1437        }
1438
1439        #[cfg(feature = "native-test")]
1440        if let Some(transport) = &self.test_transport {
1441            if streamed.is_some() {
1442                return Err(http_error(
1443                    ErrorCode::Denied,
1444                    "streamed requests have no test script",
1445                ));
1446            }
1447            transport.admit(&url)?;
1448            return Ok(PreparedRequest {
1449                method,
1450                url,
1451                headers,
1452                body: request.body,
1453                host,
1454                authority,
1455                addresses: Vec::new(),
1456                request_bytes,
1457            });
1458        }
1459
1460        // DNS resolution is cached for this context's lifetime, but every cached address is still
1461        // validated on each call, so caching narrows latency, not what may be reached.
1462        let addresses = if let Some(addresses) = self.resolved.get(&authority) {
1463            addresses.clone()
1464        } else {
1465            let remaining = self.remaining()?;
1466            #[allow(
1467                clippy::map_err_ignore,
1468                reason = "`tokio::time::error::Elapsed` carries only \"deadline has elapsed\", which \
1469                          the Timeout message already states"
1470            )]
1471            let addresses = if let Some(pin) = self
1472                .ceilings
1473                .loopback_https_pin
1474                .as_ref()
1475                .filter(|pin| pin.authority == authority && url.scheme() == "https")
1476            {
1477                vec![pin.address]
1478            } else {
1479                timeout(remaining, resolve_destination(&host, port))
1480                    .await
1481                    .map_err(|_| {
1482                        http_error(ErrorCode::Timeout, "destination resolution timed out")
1483                    })??
1484            };
1485            self.resolved.insert(authority.clone(), addresses.clone());
1486            addresses
1487        };
1488        if !plaintext_permitted(
1489            url.scheme(),
1490            &host,
1491            &addresses,
1492            &self.ceilings.plaintext_hosts,
1493        ) {
1494            return Err(http_error(
1495                ErrorCode::Denied,
1496                format!(
1497                    "plaintext HTTP to {host} is refused: not a loopback destination, and not \
1498                     listed in the broker's http.plaintextHosts"
1499                ),
1500            ));
1501        }
1502        if url.scheme() == "https" {
1503            let non_public = self
1504                .ceilings
1505                .non_public_https
1506                .iter()
1507                .any(|entry| entry.authority == authority);
1508            let pinned_loopback = self
1509                .ceilings
1510                .loopback_https_pin
1511                .as_ref()
1512                .is_some_and(|pin| pin.authority == authority && addresses == [pin.address]);
1513            if !pinned_loopback && !https_addresses_permitted(non_public, &addresses) {
1514                return Err(http_error(
1515                    ErrorCode::Denied,
1516                    if non_public {
1517                        "non-public HTTPS requires private unicast addresses"
1518                    } else {
1519                        "destination resolved to a non-public address"
1520                    },
1521                ));
1522            }
1523        }
1524
1525        Ok(PreparedRequest {
1526            method,
1527            url,
1528            headers,
1529            body: request.body,
1530            host,
1531            authority,
1532            addresses,
1533            request_bytes: streamed.map_or(request_bytes, |lengths| lengths.total),
1534        })
1535    }
1536
1537    async fn execute(
1538        &mut self,
1539        request: PreparedRequest,
1540        grant: &HttpConstraints,
1541        evidence_index: usize,
1542        metadata: ResponseMetadata,
1543    ) -> Result<Response, HttpError> {
1544        let remaining = self.remaining()?;
1545        #[cfg(feature = "native-test")]
1546        let scripted = self.test_transport.as_ref().map(|transport| {
1547            transport.response(
1548                &request.method,
1549                &request.url,
1550                &request.headers,
1551                &request.body,
1552            )
1553        });
1554        #[cfg(not(feature = "native-test"))]
1555        let scripted: Option<Result<reqwest::Response, HttpError>> = None;
1556        let response = match scripted {
1557            Some(response) => response?,
1558            None => {
1559                let client = self.pinned_client(&request.host, &request.addresses, remaining)?;
1560
1561                #[allow(
1562                    clippy::map_err_ignore,
1563                    reason = "`tokio::time::error::Elapsed` carries only \"deadline has elapsed\", which \
1564                      the Timeout message already states"
1565                )]
1566                timeout(
1567                    remaining,
1568                    client
1569                        .request(request.method.clone(), request.url)
1570                        .headers(request.headers)
1571                        .body(request.body)
1572                        .send(),
1573                )
1574                .await
1575                .map_err(|_| http_error(ErrorCode::Timeout, "HTTP request timed out"))?
1576                .map_err(|error| map_reqwest_error(&error))?
1577            }
1578        };
1579
1580        let stream::Head {
1581            status,
1582            mut headers,
1583            bytes: mut response_bytes,
1584            body: mut decoded,
1585        } = self.accept_head(&request.method, response, grant, evidence_index, metadata)?;
1586        let mut redactor = match metadata {
1587            ResponseMetadata::Ordinary => None,
1588            ResponseMetadata::SignedDownload if status != 302 => {
1589                Some(signed_download::ApiRedactor::new(&mut headers)?)
1590            }
1591            ResponseMetadata::SignedDownload => None,
1592        };
1593        let output_head = returned_head_bytes(&headers);
1594        if output_head > grant.max_response_bytes {
1595            return Err(byte_limit_error(
1596                ByteLimitKey::MaxResponseBytes,
1597                grant.max_response_bytes,
1598                output_head,
1599                "redacted headers exceed the authorized byte limit",
1600            ));
1601        }
1602        if redactor.is_some() && output_head > self.ceilings.max_header_bytes as u64 {
1603            return Err(http_error(
1604                ErrorCode::ResponseTooLarge,
1605                "redacted headers exceed the authorized byte limit",
1606            ));
1607        }
1608        let maximum = grant.max_response_bytes - output_head;
1609        let mut body = Vec::new();
1610        while let Some(chunk) = decoded.next().await? {
1611            response_bytes = response_bytes
1612                .checked_add(chunk.len() as u64)
1613                .ok_or_else(|| {
1614                    http_error(ErrorCode::ResponseTooLarge, "response size overflowed")
1615                })?;
1616            self.evidence[evidence_index].response_bytes += chunk.len() as u64;
1617            if response_bytes > grant.max_response_bytes {
1618                return Err(byte_limit_error(
1619                    ByteLimitKey::MaxResponseBytes,
1620                    grant.max_response_bytes,
1621                    response_bytes,
1622                    "response exceeds the authorized byte limit",
1623                ));
1624            }
1625            match &mut redactor {
1626                Some(redactor) => {
1627                    for bytes in chunk.chunks(stream::CHUNK_BYTES) {
1628                        redactor.push(bytes, &mut body, maximum)?;
1629                    }
1630                }
1631                None => body.extend_from_slice(&chunk),
1632            }
1633        }
1634
1635        if let Some(redactor) = &mut redactor {
1636            redactor.finish(&mut body, maximum)?;
1637        }
1638
1639        Ok(Response {
1640            status,
1641            headers,
1642            body,
1643        })
1644    }
1645
1646    fn pinned_client(
1647        &mut self,
1648        host: &str,
1649        addresses: &[SocketAddr],
1650        budget: Duration,
1651    ) -> Result<reqwest::Client, HttpError> {
1652        if let Some(pinned) = &self.pinned_client
1653            && pinned.host == host
1654            && pinned.addresses == addresses
1655        {
1656            return Ok(pinned.client.clone());
1657        }
1658        let mut builder = reqwest::Client::builder()
1659            .redirect(redirect::Policy::none())
1660            .no_proxy()
1661            .default_headers(HeaderMap::from_iter([(
1662                ACCEPT_ENCODING,
1663                HeaderValue::from_static("gzip, deflate"),
1664            )]))
1665            .connect_timeout(budget)
1666            .resolve_to_addrs(host, addresses);
1667        let pinned_ca = self
1668            .ceilings
1669            .loopback_https_pin
1670            .as_ref()
1671            .filter(|pin| {
1672                pin.authority == canonical_authority(host, addresses[0].port())
1673                    && addresses == [pin.address]
1674            })
1675            .map(|pin| pin.ca_pem.as_slice());
1676        for pem in self
1677            .ceilings
1678            .extra_ca_bundles
1679            .iter()
1680            .map(Vec::as_slice)
1681            .chain(pinned_ca)
1682        {
1683            for cert in reqwest::Certificate::from_pem_bundle(pem)
1684                .map_err(|_error| http_error(ErrorCode::Denied, "invalid configured CA bundle"))?
1685            {
1686                builder = builder.add_root_certificate(cert);
1687            }
1688        }
1689        let client = builder.build().map_err(|error| map_reqwest_error(&error))?;
1690        self.pinned_client = Some(PinnedClient {
1691            host: host.to_owned(),
1692            addresses: addresses.to_vec(),
1693            client: client.clone(),
1694        });
1695        Ok(client)
1696    }
1697
1698    fn remaining(&self) -> Result<Duration, HttpError> {
1699        self.deadline
1700            .checked_duration_since(Instant::now())
1701            .filter(|remaining| !remaining.is_zero())
1702            .ok_or_else(|| http_error(ErrorCode::Timeout, "invocation deadline expired"))
1703    }
1704}
1705
1706fn apply_request_templates(
1707    grant: &HttpConstraints,
1708    slots: &ChatSlotValues,
1709    method: &Method,
1710    url: &mut Url,
1711) -> Result<(), HttpError> {
1712    if grant.request_templates.is_empty() {
1713        return Ok(());
1714    }
1715    let decoded = percent_encoding::percent_decode_str(url.path())
1716        .decode_utf8()
1717        .map_err(|_error| {
1718            http_error(
1719                ErrorCode::Denied,
1720                "request path is not UTF-8 once decoded, so no request template matches",
1721            )
1722        })?;
1723    let segments = if decoded == "/" {
1724        Vec::new()
1725    } else {
1726        decoded.split('/').skip(1).collect::<Vec<_>>()
1727    };
1728    let Some(template) = grant
1729        .request_templates
1730        .iter()
1731        .find(|template| template.method == method.as_str() && template.path.matches(&segments))
1732    else {
1733        return Err(http_error(
1734            ErrorCode::Denied,
1735            "request matches no request template of this capability",
1736        ));
1737    };
1738    let pairs = url
1739        .query_pairs()
1740        .map(|(key, value)| (key.into_owned(), value.into_owned()))
1741        .collect::<Vec<_>>();
1742    for (key, _) in &pairs {
1743        if template.query.pinned.contains_key(key) {
1744            return Err(http_error(
1745                ErrorCode::Denied,
1746                format!("query key {key:?} is host-filled by request template {template}"),
1747            ));
1748        }
1749        if !template.query.allowed.contains(key) {
1750            return Err(http_error(
1751                ErrorCode::Denied,
1752                format!("request template {template} does not allow one of the query keys"),
1753            ));
1754        }
1755    }
1756    let slot_value = |slot| {
1757        slots.get(slot).ok_or_else(|| {
1758            http_error(
1759                ErrorCode::Denied,
1760                format!(
1761                    "request template {template} needs {{{slot}}}, which this invocation lacks"
1762                ),
1763            )
1764        })
1765    };
1766    let mut path = Vec::with_capacity(template.path.segments().len());
1767    for segment in template.path.segments() {
1768        path.push(match segment {
1769            PathSegment::Literal(literal) => literal.as_str(),
1770            PathSegment::Slot(slot) => slot_value(*slot)?,
1771        });
1772    }
1773    let mut pinned = Vec::with_capacity(template.query.pinned.len());
1774    for (key, slot) in &template.query.pinned {
1775        pinned.push((key.as_str(), slot_value(*slot)?));
1776    }
1777    url.path_segments_mut()
1778        .map_err(|()| http_error(ErrorCode::InvalidUri, "URI cannot carry a path"))?
1779        .clear()
1780        .extend(path);
1781    if pairs.is_empty() && pinned.is_empty() {
1782        url.set_query(None);
1783    } else {
1784        url.query_pairs_mut()
1785            .clear()
1786            .extend_pairs(&pairs)
1787            .extend_pairs(pinned);
1788    }
1789    Ok(())
1790}
1791
1792fn validate_configuration(
1793    grant: Option<&HttpConstraints>,
1794    ceilings: &HttpHostCeilings,
1795    timeout: Duration,
1796) -> Result<(), ConfigurationError> {
1797    for (field, value) in [
1798        ("max_requests", u128::from(ceilings.max_requests)),
1799        ("max_request_bytes", u128::from(ceilings.max_request_bytes)),
1800        (
1801            "max_response_bytes",
1802            u128::from(ceilings.max_response_bytes),
1803        ),
1804        ("max_headers", ceilings.max_headers as u128),
1805        ("max_header_bytes", ceilings.max_header_bytes as u128),
1806    ] {
1807        if value == 0 {
1808            return Err(ConfigurationError::ZeroCeiling { field });
1809        }
1810    }
1811    if timeout.is_zero() {
1812        return Err(ConfigurationError::ZeroTimeout);
1813    }
1814    if let Some(grant) = grant {
1815        grant
1816            .validate()
1817            .map_err(|source| ConfigurationError::InvalidGrant { source })?;
1818        if grant.max_requests > ceilings.max_requests
1819            || grant.max_request_bytes > ceilings.max_request_bytes
1820            || grant.max_response_bytes > ceilings.max_response_bytes
1821        {
1822            return Err(ConfigurationError::GrantExceedsCeiling);
1823        }
1824    }
1825    Ok(())
1826}
1827
1828#[derive(Debug)]
1829struct PreparedRequest {
1830    method: Method,
1831    url: Url,
1832    headers: HeaderMap,
1833    body: Vec<u8>,
1834    host: String,
1835    authority: String,
1836    addresses: Vec<SocketAddr>,
1837    request_bytes: u64,
1838}
1839
1840async fn resolve_destination(host: &str, port: u16) -> Result<Vec<SocketAddr>, HttpError> {
1841    let addresses = tokio::net::lookup_host((host, port))
1842        .await
1843        .map_err(|error| {
1844            tracing::debug!(error = %error, "http.destination.unresolved");
1845            http_error(ErrorCode::Dns, "destination could not be resolved")
1846        })?;
1847    bounded_addresses(addresses)
1848}
1849
1850fn bounded_addresses(
1851    addresses: impl IntoIterator<Item = SocketAddr>,
1852) -> Result<Vec<SocketAddr>, HttpError> {
1853    let mut unique = BTreeSet::new();
1854    for address in addresses {
1855        unique.insert(address);
1856        if unique.len() == MAX_RESOLVED_ADDRESSES {
1857            break;
1858        }
1859    }
1860    if unique.is_empty() {
1861        return Err(http_error(
1862            ErrorCode::Dns,
1863            "destination resolved to no addresses",
1864        ));
1865    }
1866    Ok(unique.into_iter().collect())
1867}
1868
1869fn unbracketed_host(host: &str) -> &str {
1870    host.strip_prefix('[')
1871        .and_then(|rest| rest.strip_suffix(']'))
1872        .unwrap_or(host)
1873}
1874
1875fn canonical_host(host: &str) -> Cow<'_, str> {
1876    if host.contains(':') {
1877        Cow::Owned(format!("[{host}]"))
1878    } else {
1879        Cow::Borrowed(host)
1880    }
1881}
1882
1883fn canonical_authority(host: &str, port: u16) -> String {
1884    format!("{}:{port}", canonical_host(host))
1885}
1886
1887fn plaintext_permitted(
1888    scheme: &str,
1889    host: &str,
1890    addresses: &[SocketAddr],
1891    allowed: &PlaintextHosts,
1892) -> bool {
1893    scheme != "http"
1894        || addresses.iter().all(|address| address.ip().is_loopback())
1895        || allowed.contains(host)
1896}
1897
1898fn authority_matches(allowed: &str, host: &str, port: u16, scheme: &str) -> bool {
1899    let allowed = allowed.trim().to_ascii_lowercase();
1900    allowed == canonical_authority(host, port)
1901        || (scheme == "https"
1902            && allowed == canonical_host(host).as_ref()
1903            && port == DEFAULT_HTTPS_PORT)
1904}
1905
1906fn is_forbidden_request_header(name: &HeaderName) -> bool {
1907    matches!(
1908        name.as_str(),
1909        "authorization"
1910            | CHATGPT_ACCOUNT_HEADER
1911            | "connection"
1912            | "content-length"
1913            | "cookie"
1914            | "host"
1915            | "keep-alive"
1916            | "proxy-authenticate"
1917            | "proxy-authorization"
1918            | "proxy-connection"
1919            | "te"
1920            | "traceparent"
1921            | "tracestate"
1922            | "trailer"
1923            | "transfer-encoding"
1924            | "upgrade"
1925    )
1926}
1927
1928fn is_forbidden_response_header(name: &HeaderName) -> bool {
1929    matches!(
1930        name.as_str(),
1931        "connection"
1932            | "keep-alive"
1933            | "proxy-authenticate"
1934            | "proxy-authorization"
1935            | "set-cookie"
1936            | "te"
1937            | "trailer"
1938            | "transfer-encoding"
1939            | "upgrade"
1940            | "www-authenticate"
1941    )
1942}
1943
1944fn encoded_request_bytes(
1945    method: &str,
1946    uri: &str,
1947    header_bytes: u64,
1948    body_bytes: u64,
1949) -> Option<u64> {
1950    REQUEST_ENCODING_OVERHEAD_BYTES
1951        .checked_add(method.len() as u64)?
1952        .checked_add(uri.len() as u64)?
1953        .checked_add(header_bytes)?
1954        .checked_add(body_bytes)
1955}
1956
1957fn https_addresses_permitted(non_public: bool, addresses: &[SocketAddr]) -> bool {
1958    !addresses.is_empty()
1959        && addresses.iter().all(|address| {
1960            if non_public {
1961                is_private_unicast(address.ip())
1962            } else {
1963                !is_forbidden_public_destination(address.ip())
1964            }
1965        })
1966}
1967
1968fn is_private_unicast(ip: IpAddr) -> bool {
1969    match ip {
1970        IpAddr::V4(ip) => ip.is_private() && !ip.is_loopback() && !ip.is_link_local(),
1971        IpAddr::V6(ip) => (ip.segments()[0] & 0xfe00) == 0xfc00,
1972    }
1973}
1974
1975fn is_forbidden_public_destination(ip: IpAddr) -> bool {
1976    match ip {
1977        IpAddr::V4(ip) => is_forbidden_ipv4(ip),
1978        IpAddr::V6(ip) => is_forbidden_ipv6(ip),
1979    }
1980}
1981
1982fn is_forbidden_ipv4(ip: Ipv4Addr) -> bool {
1983    let [a, b, c, _d] = ip.octets();
1984    a == 0
1985        || a == 10
1986        || a == 127
1987        || (a == 100 && (64..=127).contains(&b))
1988        || (a == 169 && b == 254)
1989        || (a == 172 && (16..=31).contains(&b))
1990        || (a == 192 && b == 0 && c == 0)
1991        || (a == 192 && b == 0 && c == 2)
1992        || (a == 192 && b == 88 && c == 99)
1993        || (a == 192 && b == 168)
1994        || (a == 198 && (b == 18 || b == 19))
1995        || (a == 198 && b == 51 && c == 100)
1996        || (a == 203 && b == 0 && c == 113)
1997        || a >= 224
1998}
1999
2000fn is_forbidden_ipv6(ip: Ipv6Addr) -> bool {
2001    if let Some(ipv4) = ip.to_ipv4_mapped() {
2002        return is_forbidden_ipv4(ipv4);
2003    }
2004    let segments = ip.segments();
2005    ip.is_unspecified()
2006        || ip.is_loopback()
2007        // Permit only global-unicast 2000::/3, then remove IETF special-purpose 2001::/23.
2008        || (segments[0] & 0xe000) != 0x2000
2009        || (segments[0] == 0x2001 && (segments[1] & 0xfe00) == 0)
2010        || (segments[0] == 0x2001 && segments[1] == 0x0db8)
2011        || segments[0] == 0x2002
2012        || (segments[0] == 0x3fff && (segments[1] & 0xf000) == 0)
2013}
2014
2015fn map_reqwest_error(error: &reqwest::Error) -> HttpError {
2016    if error.is_builder() {
2017        return http_error(ErrorCode::Internal, "native HTTP client setup failed");
2018    }
2019    let code = if error.is_timeout() {
2020        ErrorCode::Timeout
2021    } else if error.is_connect() {
2022        if error_chain_contains(error, &["tls", "certificate", "cert "]) {
2023            ErrorCode::Tls
2024        } else {
2025            ErrorCode::Connect
2026        }
2027    } else {
2028        ErrorCode::Protocol
2029    };
2030    http_error(code, "HTTP transport failed")
2031}
2032
2033fn error_chain_contains(error: &reqwest::Error, fragments: &[&str]) -> bool {
2034    let mut source = error.source();
2035    while let Some(current) = source {
2036        let message = current.to_string().to_ascii_lowercase();
2037        if fragments.iter().any(|fragment| message.contains(fragment)) {
2038            return true;
2039        }
2040        source = current.source();
2041    }
2042    false
2043}
2044
2045pub(crate) fn http_error(code: ErrorCode, message: impl AsRef<str>) -> HttpError {
2046    HttpError {
2047        code,
2048        message: bounded_message(message.as_ref()),
2049        byte_limit: None,
2050    }
2051}
2052
2053pub(crate) fn byte_limit_error(
2054    key: ByteLimitKey,
2055    limit: u64,
2056    observed: u64,
2057    message: &str,
2058) -> HttpError {
2059    HttpError {
2060        code: key.code(),
2061        message: bounded_message(message),
2062        byte_limit: Some(ByteLimitExceeded {
2063            key,
2064            limit,
2065            observed,
2066        }),
2067    }
2068}
2069
2070fn bounded_message(message: &str) -> String {
2071    let mut output = String::with_capacity(message.len().min(MAX_ERROR_MESSAGE_BYTES));
2072    for character in message.chars() {
2073        if output.len() + character.len_utf8() > MAX_ERROR_MESSAGE_BYTES {
2074            break;
2075        }
2076        if character.is_control() {
2077            output.push(' ');
2078        } else {
2079            output.push(character);
2080        }
2081    }
2082    output
2083}
2084
2085fn returned_head_bytes(headers: &[Header]) -> u64 {
2086    headers.iter().fold(16_u64, |total, header| {
2087        total.saturating_add((header.name.len() + header.value.len() + 4) as u64)
2088    })
2089}
2090
2091fn response_head_bytes(
2092    headers: &HeaderMap,
2093    ceilings: &HttpHostCeilings,
2094    maximum: u64,
2095) -> Result<u64, HttpError> {
2096    if headers.len() > ceilings.max_headers {
2097        return Err(http_error(
2098            ErrorCode::ResponseTooLarge,
2099            "response has too many headers",
2100        ));
2101    }
2102    let mut bytes = 16_u64;
2103    head_within(bytes, ceilings, maximum)?;
2104    for (name, value) in headers {
2105        bytes = bytes
2106            .checked_add((name.as_str().len() + value.as_bytes().len() + 4) as u64)
2107            .ok_or_else(|| http_error(ErrorCode::ResponseTooLarge, "response size overflow"))?;
2108        head_within(bytes, ceilings, maximum)?;
2109    }
2110    Ok(bytes)
2111}
2112
2113fn head_within(bytes: u64, ceilings: &HttpHostCeilings, maximum: u64) -> Result<(), HttpError> {
2114    const MESSAGE: &str = "response headers exceed an authorized bound";
2115    if bytes > maximum {
2116        return Err(byte_limit_error(
2117            ByteLimitKey::MaxResponseBytes,
2118            maximum,
2119            bytes,
2120            MESSAGE,
2121        ));
2122    }
2123    if bytes > ceilings.max_header_bytes as u64 {
2124        return Err(http_error(ErrorCode::ResponseTooLarge, MESSAGE));
2125    }
2126    Ok(())
2127}
2128
2129fn outcome_label<T>(result: &Result<T, HttpError>) -> &'static str {
2130    match result {
2131        Ok(_) => "succeeded",
2132        Err(error) => PolicyViolation::from_error(error).map_or("failed", PolicyViolation::label),
2133    }
2134}
2135
2136#[cfg(test)]
2137mod tests {
2138    use std::{
2139        net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
2140        time::Duration,
2141    };
2142
2143    use dekopon_test_support::LoopbackServer;
2144
2145    use dekopon_capability::{
2146        ChatSlot, ChatSlotValues, HttpConstraints, HttpConstraintsError, HttpPathRule,
2147        RequestTemplate, SecretUseGrant,
2148    };
2149    use dekopon_core::{Redacted, SecretBytes, SecretSinkKind};
2150
2151    use super::{
2152        BoundCredential, BufferedHttpClient, ConfigurationError, ErrorCode, Header,
2153        HttpCallEvidence, HttpHostCeilings, MAX_RESOLVED_ADDRESSES, MIN_CREDENTIAL_BYTES,
2154        PlaintextHostError, PlaintextHosts, PolicyViolation, Request, authority_matches,
2155        bounded_addresses, bounded_message, is_forbidden_public_destination, map_reqwest_error,
2156        plaintext_permitted,
2157    };
2158
2159    fn grant(authority: String, method: &str) -> HttpConstraints {
2160        HttpConstraints {
2161            allowed_hosts: vec![authority],
2162            allowed_methods: vec![method.to_owned()],
2163            max_requests: 2,
2164            max_request_bytes: 64 * 1024,
2165            max_response_bytes: 64 * 1024,
2166            allow_plaintext_loopback: true,
2167            propagate_trace: false,
2168            request_templates: Vec::new(),
2169            signed_download: None,
2170        }
2171    }
2172
2173    #[tokio::test]
2174    async fn request_hook_sees_effective_guest_facts_and_denial_precedes_resolution() {
2175        use std::sync::{Arc, Mutex};
2176
2177        let seen = Arc::new(Mutex::new(Vec::new()));
2178        let capture = {
2179            let seen = Arc::clone(&seen);
2180            Arc::new(move |facts: super::HttpRequestFacts| -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send>> {
2181                seen.lock().expect("facts").push(facts);
2182                Box::pin(async { false })
2183            }) as super::RequestAuthorizer
2184        };
2185        let mut client = BufferedHttpClient::authorized(
2186            grant("no-such-egress.invalid".to_owned(), "POST"),
2187            HttpHostCeilings::default(),
2188            Duration::from_secs(1),
2189        )
2190        .expect("client")
2191        .with_request_authorizer(capture);
2192        let error = client
2193            .send(Request {
2194                method: "POST".to_owned(),
2195                uri: "https://NO-SUCH-EGRESS.INVALID/reply?".to_owned(),
2196                headers: vec![Header {
2197                    name: "Content-Type".to_owned(),
2198                    value: b"application/json; charset=utf-8".to_vec(),
2199                }],
2200                body: br#"{"channel":"C0123","thread_ts":"1"}"#.to_vec(),
2201            })
2202            .await
2203            .expect_err("policy refusal, not DNS failure");
2204        assert_eq!(error.code, ErrorCode::Denied);
2205        let error = client
2206            .send(Request {
2207                method: "POST".to_owned(),
2208                uri: "https://no-such-egress.invalid/reply".to_owned(),
2209                headers: vec![
2210                    Header {
2211                        name: "CONTENT-TYPE".to_owned(),
2212                        value: b"application/json".to_vec(),
2213                    },
2214                    Header {
2215                        name: "content-type".to_owned(),
2216                        value: b"application/problem+json".to_vec(),
2217                    },
2218                ],
2219                body: br#"{"channel":"C0123"}"#.to_vec(),
2220            })
2221            .await
2222            .expect_err("policy refusal with duplicate content type");
2223        assert_eq!(error.code, ErrorCode::Denied);
2224        let seen = seen.lock().expect("facts");
2225        assert_eq!(seen.len(), 2);
2226        assert_eq!(seen[1].content_type, None);
2227        assert_eq!(seen[1].headers.len(), 2);
2228        let facts = &seen[0];
2229        assert_eq!(facts.method, "POST");
2230        assert_eq!(facts.host, "no-such-egress.invalid");
2231        assert_eq!(facts.path, "/reply");
2232        assert_eq!(facts.query, "");
2233        assert_eq!(
2234            facts.headers,
2235            vec![(
2236                "content-type".to_owned(),
2237                b"application/json; charset=utf-8".to_vec()
2238            )]
2239        );
2240        assert_eq!(
2241            facts.content_type.as_deref(),
2242            Some("application/json; charset=utf-8")
2243        );
2244        assert_eq!(
2245            facts.body.as_deref(),
2246            Some(br#"{"channel":"C0123","thread_ts":"1"}"#.as_slice())
2247        );
2248    }
2249
2250    #[tokio::test(flavor = "current_thread")]
2251    async fn request_decision_completes_on_the_invocation_thread() {
2252        let invocation_thread = std::thread::current().id();
2253        let (sender, receiver) = std::sync::mpsc::channel();
2254        let mut client = BufferedHttpClient::authorized(
2255            grant("no-such-egress.invalid".to_owned(), "GET"),
2256            HttpHostCeilings::default(),
2257            Duration::from_secs(1),
2258        )
2259        .expect("client")
2260        .with_request_authorizer(std::sync::Arc::new(move |_| {
2261            sender
2262                .send(std::thread::current().id())
2263                .expect("decision observation");
2264            Box::pin(async { false })
2265        }));
2266        let result = client
2267            .send(Request {
2268                method: "GET".to_owned(),
2269                uri: "https://no-such-egress.invalid/reply".to_owned(),
2270                headers: Vec::new(),
2271                body: Vec::new(),
2272            })
2273            .await;
2274        assert!(matches!(
2275            result,
2276            Err(super::HttpError {
2277                code: ErrorCode::Denied,
2278                ..
2279            })
2280        ));
2281        assert_eq!(
2282            receiver
2283                .try_recv()
2284                .expect("decision completed before the host call returned"),
2285            invocation_thread,
2286            "request decision must run inside the calling thread's poll"
2287        );
2288        assert!(matches!(
2289            receiver.try_recv(),
2290            Err(std::sync::mpsc::TryRecvError::Empty)
2291        ));
2292    }
2293
2294    #[tokio::test]
2295    async fn unmatched_template_and_lowercase_method_never_call_request_hook() {
2296        use std::sync::{
2297            Arc,
2298            atomic::{AtomicUsize, Ordering},
2299        };
2300        let calls = Arc::new(AtomicUsize::new(0));
2301        let hook = {
2302            let calls = Arc::clone(&calls);
2303            Arc::new(
2304                move |_| -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send>> {
2305                    calls.fetch_add(1, Ordering::SeqCst);
2306                    Box::pin(async { false })
2307                },
2308            ) as super::RequestAuthorizer
2309        };
2310        let mut client = BufferedHttpClient::authorized(
2311            slack_replies("127.0.0.1:9"),
2312            HttpHostCeilings::default(),
2313            Duration::from_secs(1),
2314        )
2315        .expect("client")
2316        .with_request_authorizer(hook);
2317        let result = client
2318            .send(Request {
2319                method: "POST".to_owned(),
2320                uri: "http://127.0.0.1:9/api/conversations.replies".to_owned(),
2321                headers: Vec::new(),
2322                body: Vec::new(),
2323            })
2324            .await;
2325        assert!(matches!(
2326            result,
2327            Err(super::HttpError {
2328                code: ErrorCode::Denied,
2329                ..
2330            })
2331        ));
2332        let lower_calls = std::sync::Arc::clone(&calls);
2333        let mut lowercase = BufferedHttpClient::authorized(
2334            grant("127.0.0.1:9".to_owned(), "get"),
2335            HttpHostCeilings::default(),
2336            Duration::from_secs(1),
2337        )
2338        .expect("lowercase grant admits the token")
2339        .with_request_authorizer(std::sync::Arc::new(move |_| {
2340            lower_calls.fetch_add(1, Ordering::SeqCst);
2341            Box::pin(async { false })
2342        }));
2343        let error = lowercase
2344            .send(Request {
2345                method: "get".to_owned(),
2346                uri: "http://127.0.0.1:9/".to_owned(),
2347                headers: Vec::new(),
2348                body: Vec::new(),
2349            })
2350            .await
2351            .expect_err("lowercase admitted token cannot reach Cedar or the wire");
2352        assert_eq!(error.code, ErrorCode::Denied);
2353        assert_eq!(calls.load(Ordering::SeqCst), 0);
2354    }
2355
2356    fn address(ip: &str, port: u16) -> SocketAddr {
2357        SocketAddr::new(ip.parse().expect("valid fixture address"), port)
2358    }
2359
2360    fn plaintext_hosts(entries: &[&str]) -> PlaintextHosts {
2361        PlaintextHosts::new(entries).expect("valid fixture host list")
2362    }
2363
2364    #[test]
2365    fn plaintext_reaches_loopback_with_an_empty_allow_list() {
2366        let empty = PlaintextHosts::default();
2367        assert!(empty.is_empty());
2368        assert!(plaintext_permitted(
2369            "http",
2370            "localhost",
2371            &[address("127.0.0.1", 8080)],
2372            &empty
2373        ));
2374        assert!(plaintext_permitted(
2375            "http",
2376            "::1",
2377            &[address("::1", 8080)],
2378            &empty
2379        ));
2380        assert!(!plaintext_permitted(
2381            "http",
2382            "rpi.lan",
2383            &[address("192.168.1.20", 5080)],
2384            &empty
2385        ));
2386    }
2387
2388    #[test]
2389    fn plaintext_reaches_a_listed_host_and_nothing_else() {
2390        let allowed = plaintext_hosts(&["rpi.lan", "openobserve.openobserve.svc"]);
2391        assert!(plaintext_permitted(
2392            "http",
2393            "rpi.lan",
2394            &[address("192.168.1.20", 5080)],
2395            &allowed
2396        ));
2397        assert!(plaintext_permitted(
2398            "http",
2399            "openobserve.openobserve.svc",
2400            &[address("10.43.7.9", 5080)],
2401            &allowed
2402        ));
2403        for refused in ["evil.rpi.lan", "rpi.lan.evil.test", "lan", "rpi"] {
2404            assert!(
2405                !plaintext_permitted("http", refused, &[address("192.168.1.20", 5080)], &allowed),
2406                "{refused} is not listed"
2407            );
2408        }
2409    }
2410
2411    #[test]
2412    fn listed_plaintext_hosts_match_case_insensitively() {
2413        let allowed = plaintext_hosts(&["RPi.LAN"]);
2414        assert!(plaintext_permitted(
2415            "http",
2416            "rpi.lan",
2417            &[address("192.168.1.20", 5080)],
2418            &allowed
2419        ));
2420        assert!(allowed.contains("RPI.LAN"));
2421        assert_eq!(allowed.iter().collect::<Vec<_>>(), vec!["rpi.lan"]);
2422    }
2423
2424    #[test]
2425    fn a_listed_host_does_not_relax_https_or_mixed_loopback_answers() {
2426        let allowed = plaintext_hosts(&["rpi.lan"]);
2427        assert!(plaintext_permitted(
2428            "https",
2429            "api.example.test",
2430            &[address("93.184.216.34", 443)],
2431            &allowed
2432        ));
2433        assert!(!plaintext_permitted(
2434            "http",
2435            "split.example.test",
2436            &[address("127.0.0.1", 80), address("93.184.216.34", 80)],
2437            &allowed
2438        ));
2439    }
2440
2441    #[test]
2442    fn plaintext_host_entries_must_be_bare_hostnames() {
2443        for (entry, expected) in [
2444            ("", PlaintextHostError::Empty),
2445            ("   ", PlaintextHostError::Empty),
2446            (
2447                "http://rpi.lan",
2448                PlaintextHostError::Scheme {
2449                    entry: "http://rpi.lan".to_owned(),
2450                },
2451            ),
2452            (
2453                "rpi.lan/ingest",
2454                PlaintextHostError::Path {
2455                    entry: "rpi.lan/ingest".to_owned(),
2456                },
2457            ),
2458            (
2459                "rpi.lan:5080",
2460                PlaintextHostError::Port {
2461                    entry: "rpi.lan:5080".to_owned(),
2462                },
2463            ),
2464            (
2465                "::1",
2466                PlaintextHostError::Port {
2467                    entry: "::1".to_owned(),
2468                },
2469            ),
2470            (
2471                "*.lan",
2472                PlaintextHostError::Wildcard {
2473                    entry: "*.lan".to_owned(),
2474                },
2475            ),
2476            (
2477                "rpi lan",
2478                PlaintextHostError::InvalidHost {
2479                    entry: "rpi lan".to_owned(),
2480                },
2481            ),
2482            (
2483                "user@rpi.lan",
2484                PlaintextHostError::InvalidHost {
2485                    entry: "user@rpi.lan".to_owned(),
2486                },
2487            ),
2488            (
2489                ".rpi.lan",
2490                PlaintextHostError::InvalidHost {
2491                    entry: ".rpi.lan".to_owned(),
2492                },
2493            ),
2494            (
2495                "rpi.lan.",
2496                PlaintextHostError::InvalidHost {
2497                    entry: "rpi.lan.".to_owned(),
2498                },
2499            ),
2500        ] {
2501            let error = PlaintextHosts::new([entry]).expect_err("{entry} must be refused");
2502            assert_eq!(error, expected, "{entry}");
2503        }
2504        PlaintextHosts::new(["rpi.lan", "http://other.lan"])
2505            .expect_err("a single invalid entry must refuse the list");
2506        assert!(plaintext_hosts(&["  rpi.lan  "]).contains("rpi.lan"));
2507        assert!(plaintext_hosts(&["192.168.1.20"]).contains("192.168.1.20"));
2508    }
2509
2510    #[test]
2511    fn matches_only_exact_authorities() {
2512        assert!(authority_matches(
2513            "api.example.test",
2514            "api.example.test",
2515            443,
2516            "https"
2517        ));
2518        assert!(authority_matches(
2519            "api.example.test:8443",
2520            "api.example.test",
2521            8443,
2522            "https"
2523        ));
2524        assert!(!authority_matches(
2525            "example.test",
2526            "api.example.test",
2527            443,
2528            "https"
2529        ));
2530        assert!(!authority_matches("127.0.0.1", "127.0.0.1", 80, "http"));
2531        assert!(authority_matches("127.0.0.1:80", "127.0.0.1", 80, "http"));
2532    }
2533
2534    #[test]
2535    fn renders_ipv6_literal_authorities_with_exactly_one_pair_of_brackets() {
2536        assert!(authority_matches("[::1]:8080", "::1", 8080, "http"));
2537        assert!(authority_matches(
2538            "[2606:4700:4700::1111]",
2539            "2606:4700:4700::1111",
2540            443,
2541            "https"
2542        ));
2543        assert!(!authority_matches("[[::1]]:8080", "::1", 8080, "http"));
2544        assert!(!authority_matches("::1:8080", "::1", 8080, "http"));
2545    }
2546
2547    #[test]
2548    fn rejects_private_and_special_addresses() {
2549        for ip in [
2550            IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
2551            IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
2552            IpAddr::V4(Ipv4Addr::new(169, 254, 169, 254)),
2553            IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)),
2554            IpAddr::V6(Ipv6Addr::LOCALHOST),
2555            "fd00::1".parse().expect("valid fixture"),
2556            "2001:db8::1".parse().expect("valid fixture"),
2557            "2002:7f00:1::1".parse().expect("valid fixture"),
2558            "3fff::1".parse().expect("valid fixture"),
2559            "64:ff9b::127.0.0.1".parse().expect("valid fixture"),
2560        ] {
2561            assert!(is_forbidden_public_destination(ip), "{ip}");
2562        }
2563        assert!(!is_forbidden_public_destination(
2564            "93.184.216.34".parse().expect("valid fixture")
2565        ));
2566        assert!(!is_forbidden_public_destination(
2567            "2606:4700:4700::1111".parse().expect("valid fixture")
2568        ));
2569    }
2570
2571    #[test]
2572    fn reachable_settings_follow_the_enforcing_destination_rules() {
2573        let plaintext = PlaintextHosts::new(["rpi.lan", "other.lan"]).expect("plaintext hosts");
2574        let non_public = ["api.example:8443", "other.example:8443"]
2575            .map(|authority| super::NonPublicHttpsAuthority::new(authority).expect("authority"));
2576        let reachable = super::reachable_destination_settings(
2577            ["rpi.lan:8080", " API.example:8443 ", "bare.example"],
2578            &plaintext,
2579            &non_public,
2580        );
2581        assert_eq!(
2582            reachable.plaintext_hosts.into_iter().collect::<Vec<_>>(),
2583            ["rpi.lan"]
2584        );
2585        assert_eq!(
2586            reachable.non_public_https.into_iter().collect::<Vec<_>>(),
2587            ["api.example:8443"]
2588        );
2589    }
2590
2591    #[test]
2592    fn non_public_https_requires_exact_authority_independent_of_trust() {
2593        let profile = super::NonPublicHttpsAuthority::new(
2594            "OpenObserve-TLS.openobserve.svc.cluster.local:5443",
2595        )
2596        .expect("valid exact authority");
2597        assert_eq!(
2598            profile.authority,
2599            "openobserve-tls.openobserve.svc.cluster.local:5443"
2600        );
2601        for authority in [
2602            "openobserve-tls.openobserve.svc.cluster.local",
2603            "openobserve-tls.openobserve.svc.cluster.local:5443/path",
2604            "*.openobserve.svc.cluster.local:5443",
2605            "169.254.169.254:5443",
2606            "openobserve-tls.openobserve.svc.cluster.local:5443@evil.example:443",
2607        ] {
2608            assert!(
2609                super::NonPublicHttpsAuthority::new(authority).is_err(),
2610                "{authority}"
2611            );
2612        }
2613        for address in ["10.43.0.1", "172.16.0.2", "192.168.1.1", "fd00::1"] {
2614            assert!(
2615                super::is_private_unicast(address.parse().unwrap()),
2616                "{address}"
2617            );
2618        }
2619        for address in [
2620            "127.0.0.1",
2621            "169.254.169.254",
2622            "8.8.8.8",
2623            "::1",
2624            "fe80::1",
2625            "2001:db8::1",
2626        ] {
2627            assert!(
2628                !super::is_private_unicast(address.parse().unwrap()),
2629                "{address}"
2630            );
2631        }
2632        let private = ["10.43.0.1:5443".parse().unwrap()];
2633        let mixed = [
2634            "10.43.0.1:5443".parse().unwrap(),
2635            "8.8.8.8:5443".parse().unwrap(),
2636        ];
2637        let public = ["8.8.8.8:443".parse().unwrap()];
2638        assert!(super::https_addresses_permitted(true, &private));
2639        assert!(!super::https_addresses_permitted(false, &private));
2640        assert!(!super::https_addresses_permitted(true, &mixed));
2641        assert!(!super::https_addresses_permitted(true, &public));
2642        assert!(super::https_addresses_permitted(false, &public));
2643    }
2644
2645    #[test]
2646    fn rejects_zero_or_overbroad_configuration() {
2647        let error = BufferedHttpClient::disabled(
2648            HttpHostCeilings {
2649                max_headers: 0,
2650                ..HttpHostCeilings::default()
2651            },
2652            Duration::from_secs(1),
2653        )
2654        .expect_err("zero native ceiling must fail");
2655        assert_eq!(
2656            error,
2657            ConfigurationError::ZeroCeiling {
2658                field: "max_headers"
2659            }
2660        );
2661
2662        let error = BufferedHttpClient::authorized(
2663            HttpConstraints {
2664                max_requests: 33,
2665                ..grant("api.example.test".to_owned(), "GET")
2666            },
2667            HttpHostCeilings::default(),
2668            Duration::from_secs(1),
2669        )
2670        .expect_err("grant cannot exceed native ceiling");
2671        assert_eq!(error, ConfigurationError::GrantExceedsCeiling);
2672
2673        let error = BufferedHttpClient::disabled(HttpHostCeilings::default(), Duration::MAX)
2674            .expect_err("unrepresentable runtime deadline must fail");
2675        assert_eq!(error, ConfigurationError::TimeoutOverflow);
2676    }
2677
2678    #[test]
2679    fn rejects_grant_entries_no_authority_can_match() {
2680        for host in [" api.example.test", "*", "a/b", ""] {
2681            let error = BufferedHttpClient::authorized(
2682                grant(host.to_owned(), "GET"),
2683                HttpHostCeilings::default(),
2684                Duration::from_secs(1),
2685            )
2686            .expect_err("an inexact host must not configure a client");
2687            assert_eq!(
2688                error,
2689                ConfigurationError::InvalidGrant {
2690                    source: HttpConstraintsError::InvalidHost {
2691                        value: host.to_owned()
2692                    }
2693                }
2694            );
2695        }
2696
2697        let error = BufferedHttpClient::authorized(
2698            grant("api.example.test".to_owned(), "GET POST"),
2699            HttpHostCeilings::default(),
2700            Duration::from_secs(1),
2701        )
2702        .expect_err("an inexact method must not configure a client");
2703        assert_eq!(
2704            error,
2705            ConfigurationError::InvalidGrant {
2706                source: HttpConstraintsError::InvalidMethod {
2707                    value: "GET POST".to_owned()
2708                }
2709            }
2710        );
2711    }
2712
2713    #[test]
2714    fn truncates_resolver_fan_out_instead_of_refusing_it() {
2715        let addresses = (1..=17).map(|last| SocketAddr::from(([192, 0, 2, last], 443)));
2716        let bounded =
2717            bounded_addresses(addresses).expect("a large fan-out is truncated, not refused");
2718        assert_eq!(bounded.len(), MAX_RESOLVED_ADDRESSES);
2719
2720        let duplicated = (1..=20).map(|_| SocketAddr::from(([192, 0, 2, 1], 443)));
2721        let bounded = bounded_addresses(duplicated).expect("duplicates collapse before the bound");
2722        assert_eq!(bounded.len(), 1);
2723
2724        let error = bounded_addresses(Vec::new()).expect_err("an empty answer is still a failure");
2725        assert_eq!(error.code, ErrorCode::Dns);
2726    }
2727
2728    #[test]
2729    fn client_builder_failures_are_setup_failures_not_protocol_failures() {
2730        let error = reqwest::Client::new()
2731            .get("not an absolute url")
2732            .build()
2733            .expect_err("the builder rejects a non-absolute URL");
2734        assert!(error.is_builder());
2735        assert_eq!(map_reqwest_error(&error).code, ErrorCode::Internal);
2736    }
2737
2738    #[tokio::test(flavor = "multi_thread")]
2739    async fn sends_extension_methods_duplicate_headers_and_buffered_bodies() {
2740        let server = LoopbackServer::once(
2741            b"HTTP/1.1 200 OK\r\nX-Value: one\r\nX-Value: two\r\nSet-Cookie: secret=session\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
2742        );
2743        let authority = server.authority().to_owned();
2744        let mut client = BufferedHttpClient::authorized(
2745            grant(authority.clone(), "PROPFIND"),
2746            HttpHostCeilings::default(),
2747            Duration::from_secs(5),
2748        )
2749        .expect("valid fixture authorization");
2750        let response = client
2751            .send(Request {
2752                method: "PROPFIND".to_owned(),
2753                uri: format!("http://{authority}/items?private=no"),
2754                headers: vec![
2755                    Header {
2756                        name: "x-probe".to_owned(),
2757                        value: b"one".to_vec(),
2758                    },
2759                    Header {
2760                        name: "x-probe".to_owned(),
2761                        value: b"two".to_vec(),
2762                    },
2763                ],
2764                body: b"payload".to_vec(),
2765            })
2766            .await
2767            .expect("bounded request succeeds");
2768
2769        assert_eq!(response.status, 200);
2770        assert_eq!(response.body, b"ok");
2771        assert_eq!(
2772            response
2773                .headers
2774                .iter()
2775                .filter(|header| header.name == "x-value")
2776                .count(),
2777            2
2778        );
2779        assert!(
2780            response
2781                .headers
2782                .iter()
2783                .all(|header| header.name != "set-cookie")
2784        );
2785        let request = server.request();
2786        assert!(request.starts_with(b"PROPFIND /items?private=no HTTP/1.1\r\n"));
2787        assert!(request.ends_with(b"\r\n\r\npayload"));
2788        let evidence = client.into_evidence();
2789        assert_eq!(evidence.len(), 1);
2790        assert_eq!(evidence[0].authority, authority);
2791        server.join();
2792    }
2793
2794    #[tokio::test(flavor = "multi_thread")]
2795    async fn a_vm_session_response_header_reaches_evidence_only_in_canonical_form() {
2796        const SESSION: &str = "019a3c2e-7b4d-7c1a-9e2f-0123456789ab";
2797        let cases = [
2798            (Some(SESSION), Some(SESSION)),
2799            (Some("019A3C2E-7B4D-7C1A-9E2F-0123456789AB"), None),
2800            (Some("019a3c2e-7b4d-4c1a-9e2f-0123456789ab"), None),
2801            (None, None),
2802        ];
2803        for (header, recorded) in cases {
2804            let session = header
2805                .map(|value| format!("x-dekopon-vm-session: {value}\r\n"))
2806                .unwrap_or_default();
2807            let server = LoopbackServer::once(
2808                format!("HTTP/1.1 201 Created\r\n{session}Content-Length: 2\r\nConnection: close\r\n\r\n{{}}")
2809                    .as_bytes(),
2810            );
2811            let authority = server.authority().to_owned();
2812            let mut client = BufferedHttpClient::authorized(
2813                grant(authority.clone(), "POST"),
2814                HttpHostCeilings::default(),
2815                Duration::from_secs(5),
2816            )
2817            .expect("valid fixture authorization");
2818            let response = client
2819                .send(Request {
2820                    method: "POST".to_owned(),
2821                    uri: format!("http://{authority}/v1/sessions"),
2822                    headers: Vec::new(),
2823                    body: Vec::new(),
2824                })
2825                .await
2826                .expect("bounded request succeeds");
2827            let returned = response
2828                .headers
2829                .iter()
2830                .find(|header| header.name == "x-dekopon-vm-session")
2831                .map(|header| header.value.as_slice());
2832            assert_eq!(returned, header.map(str::as_bytes));
2833            let evidence = client.into_evidence();
2834            assert_eq!(
2835                evidence[0]
2836                    .vm_session
2837                    .as_ref()
2838                    .map(dekopon_core::VmSession::as_str),
2839                recorded
2840            );
2841            server.join();
2842        }
2843    }
2844
2845    #[tokio::test(flavor = "multi_thread")]
2846    async fn a_listed_plaintext_host_still_needs_the_authorization_to_allow_it() {
2847        let ceilings = HttpHostCeilings {
2848            plaintext_hosts: plaintext_hosts(&["api.example.test"]),
2849            ..HttpHostCeilings::default()
2850        };
2851        let mut client = BufferedHttpClient::authorized(
2852            HttpConstraints {
2853                allow_plaintext_loopback: false,
2854                ..grant("api.example.test:80".to_owned(), "GET")
2855            },
2856            ceilings.clone(),
2857            Duration::from_secs(1),
2858        )
2859        .expect("valid fixture authorization");
2860        let error = client
2861            .send(Request {
2862                method: "GET".to_owned(),
2863                uri: "http://api.example.test/".to_owned(),
2864                headers: Vec::new(),
2865                body: Vec::new(),
2866            })
2867            .await
2868            .expect_err("a constraint set that forbids plaintext still forbids it");
2869        assert_eq!(error.code, ErrorCode::Denied);
2870        assert_eq!(error.message, "plaintext HTTP is not authorized");
2871
2872        let mut client = BufferedHttpClient::authorized(
2873            grant("other.example.test:80".to_owned(), "GET"),
2874            ceilings,
2875            Duration::from_secs(1),
2876        )
2877        .expect("valid fixture authorization");
2878        let error = client
2879            .send(Request {
2880                method: "GET".to_owned(),
2881                uri: "http://api.example.test/".to_owned(),
2882                headers: Vec::new(),
2883                body: Vec::new(),
2884            })
2885            .await
2886            .expect_err("a listed host the grant does not name is still not a destination");
2887        assert_eq!(error.code, ErrorCode::Denied);
2888        assert_eq!(
2889            error.message,
2890            "HTTP destination is not authorized for this invocation"
2891        );
2892    }
2893
2894    #[tokio::test(flavor = "multi_thread")]
2895    async fn denies_other_destinations_and_sensitive_request_headers() {
2896        let mut client = BufferedHttpClient::authorized(
2897            grant("127.0.0.1:10".to_owned(), "GET"),
2898            HttpHostCeilings::default(),
2899            Duration::from_secs(1),
2900        )
2901        .expect("valid fixture authorization");
2902        let error = client
2903            .send(Request {
2904                method: "GET".to_owned(),
2905                uri: "http://127.0.0.1:9/".to_owned(),
2906                headers: Vec::new(),
2907                body: Vec::new(),
2908            })
2909            .await
2910            .expect_err("different authority must fail before connection");
2911        assert_eq!(error.code, ErrorCode::Denied);
2912        assert_eq!(client.policy_violation(), Some(PolicyViolation::Denied));
2913
2914        let mut client = BufferedHttpClient::authorized(
2915            grant("127.0.0.1:9".to_owned(), "GET"),
2916            HttpHostCeilings::default(),
2917            Duration::from_secs(1),
2918        )
2919        .expect("valid fixture authorization");
2920        let error = client
2921            .send(Request {
2922                method: "GET".to_owned(),
2923                uri: "http://127.0.0.1:9/".to_owned(),
2924                headers: vec![Header {
2925                    name: "authorization".to_owned(),
2926                    value: b"Bearer secret".to_vec(),
2927                }],
2928                body: Vec::new(),
2929            })
2930            .await
2931            .expect_err("guest authorization header must fail before connection");
2932        assert_eq!(error.code, ErrorCode::InvalidHeader);
2933        assert_eq!(
2934            client.policy_violation(),
2935            Some(PolicyViolation::InvalidHttpRequest)
2936        );
2937    }
2938
2939    #[tokio::test(flavor = "multi_thread")]
2940    async fn invalid_uris_name_the_rule_they_broke() {
2941        let mut client = BufferedHttpClient::authorized(
2942            grant("api.example.test".to_owned(), "GET"),
2943            HttpHostCeilings::default(),
2944            Duration::from_secs(1),
2945        )
2946        .expect("valid fixture authorization");
2947
2948        let error = client
2949            .send(Request {
2950                method: "GET".to_owned(),
2951                uri: "/relative/only".to_owned(),
2952                headers: Vec::new(),
2953                body: Vec::new(),
2954            })
2955            .await
2956            .expect_err("a relative reference is not an absolute URL");
2957        assert_eq!(error.code, ErrorCode::InvalidUri);
2958        assert!(
2959            error.message.contains("relative URL without a base"),
2960            "{error}"
2961        );
2962
2963        let error = client
2964            .send(Request {
2965                method: "GET".to_owned(),
2966                uri: "https://api.example.test:notaport/".to_owned(),
2967                headers: Vec::new(),
2968                body: Vec::new(),
2969            })
2970            .await
2971            .expect_err("a malformed port is not an absolute URL");
2972        assert_eq!(error.code, ErrorCode::InvalidUri);
2973        assert!(error.message.contains("invalid port number"), "{error}");
2974        assert!(!error.message.contains("api.example.test"), "{error}");
2975    }
2976
2977    fn secret_grant(
2978        authority: &str,
2979        sink: SecretSinkKind,
2980        username: Option<&str>,
2981        path: &str,
2982    ) -> SecretUseGrant {
2983        SecretUseGrant {
2984            secret: "drn:secret:api/credential".parse().expect("canonical DRN"),
2985            sink,
2986            basic_username: username.map(str::to_owned),
2987            allowed_hosts: vec![authority.to_owned()],
2988            allowed_methods: vec!["GET".to_owned()],
2989            allowed_paths: vec![HttpPathRule::Exact {
2990                path: path.to_owned(),
2991            }],
2992            allow_query: false,
2993            max_injections: 1,
2994            binding_id: "api-credential".to_owned(),
2995            map_revision: None,
2996        }
2997    }
2998
2999    fn credential_for(authority: &str) -> BoundCredential {
3000        BoundCredential::bearer(
3001            "Bearer",
3002            Redacted::new("fixture-secret-value".to_owned()),
3003            vec![authority.to_owned()],
3004        )
3005        .expect("valid fixture credential")
3006    }
3007
3008    #[test]
3009    fn bound_credentials_fail_closed_on_structural_problems() {
3010        let secret = || Redacted::new("fixture-secret-value".to_owned());
3011        let destinations = || vec!["api.example.test".to_owned()];
3012        for (scheme, secret, destinations) in [
3013            ("", secret(), destinations()),
3014            ("Bea rer", secret(), destinations()),
3015            ("Bearer", Redacted::new(String::new()), destinations()),
3016            ("Bearer", Redacted::new("x".repeat(15)), destinations()),
3017            (
3018                "Bearer",
3019                Redacted::new("phrase shaped secret".to_owned()),
3020                destinations(),
3021            ),
3022            (
3023                "Bearer",
3024                Redacted::new("tab\tseparated-secret".to_owned()),
3025                destinations(),
3026            ),
3027            ("Bearer", Redacted::new("x".repeat(4097)), destinations()),
3028            (
3029                "Bearer",
3030                Redacted::new("bad\r\nheader-value-here".to_owned()),
3031                destinations(),
3032            ),
3033            ("Bearer", secret(), Vec::new()),
3034            (
3035                "Bearer",
3036                secret(),
3037                vec!["https://api.example.test/path".to_owned()],
3038            ),
3039            ("Bearer", secret(), vec!["*.example.test".to_owned()]),
3040        ] {
3041            assert!(matches!(
3042                BoundCredential::bearer(scheme, secret, destinations),
3043                Err(ConfigurationError::InvalidCredential { .. })
3044            ));
3045        }
3046        BoundCredential::bearer(
3047            "Bearer",
3048            Redacted::new("x".repeat(MIN_CREDENTIAL_BYTES)),
3049            destinations(),
3050        )
3051        .expect("a minimum-length secret is a valid credential");
3052        let error = BoundCredential::bearer(
3053            "Bearer",
3054            Redacted::new("tell-nobody-not-even-once\n".to_owned()),
3055            destinations(),
3056        )
3057        .expect_err("control bytes are refused");
3058        assert!(!error.to_string().contains("tell-nobody"), "{error}");
3059    }
3060
3061    #[test]
3062    fn drn_credential_identity_commits_the_complete_effective_scope() {
3063        let original = secret_grant(
3064            "api.example.test",
3065            SecretSinkKind::HttpBearer,
3066            None,
3067            "/v1/allowed",
3068        );
3069        let credential = BoundCredential::secret_bearer(
3070            SecretBytes::new(b"fixture-bearer-token".to_vec()),
3071            &original,
3072        )
3073        .expect("credential");
3074        let mut swapped = original;
3075        swapped.allowed_paths = vec![HttpPathRule::Exact {
3076            path: "/v1/other".to_owned(),
3077        }];
3078        let error = BufferedHttpClient::authorized_with_secret_credential(
3079            grant("api.example.test".to_owned(), "GET"),
3080            swapped,
3081            credential,
3082            HttpHostCeilings::default(),
3083            Duration::from_secs(1),
3084        )
3085        .expect_err("same DRN and binding id cannot hide a swapped scope");
3086        assert!(matches!(
3087            error,
3088            ConfigurationError::InvalidCredential { .. }
3089        ));
3090    }
3091
3092    #[test]
3093    fn bound_credentials_never_render_their_value() {
3094        let credential = credential_for("api.example.test");
3095        let debug = format!("{credential:?}");
3096        assert!(!debug.contains("fixture-secret-value"), "{debug}");
3097        assert!(debug.contains("api.example.test"), "{debug}");
3098    }
3099
3100    #[tokio::test(flavor = "multi_thread")]
3101    async fn injects_a_destination_bound_credential_after_guest_checks() {
3102        let response: &[u8] =
3103            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok";
3104        let plain_server = LoopbackServer::once(response);
3105        let plain_authority = plain_server.authority().to_owned();
3106        let server = LoopbackServer::once(response);
3107        let authority = server.authority().to_owned();
3108
3109        let request_to = |authority: &str| Request {
3110            method: "GET".to_owned(),
3111            uri: format!("http://{authority}/pulls/7"),
3112            headers: vec![Header {
3113                name: "x-probe".to_owned(),
3114                value: b"one".to_vec(),
3115            }],
3116            body: Vec::new(),
3117        };
3118
3119        let mut plain = BufferedHttpClient::authorized(
3120            grant(plain_authority.clone(), "GET"),
3121            HttpHostCeilings::default(),
3122            Duration::from_secs(5),
3123        )
3124        .expect("valid fixture authorization");
3125        plain
3126            .send(request_to(&plain_authority))
3127            .await
3128            .expect("uncredentialed request succeeds");
3129
3130        let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
3131        let facts = std::sync::Arc::clone(&seen);
3132        let mut credentialed = BufferedHttpClient::authorized_with_credential(
3133            grant(authority.clone(), "GET"),
3134            Some(credential_for(&authority)),
3135            HttpHostCeilings::default(),
3136            Duration::from_secs(5),
3137        )
3138        .expect("valid fixture authorization")
3139        .with_request_authorizer(std::sync::Arc::new(move |request| {
3140            facts.lock().expect("facts").push(request);
3141            Box::pin(async { true })
3142        }));
3143        credentialed
3144            .send(request_to(&authority))
3145            .await
3146            .expect("credentialed request succeeds");
3147
3148        let plain_request =
3149            String::from_utf8(plain_server.request()).expect("fixture request is UTF-8");
3150        assert!(
3151            !plain_request.to_ascii_lowercase().contains("authorization"),
3152            "{plain_request}"
3153        );
3154        let request = String::from_utf8(server.request()).expect("fixture request is UTF-8");
3155        assert!(
3156            request.contains("authorization: Bearer fixture-secret-value"),
3157            "{request}"
3158        );
3159
3160        let seen = seen.lock().expect("facts");
3161        assert_eq!(seen.len(), 1);
3162        assert_eq!(
3163            seen[0].headers,
3164            vec![("x-probe".to_owned(), b"one".to_vec())]
3165        );
3166        let plain_evidence = plain.into_evidence();
3167        let evidence = credentialed.into_evidence();
3168        assert!(!plain_evidence[0].credential_injected);
3169        assert!(evidence[0].credential_injected);
3170        let authority_delta = authority.len() as i64 - plain_authority.len() as i64;
3171        assert_eq!(
3172            evidence[0].request_bytes as i64 - authority_delta,
3173            plain_evidence[0].request_bytes as i64
3174        );
3175
3176        plain_server.join();
3177        server.join();
3178    }
3179
3180    #[tokio::test(flavor = "multi_thread")]
3181    async fn drn_bound_basic_auth_is_rendered_only_for_the_exact_path() {
3182        let response: &[u8] =
3183            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok";
3184        let server = LoopbackServer::once(response);
3185        let authority = server.authority().to_owned();
3186        let secret = secret_grant(
3187            &authority,
3188            SecretSinkKind::HttpBasic,
3189            Some("userA"),
3190            "/api/v1/thing",
3191        );
3192        let credential =
3193            BoundCredential::secret_basic(SecretBytes::new(b"fixture-password".to_vec()), &secret)
3194                .expect("valid Basic credential");
3195        let mut client = BufferedHttpClient::authorized_with_secret_credential(
3196            grant(authority.clone(), "GET"),
3197            secret,
3198            credential,
3199            HttpHostCeilings::default(),
3200            Duration::from_secs(5),
3201        )
3202        .expect("valid secret authorization");
3203        client
3204            .send(Request {
3205                method: "GET".to_owned(),
3206                uri: format!("http://{authority}/api/v1/thing"),
3207                headers: Vec::new(),
3208                body: Vec::new(),
3209            })
3210            .await
3211            .expect("exact path succeeds");
3212        let request = String::from_utf8(server.request()).expect("request is UTF-8");
3213        assert!(
3214            request.contains("authorization: Basic dXNlckE6Zml4dHVyZS1wYXNzd29yZA=="),
3215            "{request}"
3216        );
3217        server.join();
3218    }
3219
3220    #[tokio::test(flavor = "multi_thread")]
3221    async fn secret_scope_denies_path_prefix_confusion_and_queries_before_connection() {
3222        let secret = secret_grant(
3223            "127.0.0.1:9",
3224            SecretSinkKind::HttpBearer,
3225            None,
3226            "/api/v1/thing",
3227        );
3228        for uri in [
3229            "http://127.0.0.1:9/api/v1/things",
3230            "http://127.0.0.1:9/api/v1/thing?reflect=authorization",
3231            "http://127.0.0.1:9/api/v1/other",
3232            "http://127.0.0.1:9/api/v1/%74hing",
3233            "http://127.0.0.1:9\\api\\v1\\thing",
3234            "http://127.0.0.1:9/api/v1/x/../thing",
3235            "http://127.0.0.1:9/api//v1/thing",
3236        ] {
3237            let credential = BoundCredential::secret_bearer(
3238                SecretBytes::new(b"fixture-bearer-token".to_vec()),
3239                &secret,
3240            )
3241            .expect("credential");
3242            let mut client = BufferedHttpClient::authorized_with_secret_credential(
3243                grant("127.0.0.1:9".to_owned(), "GET"),
3244                secret.clone(),
3245                credential,
3246                HttpHostCeilings::default(),
3247                Duration::from_secs(1),
3248            )
3249            .expect("context");
3250            let error = client
3251                .send(Request {
3252                    method: "GET".to_owned(),
3253                    uri: uri.to_owned(),
3254                    headers: Vec::new(),
3255                    body: Vec::new(),
3256                })
3257                .await
3258                .expect_err("scope confusion is denied");
3259            assert_eq!(error.code, ErrorCode::Denied, "{uri}");
3260        }
3261    }
3262
3263    fn templated(authority: &str, template: serde_json::Value) -> HttpConstraints {
3264        HttpConstraints {
3265            allowed_methods: vec!["GET".to_owned(), "POST".to_owned()],
3266            signed_download: None,
3267            request_templates: vec![
3268                serde_json::from_value::<RequestTemplate>(template).expect("template fixture"),
3269            ],
3270            ..grant(authority.to_owned(), "GET")
3271        }
3272    }
3273
3274    fn slack_replies(authority: &str) -> HttpConstraints {
3275        templated(
3276            authority,
3277            serde_json::json!({
3278                "method": "GET",
3279                "path": "/api/conversations.replies",
3280                "query": {
3281                    "pinned": {"channel": "conversation.id", "ts": "conversation.thread"},
3282                    "allowed": ["limit", "cursor"]
3283                }
3284            }),
3285        )
3286    }
3287
3288    fn discord_messages(authority: &str) -> HttpConstraints {
3289        templated(
3290            authority,
3291            serde_json::json!({
3292                "method": "GET",
3293                "path": "/channels/{conversation.apiChannel}/messages",
3294                "query": {"allowed": ["limit", "before"]}
3295            }),
3296        )
3297    }
3298
3299    fn slots(values: &[(ChatSlot, &str)]) -> ChatSlotValues {
3300        let mut slots = ChatSlotValues::default();
3301        for (slot, value) in values {
3302            slots.insert(*slot, (*value).to_owned());
3303        }
3304        slots
3305    }
3306
3307    async fn sent_request_line(
3308        grant: HttpConstraints,
3309        slots: ChatSlotValues,
3310        path: &str,
3311    ) -> String {
3312        let server = LoopbackServer::once(
3313            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
3314        );
3315        let authority = server.authority().to_owned();
3316        let mut client = BufferedHttpClient::authorized(
3317            HttpConstraints {
3318                allowed_hosts: vec![authority.clone()],
3319                ..grant
3320            },
3321            HttpHostCeilings::default(),
3322            Duration::from_secs(5),
3323        )
3324        .expect("valid fixture authorization")
3325        .with_chat_slots(slots);
3326        client
3327            .send(Request {
3328                method: "GET".to_owned(),
3329                uri: format!("http://{authority}{path}"),
3330                headers: Vec::new(),
3331                body: Vec::new(),
3332            })
3333            .await
3334            .expect("templated request succeeds");
3335        let request = String::from_utf8(server.request()).expect("fixture request is UTF-8");
3336        server.join();
3337        request.lines().next().expect("request line").to_owned()
3338    }
3339
3340    async fn template_denial(
3341        grant: HttpConstraints,
3342        slots: ChatSlotValues,
3343        method: &str,
3344        path: &str,
3345    ) -> super::HttpError {
3346        template_denial_with_body(grant, slots, method, path, Vec::new(), Vec::new()).await
3347    }
3348
3349    async fn template_denial_with_body(
3350        grant: HttpConstraints,
3351        slots: ChatSlotValues,
3352        method: &str,
3353        path: &str,
3354        headers: Vec<Header>,
3355        body: Vec<u8>,
3356    ) -> super::HttpError {
3357        let mut client = BufferedHttpClient::authorized(
3358            grant,
3359            HttpHostCeilings::default(),
3360            Duration::from_secs(1),
3361        )
3362        .expect("valid fixture authorization")
3363        .with_chat_slots(slots);
3364        let error = client
3365            .send(Request {
3366                method: method.to_owned(),
3367                uri: format!("http://127.0.0.1:9{path}"),
3368                headers,
3369                body,
3370            })
3371            .await
3372            .expect_err("the request template refuses this request");
3373        assert_eq!(error.code, ErrorCode::Denied, "{method} {path}");
3374        assert!(
3375            error.message.contains("request template"),
3376            "{method} {path}: {}",
3377            error.message
3378        );
3379        error
3380    }
3381
3382    #[tokio::test(flavor = "multi_thread")]
3383    async fn a_slack_replies_template_pins_the_attested_channel_and_thread() {
3384        let line = sent_request_line(
3385            slack_replies("127.0.0.1:9"),
3386            slots(&[
3387                (ChatSlot::ConversationId, "C0123ABC"),
3388                (ChatSlot::ConversationThread, "1700000000.000100"),
3389            ]),
3390            "/api/conversations.replies?limit=5",
3391        )
3392        .await;
3393        assert_eq!(
3394            line,
3395            "GET /api/conversations.replies?limit=5&channel=C0123ABC&ts=1700000000.000100 HTTP/1.1"
3396        );
3397    }
3398
3399    #[tokio::test(flavor = "multi_thread")]
3400    async fn a_slack_replies_template_denies_another_channel_and_unlisted_keys() {
3401        let attested = slots(&[
3402            (ChatSlot::ConversationId, "C0123ABC"),
3403            (ChatSlot::ConversationThread, "1700000000.000100"),
3404        ]);
3405        for query in [
3406            "?channel=C0999ZZZ&limit=5",
3407            "?ts=1.2",
3408            "?limit=5&user=U1",
3409            "?a;channel=C0999ZZZ",
3410        ] {
3411            let error = template_denial(
3412                slack_replies("127.0.0.1:9"),
3413                attested.clone(),
3414                "GET",
3415                &format!("/api/conversations.replies{query}"),
3416            )
3417            .await;
3418            assert_eq!(error.code, ErrorCode::Denied, "{query}");
3419            assert!(!error.message.contains("C0123ABC"), "{}", error.message);
3420        }
3421    }
3422
3423    #[tokio::test(flavor = "multi_thread")]
3424    async fn hook_sees_template_rendered_path_and_pinned_query() {
3425        use std::sync::{Arc, Mutex};
3426
3427        let server = LoopbackServer::once(
3428            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
3429        );
3430        let authority = server.authority().to_owned();
3431        let grant = templated(
3432            &authority,
3433            serde_json::json!({
3434                "method": "GET",
3435                "path": "/channels/{conversation.apiChannel}/messages",
3436                "query": {
3437                    "pinned": {"channel": "conversation.id"},
3438                    "allowed": ["limit"]
3439                }
3440            }),
3441        );
3442        let seen = Arc::new(Mutex::new(Vec::new()));
3443        let facts = Arc::clone(&seen);
3444        let mut client = BufferedHttpClient::authorized(
3445            grant,
3446            HttpHostCeilings::default(),
3447            Duration::from_secs(5),
3448        )
3449        .expect("template grant")
3450        .with_chat_slots(slots(&[
3451            (ChatSlot::ConversationApiChannel, "1122334455"),
3452            (ChatSlot::ConversationId, "C0123ABC"),
3453        ]))
3454        .with_request_authorizer(Arc::new(move |request| {
3455            facts.lock().expect("facts").push(request);
3456            Box::pin(async { true })
3457        }));
3458        client
3459            .send(Request {
3460                method: "GET".to_owned(),
3461                uri: format!(
3462                    "http://{authority}/channels/%7Bconversation.apiChannel%7D/messages?limit=10"
3463                ),
3464                headers: Vec::new(),
3465                body: Vec::new(),
3466            })
3467            .await
3468            .expect("rendered request admitted");
3469        let facts = seen.lock().expect("facts");
3470        assert_eq!(facts.len(), 1);
3471        assert_eq!(facts[0].path, "/channels/1122334455/messages");
3472        assert_eq!(facts[0].query, "limit=10&channel=C0123ABC");
3473        assert!(server.request().starts_with(
3474            b"GET /channels/1122334455/messages?limit=10&channel=C0123ABC HTTP/1.1\r\n"
3475        ));
3476        server.join();
3477    }
3478
3479    #[tokio::test(flavor = "multi_thread")]
3480    async fn a_discord_template_fills_the_api_channel_into_the_path() {
3481        let attested = slots(&[(ChatSlot::ConversationApiChannel, "1122334455")]);
3482        for path in [
3483            "/channels/{conversation.apiChannel}/messages?limit=10",
3484            "/channels/%7Bconversation.apiChannel%7D/messages?limit=10",
3485        ] {
3486            assert_eq!(
3487                sent_request_line(discord_messages("127.0.0.1:9"), attested.clone(), path).await,
3488                "GET /channels/1122334455/messages?limit=10 HTTP/1.1"
3489            );
3490        }
3491    }
3492
3493    #[tokio::test(flavor = "multi_thread")]
3494    async fn a_discord_template_denies_another_channel_in_the_path_and_other_methods() {
3495        let attested = slots(&[(ChatSlot::ConversationApiChannel, "1122334455")]);
3496        for (method, path) in [
3497            ("GET", "/channels/9988776655/messages"),
3498            ("GET", "/channels/{conversation.id}/messages"),
3499            ("GET", "/channels/{conversation.apiChannel}/messages/1"),
3500            (
3501                "GET",
3502                "/channels/{conversation.apiChannel}/../9988776655/messages",
3503            ),
3504            ("POST", "/channels/{conversation.apiChannel}/messages"),
3505        ] {
3506            let error = template_denial(
3507                discord_messages("127.0.0.1:9"),
3508                attested.clone(),
3509                method,
3510                path,
3511            )
3512            .await;
3513            assert_eq!(error.code, ErrorCode::Denied, "{method} {path}");
3514            assert!(!error.message.contains("1122334455"), "{}", error.message);
3515        }
3516    }
3517
3518    #[tokio::test(flavor = "multi_thread")]
3519    async fn a_templated_get_with_a_body_is_denied_so_the_body_carries_no_second_channel() {
3520        let error = template_denial_with_body(
3521            slack_replies("127.0.0.1:9"),
3522            slots(&[
3523                (ChatSlot::ConversationId, "C0123ABC"),
3524                (ChatSlot::ConversationThread, "1700000000.000100"),
3525            ]),
3526            "GET",
3527            "/api/conversations.replies?limit=5",
3528            vec![Header {
3529                name: "content-type".to_owned(),
3530                value: b"application/x-www-form-urlencoded".to_vec(),
3531            }],
3532            b"channel=C0999ZZZ&ts=1.2".to_vec(),
3533        )
3534        .await;
3535        assert!(error.message.contains("takes no body"), "{}", error.message);
3536    }
3537
3538    #[tokio::test(flavor = "multi_thread")]
3539    async fn a_template_slot_the_invocation_lacks_is_denied_before_connection() {
3540        let error = template_denial(
3541            slack_replies("127.0.0.1:9"),
3542            slots(&[(ChatSlot::ConversationId, "C0123ABC")]),
3543            "GET",
3544            "/api/conversations.replies",
3545        )
3546        .await;
3547        assert_eq!(error.code, ErrorCode::Denied);
3548    }
3549
3550    #[test]
3551    fn a_drn_bound_bearer_secret_below_the_floor_is_refused() {
3552        let grant = secret_grant("api.example.test", SecretSinkKind::HttpBearer, None, "/v1");
3553        let error = BoundCredential::secret_bearer(
3554            SecretBytes::new(vec![b'x'; MIN_CREDENTIAL_BYTES - 1]),
3555            &grant,
3556        )
3557        .expect_err("a short resolved secret is refused");
3558        assert!(error.to_string().contains("16-byte minimum"), "{error}");
3559        assert!(!error.to_string().contains('x'), "{error}");
3560        BoundCredential::secret_bearer(SecretBytes::new(vec![b'x'; MIN_CREDENTIAL_BYTES]), &grant)
3561            .expect("a minimum-length resolved secret is a valid credential");
3562    }
3563
3564    #[test]
3565    fn a_drn_bound_basic_secret_below_the_floor_is_refused() {
3566        let grant = secret_grant(
3567            "api.example.test",
3568            SecretSinkKind::HttpBasic,
3569            Some("userA"),
3570            "/v1",
3571        );
3572        let error = BoundCredential::secret_basic(
3573            SecretBytes::new(vec![b'x'; MIN_CREDENTIAL_BYTES - 1]),
3574            &grant,
3575        )
3576        .expect_err("a short resolved password is refused");
3577        assert!(error.to_string().contains("16-byte minimum"), "{error}");
3578        assert!(!error.to_string().contains('x'), "{error}");
3579        BoundCredential::secret_basic(SecretBytes::new(vec![b'x'; MIN_CREDENTIAL_BYTES]), &grant)
3580            .expect("a minimum-length resolved password is a valid credential");
3581    }
3582
3583    #[tokio::test(flavor = "multi_thread")]
3584    async fn direct_credential_echo_is_discarded() {
3585        let response: &[u8] =
3586            b"HTTP/1.1 200 OK\r\nContent-Length: 20\r\nConnection: close\r\n\r\nfixture-bearer-token";
3587        let server = LoopbackServer::once(response);
3588        let authority = server.authority().to_owned();
3589        let secret = secret_grant(&authority, SecretSinkKind::HttpBearer, None, "/echo");
3590        let credential = BoundCredential::secret_bearer(
3591            SecretBytes::new(b"fixture-bearer-token".to_vec()),
3592            &secret,
3593        )
3594        .expect("credential");
3595        let mut client = BufferedHttpClient::authorized_with_secret_credential(
3596            grant(authority.clone(), "GET"),
3597            secret,
3598            credential,
3599            HttpHostCeilings::default(),
3600            Duration::from_secs(5),
3601        )
3602        .expect("context");
3603        let error = client
3604            .send(Request {
3605                method: "GET".to_owned(),
3606                uri: format!("http://{authority}/echo"),
3607                headers: Vec::new(),
3608                body: Vec::new(),
3609            })
3610            .await
3611            .expect_err("an echoed token is never returned");
3612        assert_eq!(error.code, ErrorCode::Denied);
3613        assert!(!error.message.contains("fixture-bearer-token"), "{error}");
3614        let evidence = client.into_evidence();
3615        assert_eq!(evidence[0].status, Some(200));
3616        assert!(evidence[0].response_bytes > 0);
3617        server.join();
3618    }
3619
3620    #[tokio::test(flavor = "multi_thread")]
3621    async fn legacy_credential_echo_is_discarded() {
3622        for body in ["fixture-secret-value", "Bearer fixture-secret-value"] {
3623            let response = format!(
3624                "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
3625                body.len()
3626            );
3627            let server = LoopbackServer::once(response.as_bytes());
3628            let authority = server.authority().to_owned();
3629            let mut client = BufferedHttpClient::authorized_with_credential(
3630                grant(authority.clone(), "GET"),
3631                Some(credential_for(&authority)),
3632                HttpHostCeilings::default(),
3633                Duration::from_secs(5),
3634            )
3635            .expect("valid fixture authorization");
3636            let error = client
3637                .send(Request {
3638                    method: "GET".to_owned(),
3639                    uri: format!("http://{authority}/echo"),
3640                    headers: Vec::new(),
3641                    body: Vec::new(),
3642                })
3643                .await
3644                .expect_err("an echoed legacy credential is never returned");
3645            assert_eq!(error.code, ErrorCode::Denied);
3646            assert!(!error.message.contains("fixture-secret-value"), "{error}");
3647            let evidence = client.into_evidence();
3648            assert_eq!(evidence[0].status, Some(200));
3649            assert!(evidence[0].credential_injected);
3650            server.join();
3651        }
3652    }
3653
3654    #[tokio::test(flavor = "multi_thread")]
3655    async fn refuses_destinations_outside_the_credential_binding() {
3656        let mut client = BufferedHttpClient::authorized_with_credential(
3657            grant("127.0.0.1:9".to_owned(), "GET"),
3658            Some(credential_for("other.example.test")),
3659            HttpHostCeilings::default(),
3660            Duration::from_secs(1),
3661        )
3662        .expect("valid fixture authorization");
3663        let error = client
3664            .send(Request {
3665                method: "GET".to_owned(),
3666                uri: "http://127.0.0.1:9/".to_owned(),
3667                headers: Vec::new(),
3668                body: Vec::new(),
3669            })
3670            .await
3671            .expect_err("unbound destinations are refused, not sent unauthenticated");
3672        assert_eq!(error.code, ErrorCode::Denied);
3673        assert_eq!(client.policy_violation(), Some(PolicyViolation::Denied));
3674        let evidence = client.into_evidence();
3675        assert_eq!(evidence.len(), 1);
3676        assert_eq!(evidence[0].authority, "127.0.0.1:9");
3677        assert_eq!(evidence[0].status, None);
3678        assert!(!evidence[0].credential_injected);
3679    }
3680
3681    fn chatgpt_client(authority: &str, secret_host: &str) -> BufferedHttpClient {
3682        let secret = secret_grant(
3683            secret_host,
3684            SecretSinkKind::HttpBearer,
3685            None,
3686            "/images/generations",
3687        );
3688        let credential = BoundCredential::secret_bearer(
3689            SecretBytes::new(b"fixture-access-token".to_vec()),
3690            &secret,
3691        )
3692        .and_then(|credential| credential.with_chatgpt_account("acct-fixture"))
3693        .expect("valid fixture credential");
3694        BufferedHttpClient::authorized_with_secret_credential(
3695            grant(authority.to_owned(), "GET"),
3696            secret,
3697            credential,
3698            HttpHostCeilings::default(),
3699            Duration::from_secs(5),
3700        )
3701        .expect("valid fixture authorization")
3702    }
3703
3704    #[tokio::test(flavor = "multi_thread")]
3705    async fn a_chatgpt_subscription_credential_injects_both_headers_outside_accounted_bytes() {
3706        let response: &[u8] =
3707            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok";
3708        let plain_server = LoopbackServer::once(response);
3709        let plain_authority = plain_server.authority().to_owned();
3710        let server = LoopbackServer::once(response);
3711        let authority = server.authority().to_owned();
3712        let request_to = |authority: &str| Request {
3713            method: "GET".to_owned(),
3714            uri: format!("http://{authority}/images/generations"),
3715            headers: vec![Header {
3716                name: "x-probe".to_owned(),
3717                value: b"one".to_vec(),
3718            }],
3719            body: Vec::new(),
3720        };
3721
3722        let mut plain = BufferedHttpClient::authorized(
3723            grant(plain_authority.clone(), "GET"),
3724            HttpHostCeilings::default(),
3725            Duration::from_secs(5),
3726        )
3727        .expect("valid fixture authorization");
3728        plain
3729            .send(request_to(&plain_authority))
3730            .await
3731            .expect("uncredentialed request succeeds");
3732
3733        let mut client = chatgpt_client(&authority, &authority);
3734        client
3735            .send(request_to(&authority))
3736            .await
3737            .expect("credentialed request succeeds");
3738
3739        let request = String::from_utf8(server.request()).expect("fixture request is UTF-8");
3740        assert!(
3741            request.contains("authorization: Bearer fixture-access-token"),
3742            "{request}"
3743        );
3744        assert!(
3745            request.contains("chatgpt-account-id: acct-fixture"),
3746            "{request}"
3747        );
3748
3749        let plain_evidence = plain.into_evidence();
3750        let evidence = client.into_evidence();
3751        assert!(evidence[0].credential_injected);
3752        let authority_delta = authority.len() as i64 - plain_authority.len() as i64;
3753        assert_eq!(
3754            evidence[0].request_bytes as i64 - authority_delta,
3755            plain_evidence[0].request_bytes as i64
3756        );
3757        let serialized = serde_json::to_string(&evidence).expect("evidence serializes");
3758        assert!(!serialized.contains("acct-fixture"), "{serialized}");
3759        assert!(!serialized.contains("fixture-access-token"), "{serialized}");
3760        assert!(!serialized.contains("chatgpt"), "{serialized}");
3761
3762        plain_server.join();
3763        server.join();
3764    }
3765
3766    #[tokio::test(flavor = "multi_thread")]
3767    async fn a_guest_chatgpt_account_header_is_refused_with_or_without_a_credential() {
3768        let forged = || Request {
3769            method: "GET".to_owned(),
3770            uri: "http://127.0.0.1:9/images/generations".to_owned(),
3771            headers: vec![Header {
3772                name: "chatgpt-account-id".to_owned(),
3773                value: b"acct-guest-forged".to_vec(),
3774            }],
3775            body: Vec::new(),
3776        };
3777
3778        let mut client = chatgpt_client("127.0.0.1:9", "127.0.0.1:9");
3779        let error = client
3780            .send(forged())
3781            .await
3782            .expect_err("a guest-set companion header must be rejected");
3783        assert_eq!(error.code, ErrorCode::InvalidHeader);
3784
3785        let mut uncompanioned = BufferedHttpClient::authorized_with_credential(
3786            grant("127.0.0.1:9".to_owned(), "GET"),
3787            Some(credential_for("127.0.0.1:9")),
3788            HttpHostCeilings::default(),
3789            Duration::from_secs(1),
3790        )
3791        .expect("valid fixture authorization");
3792        let error = uncompanioned
3793            .send(forged())
3794            .await
3795            .expect_err("a guest account header is refused beside any credential");
3796        assert_eq!(error.code, ErrorCode::InvalidHeader);
3797
3798        let mut unauthenticated = BufferedHttpClient::authorized(
3799            grant("127.0.0.1:9".to_owned(), "GET"),
3800            HttpHostCeilings::default(),
3801            Duration::from_secs(1),
3802        )
3803        .expect("valid fixture authorization");
3804        let error = unauthenticated
3805            .send(forged())
3806            .await
3807            .expect_err("a guest account header is refused without a credential");
3808        assert_eq!(error.code, ErrorCode::InvalidHeader);
3809    }
3810
3811    #[tokio::test(flavor = "multi_thread")]
3812    async fn a_chatgpt_subscription_credential_refuses_destinations_outside_its_binding() {
3813        let mut client = chatgpt_client("127.0.0.1:9", "other.example.test");
3814        let error = client
3815            .send(Request {
3816                method: "GET".to_owned(),
3817                uri: "http://127.0.0.1:9/images/generations".to_owned(),
3818                headers: Vec::new(),
3819                body: Vec::new(),
3820            })
3821            .await
3822            .expect_err("unbound destinations are refused, not sent unauthenticated");
3823        assert_eq!(error.code, ErrorCode::Denied);
3824        assert!(
3825            client
3826                .into_evidence()
3827                .iter()
3828                .all(|call| !call.credential_injected)
3829        );
3830    }
3831
3832    #[test]
3833    fn a_chatgpt_account_accompanies_only_a_secret_bearer_and_fails_closed_when_malformed() {
3834        let bearer = secret_grant("chatgpt.com", SecretSinkKind::HttpBearer, None, "/");
3835        let token = || SecretBytes::new(b"fixture-access-token".to_vec());
3836        for account in ["", "acct with space", "acct\u{7f}control"] {
3837            let error = BoundCredential::secret_bearer(token(), &bearer)
3838                .and_then(|credential| credential.with_chatgpt_account(account))
3839                .expect_err("a structurally invalid account identifier is refused");
3840            let rendered = error.to_string();
3841            assert!(rendered.contains("account identifier"), "{rendered}");
3842            assert!(!rendered.contains("fixture-access-token"), "{rendered}");
3843        }
3844        let bot = secret_grant("chatgpt.com", SecretSinkKind::HttpBot, None, "/");
3845        BoundCredential::secret_bot(token(), &bot)
3846            .and_then(|credential| credential.with_chatgpt_account("acct-fixture"))
3847            .expect_err("a Bot credential carries no ChatGPT account");
3848        credential_for("chatgpt.com")
3849            .with_chatgpt_account("acct-fixture")
3850            .expect_err("an unbound credential carries no ChatGPT account");
3851    }
3852
3853    #[tokio::test(flavor = "multi_thread")]
3854    async fn guest_authorization_is_rejected_not_overwritten_when_a_credential_exists() {
3855        let mut client = BufferedHttpClient::authorized_with_credential(
3856            grant("127.0.0.1:9".to_owned(), "GET"),
3857            Some(credential_for("127.0.0.1:9")),
3858            HttpHostCeilings::default(),
3859            Duration::from_secs(1),
3860        )
3861        .expect("valid fixture authorization");
3862        let error = client
3863            .send(Request {
3864                method: "GET".to_owned(),
3865                uri: "http://127.0.0.1:9/".to_owned(),
3866                headers: vec![Header {
3867                    name: "authorization".to_owned(),
3868                    value: b"Bearer guest-supplied".to_vec(),
3869                }],
3870                body: Vec::new(),
3871            })
3872            .await
3873            .expect_err("guest authorization must still be rejected");
3874        assert_eq!(error.code, ErrorCode::InvalidHeader);
3875    }
3876
3877    #[test]
3878    fn credential_evidence_flag_defaults_for_old_records_and_serializes_compactly() {
3879        let old_record = r#"{"method":"GET","authority":"api.example.test:443","requestBytes":10,"responseBytes":20}"#;
3880        let evidence =
3881            serde_json::from_str::<HttpCallEvidence>(old_record).expect("old records still parse");
3882        assert!(!evidence.credential_injected);
3883        let serialized = serde_json::to_string(&evidence).expect("serializes");
3884        assert!(!serialized.contains("credentialInjected"), "{serialized}");
3885
3886        let injected = HttpCallEvidence {
3887            credential_injected: true,
3888            ..evidence
3889        };
3890        let serialized = serde_json::to_string(&injected).expect("serializes");
3891        assert!(
3892            serialized.contains("\"credentialInjected\":true"),
3893            "{serialized}"
3894        );
3895    }
3896
3897    #[tokio::test]
3898    async fn an_oversized_request_names_max_request_bytes_and_its_least_size() {
3899        let mut limited = grant("127.0.0.1:9".to_owned(), "POST");
3900        limited.max_request_bytes = 64;
3901        let mut client = BufferedHttpClient::authorized(
3902            limited,
3903            HttpHostCeilings::default(),
3904            Duration::from_secs(1),
3905        )
3906        .expect("valid fixture authorization");
3907        let error = client
3908            .send(Request {
3909                method: "POST".to_owned(),
3910                uri: "http://127.0.0.1:9/upload".to_owned(),
3911                headers: Vec::new(),
3912                body: vec![b'x'; 128],
3913            })
3914            .await
3915            .expect_err("the request bound refuses before sending");
3916        assert!(matches!(
3917            error.byte_limit,
3918            Some(super::ByteLimitExceeded {
3919                key: super::ByteLimitKey::MaxRequestBytes,
3920                limit: 64,
3921                observed,
3922            }) if observed > 128
3923        ));
3924        assert_eq!(
3925            client.policy_violation(),
3926            Some(PolicyViolation::ByteLimit(error.byte_limit))
3927        );
3928    }
3929
3930    #[tokio::test(flavor = "multi_thread")]
3931    async fn response_headers_over_max_response_bytes_name_the_key() {
3932        let wire = format!(
3933            "HTTP/1.1 200 OK\r\nx-pad: {}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
3934            "p".repeat(256)
3935        );
3936        let server = LoopbackServer::once(wire.as_bytes());
3937        let authority = server.authority().to_owned();
3938        let mut limited = grant(authority.clone(), "GET");
3939        limited.max_response_bytes = 128;
3940        let mut client = BufferedHttpClient::authorized(
3941            limited,
3942            HttpHostCeilings::default(),
3943            Duration::from_secs(5),
3944        )
3945        .expect("valid fixture authorization");
3946        let error = client
3947            .send(Request {
3948                method: "GET".to_owned(),
3949                uri: format!("http://{authority}/headers"),
3950                headers: Vec::new(),
3951                body: Vec::new(),
3952            })
3953            .await
3954            .expect_err("the head alone exceeds the response bound");
3955        assert_eq!(error.code, ErrorCode::ResponseTooLarge);
3956        assert!(matches!(
3957            error.byte_limit,
3958            Some(super::ByteLimitExceeded {
3959                key: super::ByteLimitKey::MaxResponseBytes,
3960                limit: 128,
3961                observed,
3962            }) if observed > 128
3963        ));
3964        server.join();
3965    }
3966
3967    #[tokio::test(flavor = "multi_thread")]
3968    async fn streams_into_response_bound_and_never_follows_redirects() {
3969        let body = "x".repeat(512);
3970        let oversized = format!(
3971            "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
3972            body.len(),
3973            body
3974        );
3975        let server = LoopbackServer::once(oversized.as_bytes());
3976        let authority = server.authority().to_owned();
3977        let mut limited_grant = grant(authority.clone(), "GET");
3978        limited_grant.max_response_bytes = 128;
3979        let mut client = BufferedHttpClient::authorized(
3980            limited_grant,
3981            HttpHostCeilings::default(),
3982            Duration::from_secs(5),
3983        )
3984        .expect("valid fixture authorization");
3985        let error = client
3986            .send(Request {
3987                method: "GET".to_owned(),
3988                uri: format!("http://{authority}/large"),
3989                headers: Vec::new(),
3990                body: Vec::new(),
3991            })
3992            .await
3993            .expect_err("response bound must stop buffering");
3994        assert_eq!(error.code, ErrorCode::ResponseTooLarge);
3995        assert!(matches!(
3996            client.policy_violation(),
3997            Some(PolicyViolation::ByteLimit(Some(super::ByteLimitExceeded {
3998                key: super::ByteLimitKey::MaxResponseBytes,
3999                limit: 128,
4000                observed,
4001            }))) if observed > 128
4002        ));
4003        server.join();
4004
4005        let server = LoopbackServer::once(
4006            b"HTTP/1.1 302 Found\r\nLocation: http://169.254.169.254/latest\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
4007        );
4008        let authority = server.authority().to_owned();
4009        let mut client = BufferedHttpClient::authorized(
4010            grant(authority.clone(), "GET"),
4011            HttpHostCeilings::default(),
4012            Duration::from_secs(5),
4013        )
4014        .expect("valid fixture authorization");
4015        let response = client
4016            .send(Request {
4017                method: "GET".to_owned(),
4018                uri: format!("http://{authority}/redirect"),
4019                headers: Vec::new(),
4020                body: Vec::new(),
4021            })
4022            .await
4023            .expect("redirect response is returned without following");
4024        assert_eq!(response.status, 302);
4025        assert_eq!(client.into_evidence().len(), 1);
4026        server.join();
4027    }
4028
4029    #[tokio::test(flavor = "multi_thread")]
4030    async fn reaches_ipv6_literal_loopback_destinations() {
4031        let Some(server) = LoopbackServer::bound(
4032            "[::1]:0",
4033            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
4034        ) else {
4035            return;
4036        };
4037        let authority = server.authority().to_owned();
4038        assert!(authority.starts_with("[::1]:"), "{authority}");
4039
4040        let mut client = BufferedHttpClient::authorized(
4041            grant(authority.clone(), "GET"),
4042            HttpHostCeilings::default(),
4043            Duration::from_secs(5),
4044        )
4045        .expect("valid fixture authorization");
4046        let response = client
4047            .send(Request {
4048                method: "GET".to_owned(),
4049                uri: format!("http://{authority}/items"),
4050                headers: Vec::new(),
4051                body: Vec::new(),
4052            })
4053            .await
4054            .expect("an IPv6 literal loopback destination is reachable");
4055
4056        assert_eq!(response.status, 200);
4057        let request = server.request();
4058        assert!(request.starts_with(b"GET /items HTTP/1.1\r\n"));
4059        let evidence = client.into_evidence();
4060        assert_eq!(evidence[0].authority, authority);
4061        server.join();
4062    }
4063
4064    #[tokio::test(flavor = "multi_thread")]
4065    async fn reuses_one_pinned_client_across_calls_to_the_same_authority() {
4066        // Client-level pinning is deterministic, but whether hyper's connection pool reuses one TCP
4067        // connection is a background-task race this crate doesn't control, so the fixture tolerates
4068        // it without requiring it.
4069        let server = LoopbackServer::pooled(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", 2);
4070        let authority = server.authority().to_owned();
4071        let mut client = BufferedHttpClient::authorized(
4072            grant(authority.clone(), "GET"),
4073            HttpHostCeilings::default(),
4074            Duration::from_secs(5),
4075        )
4076        .expect("valid fixture authorization");
4077        for _ in 0..2 {
4078            let response = client
4079                .send(Request {
4080                    method: "GET".to_owned(),
4081                    uri: format!("http://{authority}/items"),
4082                    headers: Vec::new(),
4083                    body: Vec::new(),
4084                })
4085                .await
4086                .expect("both calls share one pooled connection");
4087            assert_eq!(response.status, 200);
4088        }
4089        assert_eq!(client.into_evidence().len(), 2);
4090        server.join();
4091    }
4092
4093    #[tokio::test(flavor = "multi_thread")]
4094    async fn a_changed_pin_set_builds_a_new_client() {
4095        let response: &[u8] =
4096            b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok";
4097        let first_server = LoopbackServer::once(response);
4098        let first = first_server.authority().to_owned();
4099        let second_server = LoopbackServer::once(response);
4100        let second = second_server.authority().to_owned();
4101
4102        let mut grant = grant(first.clone(), "GET");
4103        grant.allowed_hosts.push(second.clone());
4104        let mut client = BufferedHttpClient::authorized(
4105            grant,
4106            HttpHostCeilings::default(),
4107            Duration::from_secs(5),
4108        )
4109        .expect("valid fixture authorization");
4110        for (authority, path) in [(&first, "/first"), (&second, "/second")] {
4111            let response = client
4112                .send(Request {
4113                    method: "GET".to_owned(),
4114                    uri: format!("http://{authority}{path}"),
4115                    headers: Vec::new(),
4116                    body: Vec::new(),
4117                })
4118                .await
4119                .expect("each destination is reachable");
4120            assert_eq!(response.status, 200);
4121        }
4122
4123        assert!(
4124            first_server
4125                .request()
4126                .starts_with(b"GET /first HTTP/1.1\r\n")
4127        );
4128        assert!(
4129            second_server
4130                .request()
4131                .starts_with(b"GET /second HTTP/1.1\r\n")
4132        );
4133        let evidence = client.into_evidence();
4134        assert_eq!(evidence[0].authority, first);
4135        assert_eq!(evidence[1].authority, second);
4136        first_server.join();
4137        second_server.join();
4138    }
4139
4140    #[test]
4141    fn bounds_and_sanitizes_error_messages() {
4142        let message = bounded_message(&format!("line one\n{}", "x".repeat(400)));
4143        assert!(message.len() <= 256);
4144        assert!(!message.contains('\n'));
4145    }
4146}