Skip to content

Commit

Permalink
New state InsideAttribute for StanzaFilter, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moparisthebest committed Apr 16, 2021
1 parent efadaf3 commit b6150ec
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/stanzafilter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum StanzaState {
StanzaFirstChar,
InsideTagFirstChar,
InsideTag,
InsideAttribute(u8),
BetweenTags,
ExclamationTag(usize),
InsideCDATA,
Expand Down Expand Up @@ -71,18 +72,18 @@ impl StanzaFilter {
}
StanzaFirstChar => match b {
b'/' => self.state = EndStream,
b'!' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
b'!' | b'>' | b'\'' | b'"' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
b'?' => self.state = QuestionTag(self.cnt + 4), // 4 is length of b"xml "
_ => self.state = InsideTag,
},
InsideTagFirstChar => match b {
b'/' => self.tag_cnt -= 2,
b'!' => self.state = ExclamationTag(self.cnt + 7), // 7 is length of b"[CDATA["
b'?' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
b'?' | b'>' | b'\'' | b'"' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
_ => self.state = InsideTag,
},
InsideTag => {
if b == b'>' {
InsideTag => match b {
b'>' => {
if self.buf[self.cnt - 1] == b'/' {
// state can't be InsideTag unless we are on at least the second character, so can't go out of range
// self-closing tag
Expand All @@ -97,6 +98,13 @@ impl StanzaFilter {
}
self.state = BetweenTags;
}
b'\'' | b'"' => self.state = InsideAttribute(b),
_ => {}
},
InsideAttribute(end) => {
if b == end {
self.state = InsideTag;
}
}
QuestionTag(idx) => {
if idx == self.cnt {
Expand Down Expand Up @@ -206,13 +214,19 @@ mod tests {
async fn process_next_byte() -> std::result::Result<(), anyhow::Error> {
let mut filter = StanzaFilter::new(262_144);

//todo: <x a='/>'>This is going to be fun.</x>
assert_eq!(
StanzaReader(Cursor::new(
br###"
<?xml version='1.0'?>
<stream:stream xmlns='jabber:server' xmlns:stream='http://etherx.jabber.org/streams' xmlns:db='jabber:server:dialback' version='1.0' to='example.org' from='example.com' xml:lang='en'>
<a/><b>inside b before c<c>inside c</c></b></stream:stream>
<q>bla<![CDATA[<this>is</not><xml/>]]>bloo</q>
<x><![CDATA[ lol</x> ]]></x>
<z><x><![CDATA[ lol</x> ]]></x></z>
<a a='![CDATA['/>
<x a='/>'>This is going to be fun.</x>
<z><x a='/>'>This is going to be fun.</x></y>
<d></d><e><![CDATA[what]>]]]]></e></stream:stream>
"###,
))
Expand All @@ -225,6 +239,11 @@ mod tests {
"<b>inside b before c<c>inside c</c></b>",
"</stream:stream>",
"<q>bla<![CDATA[<this>is</not><xml/>]]>bloo</q>",
"<x><![CDATA[ lol</x> ]]></x>",
"<z><x><![CDATA[ lol</x> ]]></x></z>",
"<a a='![CDATA['/>",
"<x a='/>'>This is going to be fun.</x>",
"<z><x a='/>'>This is going to be fun.</x></y>",
"<d></d>",
"<e><![CDATA[what]>]]]]></e>",
"</stream:stream>",
Expand Down

0 comments on commit b6150ec

Please sign in to comment.