I use Hugo’s fingerprint pipe feature for all the local javascript files my website uses paired with the integrity="" attribute in each <script> tag. Everything worked perfectly until now; my browser stopped using my script files because of this error message in the console:
Failed to find a valid digest in the 'integrity' attribute for resource 'https://mysite.net/js/main.min.0e62b29cf0b0474b2ef7d54a5e40d662cc965393488fe25a78a61a2415bacf29.js' with computed SHA-256 integrity 'P7rRdXhSovg+7BMEijv7+S50RoOSKrKSSI5Vk8X90Qg='. The resource has been blocked.
I tried changing the way fingerprint is applied to the file because of this post and tried with “sha256”, and “sha512”, putting this for every local script tag:
However after building (with --ignoreCache) these are the values entered in each public/ file: <script src=https://xxlsteve.net/js/main.min.59c3ac7087aaeee4bbd948842f4f4a89bc99ac74186ad2f0ce4a1ce1b34eb91c4f3d9a9712872d5c05994083b643b04ef11a4bee521c233e0727337d1ea92641.js integrity="sha512-WcOscIeq7uS72UiEL09KibyZrHQYatLwzkoc4bNOuRxPPZqXEoctXAWZQIO2Q7BO8RpL7lIcIz4HJzN9HqkmQQ==">
The sequence in the name of the file is the correct SHA string, however the one in integrity="..." is completely different, and not even the same length. What does it correspond to?
In the meanwhile, I replaced every integrity attribute with integrity="" but I’d like to use the feature…
{{ with resources.Get "js/main.js" | minify | fingerprint "sha512" }}
<script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}"></script>
{{ end }}
There is no need to pass the integrity key/value pair through safeHTMLAttr.
@Sid To tell Go’s html/template package that an attribute key/value pair is safe, you need to pass the key/value pair (not just the value) through safeHTMLAttr. The construct for that is:
@XXL_Steve, one tip to save some computational resource:
In non-production environments, it’s likely that you don’t need to fingerprint assets (JS, CSS). To build upon Joe’s solution, you can use something like this:
I tested it once again with @Sid code, and the same problem.
The hash generated in the file name is correct, but the one in the integrity attribute isn’t.
Ex: generated file is main.min.c457f7032d0a853e494b13a51819cd0e7bcfa800d877af5e14a6a24c10aa0b5fa36dbbea4927817edbf6a8f02230408e5dd2843edc9f3e16470235e610f8e949.css but in HTML it’s integrity="sha512-xFf3Ay0KhT5JSxOlGBnNDnvPqADYd69eFKaiTBCqC1+jbbvqSSeBftv2qPAiMECOXdKEPtyfPhZHAjXmEPjpSQ=="
I believe the issue is that printed string for the integrity attribute is the wrong format. And I don’t why, and how to fix it. Because the file name is the correct sha512 string but the attribute is not even the same length of characters
The integrity attribute is the SHAxxx hash, in binary form, encoded to base64. The MDN documentation provides two methods to calculated the integrity attribute:
After further testing I came to the conclusion that integrity doesn’t work for javascript files. They work for stylesheets however. Thanks for helping!