Bypassing CSP script nonces via the browser cache

CSP script nonces can be used to mitigate XSS attacks. Each legitimate script on a page should be outfitted with a random nonce that gets regenerated at every pageload. Before the page has loaded, the attacker has no way of guessing the nonce, and thus the attacker cannot outfit the injected scripts with the correct nonce. In order to mitigate the attack, the browser will only execute scripts that carry the correct nonce.

Script nonces, however, are incompatible with caching mechanisms. Browser caches, server-side caches, Service Workers or the HTML5 AppCache can break the protective capabilities. This page demonstrates one examplary scenario in which script nonces can be circumvented when caching is active.

This attack works if a cacheable page has an XSS vulnerability where location.hash or other non-URL sources (e.g. window.name, document.referer, post messages, etc.) are echoed somewhere in front of a legitimate, nonced script.

Click the button to launch the attack.