-
-
Notifications
You must be signed in to change notification settings - Fork 342
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
fix: Discard Sessions when JSON is faulty #939
Conversation
When the JSON contains a started that can't be converted to a NSDate the SDK sets the _started to nil, which leads to a crash later as _started is non null. This is fixed now by not setting the _started if the SDK can't convert the started to a NSDate. Fixes GH-937
Sources/Sentry/SentrySession.m
Outdated
// When the string can't be converted to a date we don't override the | ||
// default value. | ||
if (nil != startedDate) { | ||
_started = startedDate; | ||
} |
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.
M: on Java we'd drop the session though, as started
is a mandatory field, and if it has an invalid started-timestamp, duration
can't be calculated properly as well.
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.
I would also like to do that, but we can't return nil
as this would be a breaking change. See:
sentry-cocoa/Sources/Sentry/SentrySession.m
Lines 41 to 46 in 10d7a5b
// We use the default constructor here to set the non nullable values to a default values, | |
// because this could cause crashes, for example, in serialize. | |
// With this approach we avoid crashes and accept the tradeoff that some session data might not | |
// be 100% accurate. | |
// Ideally we would return nil, if the passed JSON is not valid, which we can't do because it | |
// would be a breaking change. |
I put this already down for the next major, see #877 (comment)
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.
I understand its a breaking change but SentrySession is internal right.
startSession
and endSession
do not return a Session, nor we do beforeSend
with Sessions, so I'd prefer to fix the bug as this session would just mess up with session data on the server (eg wrong average duration).
I'm also fine keeping as it is and fixing it on v7 but I'd like to point this out anyway.
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.
SentrySession initWithJSONObject
is public. So making it nullable
would be a breaking change. We already had this discussion in the past and we agreed with sticking to the current approach and fix it in the next major.
I mean we can argue that this is clearly wrong and we just fix it now. I don't think anyone is using initWithJSONObject
anyway. Why would they?
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.
initializing SentrySession directly isn't really a documented API or expected to be used. I wouldn't bother bumping a major for this breaking change.
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.
I agree with Manoel here this will generate trash data and we're better off discarding this instead
Codecov Report
@@ Coverage Diff @@
## master #939 +/- ##
=======================================
Coverage 94.69% 94.69%
=======================================
Files 76 76
Lines 3504 3508 +4
=======================================
+ Hits 3318 3322 +4
Misses 186 186
Continue to review full report at Codecov.
|
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.
Can we just return null instead and drop the session? This isn't affecting anyone using startSession/endSession
nor the hybrid SDKs so it's a safe change to make
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.
nice :) LGTM
📜 Description
When the JSON contains a started that can't be converted to an NSDate the SDK sets
the _started to nil, which leads to a crash later as _started is nonnull. This is fixed now
by returning
nil
when the JSON passed to initWithJSONObject contains an error.💡 Motivation and Context
Fixes #937
💚 How did you test it?
Unit tests and SentrySessionGeneratorTests.
📝 Checklist
🔮 Next steps