Skip to content

Commit b6150ec

Browse files
New state InsideAttribute for StanzaFilter, more tests
1 parent efadaf3 commit b6150ec

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/stanzafilter.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ enum StanzaState {
99
StanzaFirstChar,
1010
InsideTagFirstChar,
1111
InsideTag,
12+
InsideAttribute(u8),
1213
BetweenTags,
1314
ExclamationTag(usize),
1415
InsideCDATA,
@@ -71,18 +72,18 @@ impl StanzaFilter {
7172
}
7273
StanzaFirstChar => match b {
7374
b'/' => self.state = EndStream,
74-
b'!' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
75+
b'!' | b'>' | b'\'' | b'"' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
7576
b'?' => self.state = QuestionTag(self.cnt + 4), // 4 is length of b"xml "
7677
_ => self.state = InsideTag,
7778
},
7879
InsideTagFirstChar => match b {
7980
b'/' => self.tag_cnt -= 2,
8081
b'!' => self.state = ExclamationTag(self.cnt + 7), // 7 is length of b"[CDATA["
81-
b'?' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
82+
b'?' | b'>' | b'\'' | b'"' => bail!("illegal stanza: {}", to_str(&self.buf[..(self.cnt + 1)])),
8283
_ => self.state = InsideTag,
8384
},
84-
InsideTag => {
85-
if b == b'>' {
85+
InsideTag => match b {
86+
b'>' => {
8687
if self.buf[self.cnt - 1] == b'/' {
8788
// state can't be InsideTag unless we are on at least the second character, so can't go out of range
8889
// self-closing tag
@@ -97,6 +98,13 @@ impl StanzaFilter {
9798
}
9899
self.state = BetweenTags;
99100
}
101+
b'\'' | b'"' => self.state = InsideAttribute(b),
102+
_ => {}
103+
},
104+
InsideAttribute(end) => {
105+
if b == end {
106+
self.state = InsideTag;
107+
}
100108
}
101109
QuestionTag(idx) => {
102110
if idx == self.cnt {
@@ -206,13 +214,19 @@ mod tests {
206214
async fn process_next_byte() -> std::result::Result<(), anyhow::Error> {
207215
let mut filter = StanzaFilter::new(262_144);
208216

217+
//todo: <x a='/>'>This is going to be fun.</x>
209218
assert_eq!(
210219
StanzaReader(Cursor::new(
211220
br###"
212221
<?xml version='1.0'?>
213222
<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'>
214223
<a/><b>inside b before c<c>inside c</c></b></stream:stream>
215224
<q>bla<![CDATA[<this>is</not><xml/>]]>bloo</q>
225+
<x><![CDATA[ lol</x> ]]></x>
226+
<z><x><![CDATA[ lol</x> ]]></x></z>
227+
<a a='![CDATA['/>
228+
<x a='/>'>This is going to be fun.</x>
229+
<z><x a='/>'>This is going to be fun.</x></y>
216230
<d></d><e><![CDATA[what]>]]]]></e></stream:stream>
217231
"###,
218232
))
@@ -225,6 +239,11 @@ mod tests {
225239
"<b>inside b before c<c>inside c</c></b>",
226240
"</stream:stream>",
227241
"<q>bla<![CDATA[<this>is</not><xml/>]]>bloo</q>",
242+
"<x><![CDATA[ lol</x> ]]></x>",
243+
"<z><x><![CDATA[ lol</x> ]]></x></z>",
244+
"<a a='![CDATA['/>",
245+
"<x a='/>'>This is going to be fun.</x>",
246+
"<z><x a='/>'>This is going to be fun.</x></y>",
228247
"<d></d>",
229248
"<e><![CDATA[what]>]]]]></e>",
230249
"</stream:stream>",

0 commit comments

Comments
 (0)