Skip to content

Commit

Permalink
webapp: Extract domain name for ssltest link
Browse files Browse the repository at this point in the history
Difference from python impl is better handling of invalid urls.
  • Loading branch information
AMDmi3 committed Dec 24, 2024
1 parent acae54d commit dc1f6f3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
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.

1 change: 1 addition & 0 deletions repology-webapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tracing = "0.1.41"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.19"
ttf-parser = "0.25.1"
url = "2.5.4"
url-escape = "0.1.1"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions repology-webapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod result;
mod state;
mod static_files;
mod template_context;
mod template_funcs;
mod url_for;
mod views;
mod xmlwriter;
Expand Down
70 changes: 70 additions & 0 deletions repology-webapp/src/template_funcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-FileCopyrightText: Copyright 2024 Dmitry Marakasov <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later

use url::Url;

/// Given an url, try to extract domain for Qualys SSL Server Test
///
/// Qualys requires domain (not an IP address) and does not allow
/// custom ports, so only return host part if
pub fn extract_domain_for_ssltest(url: &str) -> Option<String> {
let url = Url::parse(url).ok()?;
if url.port_or_known_default().is_none_or(|port| port == 443) {
url.domain().map(|domain| domain.into())
} else {
None
}
}

#[cfg(test)]
#[coverage(off)]
mod tests {
mod test_extract_domain_for_ssltest {
use super::super::*;

#[test]
fn test_basic() {
assert_eq!(
extract_domain_for_ssltest("https://example.com/").as_deref(),
Some("example.com")
);
}

#[test]
fn test_default_port() {
assert_eq!(
extract_domain_for_ssltest("https://example.com:443/").as_deref(),
Some("example.com")
);
}

#[test]
fn test_custom_port() {
assert_eq!(extract_domain_for_ssltest("https://example.com:444/"), None);
}

#[test]
fn test_custom_schema() {
assert_eq!(
extract_domain_for_ssltest("git+https://example.com/").as_deref(),
Some("example.com")
);
}

#[test]
fn test_not_https() {
assert_eq!(extract_domain_for_ssltest("http://example.com/"), None);
}

#[test]
fn test_invalid_urls() {
assert_eq!(extract_domain_for_ssltest(""), None);
assert_eq!(extract_domain_for_ssltest("!&?"), None);
assert_eq!(extract_domain_for_ssltest(" "), None);
assert_eq!(extract_domain_for_ssltest("..."), None);
assert_eq!(extract_domain_for_ssltest("\n"), None);
assert_eq!(extract_domain_for_ssltest("example.com"), None);
assert_eq!(extract_domain_for_ssltest("file:///example.com"), None);
}
}
}
6 changes: 4 additions & 2 deletions repology-webapp/templates/_macros/links.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
{%- if code == -205 -%}
(<a href="https://bb6-ie.blogspot.com/2017/04/do-not-put-ipv4-mapped-ipv6-addresses.html" rel="nofollow">related reading</a>)
{%- endif -%}
{%- if code >= -599 && code <= -500 -%}{# TODO: we only need netloc here #}
(<a href="https://www.ssllabs.com/ssltest/analyze.html?d={{ url }}" rel="nofollow">check on SSL Labs</a>)
{%- if code >= -599 && code <= -500 -%}
{%- if let Some(domain) = crate::template_funcs::extract_domain_for_ssltest(url) -%}
(<a href="https://www.ssllabs.com/ssltest/analyze.html?d={{ domain }}" rel="nofollow">check on SSL Labs</a>)
{%- endif -%}
{%- endif -%}
{%- endmacro -%}

Expand Down
1 change: 0 additions & 1 deletion repology-webapp/tests/integration_tests/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ async fn test_ipv6_redirect(pool: PgPool) {
);
}

#[ignore]
#[sqlx::test(migrator = "repology_common::MIGRATOR", fixtures("link_data"))]
async fn test_ssl_failure(pool: PgPool) {
check_response!(
Expand Down

0 comments on commit dc1f6f3

Please sign in to comment.