legacy.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.legacyUnsafeStyleSheet = exports.legacyUnsafeStyle = exports.legacyUnsafeResourceUrl = exports.legacyUnsafeScript = exports.legacyUnsafeHtml = 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. * Transitional utilities to unsafely trust random strings as
  16. * safe values. Intended for temporary use when upgrading a library that
  17. * used to accept plain strings to use safe values, but where it's not
  18. * practical to transitively update callers.
  19. *
  20. * IMPORTANT: No new code should use the conversion functions in this file,
  21. * they are intended for refactoring old code to use safe values. New code
  22. * should construct safe values via their APIs, template systems or
  23. * sanitizers. If that’s not possible it should use a reviewed conversion and
  24. * undergo security review.
  25. *
  26. * The semantics of the legacy conversions are very
  27. * different from the ones provided by reviewed conversions. The
  28. * latter are for use in code where it has been established through manual
  29. * security review that the value produced by a piece of code will always
  30. * satisfy the SafeHtml contract (e.g., the output of a secure HTML sanitizer).
  31. * In uses of legacy conversions, this guarantee is not given -- the
  32. * value in question originates in unreviewed legacy code and there is no
  33. * guarantee that it satisfies the SafeHtml contract.
  34. *
  35. * There are only three valid uses of legacy conversions:
  36. *
  37. * 1. Introducing a safe values version of a function which currently consumes
  38. * string and passes that string to a DOM API which can execute script - and
  39. * hence cause XSS - like innerHTML. For example, Dialog might expose a
  40. * setContent method which takes a string and sets the innerHTML property of
  41. * an element with it. In this case a setSafeHtmlContent function could be
  42. * added, consuming SafeHtml instead of string. setContent could then internally
  43. * use legacyUnsafeHtml to create a SafeHtml
  44. * from string and pass the SafeHtml to a safe values consumer down the line. In
  45. * this scenario, remember to document the use of legacyUnsafeHtml in the
  46. * modified setContent and consider deprecating it as well.
  47. *
  48. * 2. Automated refactoring of application code which handles HTML as string
  49. * but needs to call a function which only takes safe values types. For example,
  50. * in the Dialog scenario from (1) an alternative option would be to refactor
  51. * setContent to accept SafeHtml instead of string and then refactor
  52. * all current callers to use legacy conversions to pass SafeHtml. This is
  53. * generally preferable to (1) because it keeps the library clean of
  54. * legacy conversions, and makes code sites in application code that are
  55. * potentially vulnerable to XSS more apparent.
  56. *
  57. * 3. Old code which needs to call APIs which consume safe values types and for
  58. * which it is prohibitively expensive to refactor to use these types.
  59. * Generally, this is code where safety from XSS is either hopeless or
  60. * unimportant.
  61. */
  62. /**
  63. * Turns a string into SafeHtml for legacy API purposes.
  64. *
  65. * Please read fileoverview documentation before using.
  66. */
  67. function legacyUnsafeHtml(s) {
  68. if (process.env.NODE_ENV !== 'production' && typeof s !== 'string') {
  69. throw new Error('Expected a string');
  70. }
  71. return (0, html_impl_1.createHtml)(s);
  72. }
  73. exports.legacyUnsafeHtml = legacyUnsafeHtml;
  74. /**
  75. * Turns a string into SafeScript for legacy API purposes.
  76. *
  77. * Please read fileoverview documentation before using.
  78. */
  79. function legacyUnsafeScript(s) {
  80. if (process.env.NODE_ENV !== 'production' && typeof s !== 'string') {
  81. throw new Error('Expected a string');
  82. }
  83. return (0, script_impl_1.createScript)(s);
  84. }
  85. exports.legacyUnsafeScript = legacyUnsafeScript;
  86. /**
  87. * Turns a string into TrustedResourceUrl for legacy API purposes.
  88. *
  89. * Please read fileoverview documentation before using.
  90. */
  91. function legacyUnsafeResourceUrl(s) {
  92. if (process.env.NODE_ENV !== 'production' && typeof s !== 'string') {
  93. throw new Error('Expected a string');
  94. }
  95. return (0, resource_url_impl_1.createResourceUrl)(s);
  96. }
  97. exports.legacyUnsafeResourceUrl = legacyUnsafeResourceUrl;
  98. /**
  99. * Turns a string into SafeStyle for legacy API purposes.
  100. *
  101. * Please read fileoverview documentation before using.
  102. */
  103. function legacyUnsafeStyle(s) {
  104. if (process.env.NODE_ENV !== 'production' && typeof s !== 'string') {
  105. throw new Error('Expected a string');
  106. }
  107. return (0, style_impl_1.createStyle)(s);
  108. }
  109. exports.legacyUnsafeStyle = legacyUnsafeStyle;
  110. /**
  111. * Turns a string into SafeStyleSheet for legacy API purposes.
  112. *
  113. * Please read fileoverview documentation before using.
  114. */
  115. function legacyUnsafeStyleSheet(s) {
  116. if (process.env.NODE_ENV !== 'production' && typeof s !== 'string') {
  117. throw new Error('Expected a string');
  118. }
  119. return (0, style_sheet_impl_1.createStyleSheet)(s);
  120. }
  121. exports.legacyUnsafeStyleSheet = legacyUnsafeStyleSheet;