0

我有一个 s3 存储桶中的所有图像,我需要在主页和其他页面上显示它们。为了得到他们,我有这个代码:

   <img src="https://bucket-name.s3.eu-west-3.amazonaws.com/cover/img-name.jpeg">

在 app.js 我使用这样的头盔:

app.use(helmet({
contentSecurityPolicy: {
    useDefaults: true,
    directives: {

    // other directives


    "img-src": ["'self'",
        "https://bucket-name.s3.eu-west-3.amazonaws.com/*"
    ]
}

为什么我每次都会收到这个错误?

Refused to load the image '<URL>' because it violates the following Content Security Policy directive: "img-src 'self' data:".
4

1 回答 1

0

CSP 不支持*在路径部分使用通配符。

请使用https://bucket-name.s3.eu-west-3.amazonaws.comor https://bucket-name.s3.eu-west-3.amazonaws.com/cover/host-source 而不是https://bucket-name.s3.eu-west-3.amazonaws.com/*.

另请注意,您显示 CSP:"img-src": ["'self'", "https://bucket-name.s3.eu-west-3.amazonaws.com/*"]但浏览器显示"img-src 'self' data:"违规:

Refused to load the image '<URL>' because it violates the following
 Content Security Policy directive: "img-src 'self' data:".

这意味着您正在其他地方发布 CSP,可能是通过元标记。

于 2021-09-09T15:17:33.843 回答