Skip to content

Commit

Permalink
Fallback Uri parsing to unknown if its invalid (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Apr 28, 2023
1 parent c4f1621 commit 1e781fc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fallback Uri parsing to `unknown` if its invalid ([#1414](https://github.com/getsentry/sentry-dart/pull/1414))

## 7.5.0

### Features
Expand Down
5 changes: 4 additions & 1 deletion dart/lib/src/utils/url_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class UrlDetails {
final String? query;
final String? fragment;

late final urlOrFallback = url ?? 'unknown';
static const _unknown = 'unknown';

late final urlOrFallback =
Uri.tryParse(url ?? _unknown)?.toString() ?? _unknown;

void applyToSpan(ISentrySpan? span) {
if (span == null) {
Expand Down
7 changes: 7 additions & 0 deletions dart/test/utils/url_details_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ void main() {
final urlDetails = UrlDetails(url: null);
expect(urlDetails.urlOrFallback, "unknown");
});

test('returns fallback for invalid Uri', () {
final urlDetails = UrlDetails(url: 'htttps://[Filtered].com/foobar.txt');

expect(urlDetails.urlOrFallback, "unknown");
expect(Uri.parse(urlDetails.urlOrFallback), isNotNull);
});
}

class MockSpan extends Mock implements SentrySpan {}
5 changes: 4 additions & 1 deletion flutter/lib/src/flutter_sentry_attachment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class FlutterSentryAttachment extends SentryAttachment {
final data = await (bundle ?? rootBundle).load(key);
return data.buffer.asUint8List();
},
filename: filename ?? Uri.parse(key).pathSegments.last,
filename: filename ??
((Uri.tryParse(key)?.pathSegments.isNotEmpty == true)
? Uri.parse(key).pathSegments.last
: 'unknown'),
attachmentType: type,
contentType: contentType,
addToTransactions: addToTransactions,
Expand Down
10 changes: 10 additions & 0 deletions flutter/test/flutter_sentry_attachment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ void main() {
expect(attachment.addToTransactions, true);
await expectLater(await attachment.bytes, [102, 111, 111, 32, 98, 97, 114]);
});

test('invalid Uri fall back to unknown', () async {
final attachment = FlutterSentryAttachment.fromAsset(
'htttps://[Filtered].com/foobar.txt',
bundle: TestAssetBundle(),
addToTransactions: true,
);

expect(attachment.filename, 'unknown');
});
}

class TestAssetBundle extends CachingAssetBundle {
Expand Down

0 comments on commit 1e781fc

Please sign in to comment.