Skip to content

Support allowUrls, denyUrls #2227

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

Merged
merged 23 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
47beb08
moved regex matcher into regex utils
martinhaintz Aug 12, 2024
65c2868
add allowUrls, denyUrls for web
martinhaintz Aug 12, 2024
92f02c3
add changelog entry for allowUrls and denyUrls
martinhaintz Aug 13, 2024
11a06c5
add conditional import for non web platforms
martinhaintz Aug 13, 2024
934c9d0
fix multiplatform build
martinhaintz Aug 13, 2024
24c48e9
fix wording in sentry options
martinhaintz Aug 13, 2024
e510129
Update dart/lib/src/utils/regex_utils.dart
martinhaintz Aug 13, 2024
5d62144
Update dart/lib/src/sentry_options.dart
martinhaintz Aug 13, 2024
f623e07
Update dart/lib/src/sentry_options.dart
martinhaintz Aug 13, 2024
40cadc9
add tests for isMatchingRegexPattern
martinhaintz Aug 13, 2024
2812a96
simplified allowUrls and denyUrls handling
martinhaintz Aug 13, 2024
c0ffc67
moved allowUrls and denyUrls from dart to flutter
martinhaintz Aug 13, 2024
5598f4c
Merge branch 'main' into feat/support-allow-urls-deny-urls
martinhaintz Aug 13, 2024
8523a6d
add event processor for html
martinhaintz Aug 13, 2024
a30a862
rephrased documentation and split up tests for web and mobile platform.
martinhaintz Aug 14, 2024
1ec7235
Merge branch 'main' into feat/support-allow-urls-deny-urls
martinhaintz Aug 14, 2024
160ddcc
add expected error
martinhaintz Aug 14, 2024
dc06ba9
Update scripts/publish_validation/bin/publish_validation.dart
martinhaintz Aug 19, 2024
e38e625
Update flutter/lib/src/event_processor/url_filter/html_url_filter_eve…
martinhaintz Aug 19, 2024
5e7ccc1
Update flutter/lib/src/event_processor/url_filter/web_url_filter_even…
martinhaintz Aug 19, 2024
a4c0d59
Merge branch 'main' into feat/support-allow-urls-deny-urls
martinhaintz Aug 19, 2024
102f0e5
modified code to go through stacktrace frames
martinhaintz Sep 2, 2024
e5a3315
Merge branch 'main' into feat/support-allow-urls-deny-urls
martinhaintz Sep 2, 2024
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## Unreleased

