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

Commit

Permalink
RELNOTES: Add nonce in goog.dom.safe.setLinkHrefAndRel.
Browse files Browse the repository at this point in the history
We already do this goog.style.installSafeStyleSheet.

PiperOrigin-RevId: 367961247
  • Loading branch information
vrana authored and kjin committed Apr 14, 2021
1 parent 7d1c8f1 commit 6c84afc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions closure/goog/dom/safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ goog.dom.safe.setLinkHrefAndRel = function(link, url, rel) {
url instanceof goog.html.TrustedResourceUrl,
'URL must be TrustedResourceUrl because "rel" contains "stylesheet"');
link.href = goog.html.TrustedResourceUrl.unwrap(url);
const win = link.ownerDocument && link.ownerDocument.defaultView;
const nonce = goog.dom.safe.getStyleNonce(win);
if (nonce) {
link.setAttribute('nonce', nonce);
}
} else if (url instanceof goog.html.TrustedResourceUrl) {
link.href = goog.html.TrustedResourceUrl.unwrap(url);
} else if (url instanceof goog.html.SafeUrl) {
Expand Down
9 changes: 6 additions & 3 deletions closure/goog/dom/safe_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ testSuite({

testsetLinkHrefAndRel_trustedResourceUrl() {
const mockLink =
/** @type {!HTMLLinkElement} */ ({'href': null, 'rel': null});
/** @type {!HTMLLinkElement} */ (
{'href': null, 'rel': null, setAttribute: () => {}});

const url =
TrustedResourceUrl.fromConstant(Const.from('javascript:trusted();'));
Expand All @@ -190,7 +191,8 @@ testSuite({

testsetLinkHrefAndRel_safeUrl() {
const mockLink =
/** @type {!HTMLLinkElement} */ ({'href': null, 'rel': null});
/** @type {!HTMLLinkElement} */ (
{'href': null, 'rel': null, setAttribute: () => {}});

const url = SafeUrl.fromConstant(Const.from('javascript:trusted();'));
assertThrows(() => {
Expand All @@ -203,7 +205,8 @@ testSuite({

testsetLinkHrefAndRel_string() {
const mockLink =
/** @type {!HTMLLinkElement} */ ({'href': null, 'rel': null});
/** @type {!HTMLLinkElement} */ (
{'href': null, 'rel': null, setAttribute: () => {}});

assertThrows(() => {
safe.setLinkHrefAndRel(
Expand Down

0 comments on commit 6c84afc

Please sign in to comment.