| 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 |
|
| 16 |
goog.provide('goog.html.safeHtmlFormatterTest'); |
| 17 |
goog.setTestOnly('goog.html.safeHtmlFormatterTest'); |
| 18 |
|
| 19 |
goog.require('goog.html.SafeHtml'); |
| 20 |
goog.require('goog.html.SafeHtmlFormatter'); |
| 21 |
goog.require('goog.string'); |
| 22 |
goog.require('goog.testing.PropertyReplacer'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
|
| 25 |
|
| 26 |
var stubs; |
| 27 |
|
| 28 |
|
| 29 |
function setUp() { |
| 30 |
stubs = new goog.testing.PropertyReplacer(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
function tearDown() { |
| 35 |
stubs.reset(); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
function testFormat() { |
| 40 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 41 |
assertSameHtml('a <b class="bold"><bold></b> statement', |
| 42 |
formatter.format(goog.string.subs('a %s<bold>%s %s', |
| 43 |
formatter.startTag('b', {'class': 'bold'}), |
| 44 |
formatter.endTag('b'), |
| 45 |
formatter.text('statement')))); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
function testFormatWithGetMsg() { |
| 50 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 51 |
assertSameHtml('a <b class="bold"><bold></b> statement', |
| 52 |
formatter.format(goog.getMsg('a {$startBold}<bold>{$endBold} {$type}', { |
| 53 |
'startBold': formatter.startTag('b', {'class': 'bold'}), |
| 54 |
'endBold': formatter.endTag('b'), |
| 55 |
'type': formatter.text('statement') |
| 56 |
}))); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
function testFormatWithText() { |
| 61 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 62 |
// Escapes format. |
| 63 |
assertSameHtml('dinner <3', formatter.format('dinner <3')); |
| 64 |
// Escapes .text(). |
| 65 |
assertSameHtml('dinner <3', formatter.format(formatter.text('dinner <3'))); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
function testFormatWithSafeHtml() { |
| 70 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 71 |
assertSameHtml('User input: <b>abc</b>', formatter.format('User input: ' + |
| 72 |
formatter.safeHtml(goog.html.SafeHtml.create('b', {}, 'abc')))); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
function testFormatWithInternalMarkers() { |
| 77 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 78 |
|
| 79 |
// Immunity against something looking like our marker. |
| 80 |
assertSameHtml('{SafeHtmlFormatter:abc}', |
| 81 |
formatter.format('{SafeHtmlFormatter:abc}')); |
| 82 |
assertSameHtml('{SafeHtmlFormatter:<br>}', |
| 83 |
formatter.format('{SafeHtmlFormatter:' + formatter.startTag('br') + '}')); |
| 84 |
|
| 85 |
// If an attacker steals our random marker and we format his input using |
| 86 |
// .text() then we will get back his input (the random marker), not the tag. |
| 87 |
var br = formatter.startTag('br'); |
| 88 |
var attackerInput = br; |
| 89 |
assertSameHtml(goog.string.htmlEscape(attackerInput) + '<br>', |
| 90 |
formatter.format(formatter.text(attackerInput) + br)); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
function testInvalidTag() { |
| 95 |
assertThrows(function() { |
| 96 |
formatter.startTag('a onclick="alert(1);"'); |
| 97 |
}); |
| 98 |
assertThrows(function() { |
| 99 |
formatter.startTag('a', {'onclick': 'alert(1);'}); |
| 100 |
}); |
| 101 |
assertThrows(function() { |
| 102 |
formatter.startTag('script'); |
| 103 |
}); |
| 104 |
assertThrows(function() { |
| 105 |
formatter.endTag('script'); |
| 106 |
}); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
function testFormatBalancingTags() { |
| 111 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 112 |
|
| 113 |
// Void tags are OK. |
| 114 |
formatter.format(formatter.startTag('br')); |
| 115 |
|
| 116 |
// Balanced tags are OK. |
| 117 |
formatter.format(formatter.startTag('b') + formatter.endTag('b')); |
| 118 |
|
| 119 |
// Order of calling startTag and endTag doesn't matter. |
| 120 |
var endTag = formatter.endTag('b'); |
| 121 |
var startTag = formatter.startTag('b'); |
| 122 |
formatter.format(startTag + endTag); |
| 123 |
|
| 124 |
// Unbalanced tags throw. |
| 125 |
assertThrows(function() { |
| 126 |
formatter.format(formatter.endTag('b') + formatter.startTag('b')); |
| 127 |
}); |
| 128 |
|
| 129 |
// Unclosed tags throw. |
| 130 |
assertThrows(function() { |
| 131 |
formatter.format(formatter.startTag('b')); |
| 132 |
}); |
| 133 |
|
| 134 |
// Unopened tags throw. |
| 135 |
assertThrows(function() { |
| 136 |
formatter.format(formatter.endTag('b')); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
function testDetectDoubleEscaping() { |
| 142 |
stubs.set(goog.string, 'DETECT_DOUBLE_ESCAPING', true); |
| 143 |
stubs.set(goog.string, 'ALL_RE_', /[\x00&<>"'e]/); |
| 144 |
var formatter = new goog.html.SafeHtmlFormatter(); |
| 145 |
assertSameHtml('test', formatter.format(formatter.text('test'))); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
function assertSameHtml(expected, html) { |
| 150 |
assertEquals(expected, goog.html.SafeHtml.unwrap(html)); |
| 151 |
} |