unsafe.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2016 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 Potentially unsafe API for the HTML sanitizer.
  16. *
  17. * The HTML sanitizer enforces a default a safe policy, and also limits how the
  18. * policy can be relaxed, so that developers cannot misconfigure it and
  19. * introduce vulnerabilities.
  20. *
  21. * This file extends the HTML sanitizer's capabilities with potentially unsafe
  22. * configuration options, such as the ability to extend the tag whitelist (e.g.
  23. * to support web components).
  24. *
  25. * @supported IE 10+, Chrome 26+, Firefox 22+, Safari 7.1+, Opera 15+
  26. * @visibility {//closure/goog/html/sanitizer:approved_for_unsafe_config}
  27. */
  28. goog.provide('goog.html.sanitizer.unsafe');
  29. goog.require('goog.asserts');
  30. goog.require('goog.html.sanitizer.HtmlSanitizer.Builder');
  31. goog.require('goog.string');
  32. goog.require('goog.string.Const');
  33. /**
  34. * Extends the tag whitelist with the list of tags provided.
  35. *
  36. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  37. * that the new tags do not introduce untrusted code execution or unsanctioned
  38. * network activity.
  39. *
  40. * @param {!goog.string.Const} justification A constant string explaining why
  41. * the addition of these tags to the whitelist is safe. May include a
  42. * security review ticket number.
  43. * @param {!goog.html.sanitizer.HtmlSanitizer.Builder} builder The builder
  44. * whose tag whitelist should be extended.
  45. * @param {!Array<string>} tags A list of additional tags to allow through the
  46. * sanitizer. Note that if the tag is also present in the blacklist,
  47. * its addition to the whitelist has no effect. The tag names are
  48. * case-insensitive.
  49. * @return {!goog.html.sanitizer.HtmlSanitizer.Builder}
  50. */
  51. goog.html.sanitizer.unsafe.alsoAllowTags = function(
  52. justification, builder, tags) {
  53. goog.asserts.assertString(
  54. goog.string.Const.unwrap(justification), 'must provide justification');
  55. goog.asserts.assert(
  56. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  57. 'must provide non-empty justification');
  58. return builder.alsoAllowTagsPrivateDoNotAccessOrElse(tags);
  59. };
  60. /**
  61. * Installs custom attribute policies for the attributes provided in the list.
  62. * This can be used either on non-whitelisted attributes, effectively extending
  63. * the attribute whitelist, or on attributes that are whitelisted and already
  64. * have a policy, to override their policies.
  65. *
  66. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  67. * that the new tags do not introduce untrusted code execution or unsanctioned
  68. * network activity.
  69. *
  70. * @param {!goog.string.Const} justification A constant string explaining why
  71. * the addition of these attributes to the whitelist is safe. May include a
  72. * security review ticket number.
  73. * @param {!goog.html.sanitizer.HtmlSanitizer.Builder} builder The builder
  74. * whose attribute whitelist should be extended.
  75. * @param {!Array<(string|!goog.html.sanitizer.HtmlSanitizerAttributePolicy)>}
  76. * attrs A list of attributes whose policy should be overridden. Attributes
  77. * can come in of two forms:
  78. * - string: allow all values and just trim whitespaces for this attribute
  79. * on all tags.
  80. * - HtmlSanitizerAttributePolicy: allows specifying a policy for a
  81. * particular tag. The tagName can be '*', which means all tags. If no
  82. * policy is passed, the default is allow all values and just trim
  83. * whitespaces.
  84. * The tag and attribute names are case-insensitive.
  85. * @return {!goog.html.sanitizer.HtmlSanitizer.Builder}
  86. */
  87. goog.html.sanitizer.unsafe.alsoAllowAttributes = function(
  88. justification, builder, attrs) {
  89. goog.asserts.assertString(
  90. goog.string.Const.unwrap(justification), 'must provide justification');
  91. goog.asserts.assert(
  92. !goog.string.isEmptyOrWhitespace(goog.string.Const.unwrap(justification)),
  93. 'must provide non-empty justification');
  94. return builder.alsoAllowAttributesPrivateDoNotAccessOrElse(attrs);
  95. };
  96. /**
  97. * Turns off sanitization of TEMPLATE tag descendants. The output is still
  98. * safe to consume as a whole, but clients need to handle the contents of
  99. * TEMPLATE nodes carefully, hence its definition in the unsafe package.
  100. *
  101. * Note that this only applies to descendants of unsanitized template tags, not
  102. * to the tag itself, which must be manually added to the whitelist and removed
  103. * from the blacklist.
  104. *
  105. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  106. * that the new tags do not introduce untrusted code execution or unsanctioned
  107. * network activity.
  108. *
  109. * @param {!goog.string.Const} justification A constant string explaining why
  110. * the templates should not be sanitized, and why this is safe. May include
  111. * a security review ticket number.
  112. * @param {!goog.html.sanitizer.HtmlSanitizer.Builder} builder The builder
  113. * whose template tag descendants should not be sanitized.
  114. * @return {!goog.html.sanitizer.HtmlSanitizer.Builder}
  115. * @throws {Error} Thrown if the browser does not support TEMPLATE tags.
  116. * In this case, careful post-sanitization handling wouldn't matter.
  117. */
  118. goog.html.sanitizer.unsafe.keepUnsanitizedTemplateContents = function(
  119. justification, builder) {
  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 builder.keepUnsanitizedTemplateContentsPrivateDoNotAccessOrElse();
  126. };