Skip to content

Commit

Permalink
feat(filter): Filter subdomains of localhost as well (#3608)
Browse files Browse the repository at this point in the history
The localhost inbound filter should also filter subdomains of localhost
such as `foo.localhost`.

Closes: [#70841](getsentry/sentry#70841)
  • Loading branch information
Dav1dde authored May 16, 2024
1 parent 1603360 commit 97db3de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand Down
30 changes: 24 additions & 6 deletions relay-filter/src/localhost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@ fn matches<F: Filterable>(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<F: Filterable>(item: &F, config: &FilterConfig) -> Result<(), FilterStatKey> {
if !config.is_enabled {
Expand Down Expand Up @@ -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}'");
Expand All @@ -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!(
Expand Down

0 comments on commit 97db3de

Please sign in to comment.