92 lines
3.5 KiB
Rust
92 lines
3.5 KiB
Rust
//! Takes secrets out of text before it is logged or shown.
|
|
|
|
use regex::{Captures, Regex};
|
|
use std::borrow::Cow;
|
|
use std::sync::LazyLock;
|
|
|
|
const HIDDEN: &str = "[hidden]";
|
|
|
|
/// Headers that carry secrets: kept in the keyring only, never stored or logged.
|
|
pub fn is_secret_header(name: &str) -> bool {
|
|
let name = name.trim().to_ascii_lowercase();
|
|
["cookie", "authorization", "token", "api-key", "apikey", "secret", "password", "x-auth"].iter().any(|s| name.contains(s))
|
|
}
|
|
|
|
/// A URL without login, query string or fragment.
|
|
pub fn url(url: &str) -> Cow<'_, str> {
|
|
static URL: LazyLock<Regex> =
|
|
LazyLock::new(|| Regex::new(r#"(?i)\b([a-z][a-z0-9+.-]*)://(?:[^\s/?#]*@)?([^\s?#"<>]*)([?#][^\s"<>]*)?"#).unwrap());
|
|
URL.replace_all(url, |c: &Captures| {
|
|
let tail = c.get(3).map_or("", |m| if m.as_str().starts_with('?') { "?…" } else { "" });
|
|
format!("{}://{}{tail}", &c[1], &c[2])
|
|
})
|
|
}
|
|
|
|
/// Any text, with the secrets in its URLs, headers and `name=value` pairs hidden.
|
|
pub fn text(text: &str) -> String {
|
|
static HEADER: LazyLock<Regex> = LazyLock::new(|| {
|
|
Regex::new(r#"(?i)\b((?:proxy-)?authorization|set-cookie2?|cookies?|x-[a-z-]*(?:token|key|auth|secret)[a-z-]*)(["']?\s*[:=,]\s*)[^\r\n]+"#)
|
|
.unwrap()
|
|
});
|
|
static PAIR: LazyLock<Regex> = LazyLock::new(|| {
|
|
Regex::new(
|
|
r#"(?i)\b([\w-]*(?:token|password|passwd|passphrase|pwd|secret|api[_-]?key|session[_-]?id|signature))(["']?\s*[:=]\s*)("[^"]*"|'[^']*'|[^\s&;,"']+)"#,
|
|
)
|
|
.unwrap()
|
|
});
|
|
static SCHEME: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\b(bearer|basic)\s+[a-z0-9+/=._~-]{8,}").unwrap());
|
|
let out = url(text);
|
|
let out = HEADER.replace_all(&out, format!("${{1}}${{2}}{HIDDEN}"));
|
|
let out = PAIR.replace_all(&out, format!("${{1}}${{2}}{HIDDEN}"));
|
|
SCHEME.replace_all(&out, format!("${{1}} {HIDDEN}")).into_owned()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn urls_lose_login_query_and_fragment() {
|
|
assert_eq!(url("https://me:p@[email protected]:8080/a/b.zip?token=abc&x=1#frag"), "https://h.org:8080/a/b.zip?…");
|
|
assert_eq!(url("ftp://me@h/f"), "ftp://h/f");
|
|
assert_eq!(url("http://h/f#access_token=1"), "http://h/f");
|
|
assert_eq!(url("https://h/[email protected]"), "https://h/[email protected]");
|
|
}
|
|
|
|
#[test]
|
|
fn text_hides_every_kind_of_secret() {
|
|
for line in [
|
|
"GET https://u:S3CRET@h/f failed",
|
|
"GET https://h/f?sig=S3CRET failed",
|
|
"Cookie: a=1; sid=S3CRET",
|
|
"set-cookie: sid=S3CRET; Path=/",
|
|
r#"headers [("cookie", "sid=S3CRET")]"#,
|
|
r#"{"cookies": "a=b; c=S3CRET"}"#,
|
|
"Authorization: Digest username=\"u\", response=\"S3CRET\"",
|
|
"proxy-authorization: Basic S3CRETS3CRET",
|
|
"sent bearer S3CRET.S3CRET",
|
|
"X-Auth-Token: S3CRET",
|
|
"access_token=S3CRET&b=2",
|
|
r#"password: "my S3CRET""#,
|
|
"api-key='S3CRET'",
|
|
] {
|
|
assert!(!text(line).contains("S3CRET"), "{}", text(line));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn plain_text_stays() {
|
|
let line = "Part 3 of disk.iso: the server closed the connection (attempt 2 of 5)";
|
|
assert_eq!(text(line), line);
|
|
}
|
|
|
|
#[test]
|
|
fn secret_headers_are_known() {
|
|
for h in ["Cookie", "Set-Cookie", "AUTHORIZATION", "X-Api-Key", "X-Auth-Token", "proxy-authorization"] {
|
|
assert!(is_secret_header(h), "{h}");
|
|
}
|
|
for h in ["Referer", "User-Agent", "Accept"] {
|
|
assert!(!is_secret_header(h), "{h}");
|
|
}
|
|
}
|
|
}
|