Skip to content

Commit 99a6f98

Browse files
committed
change matching to window.location
1 parent e5a3315 commit 99a6f98

File tree

3 files changed

+32
-127
lines changed

3 files changed

+32
-127
lines changed

flutter/lib/src/event_processor/url_filter/html_url_filter_event_processor.dart

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,23 @@ class WebUrlFilterEventProcessor implements UrlFilterEventProcessor {
1515
this._options,
1616
);
1717

18+
final html.Window _window = html.window;
1819
final SentryFlutterOptions _options;
1920

2021
@override
2122
SentryEvent? apply(SentryEvent event, Hint hint) {
22-
final frames = _getStacktraceFrames(event);
23-
final lastPath = frames?.first?.absPath;
24-
25-
if (lastPath == null) {
26-
return event;
27-
}
23+
final url = _window.location.toString();
2824

2925
if (_options.allowUrls.isNotEmpty &&
30-
!isMatchingRegexPattern(lastPath, _options.allowUrls)) {
26+
!isMatchingRegexPattern(url, _options.allowUrls)) {
3127
return null;
3228
}
3329

3430
if (_options.denyUrls.isNotEmpty &&
35-
isMatchingRegexPattern(lastPath, _options.denyUrls)) {
31+
isMatchingRegexPattern(url, _options.denyUrls)) {
3632
return null;
3733
}
3834

3935
return event;
4036
}
41-
42-
Iterable<SentryStackFrame?>? _getStacktraceFrames(SentryEvent event) {
43-
if (event.exceptions?.isNotEmpty == true) {
44-
return event.exceptions?.first.stackTrace?.frames;
45-
}
46-
if (event.threads?.isNotEmpty == true) {
47-
final stacktraces = event.threads?.map((e) => e.stacktrace);
48-
return stacktraces
49-
?.where((element) => element != null)
50-
.expand((element) => element!.frames);
51-
}
52-
return null;
53-
}
5437
}

flutter/lib/src/event_processor/url_filter/web_url_filter_event_processor.dart

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,23 @@ class WebUrlFilterEventProcessor implements UrlFilterEventProcessor {
1717
this._options,
1818
);
1919

20+
final web.Window _window = web.window;
2021
final SentryFlutterOptions _options;
2122

2223
@override
2324
SentryEvent? apply(SentryEvent event, Hint hint) {
24-
final frames = _getStacktraceFrames(event);
25-
final lastPath = frames?.first?.absPath;
26-
27-
if (lastPath == null) {
28-
return event;
29-
}
25+
final url = _window.location.toString();
3026

3127
if (_options.allowUrls.isNotEmpty &&
32-
!isMatchingRegexPattern(lastPath, _options.allowUrls)) {
28+
!isMatchingRegexPattern(url, _options.allowUrls)) {
3329
return null;
3430
}
3531

3632
if (_options.denyUrls.isNotEmpty &&
37-
isMatchingRegexPattern(lastPath, _options.denyUrls)) {
33+
isMatchingRegexPattern(url, _options.denyUrls)) {
3834
return null;
3935
}
4036

4137
return event;
4238
}
43-
44-
Iterable<SentryStackFrame?>? _getStacktraceFrames(SentryEvent event) {
45-
if (event.exceptions?.isNotEmpty == true) {
46-
return event.exceptions?.first.stackTrace?.frames;
47-
}
48-
if (event.threads?.isNotEmpty == true) {
49-
final stacktraces = event.threads?.map((e) => e.stacktrace);
50-
return stacktraces
51-
?.where((element) => element != null)
52-
.expand((element) => element!.frames);
53-
}
54-
return null;
55-
}
5639
}

flutter/test/event_processor/url_filter/web_url_filter_event_processor_test.dart

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:sentry_flutter/src/event_processor/url_filter/url_filter_event_p
77

