Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix de-escaping too much in query parser #2427

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions query-grammar/src/query_grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@
)(inp)
}

fn interpret_escape(source: &str) -> String {
let mut res = String::with_capacity(source.len());
let mut in_escape = false;
let require_escape = |c: char| {
c.is_whitespace()
|| [
'-', '^', '`', ':', '{', '}', '"', '\'', '[', ']', '(', ')', '\\',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would probably make sense to move this to a const variable and give it a name

]
.contains(&c)
};

for c in source.chars() {
if in_escape {
if !require_escape(c) {
// we re-add the escape sequence
res.push('\\');
}
res.push(c);
in_escape = false;
} else {
if c == '\\' {
in_escape = true;
} else {
res.push(c);
}
}

Check warning on line 70 in query-grammar/src/query_grammar.rs

View workflow job for this annotation

GitHub Actions / clippy

this `else { if .. }` block can be collapsed

warning: this `else { if .. }` block can be collapsed --> query-grammar/src/query_grammar.rs:64:16 | 64 | } else { | ________________^ 65 | | if c == '\\' { 66 | | in_escape = true; 67 | | } else { 68 | | res.push(c); 69 | | } 70 | | } | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if = note: `#[warn(clippy::collapsible_else_if)]` on by default help: collapse nested if block | 64 ~ } else if c == '\\' { 65 + in_escape = true; 66 + } else { 67 + res.push(c); 68 + } |
}
res
}

/// Consume a word outside of any context.
// TODO should support escape sequences
fn word(inp: &str) -> IResult<&str, Cow<str>> {
Expand All @@ -67,7 +97,7 @@
))),
|s| match s {
"OR" | "AND" | "NOT" | "IN" => Err(Error::new(inp, ErrorKind::Tag)),
s if s.contains('\\') => Ok(Cow::Owned(s.replace('\\', ""))),
s if s.contains('\\') => Ok(Cow::Owned(interpret_escape(s))),
s => Ok(Cow::Borrowed(s)),
},
)(inp)
Expand Down Expand Up @@ -106,7 +136,7 @@
});
}
if s.contains('\\') {
(Some(Cow::Owned(s.replace('\\', ""))), errors)
(Some(Cow::Owned(interpret_escape(s))), errors)
} else {
(Some(Cow::Borrowed(s)), errors)
}
Expand Down Expand Up @@ -1629,6 +1659,8 @@
r#"myfield:'hello\"happy\'tax'"#,
r#""myfield":'hello"happy'tax'"#,
);
// we don't process escape sequence for chars which don't require it
test_parse_query_to_ast_helper(r#"abc\*"#, r#"abc\*"#);
}

#[test]
Expand Down
Loading