简体   繁体   English

PHP中的子资源完整性和缓存清除技术

[英]Subresource integrity and cache busting techniques in PHP

I'd like to implement Subresource Integrity and cache busting for static assets such as stylesheets and JavaScript files in my application. 我想为应用程序中的样式表和JavaScript文件等静态资产实现Subresource Integrity缓存清除 Currently I use PHP with Twig templates. 目前,我将PHP与Twig模板一起使用。

I know there are many tools out there to generate hashes for all the JS and CSS files but I am looking for how to implement the hashes into the <script> and <link> tags for hundreds of files. 我知道有很多工具可以为所有JS和CSS文件生成哈希,但是我正在寻找如何将哈希实现到数百个文件的<script><link>标记中。

This blog post described most of what I'm trying to do, however the author only covers cache busting and uses a static timestamp in the file name that he changes manually every time. 这篇博客文章描述了我正在尝试做的大部分事情,但是作者只介绍了缓存清除,并且在他每次手动更改的文件名中都使用了静态时间戳。 Using a build tool to programatically generate that timestamp isn't difficult either but with SRI the value is a hash, which is different for every file . 使用构建工具以编程方式生成该时间戳也不难,但是使用SRI时,该值是哈希值, 每个文件不同

For example, a snippet of header.html.twig : 例如, header.html.twig的片段:

<!-- cdn requests -->

<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'
    integrity='sha384-8gBf6Y4YYq7Jx97PIqmTwLPin4hxIzQw5aDmUg/DDhul9fFpbbLcLh3nTIIDJKhx'
    crossorigin='anonymous'></script>

<!-- same-origin requests -->

<script src='foo.1a516fba.min.js' 
    integrity='sha384-GlFvui4Sp4wfY6+P13kcTmnzUjsV78g61ejffDbQ1QMyqL3lVzFZhGqawasU4Vg+'></script>
<script src='bar.faf315f3.min.js'
    integrity='sha384-+vMV8w6Qc43sECfhc+5+vUA7Sg4NtwVr1J8+LNNROMdHS5tXrqGWSSebmORC6O86'></script>

Changing the src / href and integrity attributes every time is not a sane approach. 每次都更改src / hrefintegrity属性不是明智的方法。

I could write a Twig function that calls a PHP function to hash the file every time and it may work on OK on dev but that seems awfully computationally expensive. 我可以编写一个Twig函数,该函数每次都调用PHP函数对文件进行哈希处理,并且可以在dev上正常运行,但是在计算上似乎非常昂贵。

What is a feasible approach to this? 有什么可行的方法呢?

To answer your question: There is no feasible approach because this is not a proper application of Subresource Integrity. 要回答您的问题:没有可行的方法,因为这不是子资源完整性的正确应用。

According to W3C the integrity attribute is: 根据W3C ,完整性属性为:

...a mechanism by which user agents may verify that a fetched resource has been delivered without unexpected manipulation ...一种机制,通过该机制用户代理可以验证获取的资源是否已交付而无需意外操作

It was introduced because these days lots of pages are fetching their CSS and JS scripts from CDNs like you are and if a hacker were ever to gain control of a CDN they could wreak an extraordinary amount of havoc across thousands of websites by injecting malicious code into the resources delivered! 它之所以被引入是因为如今,许多页面都像您一样从CDN获取其CSS和JS脚本,如果黑客曾经获得CDN的控制权,他们可能会通过向其注入恶意代码而在数千个网站上造成巨大破坏。交付的资源!

Imagine if every version of jQuery delivered by code.jquery.com or ajax.googleapis.com suddenly contained malicious code! 想象一下,如果code.jquery.comajax.googleapis.com提供的jQuery的每个版本突然包含恶意代码! How many sites would be affected? 多少个站点会受到影响? Scary. 害怕。

By providing the agent (browser) with an integrity hash that the contents of the fetched resource should be compared against, you are ensuring the agent only continues to execute the code if it gets exactly what you told it to expect. 通过为代理程序(浏览器)提供完整性哈希,应将获取的资源的内容与之进行比较,可以确保代理程序仅在代码完全符合您的预期时才继续执行代码。 If it's different, don't trust it! 如果不同,请不要相信它!

In the case of the resources in your application, I assume they exist on the same server so there is no middle route to intercept. 对于您的应用程序中的资源,我假设它们存在于同一服务器上,因此没有中间路线可以拦截。 If a hacker gains control of your server and injects malicious code in the JS scripts, they could just as easily rehash the contents and change the integrity attribute in your HTML as well. 如果黑客控制了您的服务器并在JS脚本中注入了恶意代码,他们也可以轻松地重新哈希内容并更改HTML中的完整性属性。 Subresource Integrity offers no additional security check. 子资源完整性不提供其他安全检查。

But... 但...

Just for the sport of solving what is quite a fun problem I would suggest if you wanted to dynamically generate the hash for the integrity attribute: 仅出于解决一个非常有趣的问题的目的,我建议您是否要动态生成integrity属性的哈希值:

Use Gulp (my personal preference) to concatenate, minify and thumbprint the filename of your resource. 使用Gulp(我个人的喜好)来连接,缩小和指纹化资源的文件名。 Read the contents of the generated file using gulp.src('bar.*.min.js') . 使用gulp.src('bar.*.min.js')读取生成文件的内容。 Use the NPM sha1 package to create the hash as a variable and finally maybe use gulp-inject to change the src attribute and then gulp-replace to write the integrity attribute too. 使用NPM sha1软件包将散列创建为变量,最后使用gulp-inject更改src属性,然后使用gulp-replace编写integrity属性。 Some flow like that is what I would go for :-) 我想要的是这样的流程:-)

I hope that answers your question. 我希望能回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM