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

remove a-label only restriction on cli, add a-label query type #55

Merged
merged 1 commit into from
Mar 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions icann-rdap-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ enum QtypeArg {
/// Domain Lookup
Domain,

/// A-Label Domain Lookup
ALabel,

/// Entity Lookup
Entity,

Expand Down Expand Up @@ -520,6 +523,7 @@ fn query_type_from_cli(cli: &Cli) -> QueryType {
QtypeArg::V6Cidr => QueryType::IpV6Cidr(query_value),
QtypeArg::Autnum => QueryType::AsNumber(query_value),
QtypeArg::Domain => QueryType::Domain(query_value),
QtypeArg::ALabel => QueryType::ALable(query_value),
QtypeArg::Entity => QueryType::Entity(query_value),
QtypeArg::Ns => QueryType::Nameserver(query_value),
QtypeArg::EntityName => QueryType::EntityNameSearch(query_value),
Expand Down
19 changes: 19 additions & 0 deletions icann-rdap-cli/tests/integration/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::test_jig::TestJig;
#[case("foo.example", "foo.example")]
#[case("foo.example", "foo.example.")]
#[case("foo.example", "FOO.EXAMPLE")]
#[case("foó.example", "foó.example")] // unicode
#[tokio::test(flavor = "multi_thread")]
async fn GIVEN_domain_WHEN_query_THEN_success(#[case] db_domain: &str, #[case] q_domain: &str) {
// GIVEN
Expand Down Expand Up @@ -156,3 +157,21 @@ async fn GIVEN_url_WHEN_query_THEN_success() {
let assert = test_jig.cmd.assert();
assert.success();
}

#[tokio::test(flavor = "multi_thread")]
async fn GIVEN_idn_WHEN_query_a_label_THEN_success() {
// GIVEN
let mut test_jig = TestJig::new().await;
let mut tx = test_jig.mem.new_tx().await.expect("new transaction");
tx.add_domain(&Domain::basic().ldh_name("xn--caf-dma.example").build())
.await
.expect("add domain in tx");
tx.commit().await.expect("tx commit");

// WHEN
test_jig.cmd.arg("-t").arg("a-label").arg("café.example");

// THEN
let assert = test_jig.cmd.assert();
assert.success();
}
1 change: 1 addition & 0 deletions icann-rdap-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildstructor.workspace = true
cidr-utils.workspace = true
chrono.workspace = true
const_format.workspace = true
idna.workspace = true
lazy_static.workspace = true
pct-str.workspace = true
regex.workspace = true
Expand Down
19 changes: 18 additions & 1 deletion icann-rdap-client/src/query/qtype.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{net::IpAddr, str::FromStr};

use cidr_utils::cidr::IpInet;
use icann_rdap_common::check::string::StringCheck;
use lazy_static::lazy_static;
use pct_str::{PctString, URIReserved};
use regex::Regex;
Expand Down Expand Up @@ -29,6 +30,9 @@ pub enum QueryType {
#[strum(serialize = "Domain Lookup")]
Domain(String),

#[strum(serialize = "A-Label Domain Lookup")]
ALable(String),

#[strum(serialize = "Entity Lookup")]
Entity(String),

Expand Down Expand Up @@ -89,6 +93,7 @@ impl QueryType {
"{base_url}/domain/{}",
PctString::encode(value.chars(), URIReserved)
)),
QueryType::ALable(value) => a_label_query(value, base_url),
QueryType::Entity(value) => Ok(format!(
"{base_url}/entity/{}",
PctString::encode(value.chars(), URIReserved)
Expand Down Expand Up @@ -116,6 +121,14 @@ impl QueryType {
}
}

fn a_label_query(value: &str, base_url: &str) -> Result<String, RdapClientError> {
let domain = idna::domain_to_ascii(value).map_err(|_| RdapClientError::InvalidQueryValue)?;
Ok(format!(
"{base_url}/domain/{}",
PctString::encode(domain.chars(), URIReserved),
))
}

fn ip_cidr_query(value: &str, base_url: &str) -> Result<String, RdapClientError> {
let values = value
.split_once('/')
Expand Down Expand Up @@ -167,7 +180,7 @@ impl FromStr for QueryType {
}

// if it looks like a domain name
if is_ldh_domain(s) {
if is_domain_name(s) {
if is_nameserver(s) {
return Ok(QueryType::Nameserver(s.to_owned()));
} else {
Expand All @@ -193,6 +206,10 @@ fn is_ldh_domain(text: &str) -> bool {
LDH_DOMAIN_RE.is_match(text)
}

fn is_domain_name(text: &str) -> bool {
text.contains('.') && text.is_unicode_domain_name()
}

fn is_nameserver(text: &str) -> bool {
lazy_static! {
static ref NS_RE: Regex =
Expand Down
9 changes: 8 additions & 1 deletion icann-rdap-common/src/check/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ impl<T: ToString> StringCheck for T {

fn is_unicode_domain_name(&self) -> bool {
let s = self.to_string();
s == "." || !s.is_whitespace_or_empty()
s == "."
|| (!s.is_empty()
&& s.split_terminator('.').all(|s| {
s.chars()
.all(|c| c == '-' || (!c.is_ascii_punctuation() && !c.is_whitespace()))
}))
}
}

Expand Down Expand Up @@ -202,6 +207,8 @@ mod tests {
#[case(".", true)]
#[case("foo.bar", true)]
#[case("foo.bar.", true)]
#[case("fo_o.bar.", false)]
#[case("fo o.bar.", false)]
fn GIVEN_string_WHEN_is_unicode_domain_name_THEN_correct_result(
#[case] test_string: &str,
#[case] expected: bool,
Expand Down
Loading