Refuse an unreadable TeamSpeak identity without its key in the text
The Opus gate of the merge found that ts_session::inner_options put the raw serde_json error into the refusal of an identity it cannot read, and tsproto's key visitor reports a key that does not import as invalid_value(Unexpected::Str( key), ..), so the private key string reached LinkEvent::Disconnected(Some(text)), Event::Error and the UI status line. The classification ts::identity::from_blob already had (malformed, cut short, not an identity, unreadable, with line and column, never the value) is now ts::identity::from_json, which takes the JSON text; from_blob and inner_options both use it, so the refusal is worded in one place. A credential that is not UTF-8 is refused as "it is not text". Tests: identity::tests (a key that lost its last two characters: the JSON reader's own message quotes it, from_json and from_blob do not; no 8-character piece of the key), ts_session::tests (inner_options), link::tests (the LinkEvent::Disconnected text through SessionLink with the real connector) and session_fake (ConnectVoice through the engine: the Disconnected state, the Error event and every event after it). All three connector-side tests fail with the serde_json text put back. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c60acf9de0
commit
a856f90c6b
4 files changed
+170
-7
No files matched your search
@@ -981,6 +981,32 @@ async fn a_connect_that_fails_is_a_disconnect_with_the_reason() {
|
||||
);
|
||||
}
|
||||
|
||||
/// An identity that cannot be read ends the connect with a disconnect whose
|
||||
/// text says where it is wrong and holds none of the key: the JSON reader's
|
||||
/// own message quotes a key that does not import, and the engine shows this
|
||||
/// text as the error of the connection.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn an_identity_that_cannot_be_read_is_a_disconnect_without_the_key() {
|
||||
let (json, cut) = crate::ts::identity::json_with_a_cut_key();
|
||||
let mut options = VoiceOptions::new("ts3server://127.0.0.1:9", "tester");
|
||||
options.identity = Some(IdentityBlob::teamspeak("uid=", 8, json));
|
||||
let link = SessionLink::new(Backends::default());
|
||||
let (_cmd_tx, cmd_rx) = mpsc::unbounded_channel();
|
||||
let (event_tx, mut events) = mpsc::unbounded_channel();
|
||||
link.spawn(options, LocalCtx::standalone().link, cmd_rx, event_tx);
|
||||
let event = within("the end", events.recv()).await;
|
||||
let LinkEvent::Disconnected(Some(text)) = event else {
|
||||
panic!("a disconnect with the reason of the refusal")
|
||||
};
|
||||
assert!(
|
||||
text.starts_with(
|
||||
"the TeamSpeak identity cannot be read: not an identity at line 1, column "
|
||||
),
|
||||
"{text}"
|
||||
);
|
||||
crate::ts::identity::assert_holds_no_piece_of(&text, &cut);
|
||||
}
|
||||
|
||||
// ---- the TeamSpeak side alone ---------------------------------------------------
|
||||
|
||||
/// A `LinkSession` over a recorder, made the way a connector makes it.
|
||||
|
||||
@@ -255,8 +255,20 @@ pub fn from_blob(blob: &IdentityBlob) -> Result<Identity, IdentityError> {
|
||||
blob.kind
|
||||
)));
|
||||
}
|
||||
serde_json::from_str(&blob.json).map_err(|e| {
|
||||
// Where the JSON is wrong, not what it says: it holds a private key.
|
||||
from_json(&blob.json)
|
||||
}
|
||||
|
||||
/// The TeamSpeak identity in `json`, the text [`to_blob`] writes and the key
|
||||
/// store keeps; every reader of that text refuses it through here.
|
||||
///
|
||||
/// What does not read is refused in words that say how it is wrong
|
||||
/// (malformed, cut short, not an identity, unreadable) and where (line and
|
||||
/// column), and never what it holds: the text holds a private key, and the
|
||||
/// message of the JSON reader quotes a value it refuses (`tsproto` reports a
|
||||
/// key that does not import as an invalid value, the key string and all), so
|
||||
/// that message is never shown, not in an error, an event or a log.
|
||||
pub(crate) fn from_json(json: &str) -> Result<Identity, IdentityError> {
|
||||
serde_json::from_str(json).map_err(|e| {
|
||||
let what = match e.classify() {
|
||||
serde_json::error::Category::Syntax => "malformed",
|
||||
serde_json::error::Category::Eof => "cut short",
|
||||
@@ -271,6 +283,27 @@ pub fn from_blob(blob: &IdentityBlob) -> Result<Identity, IdentityError> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The JSON of a fresh identity whose key lost its last two characters (a key
|
||||
/// that does not import), and that cut key.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn json_with_a_cut_key() -> (String, String) {
|
||||
let mut json = serde_json::to_value(Identity::create()).unwrap();
|
||||
let key = json["key"].as_str().unwrap().to_owned();
|
||||
let cut = key[..key.len() - 2].to_owned();
|
||||
json["key"] = cut.clone().into();
|
||||
(json.to_string(), cut)
|
||||
}
|
||||
|
||||
/// Fail if `text` holds any 8 characters in a row of `secret`.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn assert_holds_no_piece_of(text: &str, secret: &str) {
|
||||
assert!(secret.len() >= 8 && secret.is_ascii());
|
||||
for piece in secret.as_bytes().windows(8) {
|
||||
let piece = std::str::from_utf8(piece).unwrap();
|
||||
assert!(!text.contains(piece), "{text:?} holds {piece:?} of the key");
|
||||
}
|
||||
}
|
||||
|
||||
/// The TeamSpeak 3 `.ini` export of an identity, which the official client
|
||||
/// and every other tool reads.
|
||||
pub fn export(nickname: &str, identity: &Identity) -> String {
|
||||
@@ -615,6 +648,27 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The JSON reader's own message quotes a key that does not import, so the
|
||||
/// refusal is worded by `from_json` and holds none of the key.
|
||||
#[test]
|
||||
fn a_key_that_does_not_import_is_refused_without_quoting_it() {
|
||||
let (json, cut) = json_with_a_cut_key();
|
||||
let raw = serde_json::from_str::<Identity>(&json).unwrap_err().to_string();
|
||||
assert!(raw.contains(&cut), "the reader's own message quotes the key: {raw}");
|
||||
for error in [
|
||||
from_json(&json).unwrap_err().to_string(),
|
||||
from_blob(&IdentityBlob::teamspeak("x", 8, json.clone())).unwrap_err().to_string(),
|
||||
] {
|
||||
assert!(
|
||||
error.starts_with(
|
||||
"the TeamSpeak identity cannot be read: not an identity at line 1, column "
|
||||
),
|
||||
"{error}"
|
||||
);
|
||||
assert_holds_no_piece_of(&error, &cut);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn improve_level_keeps_the_key() {
|
||||
let identity = synthetic();
|
||||
|
||||
@@ -43,7 +43,7 @@ use voelin_proto::link::{
|
||||
use voelin_proto::v1::{self, response::Result as Answer};
|
||||
|
||||
use self::driver::{Cmd, Config, Driver, Parts, rejected};
|
||||
use super::{ConnectOptions, ServerLink, TsLink, versions};
|
||||
use super::{ConnectOptions, ServerLink, TsLink, identity, versions};
|
||||
use crate::BackendInfo;
|
||||
use crate::link::{
|
||||
BoxDuplex, LinkCmd, LinkCtx, LinkEvent, LocalConnected, LocalConnector, LocalCtx, StreamPort,
|
||||
@@ -82,9 +82,14 @@ fn inner_options(params: &ConnectParams, ctx: &LocalCtx) -> Result<ConnectOption
|
||||
let identity = match ¶ms.credential {
|
||||
Credential::Foreign { kind: IDENTITY_KIND, blob } if blob.is_empty() => None,
|
||||
Credential::Foreign { kind: IDENTITY_KIND, blob } => {
|
||||
Some(serde_json::from_slice(blob).map_err(|e| {
|
||||
SessionError::Protocol(format!("the TeamSpeak identity cannot be read: {e}"))
|
||||
})?)
|
||||
// The refusal never quotes the JSON: it holds the private key, and the
|
||||
// JSON reader's own message would show a key that does not import.
|
||||
let json = std::str::from_utf8(blob).map_err(|_| {
|
||||
SessionError::Protocol(
|
||||
"the TeamSpeak identity cannot be read: it is not text".into(),
|
||||
)
|
||||
})?;
|
||||
Some(identity::from_json(json).map_err(|e| SessionError::Protocol(e.to_string()))?)
|
||||
}
|
||||
_ => {
|
||||
return Err(SessionError::Protocol(
|
||||
@@ -754,6 +759,33 @@ mod tests {
|
||||
assert!(!futures::executor::block_on(confirm));
|
||||
}
|
||||
|
||||
/// An identity that cannot be read ends the connect with words that say
|
||||
/// where it is wrong and hold none of the key: the JSON reader's own
|
||||
/// message quotes a key that does not import, and this text reaches the
|
||||
/// event bus and the status line.
|
||||
#[test]
|
||||
fn an_identity_that_cannot_be_read_is_refused_without_the_key() {
|
||||
let (json, cut) = identity::json_with_a_cut_key();
|
||||
let mut sent = params(&VoiceOptions::new("ts.example.com", "nick"));
|
||||
sent.credential = Credential::Foreign { kind: "teamspeak", blob: Bytes::from(json) };
|
||||
let error = inner_options(&sent, &LocalCtx::standalone()).unwrap_err();
|
||||
let SessionError::Protocol(text) = error else { panic!("a protocol error: {error:?}") };
|
||||
assert!(
|
||||
text.starts_with(
|
||||
"the TeamSpeak identity cannot be read: not an identity at line 1, column "
|
||||
),
|
||||
"{text}"
|
||||
);
|
||||
identity::assert_holds_no_piece_of(&text, &cut);
|
||||
|
||||
// Not even text.
|
||||
sent.credential =
|
||||
Credential::Foreign { kind: "teamspeak", blob: Bytes::from_static(&[b'{', 0xff]) };
|
||||
let error = inner_options(&sent, &LocalCtx::standalone()).unwrap_err();
|
||||
let SessionError::Protocol(text) = error else { panic!("a protocol error: {error:?}") };
|
||||
assert_eq!(text, "the TeamSpeak identity cannot be read: it is not text");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_params_carry_what_the_session_needs_and_no_account_yet() {
|
||||
let sent = params(&VoiceOptions::new("ts.example.com", "nick"));
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
//! dedup of a message that comes twice, history, transfers, offline messages,
|
||||
//! the features of a host, a reconnect and the media of a session are the
|
||||
//! engine's own behaviour; nothing here needs a TeamSpeak server or a
|
||||
//! TeamSpeak type.
|
||||
//! TeamSpeak type, except `a_teamspeak_identity_that_cannot_be_read_is_refused_without_the_key`,
|
||||
//! which runs the engine's own TeamSpeak connector up to its refusal of an
|
||||
//! identity.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
@@ -163,6 +165,12 @@ impl Harness {
|
||||
fn on(connector: FakeConnector) -> Self {
|
||||
let mut backends = Backends::empty();
|
||||
backends.register(Arc::new(connector.clone()));
|
||||
Self::with_backends(backends, connector)
|
||||
}
|
||||
|
||||
/// The engine on `backends`; `connector` is the fake host the test may
|
||||
/// have registered in them (a test that registers none leaves it unused).
|
||||
fn with_backends(backends: Backends, connector: FakeConnector) -> Self {
|
||||
let engine =
|
||||
Engine::start_with_backends(Settings::in_memory(), History::in_memory(), backends);
|
||||
let events = engine.subscribe();
|
||||
@@ -1502,3 +1510,46 @@ async fn a_teamspeak_identity_without_json_is_refused_and_no_session_is_made() {
|
||||
assert!(handle.close_reason().is_none());
|
||||
assert_eq!(h.connector.connects().len(), 1);
|
||||
}
|
||||
|
||||
/// A TeamSpeak identity whose key does not import ends the connect with words
|
||||
/// that say where the JSON is wrong and never show the key: the JSON reader's
|
||||
/// own message quotes it, and the text is the reason of the disconnect, the
|
||||
/// `Error` event and the status line the UI makes of it. This is the one test
|
||||
/// of the file that runs the engine's own TeamSpeak connector
|
||||
/// (`Backends::default()`): the refusal is that connector's and comes before
|
||||
/// any network.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_teamspeak_identity_that_cannot_be_read_is_refused_without_the_key() {
|
||||
let mut json = serde_json::to_value(tsclientlib::Identity::create()).unwrap();
|
||||
let key = json["key"].as_str().unwrap().to_owned();
|
||||
// The key lost its last two characters.
|
||||
let cut = key[..key.len() - 2].to_owned();
|
||||
json["key"] = cut.clone().into();
|
||||
let mut options = VoiceOptions::new("ts3server://127.0.0.1:9", "tester");
|
||||
options.identity = Some(IdentityBlob::teamspeak("uid=", 8, json.to_string()));
|
||||
|
||||
let mut h = Harness::with_backends(Backends::default(), FakeConnector::new());
|
||||
h.engine.send(Command::ConnectVoice { session: SESSION, options: Box::new(options) });
|
||||
let state = h.state(|s| s.voice == VoiceState::Disconnected).await;
|
||||
assert_eq!((state.own_channel, state.own_client), (None, None));
|
||||
let error = h.error().await;
|
||||
assert!(
|
||||
error.starts_with(
|
||||
"voice: the TeamSpeak identity cannot be read: not an identity at line 1, column "
|
||||
),
|
||||
"{error}"
|
||||
);
|
||||
// Nothing the engine reported afterwards holds a piece of the key either.
|
||||
let mut shown = vec![error];
|
||||
while let Ok(Ok(event)) =
|
||||
tokio::time::timeout(Duration::from_millis(300), h.events.recv()).await
|
||||
{
|
||||
shown.push(format!("{event:?}"));
|
||||
}
|
||||
for text in &shown {
|
||||
for piece in cut.as_bytes().windows(8) {
|
||||
let piece = std::str::from_utf8(piece).unwrap();
|
||||
assert!(!text.contains(piece), "{text:?} holds {piece:?} of the key");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user