Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Use style nonce for styles instead of script nonce.
Browse files Browse the repository at this point in the history
RELNOTES: n/a

PiperOrigin-RevId: 367386347
  • Loading branch information
vrana authored and 12wrigja committed Apr 8, 2021
1 parent f13f1c3 commit a33a9c8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
3 changes: 2 additions & 1 deletion closure/goog/cssom/cssom.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ goog.provide('goog.cssom.CssRuleType');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.dom.safe');


/**
Expand Down Expand Up @@ -373,7 +374,7 @@ goog.cssom.addCssText = function(cssText, opt_domHelper) {
var cssNode = domHelper.createElement(goog.dom.TagName.STYLE);

// If a CSP nonce is present, propagate it to style blocks
var nonce = goog.getScriptNonce();
const nonce = goog.dom.safe.getStyleNonce();
if (nonce) {
cssNode.setAttribute('nonce', nonce);
}
Expand Down
3 changes: 2 additions & 1 deletion closure/goog/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.TagName');
goog.require('goog.dom.safe');
goog.require('goog.dom.vendor');
goog.require('goog.html.SafeStyleSheet');
goog.require('goog.math.Box');
Expand Down Expand Up @@ -1331,7 +1332,7 @@ goog.style.installSafeStyleSheet = function(safeStyleSheet, opt_node) {
body.parentNode.insertBefore(head, body);
}
var el = dh.createDom(goog.dom.TagName.STYLE);
var nonce = goog.getScriptNonce();
const nonce = goog.dom.safe.getStyleNonce();
if (nonce) {
el.setAttribute('nonce', nonce);
}
Expand Down
9 changes: 5 additions & 4 deletions closure/goog/style/style_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const googDom = goog.require('goog.dom');
const googObject = goog.require('goog.object');
const googStyle = goog.require('goog.style');
const jsunit = goog.require('goog.testing.jsunit');
const safe = goog.require('goog.dom.safe');
const testSuite = goog.require('goog.testing.testSuite');
const testing = goog.require('goog.html.testing');
const userAgent = goog.require('goog.userAgent');
Expand Down Expand Up @@ -118,9 +119,9 @@ testSuite({
mockUserAgent = new MockUserAgent();
mockUserAgent.install();

if (!goog.getScriptNonce()) {
if (!safe.getStyleNonce()) {
/** @suppress {visibility} suppression added to enable type checking */
goog.cspNonce_ = 'thisIsANonce';
safe.cspStyleNonce_ = 'thisIsANonce';
}
},

Expand Down Expand Up @@ -1023,9 +1024,9 @@ testSuite({
googStyle.installSafeStyleSheet(testing.newSafeStyleSheetForTest(''));

const styles = document.head.querySelectorAll('style[nonce]');
assert(styles.length > 0);
assert(styles.length > 1);
assertEquals(
goog.cspNonce_, styles[styles.length - 1].getAttribute('nonce'));
safe.cspStyleNonce_, styles[styles.length - 1].getAttribute('nonce'));

googStyle.uninstallStyles(result);
},
Expand Down
7 changes: 1 addition & 6 deletions closure/goog/style/style_test_dom.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<!--
When changing this, make sure that style_quirks_test.html is kept in sync.
-->
<!--
Copyright The Closure Library Authors. All Rights Reserved.
Expand All @@ -11,7 +6,7 @@
-->
<meta name="viewport" content="width=device-width, initial-scale=1.0,
maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<style>
<style nonce="NONCE">

i {
font-family: Times, sans-serif;
Expand Down

0 comments on commit a33a9c8

Please sign in to comment.