- Support allowUrls and denyUrls for Flutter Web ([#2227](https://github.com/getsentry/sentry-dart/pull/2227))
```dart
await SentryFlutter.init(
(options) {
options.dsn = 'https://[email protected]/0';
options.allowUrls = ["^https://sentry.com.*\$", "my-custom-domain"];
options.denyUrls = ["^.*ends-with-this\$", "denied-url"];
...
},
appRunner: () => runApp(MyApp()),
);
```

## 8.7.0

### Features
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../../../sentry.dart';
import 'url_filter_event_processor.dart';

UrlFilterEventProcessor urlFilterEventProcessor(SentryOptions _) =>
IoUrlFilterEventProcessor();

class IoUrlFilterEventProcessor implements UrlFilterEventProcessor {
@override
SentryEvent apply(SentryEvent event, Hint hint) => event;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '../../../sentry.dart';
import 'io_url_filter_event_processor.dart'
if (dart.library.html) 'web_url_filter_event_processor.dart';

abstract class UrlFilterEventProcessor implements EventProcessor {
factory UrlFilterEventProcessor(SentryOptions options) =>
urlFilterEventProcessor(options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// We would lose compatibility with old dart versions by adding web to pubspec.
// ignore: depend_on_referenced_packages
import 'package:web/web.dart' as web show window, Window;

import '../../../sentry.dart';
import '../../utils/regex_utils.dart';
import 'url_filter_event_processor.dart';

UrlFilterEventProcessor urlFilterEventProcessor(SentryOptions options) =>
WebUrlFilterEventProcessor(options);

class WebUrlFilterEventProcessor implements UrlFilterEventProcessor {
WebUrlFilterEventProcessor(
this._options,
);

final web.Window _window = web.window;

final SentryOptions _options;

@override
SentryEvent? apply(SentryEvent event, Hint hint) {
final url = event.request?.url ?? _window.location.toString();

if (isMatchingRegexPattern(url, _options.allowUrls)) {
if (_options.denyUrls.isNotEmpty &&
isMatchingRegexPattern(url, _options.denyUrls)) {
return null;
} else {
return event;
}
} else {
return null;
}
}
}
2 changes: 2 additions & 0 deletions dart/lib/src/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'run_zoned_guarded_integration.dart';
import 'event_processor/enricher/enricher_event_processor.dart';
import 'environment/environment_variables.dart';
import 'event_processor/deduplication_event_processor.dart';
import 'event_processor/url_filter/url_filter_event_processor.dart';
import 'hint.dart';
import 'event_processor/exception/exception_event_processor.dart';
import 'hub.dart';
Expand Down Expand Up @@ -86,6 +87,7 @@ class Sentry {
options.addEventProcessor(EnricherEventProcessor(options));
options.addEventProcessor(ExceptionEventProcessor(options));
options.addEventProcessor(DeduplicationEventProcessor(options));
options.addEventProcessor(UrlFilterEventProcessor(options));

options.prependExceptionTypeIdentifier(DartExceptionTypeIdentifier());
}
Expand Down
12 changes: 3 additions & 9 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'transport/rate_limiter.dart';
import 'transport/spotlight_http_transport.dart';
import 'transport/task_queue.dart';
import 'utils/isolate_utils.dart';
import 'utils/regex_utils.dart';
import 'utils/stacktrace_utils.dart';
import 'version.dart';

Expand Down Expand Up @@ -196,7 +197,7 @@ class SentryClient {
}

var message = event.message!.formatted;
return _isMatchingRegexPattern(message, _options.ignoreErrors);
return isMatchingRegexPattern(message, _options.ignoreErrors);
}

SentryEvent _prepareEvent(SentryEvent event, {dynamic stackTrace}) {
Expand Down Expand Up @@ -415,7 +416,7 @@ class SentryClient {
}

var name = transaction.tracer.name;
return _isMatchingRegexPattern(name, _options.ignoreTransactions);
return isMatchingRegexPattern(name, _options.ignoreTransactions);
}

/// Reports the [envelope] to Sentry.io.
Expand Down Expand Up @@ -593,11 +594,4 @@ class SentryClient {
SentryId.empty(),
);
}

bool _isMatchingRegexPattern(String value, List<String> regexPattern,
{bool caseSensitive = false}) {
final combinedRegexPattern = regexPattern.join('|');
final regExp = RegExp(combinedRegexPattern, caseSensitive: caseSensitive);
return regExp.hasMatch(value);
}
}
17 changes: 17 additions & 0 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,29 @@ class SentryOptions {
/// sent. Events are picked randomly. Default is null (disabled)
double? sampleRate;

/// (Web only) Errors only occurring on these Urls will be handled and sent to sentry.
/// If an null or an empty list is used, the SDK will send all errors.
/// To use regex add the `^` and the `$` to the string.
///
/// If used on a platform other than Web, this setting will be ignored.
List<String> allowUrls = [];

/// (Web only) Errors occurring on these Urls will be ignored and are not sent to sentry.
/// If an null or an empty list is used, the SDK will send all errors.
/// In combination with `allowUrls` you can block subdomains of the domains listed in allowUrls.
/// To use regex add the `^` and the `$` to the string.
///
/// If used on a platform other than Web, this setting will be ignored.
List<String> denyUrls = [];

/// The ignoreErrors tells the SDK which errors should be not sent to the sentry server.
/// If an null or an empty list is used, the SDK will send all transactions.
/// To use regex add the `^` and the `$` to the string.
List<String> ignoreErrors = [];

/// The ignoreTransactions tells the SDK which transactions should be not sent to the sentry server.
/// If null or an empty list is used, the SDK will send all transactions.
/// To use regex add the `^` and the `$` to the string.
List<String> ignoreTransactions = [];

final List<String> _inAppExcludes = [];
Expand Down
6 changes: 6 additions & 0 deletions dart/lib/src/utils/regex_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bool isMatchingRegexPattern(String value, List<String> regexPattern,
{bool caseSensitive = false}) {
final combinedRegexPattern = regexPattern.join('|');
final regExp = RegExp(combinedRegexPattern, caseSensitive: caseSensitive);
return regExp.hasMatch(value);
}
153 changes: 153 additions & 0 deletions dart/test/event_processor/web_url_filter_event_processor_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
@TestOn('browser')
library dart_test;

import 'package:sentry/sentry.dart';
import 'package:sentry/sentry_io.dart';
import 'package:sentry/src/event_processor/url_filter/web_url_filter_event_processor.dart';
import 'package:test/test.dart';

import '../mocks.dart';
import '../mocks/mock_platform_checker.dart';

// can be tested on command line with
// `dart test -p chrome test/event_processor/web_url_filter_event_processor_test.dart --name web_url_filter`
void main() {
group('web_url_filter', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('returns event if no allowUrl and no denyUrl is set', () {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'foo.bar',
),
);

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNotNull);
});

test('returns null if allowUrl is set and does not match with url', () {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'foo.bar',
),
);
fixture.options.allowUrls = ["another.url"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNull);
});

test('returns event if allowUrl is set and does partially match with url',
() {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'foo.bar',
),
);
fixture.options.allowUrls = ["bar"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNotNull);
});

test('returns event if denyUrl is set and does not match with url', () {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'foo.bar',
),
);
fixture.options.denyUrls = ["another.url"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNotNull);
});

test('returns null if denyUrl is set and partially matches with url', () {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'foo.bar',
),
);
fixture.options.denyUrls = ["bar"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNull);
});

test(
'returns null if it is part of the allowed domain, but blocked for subdomain',
() {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'this.is/a/special/url/for-testing/this-feature',
),
);
fixture.options.allowUrls = ["^this.is/.*\$"];
fixture.options.denyUrls = ["special"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNull);
});

test(
'returns event if it is part of the allowed domain, and not of the blocked for subdomain',
() {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'this.is/a/test/url/for-testing/this-feature',
),
);
fixture.options.allowUrls = ["^this.is/.*\$"];
fixture.options.denyUrls = ["special"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNotNull);
});

test(
'returns null if it is not part of the allowed domain, and not of the blocked for subdomain',
() {
SentryEvent? event = SentryEvent(
request: SentryRequest(
url: 'another.url/for/a/test/testing/this-feature',
),
);
fixture.options.allowUrls = ["^this.is/.*\$"];
fixture.options.denyUrls = ["special"];

var eventProcessor = fixture.getSut();
event = eventProcessor.apply(event, Hint());

expect(event, isNull);
});
});
}

class Fixture {
SentryOptions options = SentryOptions(
dsn: fakeDsn,
checker: MockPlatformChecker(hasNativeIntegration: false),
);
WebUrlFilterEventProcessor getSut() {
return WebUrlFilterEventProcessor(options);
}
}
Loading