Skip to content
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

Merged
merged 5 commits into from
Feb 10, 2021

Conversation

philipphofmann
Copy link
Member

@philipphofmann philipphofmann commented Feb 8, 2021

📜 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

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the CHANGELOG if needed
  • I updated the docs if needed
  • Review from the native team if needed
  • No breaking changes

🔮 Next steps

@philipphofmann philipphofmann requested a review from a team February 8, 2021 15:55
@philipphofmann philipphofmann linked an issue Feb 8, 2021 that may be closed by this pull request
9 tasks
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
Comment on lines 59 to 63
// When the string can't be converted to a date we don't override the
// default value.
if (nil != startedDate) {
_started = startedDate;
}
Copy link
Contributor

@marandaneto marandaneto Feb 9, 2021

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.

Copy link
Member Author

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:

// 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)

Copy link
Contributor

@marandaneto marandaneto Feb 9, 2021

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.

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member

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-io
Copy link

codecov-io commented Feb 9, 2021

Codecov Report

Merging #939 (4703ae0) into master (10d7a5b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #939   +/-   ##
=======================================
  Coverage   94.69%   94.69%           
=======================================
  Files          76       76           
  Lines        3504     3508    +4     
=======================================
+ Hits         3318     3322    +4     
  Misses        186      186           
Impacted Files Coverage Δ
Sources/Sentry/SentrySession.m 95.33% <100.00%> (+0.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 10d7a5b...4193965. Read the comment docs.

Copy link
Member

@bruno-garcia bruno-garcia left a 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

@philipphofmann philipphofmann changed the title fix: Crash in Session Init with JSON fix: Discard Sessions when JSON is faulty Feb 10, 2021
@philipphofmann philipphofmann enabled auto-merge (squash) February 10, 2021 10:00
Copy link
Contributor

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :) LGTM

@philipphofmann philipphofmann merged commit aabad44 into master Feb 10, 2021
@philipphofmann philipphofmann deleted the fix/crash-in-sentry-session branch February 10, 2021 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sentry crashes on Launch, trying to serialize a broken session object
4 participants