大网数据广告活动
网站运营与故障问答

defer img loading / CSP: allow embeded img

· 52CE 我爱测

My website is defering some img loading by setting the img src value by JavaScript (from the img data-src value). In order to have still valid HTML as long as the URL is not set to img src, I am setting src to an inline embeded i…

问题原文

原帖链接 · Marcus · CC BY-SA 3.0

My website is defering some img loading by setting the img src value by JavaScript (from the img data-src value). In order to have still valid HTML as long as the URL is not set to img src, I am setting src to an inline embeded img like this:

<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
data-src="URL-TO-REAL-IMAGE.png">

This works fine, but shows an error at the browser console (Firefox, Chrome):

Refused to load the image 'data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=' because it violates the following Content Security Policy directive: "default-src 'self' * 'unsafe-inline'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.

Shouldn't 'unsafe-inline' allow exactly this? I'm confused.

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 3.0

According to https://content-security-policy.com/ you need to use the CSP

img-src 'self' data:

Allows loading resources via the data scheme (eg Base64 encoded images).

unsafe-inline appears to be only relevant for javascript sources, not image sources.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。