因为它违反了以下内容安全策略指令:"style-src 'self'"。

9

我有一个带有以下头信息的网页。

这是一个非交互式页面,只使用了 Twitter Bootstrap JS。

<head>
    <title>Versions: unknown bl version vs. 1.0.487 [flavor: HISTORIC_TIME]</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
    <script type="script" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
    <link rel="icon" href="/jenkins/view/QA/job/RoutingRegression/ws/src/main/resources/html_pages/images/favicon.png" type="image/gif" sizes="16x16">
</head>

我是一个有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

enter image description here

我看到了一些关于 StackOverflow 上如何解决这个问题的帖子,但是并不能理解。

拒绝加载样式表'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css',因为它违反了以下内容安全策略指令:"style-src 'self'"。

landing_page.html: 1 拒绝加载样式表'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css',因为它违反了以下内容安全策略指令:"style-src 'self'"。

我尝试将 <meta> 更改为

<meta content="text/html; charset=utf-8 ;script-src 'self' http://onlineerp.solution.quebec 'unsafe-inline' 'unsafe-eval';" http-equiv="content-type">

enter image description here


你是否正在使用JS框架或强制执行CSP?这里有更多关于如何实现它的信息:http://www.html5rocks.com/en/tutorials/security/content-security-policy/#source-whitelists - Patrick Berkeley
你能告诉我们关于你的服务器,客户端上运行的内容等吗?这可能是许多不同的事情,我们需要缩小范围。 - user4454229
当我在本地运行时,CSS会出现。 - Elad Benda
@rac 这是一个非交互式页面,只有 Twitter Bootstrap JS。 - Elad Benda
如果您正在从Jenkins提供此内容(如您的类似下一个问题中所述),那么问题可能出在Jenkins的默认内容安全策略上。如果最近升级了Jenkins,那么这可能解释了为什么它曾经可以工作但现在不行了。 - Dave Bacher
3个回答

11

尝试将CSP分离为单独的标签,并添加一个style-src引用,就像这样:

<meta http-equiv="content-type" content="text/html; charset=utf-8 ;">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' http://onlineerp.solution.quebec 'unsafe-inline' 'unsafe-eval'; style-src 'self' maxcdn.bootstrapcdn.com"> 

这应该表明你信任来自 maxcdn.bootstrapcdn.com 的样式。

有关内容安全策略的很好的解释在http://content-security-policy.com/


3
为什么这突然发生了,而且它昨天还能正常工作? - Elad Benda
没有帮助。请查看照片:[1]: http://i.stack.imgur.com/CMzrJ.png [2]: http://i.stack.imgur.com/R7FUm.png - Elad Benda
很难在没有你的全部代码的情况下知道问题所在,请查看https://dev59.com/_V0a5IYBdhLWcg3wfIl-。 - Rob Imig

2
将Content-Security-Policy元标签添加到您的头文件中,如下所示:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' *.bootstrapcdn.com">

它将允许您从域名bootstrapcdn.com加载JavaScript、图像、CSS、字体、AJAX请求、帧和HTML5媒体等内容。

如果您仍然遇到相同的错误报告,则问题可能在于您使用的框架。我曾经在play框架2.6.17中遇到类似的问题,由于默认启用了自己的Content-Security-Policy头,因此进行了修复:

play.filters.headers.contentSecurityPolicy="default-src 'self' *.bootstrapcdn.com"

0

你不能使用CDN来加载js,你需要复制 "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" 的内容,并在你的js目录下创建一个新文件,命名为bootstrap.min.js,并将所有内容粘贴到其中。然后在你的HTML文件头部删除包含该URL的行,并使用这个新的URL。

<script src="js/jquery.min.js"></script>

请确保这是包含所提及的URL中所有数据的文件的正确路径

谢谢


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