内容安全策略:"img-src 'self' data:"
Content Security Policy: "img-src 'self' data:"
我有一个应用程序,用户可以在其中复制图像 URL,将其粘贴到输入框,图像将加载到一个盒子上。
但是我的应用程序不断触发此消息:
Refused to load the image 'LOREM_IPSUM_URL' because it violates the following Content Security Policy directive: "img-src 'self' data:".
这是我的元标记:
<meta http-equiv="Content-Security-Policy" content="default-src *;
img-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
style-src 'self' 'unsafe-inline' *">
我在应用程序中使用 html2Canvas,当我删除它时:“img-src 'self' 数据:”
它引发了这个错误:
html2canvas.js:3025 Refused to load the image 'data:image/svg+xml,
<svg xmlns='http://www.w3.org/2000/svg'></svg>' because it violates
the following Content Security Policy directive: "default-src *".
Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
还有一些其他错误。
尝试替换此部分:
img-src * 'self' data: https:;
所以完整的标签:
<meta http-equiv="Content-Security-Policy" content="default-src *;
img-src * 'self' data: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
style-src 'self' 'unsafe-inline' *">
img-src * 'self' data: https:;
不是一个好的解决方案,因为它会使您的应用容易受到 XSS 攻击。这里最好的解决方案应该是:img-src 'self' data:image/svg+xml
。如果它不起作用,请尝试:img-src 'self' data:
如果你的指令仍然是 img-src * 'self' data: https:;
,请考虑更改它
除了上面@manzapanza 提供的内容之外,您还需要确保您的应用程序的网络配置文件中是否未配置 CSP,因为如果该设置存在,它将覆盖您的元标记设置索引文件如下例所示:
索引元标记:
<meta http-equiv="Content-Security-Policy" content="default-src *;img-src * 'self' data: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *">
正在被您的网络配置文件中的 CSP 设置覆盖。
Web 配置设置:
<add name="Content-Security-Policy" value="default-src https: http: 'unsafe-inline'; img-src * 'self' data: https:;" />
在这种情况下,考虑在系统的 Web 配置文件中设置一个。
这简单地解决了问题:
img-src 'self' data:
但要确保使用分号 (;) 分隔多个指令
对于头盔用户。
更好的做法是将 contentSecurityPolicy 设置为 false,这应该是最后一个选项。我在 my app and it solves the issue very well. My app is hosted here. Checkout my source code here.
中使用了这个
app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
"img-src": ["'self'", "https: data:"]
}
})
)
我有一个应用程序,用户可以在其中复制图像 URL,将其粘贴到输入框,图像将加载到一个盒子上。
但是我的应用程序不断触发此消息:
Refused to load the image 'LOREM_IPSUM_URL' because it violates the following Content Security Policy directive: "img-src 'self' data:".
这是我的元标记:
<meta http-equiv="Content-Security-Policy" content="default-src *;
img-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
style-src 'self' 'unsafe-inline' *">
我在应用程序中使用 html2Canvas,当我删除它时:“img-src 'self' 数据:”
它引发了这个错误:
html2canvas.js:3025 Refused to load the image 'data:image/svg+xml,
<svg xmlns='http://www.w3.org/2000/svg'></svg>' because it violates
the following Content Security Policy directive: "default-src *".
Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
还有一些其他错误。
尝试替换此部分:
img-src * 'self' data: https:;
所以完整的标签:
<meta http-equiv="Content-Security-Policy" content="default-src *;
img-src * 'self' data: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
style-src 'self' 'unsafe-inline' *">
img-src * 'self' data: https:;
不是一个好的解决方案,因为它会使您的应用容易受到 XSS 攻击。这里最好的解决方案应该是:img-src 'self' data:image/svg+xml
。如果它不起作用,请尝试:img-src 'self' data:
如果你的指令仍然是 img-src * 'self' data: https:;
除了上面@manzapanza 提供的内容之外,您还需要确保您的应用程序的网络配置文件中是否未配置 CSP,因为如果该设置存在,它将覆盖您的元标记设置索引文件如下例所示:
索引元标记:
<meta http-equiv="Content-Security-Policy" content="default-src *;img-src * 'self' data: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *">
正在被您的网络配置文件中的 CSP 设置覆盖。
Web 配置设置:
<add name="Content-Security-Policy" value="default-src https: http: 'unsafe-inline'; img-src * 'self' data: https:;" />
在这种情况下,考虑在系统的 Web 配置文件中设置一个。
这简单地解决了问题:
img-src 'self' data:
但要确保使用分号 (;) 分隔多个指令
对于头盔用户。 更好的做法是将 contentSecurityPolicy 设置为 false,这应该是最后一个选项。我在 my app and it solves the issue very well. My app is hosted here. Checkout my source code here.
中使用了这个app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
"img-src": ["'self'", "https: data:"]
}
})
)