使用内容安全策略> 插件类型在 chrome 中打开嵌入在 iframe 中的 PDF

Nar*_*esh 5 iframe google-chrome content-security-policy

我将 CSP(内容安全策略)插件类型策略设置为白名单 pdf 类型,如下所示。尝试在 iframe 中使用 src 属性打开 PDF 文件时,它在浏览器 IE 11 和 Firefox 47+ 中运行良好,但在 Chrome 50+ 中失败。让它在 chrome 中工作还需要什么?

Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'self'; style-src 'self'; frame-src 'self' plugin-types application/pdf;
Run Code Online (Sandbox Code Playgroud)

Chrome 控制台中的错误

Resource interpreted as Document but transferred with MIME type application/pdf
Refused to load 'http://127.0.0.1/module123/open.do?id=10000' (MIME type '') because it violates the following Content Security Policy Directive: 'plugin-types application/pdf'. When enforcing the 'plugin-types' directive, the plugin's media type must be explicitly declared with a 'type' attribute on the containing element (e.g. '<object type="[TYPE GOES HERE]" ...>').
Run Code Online (Sandbox Code Playgroud)

fre*_*ury 5

我有类似的问题。

为了解决这个问题,我需要将 blob: 添加到 object-src 指令中。

另外,我不需要指定插件类型。

所以它会是:

Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'self' blob:; style-src 'self'; frame-src 'self';