| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Transitional utilities to unsafely trust random strings as |
| 17 |
* goog.html types. Intended for temporary use when upgrading a library that |
| 18 |
* used to accept plain strings to use safe types, but where it's not |
| 19 |
* practical to transitively update callers. |
| 20 |
* |
| 21 |
* IMPORTANT: No new code should use the conversion functions in this file, |
| 22 |
* they are intended for refactoring old code to use goog.html types. New code |
| 23 |
* should construct goog.html types via their APIs, template systems or |
| 24 |
* sanitizers. If that’s not possible it should use |
| 25 |
* goog.html.uncheckedconversions and undergo security review. |
| 26 |
|
| 27 |
* |
| 28 |
* The semantics of the conversions in goog.html.legacyconversions are very |
| 29 |
* different from the ones provided by goog.html.uncheckedconversions. The |
| 30 |
* latter are for use in code where it has been established through manual |
| 31 |
* security review that the value produced by a piece of code will always |
| 32 |
* satisfy the SafeHtml contract (e.g., the output of a secure HTML sanitizer). |
| 33 |
* In uses of goog.html.legacyconversions, this guarantee is not given -- the |
| 34 |
* value in question originates in unreviewed legacy code and there is no |
| 35 |
* guarantee that it satisfies the SafeHtml contract. |
| 36 |
* |
| 37 |
* There are only three valid uses of legacyconversions: |
| 38 |
* |
| 39 |
* 1. Introducing a goog.html version of a function which currently consumes |
| 40 |
* string and passes that string to a DOM API which can execute script - and |
| 41 |
* hence cause XSS - like innerHTML. For example, Dialog might expose a |
| 42 |
* setContent method which takes a string and sets the innerHTML property of |
| 43 |
* an element with it. In this case a setSafeHtmlContent function could be |
| 44 |
* added, consuming goog.html.SafeHtml instead of string, and using |
| 45 |
* goog.dom.safe.setInnerHtml instead of directly setting innerHTML. |
| 46 |
* setContent could then internally use legacyconversions to create a SafeHtml |
| 47 |
* from string and pass the SafeHtml to setSafeHtmlContent. In this scenario |
| 48 |
* remember to document the use of legacyconversions in the modified setContent |
| 49 |
* and consider deprecating it as well. |
| 50 |
* |
| 51 |
* 2. Automated refactoring of application code which handles HTML as string |
| 52 |
* but needs to call a function which only takes goog.html types. For example, |
| 53 |
* in the Dialog scenario from (1) an alternative option would be to refactor |
| 54 |
* setContent to accept goog.html.SafeHtml instead of string and then refactor |
| 55 |
* all current callers to use legacyconversions to pass SafeHtml. This is |
| 56 |
* generally preferable to (1) because it keeps the library clean of |
| 57 |
* legacyconversions, and makes code sites in application code that are |
| 58 |
* potentially vulnerable to XSS more apparent. |
| 59 |
* |
| 60 |
* 3. Old code which needs to call APIs which consume goog.html types and for |
| 61 |
* which it is prohibitively expensive to refactor to use goog.html types. |
| 62 |
* Generally, this is code where safety from XSS is either hopeless or |
| 63 |
* unimportant. |
| 64 |
* |
| 65 |
* @visibility {//closure/goog/html:approved_for_legacy_conversion} |
| 66 |
* @visibility {//closure/goog/bin/sizetests:__pkg__} |
| 67 |
*/ |
| 68 |
|
| 69 |
|
| 70 |
goog.provide('goog.html.legacyconversions'); |
| 71 |
|
| 72 |
goog.require('goog.html.SafeHtml'); |
| 73 |
goog.require('goog.html.SafeScript'); |
| 74 |
goog.require('goog.html.SafeStyle'); |
| 75 |
goog.require('goog.html.SafeStyleSheet'); |
| 76 |
goog.require('goog.html.SafeUrl'); |
| 77 |
goog.require('goog.html.TrustedResourceUrl'); |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Performs an "unchecked conversion" from string to SafeHtml for legacy API |
| 82 |
* purposes. |
| 83 |
* |
| 84 |
* Please read fileoverview documentation before using. |
| 85 |
* |
| 86 |
* @param {string} html A string to be converted to SafeHtml. |
| 87 |
* @return {!goog.html.SafeHtml} The value of html, wrapped in a SafeHtml |
| 88 |
* object. |
| 89 |
*/ |
| 90 |
goog.html.legacyconversions.safeHtmlFromString = function(html) { |
| 91 |
goog.html.legacyconversions.reportCallback_(); |
| 92 |
return goog.html.SafeHtml.createSafeHtmlSecurityPrivateDoNotAccessOrElse( |
| 93 |
html, null /* dir */); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Performs an "unchecked conversion" from string to SafeScript for legacy API |
| 99 |
* purposes. |
| 100 |
* |
| 101 |
* Please read fileoverview documentation before using. |
| 102 |
* |
| 103 |
* @param {string} script A string to be converted to SafeScript. |
| 104 |
* @return {!goog.html.SafeScript} The value of script, wrapped in a SafeScript |
| 105 |
* object. |
| 106 |
*/ |
| 107 |
goog.html.legacyconversions.safeScriptFromString = function(script) { |
| 108 |
goog.html.legacyconversions.reportCallback_(); |
| 109 |
return goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse( |
| 110 |
script); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Performs an "unchecked conversion" from string to SafeStyle for legacy API |
| 116 |
* purposes. |
| 117 |
* |
| 118 |
* Please read fileoverview documentation before using. |
| 119 |
* |
| 120 |
* @param {string} style A string to be converted to SafeStyle. |
| 121 |
* @return {!goog.html.SafeStyle} The value of style, wrapped in a SafeStyle |
| 122 |
* object. |
| 123 |
*/ |
| 124 |
goog.html.legacyconversions.safeStyleFromString = function(style) { |
| 125 |
goog.html.legacyconversions.reportCallback_(); |
| 126 |
return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse( |
| 127 |
style); |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Performs an "unchecked conversion" from string to SafeStyleSheet for legacy |
| 133 |
* API purposes. |
| 134 |
* |
| 135 |
* Please read fileoverview documentation before using. |
| 136 |
* |
| 137 |
* @param {string} styleSheet A string to be converted to SafeStyleSheet. |
| 138 |
* @return {!goog.html.SafeStyleSheet} The value of style sheet, wrapped in |
| 139 |
* a SafeStyleSheet object. |
| 140 |
*/ |
| 141 |
goog.html.legacyconversions.safeStyleSheetFromString = function(styleSheet) { |
| 142 |
goog.html.legacyconversions.reportCallback_(); |
| 143 |
return goog.html.SafeStyleSheet |
| 144 |
.createSafeStyleSheetSecurityPrivateDoNotAccessOrElse(styleSheet); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Performs an "unchecked conversion" from string to SafeUrl for legacy API |
| 150 |
* purposes. |
| 151 |
* |
| 152 |
* Please read fileoverview documentation before using. |
| 153 |
* |
| 154 |
* @param {string} url A string to be converted to SafeUrl. |
| 155 |
* @return {!goog.html.SafeUrl} The value of url, wrapped in a SafeUrl |
| 156 |
* object. |
| 157 |
*/ |
| 158 |
goog.html.legacyconversions.safeUrlFromString = function(url) { |
| 159 |
goog.html.legacyconversions.reportCallback_(); |
| 160 |
return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url); |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Performs an "unchecked conversion" from string to TrustedResourceUrl for |
| 166 |
* legacy API purposes. |
| 167 |
* |
| 168 |
* Please read fileoverview documentation before using. |
| 169 |
* |
| 170 |
* @param {string} url A string to be converted to TrustedResourceUrl. |
| 171 |
* @return {!goog.html.TrustedResourceUrl} The value of url, wrapped in a |
| 172 |
* TrustedResourceUrl object. |
| 173 |
*/ |
| 174 |
goog.html.legacyconversions.trustedResourceUrlFromString = function(url) { |
| 175 |
goog.html.legacyconversions.reportCallback_(); |
| 176 |
return goog.html.TrustedResourceUrl |
| 177 |
.createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse(url); |
| 178 |
}; |
| 179 |
|
| 180 |
/** |
| 181 |
* @private {function(): undefined} |
| 182 |
*/ |
| 183 |
goog.html.legacyconversions.reportCallback_ = goog.nullFunction; |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Sets a function that will be called every time a legacy conversion is |
| 188 |
* performed. The function is called with no parameters but it can use |
| 189 |
* goog.debug.getStacktrace to get a stacktrace. |
| 190 |
* |
| 191 |
* @param {function(): undefined} callback Error callback as defined above. |
| 192 |
*/ |
| 193 |
goog.html.legacyconversions.setReportCallback = function(callback) { |
| 194 |
goog.html.legacyconversions.reportCallback_ = callback; |
| 195 |
}; |