Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions crates/socket-patch-core/src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::constants::USER_AGENT as USER_AGENT_VALUE;
use crate::utils::digest::is_hex;
use crate::utils::env_compat::{is_offline_env, proxy_url_from_env};
use crate::utils::notice::{notice_once, Notice};
use crate::utils::redact::{redact_url, redact_urls_in};
use crate::utils::redact::{redact_message, redact_url};
use crate::utils::socket_cli_config;
use crate::utils::target::is_uuid_shaped;

Expand Down Expand Up @@ -58,12 +58,24 @@ static MULTI_ORG_SHOWN: AtomicBool = AtomicBool::new(false);
/// act on ("Connection refused", a DNS or TLS failure). Causes already
/// spelled out by an outer message are skipped.
pub(crate) fn network_error_detail(e: &reqwest::Error) -> String {
// reqwest's text names the request URL: a grant URL, or one with
// userinfo from `--api-url` / a proxy, is quoted redacted.
let mut msg = redact_urls_in(&e.to_string()).into_owned();
// reqwest's text names the request URL, as `(<url>)`: a grant URL,
// or one with userinfo from `--api-url` / a proxy, is quoted redacted.
// The URL is taken from the error and redacted as one URL first: cut
// out of the text, it would end at a `)` that a secret query value
// holds.
let shown = |text: String| {
redact_message(match e.url() {
Some(url) => text.replace(
&format!("({url})"),
&format!("({})", redact_url(url.as_str())),
),
None => text,
})
};
let mut msg = shown(e.to_string());
let mut source = std::error::Error::source(e);
while let Some(cause) = source {
let part = redact_urls_in(&cause.to_string()).into_owned();
let part = shown(cause.to_string());
if !part.is_empty() && !msg.contains(&part) {
msg.push_str(": ");
msg.push_str(&part);
Expand Down Expand Up @@ -4498,6 +4510,32 @@ mod tests {
}
}

/// reqwest names the request URL in brackets. A secret query value
/// that holds a `)` is redacted whole: the URL is taken from the
/// error, not cut out of its text.
#[tokio::test]
async fn network_error_detail_redacts_the_request_url_as_one_url() {
let port = {
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
l.local_addr().unwrap().port()
};
let err = reqwest::Client::new()
.get(format!(
"http://127.0.0.1:{port}/private?token=ab)cdSECRET&v=1"
))
.send()
.await
.expect_err("nothing listens there");
let detail = network_error_detail(&err);
assert!(!detail.contains("SECRET"), "{detail}");
assert!(
detail.contains(&format!(
"(http://127.0.0.1:{port}/private?token=<redacted>&v=1)"
)),
"{detail}"
);
}

#[tokio::test]
async fn network_error_detail_keeps_the_root_cause() {
// A port that was just free: connecting is refused, deterministically.
Expand Down
13 changes: 10 additions & 3 deletions crates/socket-patch-core/src/utils/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub(crate) fn secure_url(url: &str) -> Result<reqwest::Url, String> {
.ok_or_else(|| {
format!(
"`{}` is not an https URL (plain http is accepted for a loopback host only)",
crate::utils::redact::redact_urls_in(url)
crate::utils::redact::redact_url(url)
)
})
}
Expand Down Expand Up @@ -339,8 +339,8 @@ fn redirect_refusal(from: &reqwest::Url, to: &reqwest::Url) -> Option<String> {
(!followed).then(|| {
format!(
"refusing a redirect from {} to {}, which is not an https URL",
crate::utils::redact::redact_urls_in(from.as_str()),
crate::utils::redact::redact_urls_in(to.as_str())
crate::utils::redact::redact_url(from.as_str()),
crate::utils::redact::redact_url(to.as_str())
)
})
}
Expand Down Expand Up @@ -477,6 +477,13 @@ mod tests {
)
.unwrap();
assert!(!why.contains("hunter2"), "{why}");
// Each side is one URL: a secret value that holds a `)` is cut whole.
let why = redirect_refusal(
&url("https://rubygems.org/gems/a-1.gem?token=ab)FROMSECRET"),
&url("http://cdn.example.test/a-1.gem?sig=ab)TOSECRET"),
)
.unwrap();
assert!(!why.contains("SECRET"), "{why}");
}

/// Serve one raw HTTP/1.1 response on loopback and return a response
Expand Down
Loading
Loading