Javascript source change detection

I am wondering if there is a way to detect the source code of the javascript file I am using on my website has changed? I am using ajax inside it and I feel like it should be more secure since the code can be easily changed. I can do something like this, every time the php file is opened, it can load the js file and check manually if the code has changed with some if else structures. But would it be great?

+3


source to share


1 answer


It seems that you are looking for Sub-Resource Integrity .

Subresource Integrity (SRI) is a security feature that allows browsers to verify that the files they retrieve (from a CDN, for example) are delivered without unexpected manipulation. It works by allowing you to provide a cryptographic hash that the selected file must match.

Using this technology, you can determine the hash with which your scripts are checked. If they do not match, the scripts are not executed. For example:



<script src="https://example.com/example-framework.js"
        integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
        crossorigin="anonymous"></script>

      

Please note, however, that browser support is currently not that great.

+5


source







All Articles