Update InAppWebViewClient.java

Added more checks when trying to parse an URL using URI and allow the request to go to the flutter delegate.
This commit is contained in:
Lorenzo Pichilli 2022-10-13 17:03:01 +02:00 committed by GitHub
parent 93d9b41ed8
commit 169bf2d340
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 47 deletions

View File

@ -328,34 +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;
String url = view.getUrl();
if (url == null) return; if (url != null) {
uri = new URI(url); try {
} catch (URISyntaxException e) { URI uri = new URI(url);
e.printStackTrace(); protocol = uri.getScheme();
port = uri.getPort();
credentialsProposed = null; } catch (URISyntaxException e) {
previousAuthRequestFailureCount = 0; e.printStackTrace();
}
handler.cancel();
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);
@ -367,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) {
@ -380,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;
@ -421,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);
@ -475,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());