Skip to content

Commit

Permalink
Merge branch 'bugfix/android-unsafe-trust-manager'
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Oct 5, 2022
2 parents 0579de9 + 9557612 commit 6b94751
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.4.4+3

- Removed Android unsafe trust manager

## 5.4.4+2

- Fixed LICENSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,50 +177,12 @@ public PrivateKeyAndCertificates(PrivateKey privateKey, X509Certificate[] certif
}
}

public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};

// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});

OkHttpClient okHttpClient = builder
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
public static OkHttpClient getBasicOkHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.WebResourceResponse;

Expand All @@ -21,6 +20,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;

import javax.net.ssl.SSLHandshakeException;

import okhttp3.Request;
import okhttp3.Response;

Expand Down Expand Up @@ -181,7 +182,7 @@ public void run() {
Response response = null;

try {
response = Util.getUnsafeOkHttpClient().newCall(mRequest).execute();
response = Util.getBasicOkHttpClient().newCall(mRequest).execute();
byte[] dataBytes = response.body().bytes();
InputStream dataStream = new ByteArrayInputStream(dataBytes);

Expand All @@ -198,12 +199,14 @@ public void run() {
return new WebResourceResponse(contentType, encoding, dataStream);

} catch (Exception e) {
e.printStackTrace();
if (response != null) {
response.body().close();
response.close();
}
Log.e(LOG_TAG, e.getMessage());
if (!(e instanceof SSLHandshakeException)) {
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
}
}
}
break;
Expand Down Expand Up @@ -231,7 +234,7 @@ public ContentBlockerTriggerResourceType getResourceTypeFromUrl(String url) {
Request mRequest = new Request.Builder().url(url).head().build();
Response response = null;
try {
response = Util.getUnsafeOkHttpClient().newCall(mRequest).execute();
response = Util.getBasicOkHttpClient().newCall(mRequest).execute();

if (response.header("content-type") != null) {
String[] contentTypeSplitted = response.header("content-type").split(";");
Expand All @@ -251,8 +254,10 @@ public ContentBlockerTriggerResourceType getResourceTypeFromUrl(String url) {
response.body().close();
response.close();
}
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
if (!(e instanceof SSLHandshakeException)) {
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
}
}
}
return responseResourceType;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappwebview
description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window.
version: 5.4.4+2
version: 5.4.4+3
homepage: https://github.com/pichillilorenzo/flutter_inappwebview

environment:
Expand Down

0 comments on commit 6b94751

Please sign in to comment.