//! Hides secrets before anything reaches the log or the terminal: the stream key (`rikey`) and the pairing values //! that, together with the PIN, would let someone redo the pairing proofs. Names stay, values go, so the log still //! shows which parameters were sent and in which order (U1, U4). /// Query parameters whose values never reach the log. pub const SECRET_PARAMS: [&str; 6] = ["rikey", "salt", "clientchallenge", "serverchallengeresp", "clientpairingsecret", "pin"]; /// XML elements whose text never reaches the log. pub const SECRET_ELEMENTS: [&str; 2] = ["challengeresponse", "pairingsecret"]; /// What replaces a secret value. pub const MARK: &str = ""; /// `path?k=v&…` with the value of every [`SECRET_PARAMS`] name replaced by [`MARK`] (names compared ignoring case). pub fn query(path_and_query: &str) -> String { let Some((path, q)) = path_and_query.split_once('?') else { return path_and_query.to_owned() }; let parts: Vec = q .split('&') .map(|kv| match kv.split_once('=') { Some((k, _)) if SECRET_PARAMS.iter().any(|s| s.eq_ignore_ascii_case(k)) => format!("{k}={MARK}"), _ => kv.to_owned(), }) .collect(); format!("{path}?{}", parts.join("&")) } /// `text` with the content of every `…` of [`SECRET_ELEMENTS`] replaced by [`MARK`] (names compared /// ignoring case; an element left open is redacted to the end). pub fn xml(text: &str) -> String { let lower = text.to_ascii_lowercase(); let mut out = String::with_capacity(text.len()); let mut i = 0; 'scan: while i < text.len() { for name in SECRET_ELEMENTS { let open = format!("<{name}>"); if lower.get(i..).is_some_and(|rest| rest.starts_with(&open)) { out.push_str(text.get(i..i + open.len()).unwrap_or("")); out.push_str(MARK); i += open.len(); i = lower.get(i..).and_then(|rest| rest.find("&rikeyid=7" ); } #[test] fn a_real_launch_request_never_shows_its_key() { let keys = SessionKeys { rikey: [0xA5; 16], rikeyid: 42 }; let params = LaunchParams { app_id: 5, width: 1920, height: 1080, fps: 60, keys, hdr: false, audio: AudioLayout::Stereo, }; let req = launch_request(¶ms, "0123456789ABCDEF").unwrap(); let shown = query(&req.path_and_query); assert!(!shown.to_ascii_lowercase().contains("a5a5a5a5"), "{shown}"); assert!(shown.contains("rikey=") && shown.contains("rikeyid=42") && shown.contains("appid=5")); } #[test] fn pairing_values_are_hidden_but_names_and_certs_stay() { assert_eq!( query("/pair?uniqueid=1&devicename=x&updateState=1&phrase=getservercert&salt=ABCD&clientcert=2D2D"), "/pair?uniqueid=1&devicename=x&updateState=1&phrase=getservercert&salt=&clientcert=2D2D" ); assert_eq!(query("/pair?uniqueid=1&ClientChallenge=ff"), "/pair?uniqueid=1&ClientChallenge="); assert_eq!( xml("1ffee"), "1" ); assert_eq!( xml("00112D2D"), "2D2D" ); } #[test] fn lookalikes_and_plain_text_are_untouched() { assert_eq!(query("/x?rikeyid=5&nosalt=1&pinned=2"), "/x?rikeyid=5&nosalt=1&pinned=2"); assert_eq!(query("/serverinfo"), "/serverinfo"); assert_eq!(xml("PC é"), "PC é"); } #[test] fn an_open_element_is_redacted_to_the_end() { assert_eq!(xml("abc"), ""); assert_eq!(query("?rikey=abc"), "?rikey="); } }