88
// can be tested on command line with
99
// `flutter test --platform=chrome test/event_processor/url_filter/web_url_filter_event_processor_test.dart`
10+
// The URL looks something like this: http://localhost:58551/event_processor/url_filter/web_url_filter_event_processor_test.html
1011
void main() {
1112
group(UrlFilterEventProcessor, () {
1213
late Fixture fixture;
@@ -16,57 +17,53 @@ void main() {
1617
});
1718

1819
test('returns event if no allowUrl and no denyUrl is set', () async {
19-
SentryEvent? event = SentryEvent(
20-
request: SentryRequest(
21-
url: 'foo.bar',
22-
),
23-
);
20+
final event = SentryEvent();
21+
final eventProcessor = fixture.getSut();
2422

25-
var eventProcessor = fixture.getSut();
26-
event = await eventProcessor.apply(event, Hint());
23+
final processedEvent = await eventProcessor.apply(event, Hint());
2724

28-
expect(event, isNotNull);
25+
expect(processedEvent, isNotNull);
2926
});
3027

3128
test('returns null if allowUrl is set and does not match with url',
3229
() async {
33-
final event = _createEventWithException("foo.bar");
30+
final event = SentryEvent();
3431
fixture.options.allowUrls = ["another.url"];
32+
final eventProcessor = fixture.getSut();
3533

36-
var eventProcessor = fixture.getSut();
3734
final processedEvent = await eventProcessor.apply(event, Hint());
3835

3936
expect(processedEvent, isNull);
4037
});
4138

4239
test('returns event if allowUrl is set and does partially match with url',
4340
() async {
44-
final event = _createEventWithException("foo.bar");
45-
fixture.options.allowUrls = ["bar"];
41+
final event = SentryEvent();
42+
fixture.options.allowUrls = ["event_processor_test"];
43+
final eventProcessor = fixture.getSut();
4644

47-
var eventProcessor = fixture.getSut();
4845
final processedEvent = await eventProcessor.apply(event, Hint());
4946

5047
expect(processedEvent, isNotNull);
5148
});
5249

5350
test('returns event if denyUrl is set and does not match with url',
5451
() async {
55-
final event = _createEventWithException("foo.bar");
52+
final event = SentryEvent();
5653
fixture.options.denyUrls = ["another.url"];
54+
final eventProcessor = fixture.getSut();
5755

58-
var eventProcessor = fixture.getSut();
5956
final processedEvent = await eventProcessor.apply(event, Hint());
6057

6158
expect(processedEvent, isNotNull);
6259
});
6360

6461
test('returns null if denyUrl is set and partially matches with url',
6562
() async {
66-
final event = _createEventWithException("foo.bar");
67-
fixture.options.denyUrls = ["bar"];
63+
final event = SentryEvent();
64+
fixture.options.denyUrls = ["event_processor_test"];
65+
final eventProcessor = fixture.getSut();
6866

69-
var eventProcessor = fixture.getSut();
7067
final processedEvent = await eventProcessor.apply(event, Hint());
7168

7269
expect(processedEvent, isNull);
@@ -75,13 +72,11 @@ void main() {
7572
test(
7673
'returns null if it is part of the allowed domain, but blocked for subdomain',
7774
() async {
78-
final event = _createEventWithException(
79-
"this.is/a/special/url/for-testing/this-feature");
80-
81-
fixture.options.allowUrls = ["^this.is/.*\$"];
82-
fixture.options.denyUrls = ["special"];
75+
final event = SentryEvent();
76+
fixture.options.allowUrls = [".*localhost.*\$"];
77+
fixture.options.denyUrls = ["event"];
78+
final eventProcessor = fixture.getSut();
8379

84-
var eventProcessor = fixture.getSut();
8580
final processedEvent = await eventProcessor.apply(event, Hint());
8681

8782
expect(processedEvent, isNull);
@@ -90,12 +85,11 @@ void main() {
9085
test(
9186
'returns event if it is part of the allowed domain, and not of the blocked for subdomain',
9287
() async {
93-
final event = _createEventWithException(
94-
"this.is/a/test/url/for-testing/this-feature");
95-
fixture.options.allowUrls = ["^this.is/.*\$"];
88+
final event = SentryEvent();
89+
fixture.options.allowUrls = [".*localhost.*\$"];
9690
fixture.options.denyUrls = ["special"];
91+
final eventProcessor = fixture.getSut();
9792

98-
var eventProcessor = fixture.getSut();
9993
final processedEvent = await eventProcessor.apply(event, Hint());
10094

10195
expect(processedEvent, isNotNull);
@@ -104,60 +98,15 @@ void main() {
10498
test(
10599
'returns null if it is not part of the allowed domain, and not of the blocked for subdomain',
106100
() async {
107-
final event = _createEventWithException(
108-
"another.url/for/a/test/testing/this-feature");
101+
final event = SentryEvent();
109102
fixture.options.allowUrls = ["^this.is/.*\$"];
110103
fixture.options.denyUrls = ["special"];
104+
final eventProcessor = fixture.getSut();
111105

112-
var eventProcessor = fixture.getSut();
113106
final processedEvent = await eventProcessor.apply(event, Hint());
114107

115108
expect(processedEvent, isNull);
116109
});
117-
118-
test(
119-
'returns event if denyUrl is set and not matching with url of first exception',
120-
() async {
121-
final frame1 = SentryStackFrame(absPath: "test.url");
122-
final st1 = SentryStackTrace(frames: [frame1]);
123-
final exception1 = SentryException(
124-
type: "test-type", value: "test-value", stackTrace: st1);
125-
126-
final frame2 = SentryStackFrame(absPath: "foo.bar");
127-
final st2 = SentryStackTrace(frames: [frame2]);
128-
final exception2 = SentryException(
129-
type: "test-type", value: "test-value", stackTrace: st2);
130-
131-
SentryEvent event = SentryEvent(exceptions: [exception1, exception2]);
132-
133-
fixture.options.denyUrls = ["bar"];
134-
135-
var eventProcessor = fixture.getSut();
136-
final processedEvent = await eventProcessor.apply(event, Hint());
137-
138-
expect(processedEvent, isNotNull);
139-
});
140-
141-
test(
142-
'returns event if denyUrl is set and not matching with url of first stacktraceframe',
143-
() async {
144-
final frame1 = SentryStackFrame(absPath: "test.url");
145-
final st1 = SentryStackTrace(frames: [frame1]);
146-
final thread1 = SentryThread(stacktrace: st1);
147-
148-
final frame2 = SentryStackFrame(absPath: "foo.bar");
149-
final st2 = SentryStackTrace(frames: [frame2]);
150-
final thread2 = SentryThread(stacktrace: st2);
151-
152-
SentryEvent event = SentryEvent(threads: [thread1, thread2]);
153-
154-
fixture.options.denyUrls = ["bar"];
155-
156-
var eventProcessor = fixture.getSut();
157-
final processedEvent = await eventProcessor.apply(event, Hint());
158-
159-
expect(processedEvent, isNotNull);
160-
});
161110
});
162111
}
163112

@@ -167,13 +116,3 @@ class Fixture {
167116
return UrlFilterEventProcessor(options);
168117
}
169118
}
170-
171-
SentryEvent _createEventWithException(String url) {
172-
final frame = SentryStackFrame(absPath: url);
173-
final st = SentryStackTrace(frames: [frame]);
174-
final exception =
175-
SentryException(type: "test-type", value: "test-value", stackTrace: st);
176-
SentryEvent event = SentryEvent(exceptions: [exception]);
177-
178-
return event;
179-
}

0 commit comments

Comments
 (0)