Cordova 5:白名单阻止 ajax 调用
Cordova 5: whitelist blocking ajax calls
我正在将一些项目从旧的 Cordova 版本 2.x 和 3.x 升级到最新版本 5.1。
我被迫这样做是因为 Google Play 将在 5 月劫持 4.1.1 之前的 Cordova 应用程序(没有新的提交,也没有对现有应用程序的更新)。
我正在升级的遗留项目具有白名单中允许的所有 URL。我在 WebView 中加载的页面包含在 apk 资产中,但对某些远程资源 URL 进行 ajax 调用的操作至关重要。这些资源由各种领域的客户公司发布,并且由于它们有数千个,所以将它们列入白名单是不切实际的。
现在 Cordova >4,无论你喜不喜欢,你都必须下载 whitelist plugin。
在新的 config.xml 文件中我有:
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" />
但这似乎还不够。 logcat 中有一些关于必须修改我的 HTML 以包含 Content Security Policy.
的警告
所以我将此添加到我的页面:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
现在我遇到了一个问题,因为旧版应用程序与 JellyBean 设备兼容,但白名单插件 CSP 功能仅在 KitKat 设备中受支持。
尽管如此,我已经在 Lollipop 设备中测试了升级后的应用程序,但 ajax 呼叫一直被阻止。
有没有办法在不使用 CSP 的情况下将所有可能的域列入白名单,以便我可以 运行 我的应用程序在 JellyBean 中?
如果不是,那么限制最少的内容安全警察是什么?显然通配符不起作用。
这个通配符在元标记中对我有用,试试吧。
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data:; default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
我在 config.xml 文件中有与您相同的内容,所以这应该可以工作
我自己找到了答案。
是的,可以在不使用 CSP 功能的情况下将其列入白名单。只需将这些通配符添加到 res/xml/config.xml
文件就足够了:
<allow-navigation href="*" />
<access origin="*" />
我的问题不同。白名单插件的 js 和 java 文件存在,但是在将配置文件升级为新格式时我忘记为其添加功能:
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param name="onload" value="true" />
</feature>
所有人注意 此答案不适用于 CLI。 <feature>
在这种情况下是指 SDK 和那些使用 IDE 的人。请参阅文档 The feature Element
引用:
If you use the CLI to build applications, you use the plugin command to enable device APIs. This does not modify the top-level config.xml file, so the <feature>
element does not apply to your workflow.
If you work directly in an SDK and using the platform-specific config.xml file as source, you use the <feature>
tag to enable device-level APIs and external plugins. They often appear with custom values in platform-specific config.xml files.
我正在将一些项目从旧的 Cordova 版本 2.x 和 3.x 升级到最新版本 5.1。 我被迫这样做是因为 Google Play 将在 5 月劫持 4.1.1 之前的 Cordova 应用程序(没有新的提交,也没有对现有应用程序的更新)。
我正在升级的遗留项目具有白名单中允许的所有 URL。我在 WebView 中加载的页面包含在 apk 资产中,但对某些远程资源 URL 进行 ajax 调用的操作至关重要。这些资源由各种领域的客户公司发布,并且由于它们有数千个,所以将它们列入白名单是不切实际的。
现在 Cordova >4,无论你喜不喜欢,你都必须下载 whitelist plugin。 在新的 config.xml 文件中我有:
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" />
但这似乎还不够。 logcat 中有一些关于必须修改我的 HTML 以包含 Content Security Policy.
的警告所以我将此添加到我的页面:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
现在我遇到了一个问题,因为旧版应用程序与 JellyBean 设备兼容,但白名单插件 CSP 功能仅在 KitKat 设备中受支持。
尽管如此,我已经在 Lollipop 设备中测试了升级后的应用程序,但 ajax 呼叫一直被阻止。
有没有办法在不使用 CSP 的情况下将所有可能的域列入白名单,以便我可以 运行 我的应用程序在 JellyBean 中?
如果不是,那么限制最少的内容安全警察是什么?显然通配符不起作用。
这个通配符在元标记中对我有用,试试吧。
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data:; default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
我在 config.xml 文件中有与您相同的内容,所以这应该可以工作
我自己找到了答案。
是的,可以在不使用 CSP 功能的情况下将其列入白名单。只需将这些通配符添加到 res/xml/config.xml
文件就足够了:
<allow-navigation href="*" />
<access origin="*" />
我的问题不同。白名单插件的 js 和 java 文件存在,但是在将配置文件升级为新格式时我忘记为其添加功能:
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param name="onload" value="true" />
</feature>
所有人注意 此答案不适用于 CLI。 <feature>
在这种情况下是指 SDK 和那些使用 IDE 的人。请参阅文档 The feature Element
引用:
If you use the CLI to build applications, you use the plugin command to enable device APIs. This does not modify the top-level config.xml file, so the
<feature>
element does not apply to your workflow.If you work directly in an SDK and using the platform-specific config.xml file as source, you use the
<feature>
tag to enable device-level APIs and external plugins. They often appear with custom values in platform-specific config.xml files.