From 44eaa5a6ea2305ea032e10fc4d1553fd9626b979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 21 Jan 2020 18:01:41 +0100 Subject: [PATCH] tokio-xmpp: Prevent XmppCodec from producing invalid stanza MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This bug was introduced by 2e97f4de2e44c64524bbe9990553627e7a47f805, to fix another bug where the parser would choke on whitespace. The bug would manifest whenever a stanza was sent in different parts, for example: << "foo` This commit ensures this doesn't happen anymore (by not trimming whitespaces before feeding the parser), and also ensures that whitespaces are now handled at the correct layer. The removal of xmpp_codec::test_lone_whitespace only happens because I'm not sure if it's supposed to be here anymore. Maybe it should be at a different layer? Or written differently? Signed-off-by: Maxime “pep” Buquet --- tokio-xmpp/src/client/mod.rs | 1 + tokio-xmpp/src/xmpp_codec.rs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tokio-xmpp/src/client/mod.rs b/tokio-xmpp/src/client/mod.rs index 531f003b..af61311f 100644 --- a/tokio-xmpp/src/client/mod.rs +++ b/tokio-xmpp/src/client/mod.rs @@ -177,6 +177,7 @@ impl Stream for Client { } Ok(Async::Ready(Some(Packet::Text(_)))) => { // Ignore text between stanzas + self.state = ClientState::Connected(stream); Ok(Async::NotReady) } Ok(Async::Ready(Some(Packet::StreamStart(_)))) => { diff --git a/tokio-xmpp/src/xmpp_codec.rs b/tokio-xmpp/src/xmpp_codec.rs index 658ca2a0..b1f6c42a 100644 --- a/tokio-xmpp/src/xmpp_codec.rs +++ b/tokio-xmpp/src/xmpp_codec.rs @@ -229,9 +229,8 @@ impl Decoder for XMPPCodec { }; let buf1 = buf1.as_ref().as_ref(); match from_utf8(buf1) { - Ok(mut s) => { + Ok(s) => { debug!("<< {:?}", s); - s = s.trim(); if !s.is_empty() { let mut buffer_queue = BufferQueue::new(); let tendril = FromIterator::from_iter(s.chars()); @@ -503,7 +502,7 @@ mod tests { } #[test] - fn test_lone_whitespace() { + fn test_cut_out_stanza() { let mut c = XMPPCodec::new(); let mut b = BytesMut::with_capacity(1024); b.put(r""); @@ -514,10 +513,11 @@ mod tests { }); b.clear(); - b.put(r" "); + b.put(r"Foo"); let r = c.decode(&mut b); assert!(match r { - Ok(None) => true, + Ok(Some(Packet::Stanza(_))) => true, _ => false, }); }