fix(log): redact media-server API keys and the stored Jellyfin token

Jellyfin stream urls now carry ApiKey and requests carry Token="..." in the
MediaBrowser header; neither form matched the redaction patterns. Add
patterns for ApiKey/api_key query values and a quoted Token value, and
register the stored Jellyfin token with the literal redaction layer whenever
media-server settings are bound or saved.

cc
This commit is contained in:
vicrodh committed 2026-09-15 11:15:06 -06:00
1 parent f0dff94517
commit 65a1104db9
2 files changed
+31 -1

No files matched your search

+19 -1
View File
@@ -45,6 +45,10 @@ fn patterns() -> &'static Vec<Regex> {
r#"(?i)((access|refresh)_token["':=\s]+)[A-Za-z0-9._\-]+"#,
// generic URL token param
r#"(?i)(token=)[^&\s"']+"#,
// quoted token inside an `Authorization: MediaBrowser …` header
r#"(?i)(token=")[^"]+"#,
// media-server API key query params (Jellyfin `ApiKey` / legacy `api_key`)
r#"(?i)(api_?key=)[^&\s"']+"#,
// Signed CDN URLs: Qobuz/Akamai `hmac=`, CloudFront `Signature=` /
// `Policy=` / `Key-Pair-Id=`. Value = up to the next query separator.
r#"(?i)(hmac=)[^&\s"']+"#,
@@ -80,8 +84,9 @@ pub fn register_secret(value: String) {
/// Cheap pre-check: does the line contain any substring that one of the regexes could
/// match? Avoids running the whole pattern set on the overwhelming majority of lines.
fn has_redaction_candidate(lower: &str) -> bool {
const NEEDLES: [&str; 9] = [
const NEEDLES: [&str; 11] = [
"token", "secret", "password", "bearer", "auth", "sig", "hmac", "policy", "key-pair",
"apikey", "api_key",
];
NEEDLES.iter().any(|n| lower.contains(n))
}
@@ -192,6 +197,19 @@ mod tests {
assert!(r.contains("eid=1"), "non-secret params survive: {r}");
}
#[test]
fn redacts_media_server_api_keys_and_header_tokens() {
let r = redact(
"stream http://jf:8096/Audio/x/stream?static=true&ApiKey=JFKEY123 \
old ?api_key=OLDKEY456&static=true \
Authorization: MediaBrowser Client=\"QBZ\", Token=\"HDRTOKEN789\"",
);
for leaked in ["JFKEY123", "OLDKEY456", "HDRTOKEN789"] {
assert!(!r.contains(leaked), "leaked {leaked}: {r}");
}
assert!(r.contains("static=true"), "non-secret params survive: {r}");
}
#[test]
fn literal_registry_scrubs_unlabeled_value() {
register_secret("LIVE_TOKEN_xyz".into());
+12
View File
@@ -67,6 +67,7 @@ fn ensure_bound() {
.join("users")
.join(uid.to_string());
STATE.init_at(&dir);
register_log_secrets();
*bound = Some(uid);
log::info!("[qbz-qt] media server settings bound to user {uid}");
}
@@ -83,6 +84,16 @@ pub fn init_for_user(base_dir: &std::path::Path) {
// DeviceId minted per connection attempt revokes the previous token, and a
// salt minted per request re-downloads every cover.
ensure_identities();
register_log_secrets();
}
/// Hand the stored Jellyfin token to the log redactor's literal layer, so it
/// is scrubbed even where no labeled key (`ApiKey=`, `Token="`) precedes it.
fn register_log_secrets() {
let jf = STATE.get(MediaServerKind::Jellyfin);
if !jf.token.is_empty() {
qbz_log::register_secret(jf.token);
}
}
pub fn reset() {
@@ -109,6 +120,7 @@ pub fn get(kind: MediaServerKind) -> MediaServerSettings {
pub fn put(kind: MediaServerKind, s: &MediaServerSettings) {
STATE.put(kind, s);
invalidate_cache();
register_log_secrets();
}
pub fn disconnect(kind: MediaServerKind) {