[repo] Lab relay: recorder, redacting dump, TCP/TLS-terminating/UDP relays, CLI (never compiled yet)

This commit is contained in:
2026-11-25 12:36:56 +01:00
parent fbb8833f3d
commit a2b834ed87
9 changed files with 983 additions and 1 deletions
+122
View File
@@ -0,0 +1,122 @@
//! Hides secrets when records are printed (`lab-relay dump`). The record file itself keeps everything; it never
//! leaves the lab folder.
/// Query parameters whose values are secret or let someone redo the pairing proofs.
pub const SECRET_PARAMS: [&str; 6] =
["rikey", "salt", "clientchallenge", "serverchallengeresp", "clientpairingsecret", "pin"];
/// XML elements whose text is secret.
pub const SECRET_ELEMENTS: [&str; 2] = ["challengeresponse", "pairingsecret"];
/// HTTP headers whose values are secret.
pub const SECRET_HEADERS: [&str; 2] = ["authorization", "cookie"];
/// What replaces a secret.
pub const MARK: &[u8] = b"<redacted>";
fn ends_param_value(b: u8) -> bool {
matches!(b, b'&' | b' ' | b'"' | b'\r' | b'\n' | b'<' | b'#')
}
/// Returns `text` with every secret value replaced by [`MARK`].
pub fn redact(text: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(text.len());
let mut i = 0;
let lower: Vec<u8> = text.to_ascii_lowercase();
'scan: while i < text.len() {
// `name=` at a parameter boundary.
if i == 0 || matches!(text[i - 1], b'?' | b'&' | b' ' | b'\n') {
for name in SECRET_PARAMS {
let key = [name.as_bytes(), b"="].concat();
if lower[i..].starts_with(&key) {
out.extend_from_slice(&text[i..i + key.len()]);
out.extend_from_slice(MARK);
i += key.len();
while i < text.len() && !ends_param_value(text[i]) {
i += 1;
}
continue 'scan;
}
}
}
// `<element>` … `</`.
if text[i] == b'<' {
for name in SECRET_ELEMENTS {
let open = [b"<", name.as_bytes(), b">"].concat();
if lower[i..].starts_with(&open) {
out.extend_from_slice(&text[i..i + open.len()]);
out.extend_from_slice(MARK);
i += open.len();
while i < text.len() && !text[i..].starts_with(b"</") {
i += 1;
}
continue 'scan;
}
}
}
// `Header:` at a line start, up to the end of the line.
if i == 0 || text[i - 1] == b'\n' {
for name in SECRET_HEADERS {
let key = [name.as_bytes(), b":"].concat();
if lower[i..].starts_with(&key) {
out.extend_from_slice(&text[i..i + key.len()]);
out.extend_from_slice(b" ");
out.extend_from_slice(MARK);
i += key.len();
while i < text.len() && text[i] != b'\r' && text[i] != b'\n' {
i += 1;
}
continue 'scan;
}
}
}
out.push(text[i]);
i += 1;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn r(s: &str) -> String {
String::from_utf8(redact(s.as_bytes())).unwrap()
}
#[test]
fn launch_key_is_hidden() {
assert_eq!(
r("GET /launch?uniqueid=0123&appid=1&rikey=00112233445566778899aabbccddeeff&rikeyid=7 HTTP/1.1"),
"GET /launch?uniqueid=0123&appid=1&rikey=<redacted>&rikeyid=7 HTTP/1.1"
);
}
#[test]
fn pairing_values_are_hidden_but_names_and_certs_stay() {
assert_eq!(
r("GET /pair?uniqueid=1&phrase=getservercert&salt=ABCD&clientcert=2D2D HTTP/1.1"),
"GET /pair?uniqueid=1&phrase=getservercert&salt=<redacted>&clientcert=2D2D HTTP/1.1"
);
assert_eq!(
r("<root status_code=\"200\"><paired>1</paired><pairingsecret>ffee</pairingsecret></root>"),
"<root status_code=\"200\"><paired>1</paired><pairingsecret><redacted></pairingsecret></root>"
);
}
#[test]
fn auth_headers_are_hidden() {
assert_eq!(
r("POST /api/pin HTTP/1.1\r\nAuthorization: Basic dXNlcjpwYXNz\r\nHost: x\r\n"),
"POST /api/pin HTTP/1.1\r\nAuthorization: <redacted>\r\nHost: x\r\n"
);
}
#[test]
fn lookalike_names_are_not_touched() {
assert_eq!(r("GET /x?rikeyid=5&nosalt=1"), "GET /x?rikeyid=5&nosalt=1");
}
#[test]
fn value_at_end_of_input_is_hidden() {
assert_eq!(r("?rikey=abc"), "?rikey=<redacted>");
assert_eq!(r("<challengeresponse>abc"), "<challengeresponse><redacted>");
}
}