use gitpoulpi_config::{ApiFamily, Integration, IntegrationKind};
use url::{Host, Url};
use crate::{Error, Result};
fn parse(url: &str) -> Option<Url> {
let url = url.trim();
let url = if url.contains("://") {
Url::parse(url).ok()?
} else {
Url::parse(&format!("https://{url}")).ok()?
};
matches!(url.scheme(), "http" | "https").then_some(())?;
url.host_str()?;
Some(url)
}
fn origin_string(url: &Url) -> String {
let mut out = format!("{}://{}", url.scheme(), url.host_str().unwrap_or(""));
if let Some(port) = url.port() {
out.push_str(&format!(":{port}"));
}
out
}
pub fn normalize(url: &str) -> Option<String> {
let url = parse(url)?;
let mut out = origin_string(&url);
let path = url.path().trim_end_matches('/');
if !path.is_empty() {
out.push_str(path);
}
Some(out)
}
pub fn origin_of(url: &str) -> Option<String> {
Some(origin_string(&parse(url)?))
}
pub fn host_of(url: &str) -> Option<String> {
parse(url)?.host_str().map(str::to_string)
}
pub fn integration_origin(integration: &Integration) -> Result<String> {
let raw = match integration.url.trim() {
"" => integration.kind.default_url().unwrap_or("").to_string(),
url => url.to_string(),
};
let url = if integration.kind == IntegrationKind::AzureDevops {
gitpoulpi_config::normalize_azure_url(&raw)
} else {
raw
};
normalize(&url).ok_or_else(|| Error::BadUrl(integration.url.clone()))
}
pub fn is_plain_http_remote(url: &str) -> bool {
parse(url).is_some_and(|u| u.scheme() == "http" && !host_is_loopback(&u))
}
pub fn is_loopback(url: &str) -> bool {
parse(url).is_some_and(|u| host_is_loopback(&u))
}
fn host_is_loopback(url: &Url) -> bool {
match url.host() {
Some(Host::Domain(d)) => d == "localhost" || d.ends_with(".localhost"),
Some(Host::Ipv4(ip)) => ip.is_loopback(),
Some(Host::Ipv6(ip)) => ip.is_loopback(),
None => false,
}
}
fn dot_com_api_host(kind: IntegrationKind, host: &str) -> Option<&'static str> {
let (domain, api): (&str, &'static str) = match kind.family() {
ApiFamily::Github => ("github.com", "api.github.com"),
ApiFamily::Bitbucket if kind == IntegrationKind::Bitbucket => {
("bitbucket.org", "api.bitbucket.org")
}
ApiFamily::Trello => ("trello.com", "api.trello.com"),
ApiFamily::Linear => ("linear.app", "api.linear.app"),
_ => return None,
};
(host == domain || host == format!("www.{domain}") || host == format!("api.{domain}"))
.then_some(api)
}
pub fn api_origins(kind: IntegrationKind, recorded_origin: &str) -> Vec<String> {
let mut out = Vec::new();
let Some(own) = origin_of(recorded_origin) else {
return out;
};
if let Some(host) = host_of(recorded_origin) {
if let Some(api) = dot_com_api_host(kind, &host) {
out.push(format!("https://{api}"));
}
}
out.push(own);
out.dedup();
out
}
pub fn is_public_saas_host(host: &str) -> bool {
let host = host.to_ascii_lowercase();
const EXACT: &[&str] = &[
"github.com",
"api.github.com",
"gitlab.com",
"codeberg.org",
"bitbucket.org",
"api.bitbucket.org",
"dev.azure.com",
"api.trello.com",
"trello.com",
"api.linear.app",
"linear.app",
];
EXACT.contains(&host.as_str())
|| host.ends_with(".atlassian.net")
|| host.ends_with(".youtrack.cloud")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spellings_of_one_server_compare_equal_and_lookalikes_do_not() {
let same = [
("https://GitLab.Example.com/", "https://gitlab.example.com"),
(
"https://gitlab.example.com:443",
"https://gitlab.example.com",
),
("http://gitlab.example.com:80/", "http://gitlab.example.com"),
(
"https://user:pw@gitlab.example.com/sub/?q=1#f",
"https://gitlab.example.com/sub",
),
("gitlab.example.com", "https://gitlab.example.com"),
(
"https://git.example.com:8443/a/b/",
"https://git.example.com:8443/a/b",
),
("https://BÜCHER.example", "https://xn--bcher-kva.example"),
];
for (input, want) in same {
assert_eq!(normalize(input).as_deref(), Some(want), "{input}");
}
let a = normalize("https://gitlab.example.com").unwrap();
for other in [
"https://gitlab.example.com.evil.test",
"https://gitlab-example.com",
"http://gitlab.example.com",
"https://gitlab.example.com:8443",
"https://gitlab.example.com/other",
] {
assert_ne!(normalize(other).unwrap(), a, "{other}");
}
assert_eq!(normalize("ftp://x.example"), None);
assert_eq!(normalize(""), None);
}
#[test]
fn loopback_and_plain_http() {
assert!(is_loopback("http://localhost:3000"));
assert!(is_loopback("http://127.0.0.1:8080"));
assert!(is_loopback("http://[::1]:11434"));
assert!(!is_loopback("http://192.168.1.4"));
assert!(!is_plain_http_remote("http://localhost:11434"));
assert!(is_plain_http_remote("http://nas.local:3000"));
assert!(!is_plain_http_remote("https://nas.local"));
}
#[test]
fn saas_kinds_may_reach_their_api_host_and_nothing_else() {
let gh = api_origins(IntegrationKind::Github, "https://github.com");
assert!(gh.contains(&"https://api.github.com".to_string()));
assert!(gh.contains(&"https://github.com".to_string()));
let ghe = api_origins(
IntegrationKind::GithubEnterprise,
"https://ghe.corp.example",
);
assert_eq!(ghe, vec!["https://ghe.corp.example".to_string()]);
let gl = api_origins(
IntegrationKind::GitlabSelfManaged,
"https://git.corp.example/sub",
);
assert_eq!(gl, vec!["https://git.corp.example".to_string()]);
assert!(api_origins(IntegrationKind::Trello, "https://trello.com")
.contains(&"https://api.trello.com".to_string()));
}
#[test]
fn public_saas_hosts() {
assert!(is_public_saas_host("GitHub.com"));
assert!(is_public_saas_host("acme.atlassian.net"));
assert!(!is_public_saas_host("git.corp.example"));
assert!(!is_public_saas_host("evilgithub.com"));
}
}