Skip to content
127 changes: 120 additions & 7 deletions crates/icaptcha-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,50 @@ fn sanitize_excerpt(s: &str) -> String {
out
}

/// Whether `GITLAWB_ICAPTCHA_INSECURE` explicitly enables the loopback-HTTP
/// trust relaxation used by integration tests.
///
/// Only an explicit truthy value (`1` / `true`, case-insensitive, after trim)
/// enables it. Presence alone is not enough: `=0`, `=false`, or an empty
/// value leave the relaxation off. A non-empty non-truthy value emits a
/// `tracing::warn!` (visible when the `icaptcha_client` target is at WARN+,
/// e.g. `RUST_LOG=icaptcha_client=warn`), matching operator intent rather
/// than a bare `var_os(...).is_some()` presence check.
fn icaptcha_insecure_enabled() -> bool {
match std::env::var("GITLAWB_ICAPTCHA_INSECURE") {
Ok(v) => {
let t = v.trim();
if t.eq_ignore_ascii_case("1") || t.eq_ignore_ascii_case("true") {
true
} else {
if !t.is_empty() {
tracing::warn!(
value = %t,
"GITLAWB_ICAPTCHA_INSECURE is set but not truthy (expected 1 or true); \
loopback HTTP trust relaxation remains disabled"
);
}
false
}
}
Err(_) => false,
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// Whether `u` parses as a trusted URL.
///
/// Production trusts only `https`. When `GITLAWB_ICAPTCHA_INSECURE` is set (a
/// runtime escape hatch for integration tests) the function also trusts
/// `http://127.0.0.1` and `http://localhost` so the full iCaptcha retry path
/// can be exercised against a local mockito server.
/// Production trusts only `https`. When `GITLAWB_ICAPTCHA_INSECURE` is an
/// explicit truthy value (`1` / `true`, case-insensitive, after trim) the
/// function also trusts `http://127.0.0.1` and `http://localhost` so the full
/// iCaptcha retry path can be exercised against a local mockito server.
fn is_https(u: &str) -> bool {
let Ok(parsed) = reqwest::Url::parse(u) else {
return false;
};
if parsed.scheme() == "https" {
return true;
}
if std::env::var_os("GITLAWB_ICAPTCHA_INSECURE").is_some()
if icaptcha_insecure_enabled()
&& parsed.scheme() == "http"
&& matches!(parsed.host_str(), Some("127.0.0.1" | "localhost"))
{
Expand Down Expand Up @@ -396,9 +426,22 @@ mod tests {

impl InsecureEnv {
fn new() -> Self {
let lock = ICAPTCHA_ENV_LOCK.lock().unwrap();
Self::with_value("1")
}

fn with_value(val: &str) -> Self {
// Recover from a poisoned lock so a prior failing assert in this
// suite does not cascade into unrelated tests.
let lock = ICAPTCHA_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let prev = std::env::var_os("GITLAWB_ICAPTCHA_INSECURE");
std::env::set_var("GITLAWB_ICAPTCHA_INSECURE", val);
InsecureEnv { _lock: lock, prev }
}

fn unset() -> Self {
let lock = ICAPTCHA_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let prev = std::env::var_os("GITLAWB_ICAPTCHA_INSECURE");
std::env::set_var("GITLAWB_ICAPTCHA_INSECURE", "1");
std::env::remove_var("GITLAWB_ICAPTCHA_INSECURE");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
InsecureEnv { _lock: lock, prev }
}
}
Expand All @@ -412,6 +455,76 @@ mod tests {
}
}

/// #227: `=0` / `=false` / empty / unset must NOT enable loopback HTTP;
/// only explicit truthy `1` / `true` may.
#[test]
fn insecure_env_only_truthy_enables_loopback_http() {
{
let _env = InsecureEnv::with_value("0");
assert!(
!is_https("http://127.0.0.1"),
"=0 must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_enabled());
}
{
let _env = InsecureEnv::with_value("false");
assert!(
!is_https("http://127.0.0.1"),
"=false must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_enabled());
}
{
let _env = InsecureEnv::with_value("");
assert!(
!is_https("http://127.0.0.1"),
"empty value must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_enabled());
}
{
let _env = InsecureEnv::unset();
assert!(
!is_https("http://127.0.0.1"),
"unset must leave loopback HTTP disabled"
);
assert!(!icaptcha_insecure_enabled());
}
{
let _env = InsecureEnv::with_value("1");
assert!(is_https("http://127.0.0.1"), "=1 must enable loopback HTTP");
assert!(is_https("http://localhost"));
assert!(icaptcha_insecure_enabled());
}
{
let _env = InsecureEnv::with_value("true");
assert!(
is_https("http://127.0.0.1"),
"=true must enable loopback HTTP"
);
assert!(icaptcha_insecure_enabled());
}
{
let _env = InsecureEnv::with_value("TRUE");
assert!(
is_https("http://127.0.0.1"),
"=TRUE (case-insensitive) must enable loopback HTTP"
);
}
{
// Trim is load-bearing: whitespace around a truthy token must still enable.
let _env = InsecureEnv::with_value(" 1 ");
assert!(
is_https("http://127.0.0.1"),
"\" 1 \" (trimmed) must enable loopback HTTP"
);
assert!(icaptcha_insecure_enabled());
}
// https remains trusted regardless of the env flag.
assert!(is_https("https://icaptcha.gitlawb.com"));
}

// ── P1: hostile error bodies must not reach the terminal raw ──────────

#[test]
Expand Down