Skip to content

[Android] Fix app crash when using non-native HttpClientHandler - #69565

Merged
simonrozsival merged 16 commits into
dotnet:mainfrom
simonrozsival:fix-android-certificate-validation-pal-null-reference-exception
Jun 6, 2022
Merged

[Android] Fix app crash when using non-native HttpClientHandler#69565
simonrozsival merged 16 commits into
dotnet:mainfrom
simonrozsival:fix-android-certificate-validation-pal-null-reference-exception

Conversation

@simonrozsival

@simonrozsivalsimonrozsival commented May 19, 2022

Copy link
Copy Markdown
Member

HTTP requests done with HttpClient + HttpClientHandler caused Android apps to crash.

For example, a simple HTTP request using the default HttpClient will throw an exception with the following stack trace:

varclient=newHttpClient();try{varresponse=awaitclient.GetAsync("https://microsoft.com");Console.WriteLine(response);}catch(Exceptione){Console.WriteLine(e);}
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.---> System.NullReferenceException: Object reference not set to an instance of an object
at System.Net.CertificateValidationPal.GetRemoteCertificate(SafeDeleteContextsecurityContext,BooleanretrieveChainCertificates,X509Chain&chain)
at System.Net.CertificateValidationPal.GetRemoteCertificate(SafeDeleteContextsecurityContext)
at System.Net.Security.SslStream.SelectClientCertificate(Boolean&sessionRestartAttempt)
at System.Net.Security.SslStream.AcquireClientCredentials(Byte[]&thumbPrint)
at System.Net.Security.SslStream.GenerateToken(ReadOnlySpan`1inputBuffer,Byte[]&output)
at System.Net.Security.SslStream.NextMessage(ReadOnlySpan`1incomingBuffer)
at System.Net.Security.SslStream.<ForceAuthenticationAsync>d__144`1[[System.Net.Security.AsyncReadWriteAdapter,System.Net.Security,Version=7.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a]].MoveNext()
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptionssslOptions,HttpRequestMessagerequest,Booleanasync,Streamstream,CancellationTokencancellationToken)---End of inner exception stack trace ---at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptionssslOptions,HttpRequestMessagerequest,Booleanasync,Streamstream,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItemqueueItem)
at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.<WaitWithCancellationAsync>d__1[[System.Net.Http.HttpConnection,System.Net.Http,Version=7.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a]].MoveNext()
at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessagerequest,Booleanasync,BooleandoRequestAuth,CancellationTokencancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessagerequest,HttpCompletionOptioncompletionOption,CancellationTokenSourcects,BooleandisposeCts,CancellationTokenSourcependingRequestsCts,CancellationTokenoriginalCancellationToken)
at Program.<Main>$(String[]args)in/home/simon/dotnet/runtime/src/mono/sample/Android/Program.cs:line 12

There is a missing null check in CertificateValidationpal.Android.cs which was removed recently (https://github.com/dotnet/runtime/pull/68188/files#diff-5f4278661dbb1331a13f14f1bf94b3a67e7474e8e4c32a21a99860f966ab02aeL66-L68).

Possibly also related to #69557

@ghost

Copy link
Copy Markdown

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

HTTP requests done with HttpClient + HttpClientHandler caused Android apps to crash. For example the test added in this PR would crash with this stack trace:

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.---> System.NullReferenceException: Object reference not set to an instance of an object
at System.Net.CertificateValidationPal.GetRemoteCertificate(SafeDeleteContextsecurityContext,BooleanretrieveChainCertificates,X509Chain&chain)
at System.Net.CertificateValidationPal.GetRemoteCertificate(SafeDeleteContextsecurityContext)
at System.Net.Security.SslStream.SelectClientCertificate(Boolean&sessionRestartAttempt)
at System.Net.Security.SslStream.AcquireClientCredentials(Byte[]&thumbPrint)
at System.Net.Security.SslStream.GenerateToken(ReadOnlySpan`1inputBuffer,Byte[]&output)
at System.Net.Security.SslStream.NextMessage(ReadOnlySpan`1incomingBuffer)
at System.Net.Security.SslStream.<ForceAuthenticationAsync>d__144`1[[System.Net.Security.AsyncReadWriteAdapter,System.Net.Security,Version=7.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a]].MoveNext()
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptionssslOptions,HttpRequestMessagerequest,Booleanasync,Streamstream,CancellationTokencancellationToken)---End of inner exception stack trace ---at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptionssslOptions,HttpRequestMessagerequest,Booleanasync,Streamstream,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItemqueueItem)
at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.<WaitWithCancellationAsync>d__1[[System.Net.Http.HttpConnection,System.Net.Http,Version=7.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a]].MoveNext()
at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessagerequest,Booleanasync,BooleandoRequestAuth,CancellationTokencancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessagerequest,Booleanasync,CancellationTokencancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessagerequest,HttpCompletionOptioncompletionOption,CancellationTokenSourcects,BooleandisposeCts,CancellationTokenSourcependingRequestsCts,CancellationTokenoriginalCancellationToken)
at Program.<Main>$(String[]args)in/home/simon/dotnet/runtime/src/mono/sample/Android/Program.cs:line 12

