Skip to content

Commit 48ff30b

Browse files
committed
Merge branch 'venilnoronha-issue-2513-fix'
* venilnoronha-issue-2513-fix: Fixes square#2513 - NetworkSecurityPolicy based ConnectionSpec setup.
2 parents 209c6c5 + e3cd9b9 commit 48ff30b

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

okhttp/src/main/java/okhttp3/OkHttpClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ public class OkHttpClient implements Cloneable, Call.Factory {
6565
private static final List<Protocol> DEFAULT_PROTOCOLS = Util.immutableList(
6666
Protocol.HTTP_2, Protocol.SPDY_3, Protocol.HTTP_1_1);
6767

68-
private static final List<ConnectionSpec> DEFAULT_CONNECTION_SPECS = Util.immutableList(
69-
ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT);
68+
private static final List<ConnectionSpec> DEFAULT_CONNECTION_SPECS;
7069

7170
static {
71+
List<ConnectionSpec> connSpecs = new ArrayList<>(Arrays.asList(ConnectionSpec.MODERN_TLS,
72+
ConnectionSpec.COMPATIBLE_TLS));
73+
if (Platform.get().isCleartextTrafficPermitted()) {
74+
connSpecs.add(ConnectionSpec.CLEARTEXT);
75+
}
76+
DEFAULT_CONNECTION_SPECS = Util.immutableList(connSpecs);
77+
7278
Internal.instance = new Internal() {
7379
@Override public void addLenient(Headers.Builder builder, String line) {
7480
builder.addLenient(line);

okhttp/src/main/java/okhttp3/internal/Platform.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
*
6868
* <p>Supported on Android 2.3+ and OpenJDK 7+. There are no public APIs to recover the trust
6969
* manager that was used to create an {@link SSLSocketFactory}.
70+
*
71+
* <h3>Android Cleartext Permit Detection</h3>
72+
*
73+
* <p>Supported on Android 6.0+ via {@code NetworkSecurityPolicy}.
7074
*/
7175
public class Platform {
7276
private static final Platform PLATFORM = findPlatform();
@@ -128,6 +132,10 @@ public void log(String message) {
128132
System.out.println(message);
129133
}
130134

135+
public boolean isCleartextTrafficPermitted() {
136+
return true;
137+
}
138+
131139
public static List<String> alpnProtocolNames(List<Protocol> protocols) {
132140
List<String> names = new ArrayList<>(protocols.size());
133141
for (int i = 0, size = protocols.size(); i < size; i++) {
@@ -298,6 +306,25 @@ public Android(Class<?> sslParametersClass, OptionalMethod<Socket> setUseSession
298306
} while (i < newline);
299307
}
300308
}
309+
310+
@Override public boolean isCleartextTrafficPermitted() {
311+
try {
312+
Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy");
313+
Method getInstanceMethod = networkPolicyClass.getMethod("getInstance");
314+
Object networkSecurityPolicy = getInstanceMethod.invoke(null);
315+
Method isCleartextTrafficPermittedMethod = networkPolicyClass
316+
.getMethod("isCleartextTrafficPermitted");
317+
boolean cleartextPermitted = (boolean) isCleartextTrafficPermittedMethod
318+
.invoke(networkSecurityPolicy);
319+
return cleartextPermitted;
320+
} catch (ClassNotFoundException e) {
321+
return super.isCleartextTrafficPermitted();
322+
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
323+
| InvocationTargetException e) {
324+
throw new AssertionError();
325+
}
326+
}
327+
301328
}
302329

303330
/**

0 commit comments

Comments
 (0)