safehtmlformatter_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. goog.provide('goog.html.safeHtmlFormatterTest');
  15. goog.setTestOnly('goog.html.safeHtmlFormatterTest');
  16. goog.require('goog.html.SafeHtml');
  17. goog.require('goog.html.SafeHtmlFormatter');
  18. goog.require('goog.string');
  19. goog.require('goog.testing.jsunit');
  20. function testFormat() {
  21. var formatter = new goog.html.SafeHtmlFormatter();
  22. assertSameHtml('a <b class="bold">&lt;bold&gt;</b> statement',
  23. formatter.format(goog.string.subs('a %s<bold>%s %s',
  24. formatter.startTag('b', {'class': 'bold'}),
  25. formatter.endTag('b'),
  26. formatter.text('statement'))));
  27. }
  28. function testFormatWithGetMsg() {
  29. var formatter = new goog.html.SafeHtmlFormatter();
  30. assertSameHtml('a <b class="bold">&lt;bold&gt;</b> statement',
  31. formatter.format(goog.getMsg('a {$startBold}<bold>{$endBold} {$type}', {
  32. 'startBold': formatter.startTag('b', {'class': 'bold'}),
  33. 'endBold': formatter.endTag('b'),
  34. 'type': formatter.text('statement')
  35. })));
  36. }
  37. function testFormatWithText() {
  38. var formatter = new goog.html.SafeHtmlFormatter();
  39. // Escapes format.
  40. assertSameHtml('dinner &lt;3', formatter.format('dinner <3'));
  41. // Escapes .text().
  42. assertSameHtml('dinner &lt;3', formatter.format(formatter.text('dinner <3')));
  43. }
  44. function testFormatWithSafeHtml() {
  45. var formatter = new goog.html.SafeHtmlFormatter();
  46. assertSameHtml('User input: <b>abc</b>', formatter.format('User input: ' +
  47. formatter.safeHtml(goog.html.SafeHtml.create('b', {}, 'abc'))));
  48. }
  49. function testFormatWithInternalMarkers() {
  50. var formatter = new goog.html.SafeHtmlFormatter();
  51. // Immunity against something looking like our marker.
  52. assertSameHtml('{SafeHtmlFormatter:abc}',
  53. formatter.format('{SafeHtmlFormatter:abc}'));
  54. assertSameHtml('{SafeHtmlFormatter:<br>}',
  55. formatter.format('{SafeHtmlFormatter:' + formatter.startTag('br') + '}'));
  56. // If an attacker steals our random marker and we format his input using
  57. // .text() then we will get back his input (the random marker), not the tag.
  58. var br = formatter.startTag('br');
  59. var attackerInput = br;
  60. assertSameHtml(goog.string.htmlEscape(attackerInput) + '<br>',
  61. formatter.format(formatter.text(attackerInput) + br));
  62. }
  63. function testInvalidTag() {
  64. assertThrows(function() {
  65. formatter.startTag('a onclick="alert(1);"');
  66. });
  67. assertThrows(function() {
  68. formatter.startTag('a', {'onclick': 'alert(1);'});
  69. });
  70. assertThrows(function() {
  71. formatter.startTag('script');
  72. });
  73. assertThrows(function() {
  74. formatter.endTag('script');
  75. });
  76. }
  77. function testFormatBalancingTags() {
  78. var formatter = new goog.html.SafeHtmlFormatter();
  79. // Void tags are OK.
  80. formatter.format(formatter.startTag('br'));
  81. // Balanced tags are OK.
  82. formatter.format(formatter.startTag('b') + formatter.endTag('b'));
  83. // Order of calling startTag and endTag doesn't matter.
  84. var endTag = formatter.endTag('b');
  85. var startTag = formatter.startTag('b');
  86. formatter.format(startTag + endTag);
  87. // Unbalanced tags throw.
  88. assertThrows(function() {
  89. formatter.format(formatter.endTag('b') + formatter.startTag('b'));
  90. });
  91. // Unclosed tags throw.
  92. assertThrows(function() {
  93. formatter.format(formatter.startTag('b'));
  94. });
  95. // Unopened tags throw.
  96. assertThrows(function() {
  97. formatter.format(formatter.endTag('b'));
  98. });
  99. }
  100. function assertSameHtml(expected, html) {
  101. assertEquals(expected, goog.html.SafeHtml.unwrap(html));
  102. }