Skip to content

Commit

Permalink
Merge pull request #64 from robinst/ip-require-4-parts
Browse files Browse the repository at this point in the history
Require IPv4 addresses to have 4 parts
  • Loading branch information
robinst committed Jun 24, 2023
2 parents 519de1b + 58a3d37 commit b15a1d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) fn find_authority_end(

let mut maybe_last_dot = None;
let mut last_dot = None;
let mut number_dots = 0;
let mut dot_allowed = false;
let mut hyphen_allowed = false;
let mut all_numeric = true;
Expand Down Expand Up @@ -69,7 +70,10 @@ pub(crate) fn find_authority_end(
// Same as above, except numeric
dot_allowed = true;
hyphen_allowed = true;
last_dot = maybe_last_dot;
if last_dot != maybe_last_dot {
last_dot = maybe_last_dot;
number_dots += 1;
}

if host_ended {
maybe_host = false;
Expand Down Expand Up @@ -172,15 +176,15 @@ pub(crate) fn find_authority_end(

if require_host {
if maybe_host {
// Can't have just a number without dots as the authority
if all_numeric && last_dot.is_none() && end != Some(0) {
return (None, None);
}

// If we have something that is not just numeric (not an IP address),
// check that the TLD looks reasonable. This is to avoid linking things like
// `abc@v1.1`.
if !all_numeric {
if all_numeric {
// For IPv4 addresses, require 4 numbers
if number_dots != 3 {
return (None, None);
}
} else {
// If we have something that is not just numeric (not an IP address),
// check that the TLD looks reasonable. This is to avoid linking things like
// `abc@v1.1`.
if let Some(last_dot) = last_dot {
if !valid_tld(&s[last_dot + 1..]) {
return (None, None);
Expand Down
6 changes: 6 additions & 0 deletions tests/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ fn domain_with_userinfo_and_port() {
#[test]
fn domain_ipv4() {
assert_linked("https://127.0.0.1/", "|https://127.0.0.1/|");
assert_linked("1.0.0.0", "|1.0.0.0|");
assert_linked("1.0.0.0/foo/bar", "|1.0.0.0/foo/bar|");
assert_not_linked("1.0 ");
assert_not_linked("1.0.0");
assert_not_linked("1.0.0.0.0");
assert_not_linked("1.0.0.");
}

#[test]
Expand Down

0 comments on commit b15a1d5

Please sign in to comment.