Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(integrations): split download client to avoid leaking api key on redirect

Steven Enamakelb794058aca90
diff --git a/crates/openhuman-core/src/integrations/client/construct.rs b/crates/openhuman-core/src/integrations/client/construct.rs
index fa9066f950..50511a3948 100644
--- a/crates/openhuman-core/src/integrations/client/construct.rs
+++ b/crates/openhuman-core/src/integrations/client/construct.rs
@@ -54,6 +54,8 @@ pub struct IntegrationClient {
// Content-Type and Content-Disposition, and the presigned-redirect hop
// must not carry attribution headers (see `new_inner`).
pub(super) download_client: reqwest::Client,
+ /// Follows storage redirects without inheriting any backend credential.
+ pub(super) presigned_client: reqwest::Client,
pub(super) pricing: tokio::sync::OnceCell<IntegrationPricing>,
}
@@ -103,12 +105,15 @@ impl IntegrationClient {
pub(super) fn validate_credential_endpoint(&self) -> anyhow::Result<()> {
let endpoint = &self.backend_url;
if self.uses_api_key()
- && !crate::inference::provider::openhuman_backend_model::is_managed_endpoint_for_api_key(endpoint)
+ && !crate::inference::provider::openhuman_backend_model::is_managed_endpoint_for_api_key(
+ endpoint,
+ )
{
anyhow::bail!("TinyHumans API key requires the managed backend or a loopback endpoint");
}
- if !crate::inference::provider::openhuman_backend_model::is_safe_endpoint_for_managed_bearer(endpoint)
- {
+ if !crate::inference::provider::openhuman_backend_model::is_safe_endpoint_for_managed_bearer(
+ endpoint,
+ ) {
anyhow::bail!("backend credential requires HTTPS or a loopback HTTP endpoint");
}
Ok(())
@@ -138,25 +143,24 @@ impl IntegrationClient {
// client is built here.
//
// `download_client` deliberately does NOT carry the product identity.
- // Its one caller (`get_bytes`) fetches
- // `/agent-integrations/file-storage/files/{id}/download`, which answers
- // a 302 to a presigned S3 URL. reqwest follows redirects by default and
- // strips only *sensitive* headers (Authorization, Cookie, …) when the
- // host changes — a custom header like `x-sdk-name` survives the hop, so
- // tagging this transport would disclose the product identity to the
- // storage provider. Attaching it per-request would not help: redirected
- // requests carry the original request headers too.
+ // Its one caller (`get_bytes`) fetches a route that can redirect to
+ // presigned storage. The first client stops at the redirect; the second
+ // follows it without x-api-key or any other backend header.
//
- // The lost attribution is deliberate and cheap: the header cannot be
- // scoped to the first hop without hand-rolling redirect following, and
- // any session that downloads a file has already made SDK-path calls that
- // are tagged.
+ // These raw clients do not add product attribution to storage requests.
let download_client = crate::util::tls::tls_client_builder()
.http1_only()
+ .redirect(reqwest::redirect::Policy::none())
.timeout(Duration::from_secs(15 * 60))
.connect_timeout(Duration::from_secs(15))
.build()
.expect("failed to build integration download HTTP client");
+ let presigned_client = crate::util::tls::tls_client_builder()
+ .http1_only()
+ .timeout(Duration::from_secs(15 * 60))
+ .connect_timeout(Duration::from_secs(15))
+ .build()
+ .expect("failed to build presigned download HTTP client");
let auth_token = credential.secret().to_owned();
Self {
@@ -165,6 +169,7 @@ impl IntegrationClient {
credential,
budget_config,
download_client,
+ presigned_client,
pricing: tokio::sync::OnceCell::new(),
}
}
diff --git a/crates/openhuman-core/src/integrations/client/download.rs b/crates/openhuman-core/src/integrations/client/download.rs
index 36f4a84788..5148c4be14 100644
--- a/crates/openhuman-core/src/integrations/client/download.rs
+++ b/crates/openhuman-core/src/integrations/client/download.rs
@@ -44,10 +44,8 @@ fn parse_content_disposition_filename(value: &str) -> Option<String> {
impl IntegrationClient {
/// Authenticated GET returning the raw response body plus content-type and
/// any `Content-Disposition` filename. Used for backend download routes
- /// that `302`-redirect to a presigned S3 URL: reqwest follows redirects by
- /// default and its redirect policy strips sensitive headers (including
- /// `Authorization`) on cross-host hops, so the bearer token never leaks to
- /// S3 while the presigned URL still authorizes the fetch.
+ /// that redirect to a presigned S3 URL. The storage hop uses a separate
+ /// client with no backend credential headers.
pub async fn get_bytes(
&self,
path: &str,
@@ -68,13 +66,32 @@ impl IntegrationClient {
let url = crate::util::url::join_url(&self.backend_url, path);
tracing::debug!("[integrations] GET(bytes) {}", url);
- let resp = self
+ let mut resp = self
.download_client
.get(&url)
.headers(self.auth_headers()?)
.send()
.await
.map_err(|error| Self::report_transport_error(error, "get_bytes", path, &url))?;
+ if resp.status().is_redirection() {
+ let location = resp
+ .headers()
+ .get(reqwest::header::LOCATION)
+ .ok_or_else(|| anyhow::anyhow!("download redirect missing Location"))?
+ .to_str()?;
+ let redirect_url = resp.url().join(location)?;
+ anyhow::ensure!(
+ crate::inference::provider::openhuman_backend_model::is_safe_endpoint_for_managed_bearer(redirect_url.as_str()),
+ "download redirect requires HTTPS or a loopback HTTP endpoint"
+ );
+ // The second request has no auth headers, including x-api-key.
+ resp = self
+ .presigned_client
+ .get(redirect_url)
+ .send()
+ .await
+ .map_err(|error| Self::report_transport_error(error, "get_bytes", path, &url))?;
+ }
let status = resp.status();
if !status.is_success() {
let body_text = resp.text().await.unwrap_or_default();
diff --git a/crates/openhuman-core/src/integrations/client/pricing_tests.rs b/crates/openhuman-core/src/integrations/client/pricing_tests.rs
index b9fa7f55b4..20f6d160b2 100644
--- a/crates/openhuman-core/src/integrations/client/pricing_tests.rs
+++ b/crates/openhuman-core/src/integrations/client/pricing_tests.rs
@@ -24,7 +24,7 @@ fn api_key_constructs_a_client_that_authenticates_with_the_key() {
let config = crate::config::Config::default();
let client = build_client_with_credential(
&config,
- "https://api.example.test".to_owned(),
+ "https://api.tinyhumans.ai".to_owned(),
Some(BackendCredential::ApiKey(" tiny_test_abc ".into())),
)
.expect("an API key alone must build the integrations client");
@@ -35,6 +35,18 @@ fn api_key_constructs_a_client_that_authenticates_with_the_key() {
assert!(headers.get(reqwest::header::AUTHORIZATION).is_none());
}
+#[test]
+fn api_key_rejects_foreign_or_plaintext_integration_endpoints() {
+ for endpoint in ["https://example.com", "http://example.com"] {
+ let client = IntegrationClient::new_with_credential(
+ endpoint.to_owned(),
+ BackendCredential::ApiKey("tiny_test_abc".into()),
+ );
+ assert!(client.validate_credential_endpoint().is_err());
+ assert!(client.auth_headers().is_err());
+ }
+}
+
#[test]
fn missing_or_blank_credential_builds_no_client() {
let config = crate::config::Config::default();
diff --git a/crates/openhuman-core/src/integrations/client_api_key_tests.rs b/crates/openhuman-core/src/integrations/client_api_key_tests.rs
index 9961be9d78..a42b722cd6 100644
--- a/crates/openhuman-core/src/integrations/client_api_key_tests.rs
+++ b/crates/openhuman-core/src/integrations/client_api_key_tests.rs
@@ -79,6 +79,44 @@ async fn binary_download_sends_the_api_key_as_x_api_key() {
assert_eq!(seen[0].1, None);
}
+#[tokio::test]
+async fn presigned_download_redirect_does_not_forward_api_key() {
+ let seen: Seen = Arc::default();
+ let storage = Router::new().route(
+ "/blob",
+ get({
+ let seen = Arc::clone(&seen);
+ move |headers: HeaderMap| async move {
+ record(&seen, &headers);
+ "stored bytes"
+ }
+ }),
+ );
+ let storage_url = start_mock_backend(storage).await;
+ let location = format!("{storage_url}/blob");
+ let backend = Router::new().route(
+ "/agent-integrations/file-storage/files/f1/download",
+ get(move || {
+ let location = location.clone();
+ async move {
+ (
+ StatusCode::FOUND,
+ [(axum::http::header::LOCATION, location)],
+ )
+ }
+ }),
+ );
+ let client = api_key_client(start_mock_backend(backend).await);
+ let (bytes, _, _) = client
+ .get_bytes("/agent-integrations/file-storage/files/f1/download")
+ .await
+ .unwrap();
+ assert_eq!(&bytes[..], b"stored bytes");
+ let seen = seen.lock();
+ assert_eq!(seen.len(), 1);
+ assert_eq!(seen[0], (None, None));
+}
+
#[tokio::test]
async fn api_key_401_is_api_key_rejected_not_session_expired() {
use crate::core::observability::is_session_expired_message;
diff --git a/crates/openhuman-core/src/platform/socket/url_tests.rs b/crates/openhuman-core/src/platform/socket/url_tests.rs
index 4a965ec4d6..d6bc1fb631 100644
--- a/crates/openhuman-core/src/platform/socket/url_tests.rs
+++ b/crates/openhuman-core/src/platform/socket/url_tests.rs
@@ -10,7 +10,9 @@ fn credential_socket_endpoint_requires_tls_or_loopback() {
"ws://api.tinyhumans.ai/socket.io/"
));
assert!(!is_safe_socket_endpoint("ws://example.com/socket.io/"));
- assert!(!is_safe_socket_endpoint("ws://127.attacker.example/socket.io/"));
+ assert!(!is_safe_socket_endpoint(
+ "ws://127.attacker.example/socket.io/"
+ ));
}
#[test]