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 #67: Mistaking UN*X absolute paths as bot commands #68

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 15 additions & 12 deletions src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
///! nom parsers for text elements

Check warning on line 1 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

this is an outer doc comment and does not apply to the parent module or crate

warning: this is an outer doc comment and does not apply to the parent module or crate --> src/parser/parse_from_text/text_elements.rs:1:1 | 1 | ///! nom parsers for text elements | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_doc_comments = note: `#[warn(clippy::suspicious_doc_comments)]` on by default help: use an inner doc comment to document the parent module or crate | 1 | //! nom parsers for text elements |

Check warning on line 1 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

this is an outer doc comment and does not apply to the parent module or crate

warning: this is an outer doc comment and does not apply to the parent module or crate --> src/parser/parse_from_text/text_elements.rs:1:1 | 1 | ///! nom parsers for text elements | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_doc_comments = note: `#[warn(clippy::suspicious_doc_comments)]` on by default help: use an inner doc comment to document the parent module or crate | 1 | //! nom parsers for text elements |

Check warning on line 1 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

this is an outer doc comment and does not apply to the parent module or crate

warning: this is an outer doc comment and does not apply to the parent module or crate --> src/parser/parse_from_text/text_elements.rs:1:1 | 1 | ///! nom parsers for text elements | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_doc_comments = note: `#[warn(clippy::suspicious_doc_comments)]` on by default help: use an inner doc comment to document the parent module or crate | 1 | //! nom parsers for text elements |
use crate::parser::link_url::LinkDestination;

use super::hashtag_content_char_ranges::hashtag_content_char;
use super::Element;
use nom::{
bytes::{
complete::{tag, take, take_while, take_while1},
streaming::take_till1,
},
character,
character::complete::char,
combinator::{peek, recognize, verify},
sequence::tuple,
AsChar, IResult, Offset, Slice,
};

use super::base_parsers::CustomError;
use super::hashtag_content_char_ranges::hashtag_content_char;
use super::Element;
use crate::parser::link_url::LinkDestination;

fn linebreak(input: &str) -> IResult<&str, char, CustomError<&str>> {
char('\n')(input)
}

fn hashtag(input: &str) -> IResult<&str, Element, CustomError<&str>> {
let (input, content) = recognize(tuple((
character::complete::char('#'),
take_while1(hashtag_content_char),
)))(input)?;
let (input, content) = recognize(tuple((char('#'), take_while1(hashtag_content_char))))(input)?;

Ok((input, Element::Tag(content)))
}
Expand Down Expand Up @@ -230,7 +228,7 @@
*/
fn is_allowed_bot_cmd_suggestion_char(char: char) -> bool {
match char {
'@' | '\\' | '_' | '/' | '.' | '-' => true,
'@' | '\\' | '_' | '.' | '-' | '/' => true,
_ => char.is_alphanum(),
}
}
Expand All @@ -248,8 +246,11 @@
s.len() < 256
}),
)))(input)?;

Ok((input, Element::BotCommandSuggestion(content)))
if content.slice(1..).contains('/') {
Ok((input, Element::Text(content)))
} else {
Ok((input, Element::BotCommandSuggestion(content)))
}
}

pub(crate) fn parse_text_element(
Expand All @@ -269,7 +270,9 @@
if prev_char == Some(' ') || prev_char.is_none() {
bot_command_suggestion(input)
} else {
Err(nom::Err::Error(CustomError::PrecedingWhitespaceMissing))
Err(nom::Err::Error(
CustomError::<&str>::PrecedingWhitespaceMissing,
))
}
} {
Ok((i, elm))
Expand Down
6 changes: 6 additions & 0 deletions tests/text_to_ast/text_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ fn command_suggestions() {
);
}

#[test]
fn unix_abs_path_is_not_command() {
let input = "/etc/nginx";
assert_eq!(parse_only_text(input), vec![Text("/etc/nginx")]);
}

#[test]
fn invalid_command_suggestions() {
let input = "/1\n /hello world";
Expand Down
Loading