Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- allow country TLDs in scheme-less links

## 0.14.0 - Bug fixes and scheme-less links

- Parse scheme-less links for some TLDs
Expand Down
1 change: 1 addition & 0 deletions message_parser_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type LinkDestination = {
target: string;
hostname: null | string;
punycode: null | PunycodeWarning;
scheme: null | string;
};
export type ParsedElement =
| { t: "Text"; c: string }
Expand Down
19 changes: 19 additions & 0 deletions src/parser/link_url/allowed_tlds/country_tlds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// extracted from first table on https://de.wikipedia.org/wiki/Liste_länderspezifischer_Top-Level-Domains
pub const COUNTRY_TLDS: [&str; 254] = [
"ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax",
"az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bl", "bm", "bn", "bo", "bq", "br",
"bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm",
"cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec",
"ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd",
"ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy",
"hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it",
"je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la",
"lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mf", "mg",
"mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my",
"mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe",
"pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs",
"ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so",
"sr", "ss", "st", "su", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl",
"tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va",
"vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zr", "zw",
];
36 changes: 36 additions & 0 deletions src/parser/link_url/allowed_tlds/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod country_tlds;

const ALLOWED_TOP_LEVEL_DOMAINS: &[&str] = &[
// originals from RFC920 + net
"com", "org", "net", "edu", "gov", "mil", // for deltachat
"chat",
];

pub fn check_if_tld_is_allowed(tld: &str) -> bool {
if ALLOWED_TOP_LEVEL_DOMAINS.iter().any(|item| *item == tld) {
true
} else {
country_tlds::COUNTRY_TLDS.binary_search(&tld).is_ok()
}
}

#[cfg(test)]
mod test {
use crate::parser::link_url::allowed_tlds::check_if_tld_is_allowed;

#[test]
fn test_check_tld() {
assert!(check_if_tld_is_allowed("chat"));
assert!(check_if_tld_is_allowed("com"));

assert!(check_if_tld_is_allowed("de"));
assert!(check_if_tld_is_allowed("at"));
assert!(check_if_tld_is_allowed("uk"));
assert!(check_if_tld_is_allowed("fr"));
}

#[test]
fn test_check_tld_not_allowed() {
assert!(!check_if_tld_is_allowed("doesnotexist"));
}
}
1 change: 1 addition & 0 deletions src/parser/link_url/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod allowed_tlds;
mod ip;
mod parenthesis_counter;
mod parse_link;
Expand Down
25 changes: 13 additions & 12 deletions src/parser/link_url/parse_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::parser::{
};

use super::{
allowed_tlds::check_if_tld_is_allowed,
parenthesis_counter::count_chars_in_complete_parenthesis,
punycode_warning::get_puny_code_warning,
};
Expand All @@ -46,14 +47,6 @@ fn is_allowed_generic_scheme(scheme: &str) -> bool {
)
}

const ALLOWED_TOP_LEVEL_DOMAINS: &[&str] = &[
// originals from RFC920 + net
".com", ".org", ".net", ".edu", ".gov", ".mil",
// for deltachat
".chat",
// !todo country codes here next
];

// These ranges have been extracted from RFC3987, Page 8.
const UCSCHAR_RANGES: [RangeInclusive<u32>; 17] = [
0xa0..=0xd7ff,
Expand Down Expand Up @@ -294,10 +287,18 @@ fn parse_iri(input: &str) -> IResult<&str, LinkDestination, CustomError<&str>> {

// now with host, if we dont have a scheme we need to check it for TLD
if scheme.is_empty() {
ALLOWED_TOP_LEVEL_DOMAINS
.iter()
.find(|&&tld| host.ends_with(tld))
.ok_or(nom::Err::Failure(CustomError::<&str>::InvalidLink))?;
if !host.contains('.') {
return Err(nom::Err::Failure(CustomError::<&str>::InvalidLink));
}

let tld = host
.split('.')
.last()
.ok_or(nom::Err::Failure(CustomError::<&str>::InvalidLinkNoTLD))?;

if !check_if_tld_is_allowed(tld) {
return Err(nom::Err::Failure(CustomError::<&str>::InvalidLink));
}
}

let (input, path) = opt(alt((
Expand Down
1 change: 1 addition & 0 deletions src/parser/parse_from_text/base_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum CustomError<I> {
Nom(I, ErrorKind),
InvalidEmail,
InvalidLink,
InvalidLinkNoTLD,
UnexpectedContent,
PrecedingWhitespaceMissing,
OptionIsUnexpectedNone,
Expand Down
Loading