Merge pull request #1237 from kamilpowalowski/bugfix/android-oreo-crash

fix: Prevent Android java.lang.NullPointerException in InAppWebViewCl…
This commit is contained in:
Lorenzo Pichilli 2022-10-13 17:06:15 +02:00 committed by GitHub
commit 8889b0d9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 45 deletions

View File

@ -328,32 +328,22 @@ public class InAppWebViewClient extends WebViewClient {
@Override @Override
public void onReceivedHttpAuthRequest(final WebView view, final HttpAuthHandler handler, final String host, final String realm) { public void onReceivedHttpAuthRequest(final WebView view, final HttpAuthHandler handler, final String host, final String realm) {
final String url = view.getUrl();
URI uri; String protocol = "https";
try { int port = 0;
uri = new URI(view.getUrl());
} catch (URISyntaxException e) { if (url != null) {
e.printStackTrace(); try {
URI uri = new URI(url);
credentialsProposed = null; protocol = uri.getScheme();
previousAuthRequestFailureCount = 0; port = uri.getPort();
} catch (URISyntaxException e) {
handler.cancel(); e.printStackTrace();
return; }
} }
final String protocol = uri.getScheme();
final int port = uri.getPort();
previousAuthRequestFailureCount++; previousAuthRequestFailureCount++;
Map<String, Object> obj = new HashMap<>();
obj.put("host", host);
obj.put("protocol", protocol);
obj.put("realm", realm);
obj.put("port", port);
obj.put("previousFailureCount", previousAuthRequestFailureCount);
if (credentialsProposed == null) if (credentialsProposed == null)
credentialsProposed = CredentialDatabase.getInstance(view.getContext()).getHttpAuthCredentials(host, protocol, realm, port); credentialsProposed = CredentialDatabase.getInstance(view.getContext()).getHttpAuthCredentials(host, protocol, realm, port);
@ -365,6 +355,8 @@ public class InAppWebViewClient extends WebViewClient {
URLProtectionSpace protectionSpace = new URLProtectionSpace(host, protocol, realm, port, view.getCertificate(), null); URLProtectionSpace protectionSpace = new URLProtectionSpace(host, protocol, realm, port, view.getCertificate(), null);
HttpAuthenticationChallenge challenge = new HttpAuthenticationChallenge(protectionSpace, previousAuthRequestFailureCount, credentialProposed); HttpAuthenticationChallenge challenge = new HttpAuthenticationChallenge(protectionSpace, previousAuthRequestFailureCount, credentialProposed);
final String finalProtocol = protocol;
final int finalPort = port;
channel.invokeMethod("onReceivedHttpAuthRequest", challenge.toMap(), new MethodChannel.Result() { channel.invokeMethod("onReceivedHttpAuthRequest", challenge.toMap(), new MethodChannel.Result() {
@Override @Override
public void success(Object response) { public void success(Object response) {
@ -378,7 +370,7 @@ public class InAppWebViewClient extends WebViewClient {
String password = (String) responseMap.get("password"); String password = (String) responseMap.get("password");
Boolean permanentPersistence = (Boolean) responseMap.get("permanentPersistence"); Boolean permanentPersistence = (Boolean) responseMap.get("permanentPersistence");
if (permanentPersistence != null && permanentPersistence) { if (permanentPersistence != null && permanentPersistence) {
CredentialDatabase.getInstance(view.getContext()).setHttpAuthCredential(host, protocol, realm, port, username, password); CredentialDatabase.getInstance(view.getContext()).setHttpAuthCredential(host, finalProtocol, realm, finalPort, username, password);
} }
handler.proceed(username, password); handler.proceed(username, password);
return; return;
@ -419,20 +411,21 @@ public class InAppWebViewClient extends WebViewClient {
@Override @Override
public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError sslError) { public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError sslError) {
URI uri; final String url = sslError.getUrl();
String host = "";
String protocol = "https";
final String realm = null;
int port = 0;
try { try {
uri = new URI(sslError.getUrl()); URI uri = new URI(url);
host = uri.getHost();
protocol = uri.getScheme();
port = uri.getPort();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
handler.cancel();
return;
} }
final String host = uri.getHost();
final String protocol = uri.getScheme();
final String realm = null;
final int port = uri.getPort();
URLProtectionSpace protectionSpace = new URLProtectionSpace(host, protocol, realm, port, sslError.getCertificate(), sslError); URLProtectionSpace protectionSpace = new URLProtectionSpace(host, protocol, realm, port, sslError.getCertificate(), sslError);
ServerTrustChallenge challenge = new ServerTrustChallenge(protectionSpace); ServerTrustChallenge challenge = new ServerTrustChallenge(protectionSpace);
@ -473,23 +466,21 @@ public class InAppWebViewClient extends WebViewClient {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override @Override
public void onReceivedClientCertRequest(final WebView view, final ClientCertRequest request) { public void onReceivedClientCertRequest(final WebView view, final ClientCertRequest request) {
final String url = view.getUrl();
InAppWebView webView = (InAppWebView) view;
URI uri;
try {
uri = new URI(view.getUrl());
} catch (URISyntaxException e) {
e.printStackTrace();
request.cancel();
return;
}
final String host = request.getHost(); final String host = request.getHost();
final String protocol = uri.getScheme(); String protocol = "https";
final String realm = null; final String realm = null;
final int port = request.getPort(); final int port = request.getPort();
if (url != null) {
try {
URI uri = new URI(url);
protocol = uri.getScheme();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
URLProtectionSpace protectionSpace = new URLProtectionSpace(host, protocol, realm, port, view.getCertificate(), null); URLProtectionSpace protectionSpace = new URLProtectionSpace(host, protocol, realm, port, view.getCertificate(), null);
ClientCertChallenge challenge = new ClientCertChallenge(protectionSpace, request.getPrincipals(), request.getKeyTypes()); ClientCertChallenge challenge = new ClientCertChallenge(protectionSpace, request.getPrincipals(), request.getKeyTypes());