-
-
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 all 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
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,88 @@ | ||
| 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> { | ||
| /// Generic, type-safe extraction with a few built-in coercions: | ||
| /// - num -> int | ||
| /// - num -> double | ||
| /// - 0/1 -> bool | ||
| /// - String (ISO) -> DateTime | ||
| T? getValueOrNull<T>(String key) { | ||
| final value = this[key]; | ||
| if (value == null) return null; | ||
|
|
||
| final convertedValue = _tryConvertValue<T>(key, value); | ||
| if (convertedValue != null) return convertedValue; | ||
|
|
||
| _logTypeMismatch(key, _expectedTypeFor<T>(), value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
|
|
||
| T? _tryConvertValue<T>(String key, Object value) { | ||
| // Direct hit. | ||
| if (value is T) return value as T; | ||
|
|
||
| // num -> int | ||
| if (T == int) { | ||
| if (value is num) return value.toInt() as T; | ||
| return null; | ||
| } | ||
|
|
||
| // num -> double | ||
| if (T == double) { | ||
| if (value is num) return value.toDouble() as T; | ||
| return null; | ||
| } | ||
|
|
||
| // 0/1 -> bool | ||
| if (T == bool) { | ||
| // if value is bool directly already handled above | ||
| if (value is num) { | ||
| if (value == 0) return false as T; | ||
| if (value == 1) return true as T; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // String(ISO8601) -> DateTime | ||
| if (T == DateTime) { | ||
| if (value is! String) { | ||
| _logTypeMismatch( | ||
| key, 'String (for DateTime)', value.runtimeType.toString()); | ||
| return null; | ||
| } | ||
| final dt = DateTime.tryParse(value); | ||
| if (dt == null) { | ||
| _logParseError(key, 'DateTime', value); | ||
| return null; | ||
| } | ||
| return dt as T; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| String _expectedTypeFor<T>() { | ||
| if (T == DateTime) { | ||
| return 'String (for DateTime)'; | ||
| } | ||
| return T.toString(); | ||
| } | ||
|
|
||
| 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, Object 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️