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

Avoid Unhandled Exception on .NET 461 if the Registry Access threw an exception. #1101

Merged
merged 32 commits into from
Jul 10, 2021

Conversation

lucas-zimerman
Copy link
Collaborator

@lucas-zimerman lucas-zimerman commented Jul 2, 2021

SentryOptions .Ctor calls Runtime.Current.Name and that parameter was unsafe since it calls SetNetFxReleaseAndVersion on NET461.
Where, inside of it, is called RegistryKey.OpenBaseKey, which may throw an Exception by executing it.

The goal of this PR is to block any access to the functions that rely on RegistryKey in case of failure, avoiding unhandled exceptions to the user.

My only question is what to do in case of an exception inside of GetLatest? it would be nice to log the error but we don't have access to SentryOptions from it.

Close #969

#skip-changelog

@lucas-zimerman lucas-zimerman added the Bug Something isn't working label Jul 2, 2021
src/Sentry/SentryOptions.cs Outdated Show resolved Hide resolved
src/Sentry/Integrations/NetFxInstallationsIntegration.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jul 5, 2021

Codecov Report

Merging #1101 (a15349b) into main (00be88c) will increase coverage by 0.05%.
The diff coverage is 88.05%.

❗ Current head a15349b differs from pull request most recent head 58a0cd7. Consider uploading reports for the commit 58a0cd7 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1101      +/-   ##
==========================================
+ Coverage   80.72%   80.77%   +0.05%     
==========================================
  Files         194      195       +1     
  Lines        6359     6414      +55     
  Branches     1411     1416       +5     
==========================================
+ Hits         5133     5181      +48     
- Misses        764      771       +7     
  Partials      462      462              
Impacted Files Coverage Δ
.../Sentry.AspNetCore.Grpc/SentryBuilderExtensions.cs 43.75% <ø> (ø)
...DependencyInjection/ServiceCollectionExtensions.cs 100.00% <ø> (ø)
...entry.AspNetCore/SentryAspNetCoreLoggerProvider.cs 100.00% <ø> (ø)
.../Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs 100.00% <ø> (ø)
src/Sentry.AspNetCore/SentryStartupFilter.cs 100.00% <ø> (ø)
src/Sentry/SentryOptions.cs 89.17% <58.33%> (-2.11%) ⬇️
...ntry/Integrations/NetFxInstallationsIntegration.cs 57.14% <60.00%> (-42.86%) ⬇️
src/Sentry/SentryHttpMessageHandler.cs 84.21% <90.00%> (+4.90%) ⬆️
src/Sentry.Google.Cloud.Functions/SentryStartup.cs 100.00% <100.00%> (ø)
...Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs 70.42% <100.00%> (-0.82%) ⬇️
... and 6 more

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 733245e...58a0cd7. Read the comment docs.

Co-authored-by: Alexey Golub <tyrrrrrr@gmail.com>
Copy link
Contributor

@Tyrrrz Tyrrrz left a comment

Choose a reason for hiding this comment

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

Looks good 👍

CHANGELOG.md Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
@lucas-zimerman
Copy link
Collaborator Author

Ok, error flow has changed:

  • It'll no longer crash inside of SentryOptions since StackTraceMode isn't changed there.

  • Because of that, the Runtime on NET461 will be created when NetFxInstallationsIntegration is registered.
    ** Because of that, the Registration will throw an exception and an additional try/catch had to be placed inside of hub to avoid _rootScope not being pushed here

    // Push the first scope so the async local starts from here
    _rootScope = PushScope();
    _enricher = new Enricher(options);

    Resulting in a crash if Disposed (or another side effect untested)

  • StackTraceMode inside of SentryOptions is safer, avoiding Exceptions when Accessing Runtime.Current and logging the errors if a DiagnosticLogger is available.

  • Lastly, the Current Runtime got split into two functions, one that returns the runtime and another that sets SetNetFxReleaseAndVersion or SetNetFxReleaseAndVersion. that was made to avoid runtime being null whenever an exception happened inside of the second part and entering on a bad logic that would throw an exception any time the runtime was called.

Copy link
Contributor

@Tyrrrz Tyrrrz left a comment

Choose a reason for hiding this comment

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

Looks good! Some visual nits

src/Sentry/Internal/Hub.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/Runtime.cs Outdated Show resolved Hide resolved
src/Sentry/SentryOptions.cs Show resolved Hide resolved
src/Sentry/SentryOptions.cs Outdated Show resolved Hide resolved
src/Sentry/SentryOptions.cs Show resolved Hide resolved
Co-authored-by: Alexey Golub <tyrrrrrr@gmail.com>
CHANGELOG.md Outdated Show resolved Hide resolved
samples/Sentry.Samples.Console.Basic/Program.cs Outdated Show resolved Hide resolved
src/Sentry/Internal/Hub.cs Outdated Show resolved Hide resolved
src/Sentry/PlatformAbstractions/FrameworkInfo.NetFx.cs Outdated Show resolved Hide resolved
src/Sentry/SentryOptions.cs Outdated Show resolved Hide resolved
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.

I have a couple of nits but otherwise LGTM

src/Sentry/SentryOptions.cs Outdated Show resolved Hide resolved
src/Sentry/SentryOptions.cs Outdated Show resolved Hide resolved
@bruno-garcia bruno-garcia merged commit b496520 into main Jul 10, 2021
@bruno-garcia bruno-garcia deleted the feat/opt-out-netfxinstalaltions branch July 10, 2021 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Opt-out for .NET Framework installations detection
4 participants