There is a missing null check in CertificateValidationpal.Android.cs which was removed recently (https://github.com/dotnet/runtime/pull/68188/files#diff-5f4278661dbb1331a13f14f1bf94b3a67e7474e8e4c32a21a99860f966ab02aeL66-L68).

Possibly also related to #69557

Author:simonrozsival
Assignees:-
Labels:

os-android

Milestone:-

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

ref X509Chain? chain)
{
SafeSslHandle sslContext = ((SafeDeleteSslContext)securityContext).SslContext;
SafeSslHandle? sslContext = ((SafeDeleteSslContext)securityContext)?.SslContext;

@akoeplingerakoeplingerMay 19, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this shouldn't happen given that securityContext isn't marked as nullable, which suggests to me something higher up the call chain is doing something unexpected.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The underlaying handle is nullable.

 private SafeFreeCredentials? _credentialsHandle;
private SafeDeleteSslContext? _securityContext;

But is seems like it is like this for long time

remoteCert=CertificateValidationPal.GetRemoteCertificate(_securityContext!);

Making the parameter nullable make sense to me. I think it will be null when we try to select client certificate before the handshake. cc: @rzikm

@simonrozsivalsimonrozsivalMay 20, 2022

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok thanks, that sounds like indeed we're expecting a null securityContext here and should make it consistent across platforms.

@steveisok
steveisok requested a review from wfurtMay 19, 2022 17:15
@wfurt

Copy link
Copy Markdown
Member

On a side note, #69527 removes the dual SafeHandle wrapping for Linux. We could do something similar for Android in future.

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run outerloop-mono

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries-mono outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

I re-enabled the functional tests for System.Net.Http on Android and disabled all the tests that are failing. The bug that's fixed in this PR fixed several dozen outerloop tests.

@dotnetdotnet deleted a comment from azure-pipelinesBotMay 27, 2022
@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-libraries-mono outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
MemberAuthor

I think this PR is now ready:

  • the failing runtime test has something to do with Tar on Linux
  • the failing runtime-extra-platforms tests are unrelated to this PR (Browser, iOS, tvOS, Linux)
  • the failing runtime-libraries-mono outerloop tests are unrelated to this PR
    • the only Android-related leg doesn't have any test failures related to System.Net.Http

@steveisok
steveisok self-requested a review May 31, 2022 14:38
@simonrozsival
simonrozsival merged commit aafe4d4 into dotnet:mainJun 6, 2022
@simonrozsival
simonrozsival deleted the fix-android-certificate-validation-pal-null-reference-exception branch June 6, 2022 09:08
@ghostghost locked as resolved and limited conversation to collaborators Jul 6, 2022
@karelzkarelz added this to the 7.0.0 milestone Jul 19, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@simonrozsival@wfurt@marek-safar@steveisok@akoeplinger@karelz