From 17960780f358ba0896988c74bf511a27d6f02898 Mon Sep 17 00:00:00 2001 From: Farooq Karimi Zadeh Date: Wed, 15 May 2024 17:19:52 +0330 Subject: [PATCH 1/4] add new testcases --- tests/links.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/links.rs b/tests/links.rs index a743ae5..48ebbdb 100644 --- a/tests/links.rs +++ b/tests/links.rs @@ -45,6 +45,25 @@ fn basic_parsing() { assert_eq!(rest.len(), 0); assert_eq!(input, &link_destination.target); } + + } + +#[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] From ea5784b5746834be4b3822866418e30c9ed5da0d Mon Sep 17 00:00:00 2001 From: Farooq Karimi Zadeh Date: Wed, 15 May 2024 17:26:05 +0330 Subject: [PATCH 2/4] fix #71 --- src/parser/link_url/parse_link.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/parser/link_url/parse_link.rs b/src/parser/link_url/parse_link.rs index 9ff1d93..593f9bf 100644 --- a/src/parser/link_url/parse_link.rs +++ b/src/parser/link_url/parse_link.rs @@ -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() @@ -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, From b02cb593dd6134b1e24a5a43cecfb1a2879cfbd7 Mon Sep 17 00:00:00 2001 From: Farooq Karimi Zadeh Date: Wed, 15 May 2024 17:27:05 +0330 Subject: [PATCH 3/4] fix formatting --- tests/links.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/links.rs b/tests/links.rs index 48ebbdb..0f8f94a 100644 --- a/tests/links.rs +++ b/tests/links.rs @@ -45,20 +45,12 @@ fn basic_parsing() { assert_eq!(rest.len(), 0); assert_eq!(input, &link_destination.target); } - - } +} #[test] fn bare_scheme_no_parse() { // bare scheme shouldn't be linkified - let bare = vec![ - "tel", - "tel:", - "bitcoin:", - "mailto", - "https://", - "http://" - ]; + let bare = vec!["tel", "tel:", "bitcoin:", "mailto", "https://", "http://"]; for input in bare { let result = LinkDestination::parse(input); From b7a0367600e1770cda5aeb485baa60a805918d26 Mon Sep 17 00:00:00 2001 From: Farooq Karimi Zadeh Date: Wed, 15 May 2024 17:35:27 +0330 Subject: [PATCH 4/4] fix warning --- src/parser/link_url/parse_link.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/link_url/parse_link.rs b/src/parser/link_url/parse_link.rs index 593f9bf..f4e288b 100644 --- a/src/parser/link_url/parse_link.rs +++ b/src/parser/link_url/parse_link.rs @@ -420,7 +420,7 @@ 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, colon) = char(':')(input)?; + 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) {