reviewed.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.styleSheetSafeByReview = exports.styleSafeByReview = exports.resourceUrlSafeByReview = exports.scriptSafeByReview = exports.htmlSafeByReview = void 0;
  8. require("../environment/dev");
  9. var html_impl_1 = require("../internals/html_impl");
  10. var resource_url_impl_1 = require("../internals/resource_url_impl");
  11. var script_impl_1 = require("../internals/script_impl");
  12. var style_impl_1 = require("../internals/style_impl");
  13. var style_sheet_impl_1 = require("../internals/style_sheet_impl");
  14. /**
  15. * Utilities to convert arbitrary strings to values of the various
  16. * Safe HTML types, subject to security review. These are also referred to as
  17. * "reviewed conversions".
  18. *
  19. * These functions are intended for use-cases that cannot be expressed using an
  20. * existing safe API (such as a type's builder) and instead require custom code
  21. * to produce values of a Safe HTML type. A security review is required to
  22. * verify that the custom code is indeed guaranteed to produce values that
  23. * satisfy the target type's security contract.
  24. *
  25. * Code using restricted conversions should be structured such that this
  26. * property is straightforward to establish. In particular, correctness should
  27. * only depend on the code immediately surrounding the reviewed conversion, and
  28. * not on assumptions about values received from outside the enclosing function
  29. * (or, at the most, the enclosing file).
  30. */
  31. /**
  32. * Asserts that the provided justification is valid (non-empty). Throws an
  33. * exception if that is not the case.
  34. */
  35. function assertValidJustification(justification) {
  36. if (typeof justification !== 'string' || justification.trim() === '') {
  37. var errMsg = 'Calls to uncheckedconversion functions must go through security review.';
  38. errMsg += ' A justification must be provided to capture what security' +
  39. ' assumptions are being made.';
  40. throw new Error(errMsg);
  41. }
  42. }
  43. /**
  44. * Performs a "reviewed conversion" to SafeHtml from a plain string that is
  45. * known to satisfy the SafeHtml type contract.
  46. *
  47. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  48. * that the value of `html` satisfies the SafeHtml type contract in all
  49. * possible program states. An appropriate `justification` must be provided
  50. * explaining why this particular use of the function is safe.
  51. */
  52. function htmlSafeByReview(html, justification) {
  53. if (process.env.NODE_ENV !== 'production') {
  54. assertValidJustification(justification);
  55. }
  56. return (0, html_impl_1.createHtml)(html);
  57. }
  58. exports.htmlSafeByReview = htmlSafeByReview;
  59. /**
  60. * Performs a "reviewed conversion" to SafeScript from a plain string that
  61. * is known to satisfy the SafeScript type contract.
  62. *
  63. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  64. * that the value of `script` satisfies the SafeScript type contract in
  65. * all possible program states. An appropriate `justification` must be provided
  66. * explaining why this particular use of the function is safe.
  67. */
  68. function scriptSafeByReview(script, justification) {
  69. if (process.env.NODE_ENV !== 'production') {
  70. assertValidJustification(justification);
  71. }
  72. return (0, script_impl_1.createScript)(script);
  73. }
  74. exports.scriptSafeByReview = scriptSafeByReview;
  75. /**
  76. * Performs a "reviewed conversion" to TrustedResourceUrl from a plain string
  77. * that is known to satisfy the SafeUrl type contract.
  78. *
  79. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  80. * that the value of `url` satisfies the TrustedResourceUrl type
  81. * contract in all possible program states. An appropriate `justification` must
  82. * be provided explaining why this particular use of the function is safe.
  83. */
  84. function resourceUrlSafeByReview(url, justification) {
  85. if (process.env.NODE_ENV !== 'production') {
  86. assertValidJustification(justification);
  87. }
  88. return (0, resource_url_impl_1.createResourceUrl)(url);
  89. }
  90. exports.resourceUrlSafeByReview = resourceUrlSafeByReview;
  91. /**
  92. * Performs a "reviewed conversion" to SafeStyle from a plain string that is
  93. * known to satisfy the SafeStyle type contract.
  94. *
  95. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  96. * that the value of `style` satisfies the SafeStyle type contract in all
  97. * possible program states. An appropriate `justification` must be provided
  98. * explaining why this particular use of the function is safe.
  99. */
  100. function styleSafeByReview(style, justification) {
  101. if (process.env.NODE_ENV !== 'production') {
  102. assertValidJustification(justification);
  103. }
  104. return (0, style_impl_1.createStyle)(style);
  105. }
  106. exports.styleSafeByReview = styleSafeByReview;
  107. /**
  108. * Performs a "reviewed conversion" to SafeStyleSheet from a plain string that
  109. * is known to satisfy the SafeStyleSheet type contract.
  110. *
  111. * IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
  112. * that the value of `stylesheet` satisfies the SafeStyleSheet type
  113. * contract in all possible program states. An appropriate `justification` must
  114. * be provided explaining why this particular use of the function is safe; this
  115. * may include a security review ticket number.
  116. */
  117. function styleSheetSafeByReview(stylesheet, justification) {
  118. if (process.env.NODE_ENV !== 'production') {
  119. assertValidJustification(justification);
  120. }
  121. return (0, style_sheet_impl_1.createStyleSheet)(stylesheet);
  122. }
  123. exports.styleSheetSafeByReview = styleSheetSafeByReview;