diff --git a/crates/voelin-store/src/keystore.rs b/crates/voelin-store/src/keystore.rs index 8603b2c..a7d53b2 100644 --- a/crates/voelin-store/src/keystore.rs +++ b/crates/voelin-store/src/keystore.rs @@ -20,6 +20,10 @@ //! (it could only have been damaged outside this crate) lists and reads with //! level 0 instead of failing the whole list, so that signing out is never //! blocked by it. +//! +//! A TeamSpeak blob that is refused says why in words that carry none of its +//! input: its `data` is the identity's private key, and an error reaches log +//! lines. use std::path::Path; use std::sync::{Mutex, MutexGuard, PoisonError}; @@ -168,9 +172,13 @@ impl KeyStore for SqliteKeyStore { /// over, from failing the list of TeamSpeak identities for everyone. The /// second keeps a blob from overwriting, by its uid, the key of a different /// identity that is stored under that uid. +/// +/// The parse error of serde is dropped, not forwarded: it can quote the text +/// it choked on, which is a private key, into a log line. fn check_teamspeak(blob: &IdentityBlob) -> KsResult<()> { - let identity: Identity = - serde_json::from_str(&blob.json).map_err(|e| backend(Error::Json(e)))?; + let Ok(identity) = serde_json::from_str::(&blob.json) else { + return Err(KeyStoreError::backend(Refused::NotATeamSpeakIdentity)); + }; if identity.key().to_pub().get_uid() != blob.uid { return Err(KeyStoreError::Blob(BlobError::UidMismatch)); } @@ -185,6 +193,10 @@ fn backend(error: impl Into) -> KeyStoreError { /// A [`KeyStore::put`] this store refuses. #[derive(Debug, thiserror::Error)] enum Refused { + /// The JSON is not that of a `tsproto::Identity`. The text is a private + /// key, so the error does not repeat it, nor serde's account of it. + #[error("the TeamSpeak identity JSON is not that of an identity")] + NotATeamSpeakIdentity, /// `uid` is unique across kinds in this table. #[error("the uid {uid:?} is already stored as an identity of kind {kind:?}")] UidHeld { uid: String, kind: String }, diff --git a/crates/voelin-store/tests/keystore.rs b/crates/voelin-store/tests/keystore.rs index 6d57660..eed3e05 100644 --- a/crates/voelin-store/tests/keystore.rs +++ b/crates/voelin-store/tests/keystore.rs @@ -561,6 +561,44 @@ fn a_teamspeak_blob_that_is_no_identity_is_refused() { assert_eq!(store.with_store(|s| s.identities().unwrap().len()), 1); } +#[test] +fn a_refused_teamspeak_blob_does_not_quote_its_key_in_the_error() { + // The JSON of a TeamSpeak identity holds the private key. serde's account of + // a key it cannot read quotes the string it was given, so the store must not + // forward it: the error reaches Display and Debug, and so any log line. + const KEY_TEXT: &str = "PRIVATE-KEY-TEXT-0123456789"; + let json = format!(r#"{{"key":"{KEY_TEXT}","counter":1,"max_counter":1}}"#); + let parse_error = serde_json::from_str::(&json).unwrap_err(); + assert!( + parse_error.to_string().contains(KEY_TEXT), + "the case proves nothing if serde does not quote the key: {parse_error}" + ); + + let store = SqliteKeyStore::open_in_memory().unwrap(); + let error = store.put(&IdentityBlob::teamspeak("some/uid=", 0, json.as_str())).unwrap_err(); + assert!(matches!(error, KeyStoreError::Backend(_)), "{error:?}"); + let mut texts = vec![format!("{error}"), format!("{error:?}"), format!("{error:#?}")]; + let mut source = std::error::Error::source(&error); + while let Some(cause) = source { + texts.extend([format!("{cause}"), format!("{cause:?}"), format!("{cause:#?}")]); + source = cause.source(); + } + for text in &texts { + assert!(!text.contains(KEY_TEXT), "the error repeats the key: {text}"); + } + assert!(store.list().unwrap().is_empty(), "nothing was stored"); + + // The same holds when the JSON parses and only the uid is wrong: that error + // is a fixed text as well. + let stranger = Identity::create(); + let json = serde_json::to_string(&stranger).unwrap(); + let key_text = json.split('"').nth(3).unwrap().to_owned(); + let error = store.put(&IdentityBlob::teamspeak("some/uid=", 0, json)).unwrap_err(); + for text in [format!("{error}"), format!("{error:?}")] { + assert!(!text.contains(&key_text), "the error repeats the key: {text}"); + } +} + #[test] fn the_level_is_computed_from_the_key_never_stored_and_never_blocks_a_read() { let scratch = Scratch::new("level");