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

Commit

Permalink
Removes support for passing values with a javascript: scheme to `Sa…
Browse files Browse the repository at this point in the history
…feUrl.fromConstant`

These urls are incompatible with strict CSP and can thus cause production breakages.

RELNOTES: n/a
PiperOrigin-RevId: 563074952
Change-Id: I8bba86f22c57bd8f8c6a6bbb7d2dade132f296c4
  • Loading branch information
Closure Team authored and copybara-github committed Sep 6, 2023
1 parent b54d5fa commit 1e1e2c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions closure/goog/html/safeurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ goog.html.SafeUrl.unwrap = function(safeUrl) {
*/
goog.html.SafeUrl.fromConstant = function(url) {
'use strict';
return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(
goog.string.Const.unwrap(url));
const str = goog.string.Const.unwrap(url);
if (goog.DEBUG && goog.html.SafeUrl.extractScheme(str) === 'javascript:') {
throw Error('Building a SafeUrl with a javascript scheme is not supported');
}
return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(str);
};


Expand Down
4 changes: 4 additions & 0 deletions closure/goog/html/safeurl_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ testSuite({
() => new (/** @type {?} */ (SafeUrl.ABOUT_BLANK)).constructor(''));
},

testFromConstant_throwsOnJavaScriptUrl() {
assertThrows(() => SafeUrl.fromConstant(Const.from('javascript:foo')));
},

testSafeUrl() {
const safeUrl = SafeUrl.fromConstant(Const.from('#'));
const extracted = SafeUrl.unwrap(safeUrl);
Expand Down

0 comments on commit 1e1e2c1

Please sign in to comment.