-
-
Notifications
You must be signed in to change notification settings - Fork 277
Fix unsafe json access in sentry_device
#3309
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
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e1d5afa
Add type safe access extension to Map<String, dynamic>
buenaflor 5551800
Add tests
buenaflor 0e3927a
Update CHANGELOG
buenaflor 3e5fad6
Fix analyze
buenaflor 2a0b9c7
getBool should be able to handle numerical values 1 and 0
buenaflor d177ad0
UpdatE
buenaflor 3563704
Update
buenaflor 7730cfa
Merge branch 'main' into fix/unsafe-access-sentry-device
buenaflor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import 'package:meta/meta.dart'; | ||
| import '../sentry.dart'; | ||
| import '../protocol/sentry_level.dart'; | ||
|
|
||
| /// Extension providing type-safe value extraction from JSON maps | ||
| @internal | ||
| extension TypeSafeMapExtension on Map<String, dynamic> { | ||
| /// Type-safe string extraction | ||
| String? getString(String key) { | ||
| final value = this[key]; | ||
| if (value == null) return null; | ||
| if (value is String) return value; | ||
|
|
||
| _logTypeMismatch(key, 'String', value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
|
|
||
| /// Type-safe int extraction with num support | ||
| int? getInt(String key) { | ||
buenaflor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final value = this[key]; | ||
| if (value == null) return null; | ||
| if (value is int) return value; | ||
| if (value is num) return value.toInt(); | ||
|
|
||
| _logTypeMismatch(key, 'int', value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
|
|
||
| /// Type-safe double extraction with num support | ||
| double? getDouble(String key) { | ||
| final value = this[key]; | ||
| if (value == null) return null; | ||
| if (value is double) return value; | ||
| if (value is num) return value.toDouble(); | ||
|
|
||
| _logTypeMismatch(key, 'double', value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
|
|
||
| /// Type-safe bool extraction with support for 0/1 as false/true | ||
| bool? getBool(String key) { | ||
| final value = this[key]; | ||
| if (value == null) return null; | ||
| if (value is bool) return value; | ||
|
|
||
| // Handle numeric 0 and 1 as boolean values | ||
| if (value is num) { | ||
| if (value == 0) return false; | ||
| if (value == 1) return true; | ||
| } | ||
|
||
|
|
||
| _logTypeMismatch(key, 'bool', value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
|
|
||
| /// Type-safe DateTime extraction from string | ||
| DateTime? getDateTime(String key) { | ||
| final value = this[key]; | ||
| if (value == null) return null; | ||
| if (value is! String) { | ||
| _logTypeMismatch( | ||
| key, 'String (for DateTime)', value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
|
|
||
| final dateTime = DateTime.tryParse(value); | ||
| if (dateTime == null) { | ||
| _logParseError(key, 'DateTime', value); | ||
| } | ||
| return dateTime; | ||
| } | ||
|
|
||
| void _logTypeMismatch(String key, String expected, String actual) { | ||
| Sentry.currentHub.options.log( | ||
| SentryLevel.warning, | ||
| 'Type mismatch in JSON deserialization: key "$key" expected $expected but got $actual', | ||
| ); | ||
| } | ||
|
|
||
| void _logParseError(String key, String expected, dynamic value) { | ||
| Sentry.currentHub.options.log( | ||
| SentryLevel.warning, | ||
| 'Parse error in JSON deserialization: key "$key" could not be parsed as $expected from value "$value"', | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.