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 bare schemes being parsed as links(#71) #72

Merged
merged 4 commits into from
May 15, 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
8 changes: 6 additions & 2 deletions src/parser/link_url/parse_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {
.saturating_add(authority.len())
.saturating_add(host.len())
.saturating_add(path.len());
if ihier_len == 3 {
return Err(nom::Err::Error(CustomError::InvalidLink));
}
// compute length of authority + host + path
let mut len = scheme
.len()
Expand Down Expand Up @@ -417,8 +420,9 @@ fn parse_generic(input: &str) -> IResult<&str, LinkDestination, CustomError<&str
if !is_allowed_generic_scheme(scheme) {
return Err(nom::Err::Error(CustomError::InvalidLink));
}
let (input, rest) = take_while(is_not_white_space)(input)?;
let len = scheme.len().saturating_add(rest.len());
let (input, _colon) = char(':')(input)?;
let (input, rest) = take_while1(is_not_white_space)(input)?;
let len = scheme.len().saturating_add(rest.len()).saturating_add(1);
if let Some(target) = i.get(0..len) {
return Ok((
input,
Expand Down
11 changes: 11 additions & 0 deletions tests/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ fn basic_parsing() {
}
}

#[test]
fn bare_scheme_no_parse() {
// bare scheme shouldn't be linkified
let bare = vec!["tel", "tel:", "bitcoin:", "mailto", "https://", "http://"];

for input in bare {
let result = LinkDestination::parse(input);
assert!(result.is_err());
}
}

#[test]
fn invalid_domains() {
let test_cases = vec![";?:/hi", "##://thing"];
Expand Down
Loading