Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filter): Filter subdomains of localhost as well #3608

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading