我想知道是否有一种方法可以检测我在网站上使用的 javascript 文件的源代码是否已更改?我在其中使用了 ajax,我觉得它应该更安全,因为代码可以很容易地更改。我可以做一些事情,比如每次打开 php 文件时,它可以下载 js 文件,并在一些 if else 结构的帮助下手动检查代码是否已更改。但它会健康吗?
最佳答案
你好像在找Subresource Integrity .
Subresource Integrity (SRI) is a security feature that enables browsers to verify that files they fetch (for example, from a CDN) are delivered without unexpected manipulation. It works by allowing you to provide a cryptographic hash that a fetched file must match.
使用该技术,您可以定义用于检查脚本的散列。如果它们不匹配,则不执行脚本。例如:
<script src="https://example.com/example-framework.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous"></script>
但是请注意,browser support目前不是很好。
关于javascript 源代码更改检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43785903/