From bc27f6314983693dd174fcbffa34fffd8138ac26 Mon Sep 17 00:00:00 2001 From: Didier Wenzek Date: Mon, 4 Nov 2024 18:48:07 +0100 Subject: [PATCH] c8y http proxy wrongly assumes any hostname is from the same tenant When configured with hostnames without domains, c8y_api::http_proxy::maybe_tenant_url wrongly assumes any hostname, given without a domain, is related to the same tenant. Signed-off-by: Didier Wenzek --- crates/core/c8y_api/src/http_proxy.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/core/c8y_api/src/http_proxy.rs b/crates/core/c8y_api/src/http_proxy.rs index 7cfd1b1c72..cb7af1e088 100644 --- a/crates/core/c8y_api/src/http_proxy.rs +++ b/crates/core/c8y_api/src/http_proxy.rs @@ -123,13 +123,13 @@ impl C8yEndPoint { let url = Url::parse(url).ok()?; let url_host = url.domain()?; - let (_, host) = url_host.split_once('.').unwrap_or((url_host, "")); + let (_, host) = url_host.split_once('.').unwrap_or(("", url_host)); let (_, c8y_http_host) = tenant_http_host .split_once('.') - .unwrap_or((tenant_http_host, "")); + .unwrap_or(("", tenant_http_host)); let (_, c8y_mqtt_host) = tenant_mqtt_host .split_once('.') - .unwrap_or((tenant_mqtt_host, "")); + .unwrap_or(("", tenant_mqtt_host)); // The configured `c8y.http` setting may have a port value specified, // but the incoming URL is less likely to have any port specified. @@ -410,6 +410,13 @@ mod tests { assert_eq!(c8y.maybe_tenant_url(url), Some(url.parse().unwrap())); } + #[test] + fn url_is_not_my_tenant_with_hostname_without_commas() { + let c8y = C8yEndPoint::new("custom-domain", "non-custom-mqtt-domain", "test_device"); + let url = "http://unrelated-domain/path"; + assert!(c8y.maybe_tenant_url(url).is_none()); + } + #[ignore = "Until #2804 is fixed"] #[test] fn url_is_my_tenant_check_not_too_broad() {