我的PhoneGap应用程序出现"No Content-Security-Policy meta tag found."错误。

94

在我的系统中更新 Cordova 5.0 后,我创建了新的应用程序。当我在设备上测试我的应用程序时,控制台日志中出现了错误:

No Content-Security-Policy meta tag found.
Please add one when using the Cordova-plugin-whitelist plugin.: 23.
我在头部区域添加了meta标签。
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src: 'self' 'unsafe-inline' 'unsafe-eval'>

但是,我又一次遇到了同样的错误,在我使用的应用程序中,我使用了应用内浏览器插件和其他7个网站的链接。


3
你是否正确安装了 cordova-plugin-whitelist 插件 - https://github.com/apache/cordova-plugin-whitelist ?之后,你需要在你的 config.xml 文件中添加 <allow-navigation href="http://*/*" /> - Keval
1
谢谢Keval,添加了<allow-navigation href="http: //*/*" />之后,我的应用程序现在运行良好。再次感谢。 - user4809193
如何配置Cordova-android 4.0的白名单? - Richard Le Mesurier
3
当代码中缺少一个字符时可能会导致错误,为什么Stack Overflow不允许小于六个字符的编辑呢?这很容易修复,只是想在未来为他人节省几秒钟时间。meta标签的content属性末尾缺少一个双引号。 - Jason D.
6个回答

86
加入cordova-plugin-whitelist后,如果要保持特定网页链接的访问权限,则必须告诉应用程序允许访问所有网页链接或特定链接。

您可以将以下代码添加到应用程序根目录中的config.xml文件中:

文档中推荐的方法是:

<allow-navigation href="http://example.com/*" />
或:
<allow-navigation href="http://*/*" />

来自插件文档:

导航白名单

控制 WebView 本身可导航到哪些 URL。仅适用于顶级导航。

注意事项:在 Android 上,它还适用于非 http(s) 方案的 iframe。

默认情况下,只允许导航至 file:// URL。若要允许其他 URL,您必须在 config.xml 中添加 <allow-navigation> 标签:

<!-- Allow links to example.com -->
<allow-navigation href="http://example.com/*" />

<!-- Wildcards are allowed for the protocol, as a prefix
     to the host, or as a suffix to the path -->
<allow-navigation href="*://*.example.com/*" />

<!-- A wildcard can be used to whitelist the entire network,
     over HTTP and HTTPS.
     *NOT RECOMMENDED* -->
<allow-navigation href="*" />

<!-- The above is equivalent to these three declarations -->
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="data:*" />

@AmeePrajapati 请尝试访问http://stackoverflow.com/questions/37044969/cordova-5-1-1-there-was-a-network-error-message-in-onreceivederror-method-when。 - Kush Patel

38
你需要在应用的index.html的头部添加一个CSP元标签。
根据https://github.com/apache/cordova-plugin-whitelist#content-security-policy

Content Security Policy

Controls which network requests (images, XHRs, etc) are allowed to be made (via webview directly).

On Android and iOS, the network request whitelist (see above) is not able to filter all types of requests (e.g. <video> & WebSockets are not blocked). So, in addition to the whitelist, you should use a Content Security Policy <meta> tag on all of your pages.

On Android, support for CSP within the system webview starts with KitKat (but is available on all versions using Crosswalk WebView).

Here are some example CSP declarations for your .html pages:

<!-- Good default declaration:
    * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
    * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
    * Disables use of eval() and inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
        * Enable inline JS: add 'unsafe-inline' to default-src
        * Enable eval(): add 'unsafe-eval' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *">

<!-- Allow requests to foo.com -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' foo.com">

<!-- Enable all requests, inline styles, and eval() -->
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

<!-- Allow XHRs via https only -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">

<!-- Allow iframe to https://cordova.apache.org/ -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">

当我添加了CSP声明后,谷歌地图的以下代码就会失败,像这样。有什么想法吗?var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); //line 17311-09 21:17:30.724: D/SystemWebChromeClient(25692): file:///android_asset/www/index.html: Line 173 : Uncaught ReferenceError: google is not defined - shamaleyte
1
我需要使用/>来关闭元标签以便被识别。 - metamagikum

23

您的meta标签存在错误。

您的:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src: 'self' 'unsafe-inline' 'unsafe-eval'>

已更正:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"/>
注意 "script-src" 后面的冒号以及 meta 标签的结尾双引号。

3
当我加入<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"/>时,使用Ionic框架的实时重新加载功能停止工作,因此请注意其他人。 - CommonSenseCode
要重新激活Ionic实时重载,请将http://localhost:35729添加到script-src指令中,将ws://localhost:35729添加到connect-src指令中,并使用@codePlusPlus。 - kolli
@kolli,你能展示一下新指令的样子吗?不太清楚如何将它们添加到指令中。 - jessewolfe
我看到信息在原帖中。但是为了澄清: 请注意,通过“添加”,这意味着您可以将 script-src 'self' 'unsafe-inline' 'unsafe-eval' 替换为 script-src 'self' http://localhost:35279 'unsafe-inline' 'unsafe-eval', 并且您需要在内容属性的末尾添加一个新指令,并用分号隔开: ; script-src ws://localhost:35279 - jessewolfe
以上内容更正...对于第二部分,应该是; connect-src 'self' ws://localhost:35279。请注意,在我添加了'self'之前,我一直收到一个错误(无法访问file://<path to index.html>,因为CSP违规)。 - jessewolfe
显示剩余3条评论

2

对我来说,只需要重新安装白名单插件即可:

cordova plugin remove cordova-plugin-whitelist

然后

cordova plugin add cordova-plugin-whitelist

看起来从之前的Cordova版本更新不成功。


1
对我来说,问题在于我使用了过时的 cordova androidios 平台版本。因此,升级到 android@5.1.1ios@4.0.1 就解决了问题。
你可以升级到这些特定版本:
cordova platforms rm android
cordova platforms add android@5.1.1
cordova platforms rm ios
cordova platforms add ios@4.0.1

您是不是想说的是 Android 5.1.1 - mix3d
我同时遵循了@Maxim和Pierre-Alexis de Solminihac的建议,最终使我的应用程序正常工作。谢谢! - Zalakain

0

关于连接还有另一个问题。一些 Android 版本可以连接,但一些版本则不能。因此,这里提供了另一个解决方案。

在 AndroidManifest.xml 中:

<application ... android:usesCleartextTraffic="true">
        ...
    </application>

只需添加 'android:usesCleartextTraffic="true"',问题终于解决了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接