Chrome 64 Uncaught DOMException: Failed to execute 'insertRule' on 'CSSStyleSheet': Cannot access StyleSheet to insertRule
We believe this commit to Chromium to be the root cause of our issue:
Update behavior of CSSStyleSheet to match spec for Security origin
The quick solution for us was to simply reorder the CSS. It seems that previously the culprit plugin was inserting CSS rules to this remote CSS:
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,400,700">
Simply reordering our stylesheets to ensure a script from our site was in first position (document.styleSheets[0]
), fixed the issue:
<link type="text/css" rel="stylesheet" href="/Public/js/jquery-ui/jquery-ui.css" />
<link type="text/css" rel="stylesheet" href="/Public/js/jquery-ui/jquery-ui.theme.min.css" />
<link type="text/css" rel="stylesheet" href="/Public/css/msgPop.css" />
<link type="text/css" rel="stylesheet" href="/Public/js/select2/select2.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,400,700">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
Why Chrome cannot access to external stylesheet using insertRule with Javascript?
Change the index for your insert. In CSS files all @ Rules must be at the top of the file. So, if you have 4 @ rules at the top of the CSS file, change the index to 4 thus:
document.styleSheets[0].insertRule(".example{position: relative}", 4);
That should work.
Here is the spec reference (see the note):
https://drafts.csswg.org/cssom/#insert-a-css-rule
Uncaught DOMException: Failed to execute 'addRule' on 'CSSStyleSheet': Cannot access StyleSheet to insertRule
Make sure the styles.css
exists on your server. If it doesn't, that might be causing it to be inaccessible.
Worst-case, you could create and insert another stylesheet.
const styleSheet = document.createElement('style');
document.head.appendChild(styleSheet);
styleSheet.textContent = ".new-plunder-form-username:focus {border: 1.5px solid #75b4d9; box-shadow: 0 0 5px #75b4d9;}";
Uncaught DOMException: Failed to read the 'rules' property from 'CSSStyleSheet'
This was a good story and a new 'gotcha' for web developers, so I just had to share:
Chrome 64.0.3282.0 (released January 2018, full change list) introduced a change to security rules for stylesheets. I'm irritated that I couldn't find this change in any changelog less detailed than the full commit list.
Commit a4ebe08 in Chromium is described:
Update behavior of CSSStyleSheet to match spec for Security origin
Spec is here:
https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interfaceUpdated: the following methods now throw a SecurityError if the
style sheet is not accessible:
- cssRules() / rules()
- insertRule()
- deleteRule()
This commit is a fix for the bug Security: Inconsistent CORS implementation regarding CSS and the link element. The linked W3C spec describes in detail where use of the CSS Object Model requires same-origin access.
All that said, why was this issue showing up in App Lab? We shouldn't experience any CORS issues, because we only load stylesheets from our own origin:
The final clue was that we couldn't reproduce this issue in a private tab. We started looking at Chrome extensions and realized that some affected users had the Loom Video Recorder extension enabled, which seems to inject its own CSS into the page. Since our (naïve) function was iterating through all loaded stylesheets, it was attempting to access this stylesheet injected by the extension and thus causing the CORS error.
That said, there's still some open issues and debate around this change in Chrome:
- This comment on the original security bug complains that the only way now to detect that the stylesheet is not accessible from JavaScript is with a
try/catch
. - A Chromium bug opened January 23rd (document.styleSheets.cssRules is null even with Access-Control-Allow-Origin: *) suggests there may be an implementation issue with the new security rule that breaks certain workarounds.
- The spec being implemented seems pretty stable, but it still has "Working Draft" status so who knows where it will land and what other browsers will implement.
To fix our problem, we just tore out the entire function. We don't support IE9 anymore, and we know all of our supported browsers handle media queries properly.
Related (but not quite duplicate) questions:
- cannot access rules in external CSSStyleSheet
- Cannot access cssRules from local css file in Chrome
Cannot access cssRules from local css file in Chrome 64
TL;DR: As of Chrome 64 you'll need to use a local development server to test functionality that depends on the CSS Object Model.
Accessing CSS rules in a stylesheet loaded from the local filesystem violates a Cross-Origin Resource Sharing (CORS) policy - but Chrome didn't enforce this until recently, and other browsers don't seem to enforce it yet.
Chrome 64.0.3282.0 (released January 2018, full change list) includes a change to security rules for stylesheets. I couldn't find this change in any changelog less detailed than the full commit list.
Commit a4ebe08 in Chromium is described:
Update behavior of CSSStyleSheet to match spec for Security origin
Spec is here:
https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interfaceUpdated: the following methods now throw a SecurityError if the
style sheet is not accessible:
- cssRules() / rules()
- insertRule()
- deleteRule()
This commit is a fix for the bug Security: Inconsistent CORS implementation regarding CSS and the link element. The linked W3C spec describes in detail where use of the CSS Object Model requires same-origin access.
This is a real security constraint and the solution you posted (online/localhost) is probably the most typical workaround. For more information check out MDN's How do you set up a local testing server? - it discusses why and how to use a local development server to avoid CORS issues.
That said, there's still some open issues and debate around this change.
- This comment on the original security bug complains that the only way now to detect that the stylesheet is not accessible from JavaScript is with a
try/catch
. - A Chromium bug opened January 23rd (document.styleSheets.cssRules is null even with Access-Control-Allow-Origin: *) suggests there may be an implementation issue with the new security rule that breaks certain workarounds.
- The spec being implemented seems pretty stable, but it still has "Working Draft" status so who knows where it will land and what other browsers will implement.
Insert Multiple CSS rules into a stylesheet
How about just modifying a <style>
tag?
<style id="custom-styles"></style>
<script>
var red = '#f00';
document.getElementById('custom-styles').innerHTML = 'body { color: ' + red + '; }';
</script>
Here's a small demo: http://jsbin.com/mamekame/2/edit?html,js,output
Cannot Access cssRules for Stylesheet CORS
I ended up finding a solution...
All thanks to Paulo Belo from this link Uncaught DOMException: Failed to read the 'cssRules' property
stylesheet.crossOrigin = "anonymous"
solved my problem giving me access to the cssRules.
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin
Note this fix does not work with existing stylesheets that are throwing this error.
Exception: DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules at CSSStyleSheet.s
This fix only works for your own uploaded sheets or in my case the ones from my CDN.
Related Topics
Custom CSS Properties, Why Not
Allow Certain Style Attributes with Ngsanitize
Make Named Anchor Bookmarks Appear Always at Top of the Screen When Clicked
Laravel 8 + Nginx - App.CSS and App.Js Resources from Public/ Not Loading - 404 Not Found
How to Check If CSS Calc() Is Available Using JavaScript
React-Native Updating List View Datasource
CSS Class Selector Styles Not Being Applied in React Project
Modify Pseudo Select :After in JavaScript
Getting the Rgb Values for a CSS/HTML Named Color in JavaScript
How to Center the Text in the Headers for an Ag-Grid Control
How to Activate a CSS3 (Webkit) Animation Using JavaScript
Get Current CSS Property Value During a Transition in JavaScript