如何解决aosp中不允许的明文

How to resolve cleartext not permitted in aosp

我知道 android 默认禁用明文。我可以知道我可以在 aosp 中启用的确切位置,而不是添加所有带有网络配置文件的包吗?

我可以通过添加以下行来允许在哪里?

cleartextTrafficPermitted="true

external/okhttp/android/main/java/com/squareup/okttp/Handler

 public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) {
        OkHttpClient client = new OkHttpClient();

        // Explicitly set the timeouts to infinity.
        client.setConnectTimeout(0, TimeUnit.MILLISECONDS);
        client.setReadTimeout(0, TimeUnit.MILLISECONDS);
        client.setWriteTimeout(0, TimeUnit.MILLISECONDS);

        // Set the default (same protocol) redirect behavior. The default can be overridden for
        // each instance using HttpURLConnection.setInstanceFollowRedirects().
        client.setFollowRedirects(HttpURLConnection.getFollowRedirects());

        // Do not permit http -> https and https -> http redirects.
        client.setFollowSslRedirects(false);

        // Permit cleartext traffic only (this is a handler for HTTP, not for HTTPS).
        client.setConnectionSpecs(CLEARTEXT_ONLY);

        // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using
        // ProxySelector.getDefault().
        if (proxy != null) {
            client.setProxy(proxy);
        }

        // OkHttp requires that we explicitly set the response cache.
        OkUrlFactory okUrlFactory = new OkUrlFactory(client);

        // Use the installed NetworkSecurityPolicy to determine which requests are permitted over
        // http.
        OkUrlFactories.setUrlFilter(okUrlFactory, CLEARTEXT_FILTER);

        ResponseCache responseCache = ResponseCache.getDefault();
        if (responseCache != null) {
            AndroidInternal.setResponseCache(okUrlFactory, responseCache);
        }
        return okUrlFactory;
    }

    private static final class CleartextURLFilter implements URLFilter {
        @Override
        public void checkURLPermitted(URL url) throws IOException {
            String host = url.getHost();
            if (!NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(host)) {
                throw new IOException("Cleartext HTTP traffic to " + host + " not permitted");
            }
        }
    }

在任何应用程序中,如果我使用 http,我都会收到错误消息,因为不允许到 124.60.5.6 的明文 HTTP 流量";

因此,可以在 aosp 中进行更改,而不是在应用程序中进行更改吗?

您需要转到 AndroidManifest.xml 并添加

<application
    android:usesCleartextTraffic="true"
    android:networkSecurityConfig="@xml/network_security_config"
    ....
</application>

我强烈建议您创建 network_security_config 以仅允许您的域和子域。这是一个快速 tutorial

看来够了

builder.setCleartextTrafficPermitted(true);

第 189 行似乎就足够了,因为您使用的是较旧的应用程序,这些应用程序可能没有任何网络配置,只使用默认配置。

来源:https://android.googlesource.com/platform/frameworks/base.git/+/refs/heads/master/core/java/android/security/net/config/NetworkSecurityConfig.java#189


旧答案

我希望你已经完成了关于绕过安全功能的影响的功课。话虽这么说,负责异常的 class 在包 android.security.net.config 的框架中,而 class 负责 NetworkSecurityConfig

在撰写此答案时,静态构建器 class 有一个 属性 boolean mCleartextTrafficPermittedSet,默认设置为 false。您可能必须将其默认为 true,这使得方法 getEffectiveCleartextTrafficPermitted()NetworkSecurityConfig class return mCleartextTrafficPermitted 中,在 return returns DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED 默认设置为 true

流程是

getEffectiveCleartextTrafficPermitted() returns mCleartextTrafficPermitted returns DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED returns true 默认.

如果这一切都令人困惑,请在创建构建器时对构建器调用 setCleartextTrafficPermitted(true)

class 的来源可在此处获得:https://android.googlesource.com/platform/frameworks/base.git/+/refs/heads/master/core/java/android/security/net/config/NetworkSecurityConfig.java

注意:我没有尝试过这个,只是通过源代码推断出以上内容。如果有什么不对,欢迎您尝试纠正我。

通过@Shadow 更新进行编辑:

在 NetworkSecurityConfig 中,将布尔变量从 true 更改为 false。

   //public static final boolean DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED = true;
    public static final boolean DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED = false;

同样在 ManifestConfigSource 中,注释以下行,

  /*boolean usesCleartextTraffic =
                        (mApplicationInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0
                        && mApplicationInfo.targetSandboxVersion < 2;*/

并直接申请 usesCleartextTraffic as true.

 boolean usesCleartextTraffic =true;