CVE-2022-31100: Reachable Assertion (CWE-617)
value: rulex is a new, portable, regular expression language. When parsing untrusted rulex expressions, rulex may crash, possibly enabling a Denial of Service attack. This happens when the expression contains a multi-byte UTF-8 code point in a string literal or after a backslash, because rulex tries to slice into the code point and panics as a result. This is a security concern for you, if your service parses untrusted rulex expressions (expressions provided by an untrusted user), and your service becomes unavailable when the thread running rulex panics. The crashes are fixed in version **0.4.3**. Affected users are advised to update to this version. The only known workaround for this issue is to assume that regular expression parsing will panic and to add logic to catch panics.
CWE description: The product contains an assert() or similar statement that can be triggered by an attacker, which leads to an application exit or other behavior that is more severe than necessary.
CVSS v3 base score: 6.5
Published: 2022-06-27T22:15Z
Repository: https://github.com/rulex-rs/rulex
Language: Rust
Files changed: rulex-lib/src/parse/parsers.rs, rulex-lib/src/parse/tokenize.rs, rulex-lib/tests/testcases/basics/6.txt, rulex-lib/tests/testcases/basics/7.txt
Commit date: 2022-06-18 22:20:58 +0200
Commit message: Fix panics due to slicing UTF8 strings incorrectly

Diff: diff --git a/rulex-lib/src/parse/parsers.rs b/rulex-lib/src/parse/parsers.rs
index 7d14d3d..116012c 100644
--- a/rulex-lib/src/parse/parsers.rs
+++ b/rulex-lib/src/parse/parsers.rs
@@ -592,30 +592,37 @@ fn parse_quoted_text(input: &str) -> Result<Cow<'_, str>, ParseErrorKind> {
             let mut s = strip_first_last(input);
             let mut buf = String::new();
 
             loop {
                 let mut chars = s.chars();
+                let char_len;
                 match chars.next() {
-                    Some('\\') => match chars.next() {
-                        Some('\\') => {
-                            buf.push('\\');
-                            s = &s[1..];
+                    Some('\\') => {
+                        char_len = 1;
+                        match chars.next() {
+                            Some('\\') => {
+                                buf.push('\\');
+                                s = &s[1..];
+                            }
+                            Some('"') => {
+                                buf.push('"');
+                                s = &s[1..];
+                            }
+                            _ => {
+                                return Err(ParseErrorKind::InvalidEscapeInStringAt(
+                                    input.len() - s.len(),
+                                ));
+                            }
                         }
-                        Some('"') => {
-                            buf.push('"');
-                            s = &s[1..];
-                        }
-                        _ => {
-                            return Err(ParseErrorKind::InvalidEscapeInStringAt(
-                                input.len() - s.len(),
-                            ));
-                        }
-                    },
-                    Some(c) => buf.push(c),
+                    }
+                    Some(c) => {
+                        char_len = c.len_utf8();
+                        buf.push(c)
+                    }
                     None => break,
                 }
-                s = &s[1..];
+                s = &s[char_len..];
             }
             Cow::Owned(buf)
         }
         _ => Cow::Borrowed(strip_first_last(input)),
     })
diff --git a/rulex-lib/src/parse/tokenize.rs b/rulex-lib/src/parse/tokenize.rs
index 36de633..ec1d2e6 100644
--- a/rulex-lib/src/parse/tokenize.rs
+++ b/rulex-lib/src/parse/tokenize.rs
@@ -159,12 +159,12 @@ pub(crate) fn tokenize(mut input: &str) -> Vec<(Token, Span)> {
                         } else if let Some(rest) = input.strip_prefix("\\k<") {
                             match rest.find('>') {
                                 Some(len) => (len + 4, Token::ErrorMsg(ParseErrorMsg::BackslashK)),
                                 _ => (2, Token::ErrorMsg(ParseErrorMsg::Backslash)),
                             }
-                        } else if input.len() >= 2 {
-                            (2, Token::ErrorMsg(ParseErrorMsg::Backslash))
+                        } else if let Some(next) = input.chars().nth(1) {
+                            (1 + next.len_utf8(), Token::ErrorMsg(ParseErrorMsg::Backslash))
                         } else {
                             (1, Token::Error)
                         }
                     };
                 };
diff --git a/rulex-lib/tests/testcases/basics/6.txt b/rulex-lib/tests/testcases/basics/6.txt
new file mode 100644
index 0000000..c7955bc
--- /dev/null
+++ b/rulex-lib/tests/testcases/basics/6.txt
@@ -0,0 +1,5 @@
+# unicode strings
+
+'ä Ŧ 🤦🏼‍♂️'
+-----
+\xE4 Ŧ \x{1F926}\x{1F3FC}\x{200D}\x{2642}\x{FE0F}
\ No newline at end of file
diff --git a/rulex-lib/tests/testcases/basics/7.txt b/rulex-lib/tests/testcases/basics/7.txt
new file mode 100644
index 0000000..8d7e7b8
--- /dev/null
+++ b/rulex-lib/tests/testcases/basics/7.txt
@@ -0,0 +1,7 @@
+#! expect=error
+# this mustn't panic when tokenizing
+
+C ؾ\ ؾ \u{ؾ} U+ؾ Uؾ' ؾ'
+-----
+ERROR: Backslash escapes are not supported
+SPAN: 42..44
\ No newline at end of file

Vulnerable code:                     Some('\\') => match chars.next() {
                        Some('\\') => {
                            buf.push('\\');
                            s = &s[1..];
                        Some('"') => {
                            buf.push('"');
                            s = &s[1..];
                        }
                        _ => {
                            return Err(ParseErrorKind::InvalidEscapeInStringAt(
                                input.len() - s.len(),
                            ));
                        }
                    },
                    Some(c) => buf.push(c),
                s = &s[1..];
                        } else if input.len() >= 2 {
                            (2, Token::ErrorMsg(ParseErrorMsg::Backslash))
Fixed code:                 let char_len;
                    Some('\\') => {
                        char_len = 1;
                        match chars.next() {
                            Some('\\') => {
                                buf.push('\\');
                                s = &s[1..];
                            }
                            Some('"') => {
                                buf.push('"');
                                s = &s[1..];
                            }
                            _ => {
                                return Err(ParseErrorKind::InvalidEscapeInStringAt(
                                    input.len() - s.len(),
                                ));
                            }
                    }
                    Some(c) => {
                        char_len = c.len_utf8();
                        buf.push(c)
                    }
                s = &s[char_len..];
                        } else if let Some(next) = input.chars().nth(1) {
                            (1 + next.len_utf8(), Token::ErrorMsg(ParseErrorMsg::Backslash))
# unicode strings

'ä Ŧ 🤦🏼‍♂️'
-----
\xE4 Ŧ \x{1F926}\x{1F3FC}\x{200D}\x{2642}\x{FE0F}
#! expect=error
# this mustn't panic when tokenizing

C ؾ\ ؾ \u{ؾ} U+ؾ Uؾ' ؾ'
-----
ERROR: Backslash escapes are not supported
SPAN: 42..44