将内容安全策略scipt-src sha功能与Apache HTTP Server一起使用 [英] Use Content Security Policy scipt-src sha feature with Apache HTTP Server

查看:207
本文介绍了将内容安全策略scipt-src sha功能与Apache HTTP Server一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache 2.4,我正在尝试实施内容安全策略。
我在 httpd.conf

中设置了以下标题

 标题集内容 - 安全策略默认'无'; script-src'self'

现在,我有内联在html中使用脚本标记的javascript代码,如

 < script> alert('在此处执行操作)< /脚本> 

请注意,脚本标记包含有意义的JS代码,而不是我为示例目的而提供的警报。 / p>

现在,内容安全策略会阻止此类内联脚本。将这些列入白名单的一种方法是为上述脚本计算 SHA 哈希,并在标题中明确地将其传递为

 标题集内容安全策略默认'无'; script-src'self'sha256-dhfds73748 ==

这允许脚本运行。



现在我可以使用grunt在构建时为这些类型的脚本生成SHA。



但我必须在Apache httpd.conf 中手动设置这些,因为我对这些脚本进行了更改,然后手动重启我的Apache HTTP Server吗?



我可以在运行时在Apache配置中设置此值吗?



请注意,我不想将此类代码移至一个单独的js文件
,然后通过< script src =path / to / my / file.js>< / script> 调用它,除非这是唯一可行的方法。

解决方案

这是静态html还是这是一个Web应用程序?如果它是一个Web应用程序,动态策略相当容易。它是一个使用某种模板的静态网站(例如Jekyll),您可以使用csp元标记轻松更新策略。



如果上述情况都不正确,我将不得不推荐具有特定Apache经验的人。我不认为在许多静态页面上管理元标记是一种很好的方法。


I'm using Apache 2.4 and I'm trying to implement Content Security Policy. I have set the following header in my httpd.conf

Header set Content-Security-Policy "default 'none'; script-src 'self'"

Now, I have inline javascript code using script tag inside html, as

<script>alert('Do something here')</script>

Note that script tag contains meaningful JS code, not this alert stuff I have put for sample purposes.

Now, Content Security Policy blocks such inline scripts. One way to whitelist these is to compute SHA hash for the aforementioned script and explicitly pass it in header as

Header set Content-Security-Policy "default 'none'; script-src 'self' sha256-dhfds73748=="

This allows the script to run.

Now I can generate SHAs for these type of scripts at build time using grunt.

But do I have to manually set these inside Apache httpd.conf as I make changes to these scripts and then manually restart my Apache HTTP Server?

Can I set this value at runtime inside Apache config?

Please note that I don't want to move the such code to a separate js file and then call it via <script src="path/to/my/file.js"></script> unless it is the only way possible.

解决方案

Is this for static html or is this a web application? If it's a web application, a dynamic policy is fairly easy. It it's a static site using some sort of templating (e.g. Jekyll) you can use the csp meta tags to easily update a policy.

If neither is true above, I'll have to defer to those with specific Apache experience. I don't think managing meta tags over many static pages is a good approach.

这篇关于将内容安全策略scipt-src sha功能与Apache HTTP Server一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