Do not quote a TeamSpeak key in the error of a refused put
SqliteKeyStore::put checked a TeamSpeak blob by parsing its JSON as a tsproto::Identity and forwarded serde's error. tsproto's key visitor answers a key it cannot decode with invalid_value(Unexpected::Str(key)), so the message read `invalid value: string "<the key text>", expected a P256 private ecc key`: the private key reached Display and Debug of the KeyStoreError, and so any log line that reports it. check_teamspeak now drops the serde error and returns a Backend error of fixed text (Refused::NotATeamSpeakIdentity). The test puts a blob with a malformed key string, first checks that serde does quote it (so the case proves something), and asserts that neither Display, Debug nor pretty Debug of the error or of any error in its source chain contains the string. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
143bc945f7
commit
f3345bccf2
2 files changed
+52
-2
No files matched your search
@@ -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::<Identity>(&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<Error>) -> 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 },
|
||||
|
||||
@@ -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::<Identity>(&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");
|
||||
|
||||
Reference in new issue
Block a user