我正在努力使reCAPTCHA与严格的内容一起工作 安全 </跨度> 政策 </跨度> 。这是我的基本版本,它可以正常工作:
HTML
&lt; script src =’//www.google.com/recaptcha/api.js’async defer>&lt; / script&gt;
HTTP标头
内容- 安全 </跨度> - 政策 </跨度> :default-src’self’; script-src’self’www.google.com www.gstatic.com; style-src’self’https:‘unsafe-inline’; frame-src www.google.com
reCAPTCHA服务已更新 style-src 'unsafe-inline' 不再需要了。 HTML和HTTP标题下面应该可以工作 这个 已结束的问题
style-src 'unsafe-inline'
HTML:
<script src="https://www.google.com/recaptcha/api.js?render=site-key" nonce="{NONCE}"></script>
HTTP标头:
Content-Security-Policy: default-src 'self'; script-src 'nonce-{NONCE}'; img-src www.gstatic.com; frame-src www.google.com; object-src 'none'; base-uri 'none';
在AS.Net应用程序的web.config文件中使用以下代码。
<child-src> <add source="www.google.com"/>
我不相信reCAPTCHA在不允许内联样式的情况下支持CSP。我发现 这个公开的问题 在reCAPTCHA Github存储库中。
此示例页面使用CSP进行reCAPTCHA 有效,因为它没有定义 style-src 要么 default-src 。检查页面的DOM会显示这个插入的div:
style-src
default-src
<div class="g-recaptcha" data-sitekey="6LdOLRgTAAAAAPYECt9KLIL_LLwOuuuHAUw7QUTm"> <div style="width: 304px; height: 78px;"> <div> <iframe src="https://www.google.com/recaptcha/api2/anchor?k=6LdOLRgTAAAAAPYECt9KLIL_LLwOuuuHAUw7QUTm&co=aHR0cHM6Ly9jc3AtZXhwZXJpbWVudHMuYXBwc3BvdC5jb206NDQz&hl=en&v=r20160926121436&size=normal&cb=g72al0v10dxg" title="recaptcha widget" width="304" height="78" role="presentation" frameborder="0" scrolling="no" name="undefined"></iframe> </div> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none; display: none; "></textarea> </div> </div>
内联样式 textarea 评估为 MammJ3J+TGIHdHxYsGLjD6DzRU0ZmxXKZ2DvTePAF0o= 由下式决定:
textarea
MammJ3J+TGIHdHxYsGLjD6DzRU0ZmxXKZ2DvTePAF0o=
echo -n "width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none; display: none; " | openssl dgst -sha256 -binary | base64
这与您的错误消息中指示的哈希相匹配。但是,包括哈希 style-src 不适用于Chrome中的内嵌样式 根据这个开放的bug 。看来这将得到支持 unsafe-hashed-attributes 据我所知,还没有进入Chrome版本。
unsafe-hashed-attributes
对于它的价值,添加哈希使其在Firefox中可用。