Ümit Aparı
Ümit Aparı

Reputation: 568

javascript source code change detection

I'm wondering if there is a way to detect the source code of javascript file that I'm using on my web site has changed? I am using ajax inside of it and I feel like it should me more secure since the code could easily be changed. I can do something like everytime the php file opens it could download the js file and check manually if code has changed with the help of some if else structures. But would it be healthy?

Upvotes: 4

Views: 914

Answers (1)

str
str

Reputation: 44969

It seems like you are looking for 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.

Using that technology, you can define a hash that your scripts are checked against. 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>

Note, however, that the browser support currently is not that great.

Upvotes: 5

Related Questions