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
Uncaught DOMException: Failed to read the 'cssRules' property
In latest Chrome, CORS security rules are applicable for style-sheets also (Similar to Iframe rules).
You can load and render them but, cannot access the content through javascript (If loaded from Cross-Domain ).
If your CSS Stylesheet is from Same domain as of HTML /or included in same HTML file, you will be able to access document.styleSheets[elem].cssRules
otherwise it will throw error
Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules
Cross Domain Stylesheet
Same Domain Stylesheet
cannot access rules in external CSSStyleSheet
As of Chrome 64, new CORS rules are enforced for stylesheets. You'll need to use a local development server to do local testing of functionality that depends on the CSS Object Model. For details, see Cannot access cssRules from local css file in Chrome.
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;}";
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.
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.
Related Topics
Creating a Custom Attribute with Angularjs
Modify CSS Classes Using JavaScript
How to Have HTML-Webpack-Plugin Generate <Style> Elements from CSS
Highcharts.Js - Background Color of Axis Only
Enabling Line Wrap with React-Syntax-Highlighter
CSS or JavaScript to Highlight Certain Area of Image Opacity
How to Slow Down Scrolling Speed with JavaScript
Leaflet + Polymer 2 Map Loading with Distorted Tile However Works with Non-Polymer Code
How to Get JavaScript Variable Value into CSS
How to See the JavaScript Errors of Phonegap App in Xcode
Phonegap Build iOS App Has Blank White Screen After Splash Screen
Inject a Local JavaScript into Uiwebview
Document.Body.Scrollheight Yielding Two Different Results in Firefox/Chrome
Alert, Confirm, and Prompt Not Working After Using History API on Safari, iOS
How Much of an Effect Can Different Operating Systems Have on Displaying Web Pages
Change the Fixed Navbar's Classes Depending on the Background of the Page Section It Hovers