forked from salesforce/secure-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
353 lines (340 loc) · 13.6 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*!
* Copyright (c) 2014, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Salesforce.com, nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
(function(root) {
'use strict';
var assert;
var secureFilters;
var _;
if (typeof module !== 'undefined' && module.exports) {
assert = require('assert');
secureFilters = require('./index');
_ = require('underscore');
} else {
assert = root.assert;
secureFilters = root.secureFilters;
_ = root._;
}
// Test character inside the Unicode BMP:
var SNOWMAN = "\u2603";
// Test character outside of the Unicode BMP:
var FACE_WITHOUT_MOUTH = "\uD83D\uDE36"; // U+1F636, UTF-16: D83D DE36, UTF-8: F0 9F 98 B6
var ASCII = "\0";
for (var i = 1; i <= 0x7F; i++) {
ASCII += String.fromCharCode(i);
}
var ALL_CASES = [
{
input: '&&\'d',
html: '&&amp;'d',
js: '\\x26\\x26amp\\x3B\\x27d',
jsAttr: '\x26\x26amp\x3B\x27d',
uri: '%26%26amp%3B%27d',
css: '\\26 \\26 amp\\3b \\27 d',
style: '\26 \26 amp\3b \27 d'
},
{
input: '\' onload="alert(1)"',
html: '' onload="alert(1)"',
js: '\\x27\\x20onload\\x3D\\x22alert\\x281\\x29\\x22',
jsAttr: '\x27\x20onload\x3D\x22alert\x281\x29\x22',
uri: '%27%20onload%3D%22alert%281%29%22',
css: '\\27 \\20 onload\\3d \\22 alert\\28 1\\29 \\22 ',
style: '\27 \20 onload\3d \22 alert\28 1\29 \22 '
},
{
input: '<ha>, \'ha\', "ha"',
html: '<ha>, 'ha', "ha"',
js: '\\x3Cha\\x3E,\\x20\\x27ha\\x27,\\x20\\x22ha\\x22',
jsAttr: '\x3Cha\x3E,\x20\x27ha\x27,\x20\x22ha\x22',
uri: '%3Cha%3E%2C%20%27ha%27%2C%20%22ha%22',
css: '\\3c ha\\3e \\2c \\20 \\27 ha\\27 \\2c \\20 \\22 ha\\22 ',
style: '\3c ha\3e \2c \20 \27 ha\27 \2c \20 \22 ha\22 '
},
{
label: "ESAPI bad JS chars",
input: "!@$%()=+{}[]",
html: "!@$%()=+{}[]",
js: "\\x21\\x40\\x24\\x25\\x28\\x29\\x3D\\x2B\\x7B\\x7D\\x5B\\x5D",
jsAttr: "\x21\x40\x24\x25\x28\x29\x3D\x2B\x7B\x7D\x5B\x5D",
uri: "%21%40%24%25%28%29%3D%2B%7B%7D%5B%5D",
css: '\\21 \\40 \\24 \\25 \\28 \\29 \\3d \\2b \\7b \\7d \\5b \\5d ',
style: '\21 \40 \24 \25 \28 \29 \3d \2b \7b \7d \5b \5d '
},
{
label: "ESAPI maybe bad chars",
input: " ,.-_ ",
html: " ,.-_ ",
js: "\\x20,.-_\\x20",
jsAttr: "\x20,.-_\x20",
uri: "%20%2C.-_%20",
css: '\\20 \\2c \\2e \\2d \\5f \\20 ',
style: '\20 \2c \2e \2d \5f \20 '
},
{
label: "ASCII punctuation",
input: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', // punctuation in ASCII range (lexical order)
html: '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~',
js: '\\x21\\x22\\x23\\x24\\x25\\x26\\x27\\x28\\x29\\x2A\\x2B,-.\\x2F\\x3A\\x3B\\x3C\\x3D\\x3E\\x3F\\x40\\x5B\\x5C\\x5D\\x5E_\\x60\\x7B\\x7C\\x7D\\x7E',
jsAttr: '\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B,-.\x2F\x3A\x3B\x3C\x3D\x3E\x3F\x40\x5B\x5C\x5D\x5E_\x60\x7B\x7C\x7D\x7E',
uri: '%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D%7E',
css: '\\21 \\22 \\23 \\24 \\25 \\26 \\27 \\28 \\29 \\2a \\2b \\2c \\2d \\2e \\2f \\3a \\3b \\3c \\3d \\3e \\3f \\40 \\5b \\5c \\5d \\5e \\5f \\60 \\7b \\7c \\7d \\7e ',
style: '\21 \22 \23 \24 \25 \26 \27 \28 \29 \2a \2b \2c \2d \2e \2f \3a \3b \3c \3d \3e \3f \40 \5b \5c \5d \5e \5f \60 \7b \7c \7d \7e '
},
{
label: 'every ASCII char',
input: ASCII,
html:
' \t\n \r '+ // most controls -> space (including NUL)
' '+ // 0x10 to 0x1F -> space
' ' + // space preserved
'!"#$%&'()*+'+
',-.'+ // safe punctuation
'/'+
'0123456789'+ // in alphanum
':;<=>?@'+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ // in alphanum
'[\]^'+
'_'+ // safe punctuation
'`'+
'abcdefghijklmnopqrstuvwxyz'+ // in alphanum
'{|}~'+
' ', // 0x7f -> space
js:
'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\\x28\\x29\\x2A\\x2B'+
',-.'+ // safe punctuation
'\\x2F'+
'0123456789'+ // in alphanum
'\\x3A\\x3B\\x3C\\x3D\\x3E\\x3F\\x40'+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ // in alphanum
'\\x5B\\x5C\\x5D\\x5E'+
'_'+ // safe punctuation
'\\x60'+
'abcdefghijklmnopqrstuvwxyz'+ // in alphanum
'\\x7B\\x7C\\x7D\\x7E\\x7F',
jsAttr:
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B'+
',-.'+
'\x2F'+
'0123456789'+
'\x3A\x3B\x3C\x3D\x3E\x3F\x40'+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+
'\x5B\x5C\x5D\x5E'+
'_'+
'\x60'+
'abcdefghijklmnopqrstuvwxyz'+
'\x7B\x7C\x7D\x7E\x7F',
uri: '%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C'+
'-.'+ // uri-safe punctuation
'%2F'+
'0123456789'+ // in alphanum
'%3A%3B%3C%3D%3E%3F%40'+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ // in alphanum
'%5B%5C%5D%5E'+
'_'+ // uri-safe punctuation
'%60'+
'abcdefghijklmnopqrstuvwxyz'+ // in alphanum
'%7B%7C%7D%7E%7F',
css: '\\fffd '+ // undefined behaviour
'\\1 \\2 \\3 \\4 \\5 \\6 \\7 \\8 \\9 \\a \\b \\c \\d \\e \\f \\10 \\11 \\12 \\13 \\14 \\15 \\16 \\17 \\18 \\19 \\1a \\1b \\1c \\1d \\1e \\1f \\20 \\21 \\22 \\23 \\24 \\25 \\26 \\27 \\28 \\29 \\2a \\2b \\2c \\2d \\2e \\2f '+
'0123456789'+ // alphanum
'\\3a \\3b \\3c \\3d \\3e \\3f \\40 '+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ // alphanum
'\\5b \\5c \\5d \\5e \\5f \\60 '+
'abcdefghijklmnopqrstuvwxyz'+ // alphanum
'\\7b \\7c \\7d \\7e \\7f ',
style: '\fffd '+ // undefined behaviour
'\1 \2 \3 \4 \5 \6 \7 \8 \9 \a \b \c \d \e \f \10 \11 \12 \13 \14 \15 \16 \17 \18 \19 \1a \1b \1c \1d \1e \1f \20 \21 \22 \23 \24 \25 \26 \27 \28 \29 \2a \2b \2c \2d \2e \2f '+
'0123456789'+ // alphanum
'\3a \3b \3c \3d \3e \3f \40 '+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ // alphanum
'\5b \5c \5d \5e \5f \60 '+
'abcdefghijklmnopqrstuvwxyz'+ // alphanum
'\7b \7c \7d \7e \7f '
},
{
label: "URI-encoded input",
input: '%3Cscript%3E', // i.e., already uri-encoded
html: '%3Cscript%3E',
js: '\\x253Cscript\\x253E',
jsAttr: '\x253Cscript\x253E',
uri: '%253Cscript%253E',
css: '\\25 3Cscript\\25 3E',
style: '\25 3Cscript\25 3E'
},
{
input: "é, ß, "+SNOWMAN+", "+FACE_WITHOUT_MOUTH,
html: "é, ß, "+SNOWMAN+", "+FACE_WITHOUT_MOUTH,
js: "\\u00E9,\\x20\\u00DF,\\x20\\u2603,\\x20\\uD83D\\uDE36",
jsAttr: "\u00E9,\x20\u00DF,\x20\u2603,\x20\uD83D\uDE36",
uri: '%C3%A9%2C%20%C3%9F%2C%20%E2%98%83%2C%20%F0%9F%98%B6',
css: '\\e9 \\2c \\20 \\df \\2c \\20 \\2603 \\2c \\20 '+FACE_WITHOUT_MOUTH, // U+10000 and up are preserved
style: '\e9 \2c \20 \df \2c \20 \2603 \2c \20 '+FACE_WITHOUT_MOUTH
},
{
label: 'CDATA',
input: '<![CDATA[ blah ]]>',
html: '<![CDATA[ blah ]]>',
js: '\\x3C\\x21\\x5BCDATA\\x5B\\x20blah\\x20\\x5D\\x5D\\x3E',
jsAttr: '\x3C\x21\x5BCDATA\x5B\x20blah\x20\x5D\x5D\x3E',
uri: '%3C%21%5BCDATA%5B%20blah%20%5D%5D%3E',
css: '\\3c \\21 \\5b CDATA\\5b \\20 blah\\20 \\5d \\5d \\3e ',
style: '\3c \21 \5b CDATA\5b \20 blah\20 \5d \5d \3e '
},
{
label: 'nbsp',
input: '\u00A0',
html: '\u00A0', // un-encoded
js: '\\u00A0',
jsAttr: '\u00A0',
uri: '%C2%A0',
css: '\\a0 ',
style: '\a0 '
},
{
label: 'README html example',
input: '"><script>alert(\'pwn\')</script>',
html: '"><script>alert('pwn')</script>'
},
{
label: 'README html example 2',
input: "x' onerror='alert(1)",
html: 'x' onerror='alert(1)'
},
{
label: 'integer literal',
input: 1234,
html: "1234",
js: "1234",
jsAttr: "1234",
uri: "1234",
jsObj: "1234",
css: "1234",
style: "1234"
},
{
label: 'boolean literal',
input: true,
html: "true",
js: "true",
jsAttr: "true",
uri: "true",
jsObj: "true",
css: "true",
style: "true"
},
{
label: 'float literal',
input: 1234.5678,
html: "1234.5678",
js: "1234.5678",
jsAttr: "1234.5678",
uri: "1234.5678",
jsObj: "1234.5678",
css: "1234\\2e 5678",
style: "1234\2e 5678"
},
{
label: 'object literal',
input: {key:"</script><script>alert(\"hah!\")"},
jsObj: '{"key":"\\x3C\\x2Fscript\\x3E\\x3Cscript\\x3Ealert\\x28\\"hah\\x21\\"\\x29"}'
},
{
label: 'object literal w/ unicode',
input: {key:"snowman:"+SNOWMAN},
jsObj: '{"key":"snowman:\\u2603"}'
},
{
label: 'object w/ LINE SEPARATOR U+2028 and PARAGRAPH SEPARATOR U+2029',
input: {"line\u2028sep":"para\u2029sep"},
jsObj: '{"line\\u2028sep":"para\\u2029sep"}'
},
{
label: 'array literal',
input: [1,2.3,"ouch",'</script><script>alert(\"hah!\")'],
jsObj: '[1,2.3,"ouch","\\x3C\\x2Fscript\\x3E\\x3Cscript\\x3Ealert\\x28\\"hah\\x21\\"\\x29"]'
},
{
label: 'CDATA in object',
input: {"open":"<![CDATA[", "close": "]]>"},
jsObj: '{"open":"\\x3C\\x21[CDATA[","close":"\\x5D\\x5D\\x3E"}'
},
{
label: "nested array doesn't trigger CDATA protection",
input: [[['a']],['b']],
jsObj: '[[["a"]],["b"]]'
}
];
describe('secure filters', function() {
_.each(ALL_CASES, function(c) {
var input = c.input;
var label = c.label || 'input "'+c.input+'"';
describe('for '+label, function() {
_.each(c, function(expect, filterName) {
if (filterName === 'input' || filterName === 'label') {
return;
}
var func = secureFilters[filterName];
assert(func);
assert.strictEqual(typeof func, "function");
it('filter '+filterName+' produces "'+expect+'"', function() {
var output = func(input);
assert.strictEqual(output, expect);
});
});
});
});
});
describe('exporting to EJS', function() {
function checkAllFilters(ejs) {
assert(ejs.filters);
assert(ejs.filters instanceof Object);
var keys = _.keys(ejs.filters);
assert.equal(keys.length, 7);
assert('html' in ejs.filters);
assert('js' in ejs.filters);
assert('jsAttr' in ejs.filters);
assert('uri' in ejs.filters);
assert('jsObj' in ejs.filters);
assert('css' in ejs.filters);
assert('style' in ejs.filters);
}
it('.configure()s an empty object', function() {
var mockEjs = {};
secureFilters.configure(mockEjs);
checkAllFilters(mockEjs);
});
it('.configure()s an object with .filters', function() {
var mockEjs = { filters: {} };
secureFilters.configure(mockEjs);
checkAllFilters(mockEjs);
});
});
}(this));