Skip to content

Fallback Uri parsing to unknown if its invalid #1414

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 2 commits into from
Apr 28, 2023
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
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