Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(auth): bound and scrub remote error bodies in auth login
lhzapata19499cea436c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 59954396..31ae0570 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,15 @@ version in [`docs/changelog/`](docs/changelog/):## [Unreleased]+### Fixed+- `ai-memory auth login openai-oauth` and `auth login copilot` no longer print+ a failed endpoint's whole response body. The body is read up to 1 MiB,+ scrubbed of secrets and terminal control sequences, and cut to 1 KiB, the+ same handling provider errors already had for size. Provider error bodies+ (`LlmError::Provider`, including `oidc-device` login and token refresh) are+ now scrubbed too, before the cut, so a secret at the 1 KiB boundary is+ still redacted. (#1359)+## [3.1.0] - 2026-10-16### Addeddiff --git a/crates/ai-memory-cli/src/commands/auth.rs b/crates/ai-memory-cli/src/commands/auth.rsindex d8ce8613..17a9691b 100644--- a/crates/ai-memory-cli/src/commands/auth.rs+++ b/crates/ai-memory-cli/src/commands/auth.rs@@ -8,7 +8,7 @@ use ai_memory_llm::{CODEX_CLIENT_ID, CopilotToken, DeviceAuthorizationResponse, GITHUB_ACCESS_TOKEN_URL,GITHUB_COPILOT_CLIENT_ID, GITHUB_DEVICE_CODE_URL, OIDC_DEFAULT_SCOPE, OPENAI_OAUTH_TOKEN_URL,OidcDiscovery, OidcToken, OidcTokenResponse, OpenAiOAuthToken, OpenAiOAuthTokenResponse,- PollOutcome, discover, poll_token_once, request_device_code,+ PollOutcome, discover, poll_token_once, provider_error_body, request_device_code,};use anyhow::{Context, Result, anyhow, bail};use secrecy::ExposeSecret as _;@@ -332,7 +332,7 @@ async fn start_device_authorization(client: &reqwest::Client) -> Result<DeviceAu.context("starting openai-oauth device authorization")?;let status = resp.status();if !status.is_success() {- let body = resp.text().await.unwrap_or_default();+ let body = provider_error_body(resp).await;bail!("openai-oauth device authorization failed ({status}): {body}");}resp.json::<DeviceAuthorization>()@@ -377,7 +377,7 @@ async fn poll_device_authorization(&& resp.status() != reqwest::StatusCode::NOT_FOUND{let status = resp.status();- let body = resp.text().await.unwrap_or_default();+ let body = provider_error_body(resp).await;bail!("openai-oauth authorization polling failed ({status}): {body}");}sleep(interval).await;@@ -402,7 +402,7 @@ async fn exchange_authorization_code(.context("exchanging openai-oauth authorization code")?;let status = resp.status();if !status.is_success() {- let body = resp.text().await.unwrap_or_default();+ let body = provider_error_body(resp).await;bail!("openai-oauth token exchange failed ({status}): {body}");}resp.json::<OpenAiOAuthTokenResponse>()@@ -423,7 +423,7 @@ async fn start_github_device_authorization(.context("starting copilot GitHub device authorization")?;let status = resp.status();if !status.is_success() {- let body = resp.text().await.unwrap_or_default();+ let body = provider_error_body(resp).await;bail!("copilot GitHub device authorization failed ({status}): {body}");}resp.json::<GitHubDeviceAuthorization>()@@ -458,7 +458,7 @@ async fn poll_github_device_authorization(.context("polling copilot GitHub device authorization")?;let status = resp.status();if !status.is_success() {- let body = resp.text().await.unwrap_or_default();+ let body = provider_error_body(resp).await;bail!("copilot GitHub authorization polling failed ({status}): {body}");}let body = respdiff --git a/crates/ai-memory-llm/src/lib.rs b/crates/ai-memory-llm/src/lib.rsindex e7db3e75..fa0018d8 100644--- a/crates/ai-memory-llm/src/lib.rs+++ b/crates/ai-memory-llm/src/lib.rs@@ -151,6 +151,7 @@ pub use opencode::{};pub use provider::{LlmProvider, complete_structured, complete_structured_with_operation_id};pub use reranker::{LlmReranker, RerankCandidate, RerankScore, RerankStage, Reranker};+pub use response::provider_error_body;pub use stored_token::StoredOAuthToken;pub use types::{ChatMessage, ChatRequest, ChatResponse, ExtraHeaders, LlmOperationId, ReasoningEffort, Role,diff --git a/crates/ai-memory-llm/src/response.rs b/crates/ai-memory-llm/src/response.rsindex 10adf6aa..3b144e6d 100644--- a/crates/ai-memory-llm/src/response.rs+++ b/crates/ai-memory-llm/src/response.rs@@ -1,3 +1,4 @@+use ai_memory_core::Sanitizer;use serde::de::DeserializeOwned;use crate::error::{LlmError, LlmResult};@@ -40,13 +41,89 @@ pub(crate) async fn response_json_limited<T: DeserializeOwned>(serde_json::from_slice(&bytes).map_err(LlmError::from)}-pub(crate) async fn provider_error_body(resp: reqwest::Response) -> String {+/// Read a failed response's body for an error message: at most 1 MiB is+/// read, secrets and terminal control sequences are scrubbed, and at most+/// 1 KiB is kept.+///+/// Gateways and providers echo request content in error bodies, and the+/// result is printed to a terminal or carried in [`LlmError::Provider`].+/// Scrubbing runs before the cut so a secret straddling the 1 KiB boundary+/// is still redacted rather than left as an unrecognisable prefix.+pub async fn provider_error_body(resp: reqwest::Response) -> String {match response_bytes_limited(resp, MAX_PROVIDER_ERROR_BYTES).await {Ok(bytes) => {- let body = String::from_utf8_lossy(&bytes);- truncate_with_ellipsis(body.as_ref(), DISPLAY_ERROR_BYTES)+ let body = Sanitizer::builtin().scrub(&String::from_utf8_lossy(&bytes));+ truncate_with_ellipsis(&body, DISPLAY_ERROR_BYTES)}Err(LlmError::Provider { body, .. }) => body,Err(e) => format!("<failed to read provider error body: {e}>"),}}++#[cfg(test)]+mod tests {+ use super::*;++ /// Answer one request on a loopback port with `status` and `body`.+ async fn serve_once(status: &'static str, body: String) -> String {+ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};++ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ tokio::spawn(async move {+ let Ok((mut stream, _)) = listener.accept().await else {+ return;+ };+ let mut buf = [0_u8; 4096];+ let _ = stream.read(&mut buf).await;+ let head = format!(+ "HTTP/1.1 {status}\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",+ body.len()+ );+ let _ = stream.write_all(head.as_bytes()).await;+ let _ = stream.write_all(body.as_bytes()).await;+ });+ format!("http://{addr}/")+ }++ async fn error_body_for(body: String) -> String {+ let url = serve_once("400 Bad Request", body).await;+ let resp = reqwest::Client::new().get(url).send().await.unwrap();+ provider_error_body(resp).await+ }++ #[tokio::test]+ async fn provider_error_body_redacts_a_secret_straddling_the_display_cut() {+ // The cut keeps `ghp_` plus 12 characters: too short for the GitHub+ // token pattern, so only scrubbing before the cut redacts it.+ let secret = format!("ghp_{}", "FAKEfake0123".repeat(3));+ let prefix = format!("{} ", "x".repeat(DISPLAY_ERROR_BYTES - 17));+ let body = format!("{prefix}{secret}");++ let shown = error_body_for(body).await;++ assert!(!shown.contains("ghp_"), "secret prefix leaked: {shown}");+ assert!(shown.starts_with(&prefix), "unrelated text was lost");+ }++ #[tokio::test]+ async fn provider_error_body_strips_terminal_control_sequences() {+ let shown = error_body_for("before\x1b]0;retitled\x07\x1b[2Jafter".to_string()).await;++ assert!(+ !shown.contains('\x1b'),+ "escape sequence survived: {shown:?}"+ );+ assert!(!shown.contains('\x07'), "bell survived: {shown:?}");+ assert!(shown.starts_with("before"));+ }++ #[tokio::test]+ async fn provider_error_body_caps_what_it_reads_and_what_it_shows() {+ let shown = error_body_for("a".repeat(MAX_PROVIDER_ERROR_BYTES - 1)).await;+ assert!(shown.len() <= DISPLAY_ERROR_BYTES + "…".len());++ let oversized = error_body_for("a".repeat(MAX_PROVIDER_ERROR_BYTES + 1)).await;+ assert!(oversized.contains("exceeded"), "unexpected: {oversized}");+ }+}