diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab7e54cb0..df1c3c3445 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ **Features**: - Apple trace-based sampling rules to standalone spans. ([#3476](https://github.com/getsentry/relay/pull/3476)) +- Localhost inbound filter filters sudomains of localhost. ([#3608](https://github.com/getsentry/relay/pull/3608)) **Internal**: diff --git a/relay-filter/src/localhost.rs b/relay-filter/src/localhost.rs index a8d3dccb37..9aa9727bf7 100644 --- a/relay-filter/src/localhost.rs +++ b/relay-filter/src/localhost.rs @@ -16,21 +16,27 @@ fn matches(item: &F) -> bool { } if let Some(url) = item.url() { + if url.scheme() == "file" { + return true; + } + if let Some(host) = url.host_str() { for &local_domain in LOCAL_DOMAINS { - if host == local_domain { + if host_matches_or_is_subdomain_of(host, local_domain) { return true; } } } - if url.scheme() == "file" { - return true; - } } false } +fn host_matches_or_is_subdomain_of(host: &str, domain: &str) -> bool { + host.strip_suffix(domain) + .map_or(false, |s| s.is_empty() || s.ends_with('.')) +} + /// Filters events originating from the local host. pub fn should_filter(item: &F, config: &FilterConfig) -> Result<(), FilterStatKey> { if !config.is_enabled { @@ -133,7 +139,12 @@ mod tests { #[test] fn test_filter_local_domains() { - for domain in &["127.0.0.1", "localhost"] { + for domain in &[ + "127.0.0.1", + "localhost", + "foo.localhost", + "foo.bar.baz.localhost", + ] { let event = get_event_with_domain(domain); let filter_result = should_filter(&event, &FilterConfig { is_enabled: true }); assert_ne!(filter_result, Ok(()), "Failed to filter domain '{domain}'"); @@ -142,7 +153,14 @@ mod tests { #[test] fn test_dont_filter_non_local_domains() { - for domain in &["my.dom.com", "123.123.123.44"] { + for domain in &[ + "my.dom.com", + "123.123.123.44", + "localhost.com", + "foolocalhost", + "localhostbar", + "alocalhostgoesintoabar", + ] { let event = get_event_with_domain(domain); let filter_result = should_filter(&event, &FilterConfig { is_enabled: true }); assert_eq!(