uncheckedconversions.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * @fileoverview Unchecked conversions to create values of goog.html types from
  16. * plain strings. Use of these functions could potentially result in instances
  17. * of goog.html types that violate their type contracts, and hence result in
  18. * security vulnerabilties.
  19. *
  20. * Therefore, all uses of the methods herein must be carefully security
  21. * reviewed. Avoid use of the methods in this file whenever possible; instead
  22. * prefer to create instances of goog.html types using inherently safe builders
  23. * or template systems.
  24. *
  25. *
  26. *
  27. * @visibility {//closure/goog/html:approved_for_unchecked_conversion}
  28. * @visibility {//closure/goog/bin/sizetests:__pkg__}
  29. */
  30. goog.provide('goog.html.uncheckedconversions');
  31. goog.require('goog.asserts');
  32. goog.require('goog.html.SafeHtml');
  33. goog.require('goog.html.SafeScript');
  34. goog.require('goog.html.SafeStyle');
  35. goog.require('goog.html.SafeStyleSheet');
  36. goog.require('goog.html.SafeUrl');
  37. goog.require('goog.html.TrustedResourceUrl');
  38. goog.require('goog.string');
  39. goog.require('goog.string.Const');
  40. /**
  41. * Performs an "unchecked conversion" to SafeHtml from a plain string that is
  42. * known to satisfy the SafeHtml type contract.
  43. *
  44. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  45. * that the value of {@code html} satisfies the SafeHtml type contract in all
  46. * possible program states.
  47. *
  48. *
  49. * @param {!goog.string.Const} justification A constant string explaining why
  50. * this use of this method is safe. May include a security review ticket
  51. * number.
  52. * @param {string} html A string that is claimed to adhere to the SafeHtml
  53. * contract.
  54. * @param {?goog.i18n.bidi.Dir=} opt_dir The optional directionality of the
  55. * SafeHtml to be constructed. A null or undefined value signifies an
  56. * unknown directionality.
  57. * @return {!goog.html.SafeHtml} The value of html, wrapped in a SafeHtml
  58. * object.
  59. */
  60. goog.html.uncheckedconversions.safeHtmlFromStringKnownToSatisfyTypeContract =
  61. function(justification, html, opt_dir) {
  62. // unwrap() called inside an assert so that justification can be optimized
  63. // away in production code.
  64. goog.asserts.assertString(
  65. goog.string.Const.unwrap(justification), 'must provide justification');
  66. goog.asserts.assert(
  67. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  68. 'must provide non-empty justification');
  69. return goog.html.SafeHtml.createSafeHtmlSecurityPrivateDoNotAccessOrElse(
  70. html, opt_dir || null);
  71. };
  72. /**
  73. * Performs an "unchecked conversion" to SafeScript from a plain string that is
  74. * known to satisfy the SafeScript type contract.
  75. *
  76. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  77. * that the value of {@code script} satisfies the SafeScript type contract in
  78. * all possible program states.
  79. *
  80. *
  81. * @param {!goog.string.Const} justification A constant string explaining why
  82. * this use of this method is safe. May include a security review ticket
  83. * number.
  84. * @param {string} script The string to wrap as a SafeScript.
  85. * @return {!goog.html.SafeScript} The value of {@code script}, wrapped in a
  86. * SafeScript object.
  87. */
  88. goog.html.uncheckedconversions.safeScriptFromStringKnownToSatisfyTypeContract =
  89. function(justification, script) {
  90. // unwrap() called inside an assert so that justification can be optimized
  91. // away in production code.
  92. goog.asserts.assertString(
  93. goog.string.Const.unwrap(justification), 'must provide justification');
  94. goog.asserts.assert(
  95. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  96. 'must provide non-empty justification');
  97. return goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse(
  98. script);
  99. };
  100. /**
  101. * Performs an "unchecked conversion" to SafeStyle from a plain string that is
  102. * known to satisfy the SafeStyle type contract.
  103. *
  104. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  105. * that the value of {@code style} satisfies the SafeStyle type contract in all
  106. * possible program states.
  107. *
  108. *
  109. * @param {!goog.string.Const} justification A constant string explaining why
  110. * this use of this method is safe. May include a security review ticket
  111. * number.
  112. * @param {string} style The string to wrap as a SafeStyle.
  113. * @return {!goog.html.SafeStyle} The value of {@code style}, wrapped in a
  114. * SafeStyle object.
  115. */
  116. goog.html.uncheckedconversions.safeStyleFromStringKnownToSatisfyTypeContract =
  117. function(justification, style) {
  118. // unwrap() called inside an assert so that justification can be optimized
  119. // away in production code.
  120. goog.asserts.assertString(
  121. goog.string.Const.unwrap(justification), 'must provide justification');
  122. goog.asserts.assert(
  123. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  124. 'must provide non-empty justification');
  125. return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
  126. style);
  127. };
  128. /**
  129. * Performs an "unchecked conversion" to SafeStyleSheet from a plain string
  130. * that is known to satisfy the SafeStyleSheet type contract.
  131. *
  132. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  133. * that the value of {@code styleSheet} satisfies the SafeStyleSheet type
  134. * contract in all possible program states.
  135. *
  136. *
  137. * @param {!goog.string.Const} justification A constant string explaining why
  138. * this use of this method is safe. May include a security review ticket
  139. * number.
  140. * @param {string} styleSheet The string to wrap as a SafeStyleSheet.
  141. * @return {!goog.html.SafeStyleSheet} The value of {@code styleSheet}, wrapped
  142. * in a SafeStyleSheet object.
  143. */
  144. goog.html.uncheckedconversions
  145. .safeStyleSheetFromStringKnownToSatisfyTypeContract = function(
  146. justification, styleSheet) {
  147. // unwrap() called inside an assert so that justification can be optimized
  148. // away in production code.
  149. goog.asserts.assertString(
  150. goog.string.Const.unwrap(justification), 'must provide justification');
  151. goog.asserts.assert(
  152. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  153. 'must provide non-empty justification');
  154. return goog.html.SafeStyleSheet
  155. .createSafeStyleSheetSecurityPrivateDoNotAccessOrElse(styleSheet);
  156. };
  157. /**
  158. * Performs an "unchecked conversion" to SafeUrl from a plain string that is
  159. * known to satisfy the SafeUrl type contract.
  160. *
  161. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  162. * that the value of {@code url} satisfies the SafeUrl type contract in all
  163. * possible program states.
  164. *
  165. *
  166. * @param {!goog.string.Const} justification A constant string explaining why
  167. * this use of this method is safe. May include a security review ticket
  168. * number.
  169. * @param {string} url The string to wrap as a SafeUrl.
  170. * @return {!goog.html.SafeUrl} The value of {@code url}, wrapped in a SafeUrl
  171. * object.
  172. */
  173. goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract =
  174. function(justification, url) {
  175. // unwrap() called inside an assert so that justification can be optimized
  176. // away in production code.
  177. goog.asserts.assertString(
  178. goog.string.Const.unwrap(justification), 'must provide justification');
  179. goog.asserts.assert(
  180. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  181. 'must provide non-empty justification');
  182. return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);
  183. };
  184. /**
  185. * Performs an "unchecked conversion" to TrustedResourceUrl from a plain string
  186. * that is known to satisfy the TrustedResourceUrl type contract.
  187. *
  188. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  189. * that the value of {@code url} satisfies the TrustedResourceUrl type contract
  190. * in all possible program states.
  191. *
  192. *
  193. * @param {!goog.string.Const} justification A constant string explaining why
  194. * this use of this method is safe. May include a security review ticket
  195. * number.
  196. * @param {string} url The string to wrap as a TrustedResourceUrl.
  197. * @return {!goog.html.TrustedResourceUrl} The value of {@code url}, wrapped in
  198. * a TrustedResourceUrl object.
  199. */
  200. goog.html.uncheckedconversions
  201. .trustedResourceUrlFromStringKnownToSatisfyTypeContract = function(
  202. justification, url) {
  203. // unwrap() called inside an assert so that justification can be optimized
  204. // away in production code.
  205. goog.asserts.assertString(
  206. goog.string.Const.unwrap(justification), 'must provide justification');
  207. goog.asserts.assert(
  208. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  209. 'must provide non-empty justification');
  210. return goog.html.TrustedResourceUrl
  211. .createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse(url);
  212. };