legacy.mjs 4.6 KB

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