forked from angular/domino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxss.js
361 lines (303 loc) · 13 KB
/
xss.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
354
355
356
357
358
359
360
361
'use strict';
var domino = require('../lib');
var puppeteer = require("puppeteer");
var NodeUtils = require('../lib/NodeUtils');
exports = exports.xss = {};
// Tests for HTML serialization concentrating on possible "Mutation based
// XSS vectors"; see https://cure53.de/fp170.pdf
// If we change HTML serialization such that any of these tests fail, please
// review the change very carefully for potential XSS vectors!
async function alertFired(html) {
let alerted = false;
const page = await incognito.newPage();
page.on("dialog", async dialog => {
alerted = true;
await dialog.accept();
});
await page.goto("data:text/html," + html, {waitUntil: 'load'});
return alerted;
}
/** @type {puppeteer.Browser} */
let browser;
/** @type {puppeteer.BrowserContext} */
let incognito;
exports.before = async function() {
browser = await puppeteer.launch({headless:"new"});
incognito = await browser.createIncognitoBrowserContext();
}
exports.after = async function() {
await incognito.close();
await browser.close();
}
exports.fp170_31 = function() {
var document = domino.createDocument(
'<img src="test.jpg" alt="``onload=xss()" />'
);
// In particular, ensure alt attribute is quoted, not: ...alt=``onload=xss()
document.body.innerHTML.should.equal(
'<img src="test.jpg" alt="``onload=xss()">'
);
};
exports.fp170_32 = function() {
var document = domino.createDocument(
'<article xmlns="urn:img src=x onerror=xss()//">123'
);
// XXX check XML serialization as well, once that's implemented
// In particular, ensure that the xmlns string isn't used as an XML prefix
// when serializing (and, of course, that attribute value is quoted)
document.body.innerHTML.should.equal(
'<article xmlns="urn:img src=x onerror=xss()//">123</article>'
);
};
exports.fp170_33 = function() {
var document = domino.createDocument(
'<p style="font -family:\'ar\\27\\3bx\\3aexpression\\28xss\\28\\29\\29\\3bial\'"></p>'
);
// Be sure domino doesn't decode the backslash escapes
// (especially in the future if we parse the CSS values more fully)
document.body.innerHTML.should.equal(
'<p style="font -family:\'ar\\27\\3bx\\3aexpression\\28xss\\28\\29\\29\\3bial\'"></p>'
);
};
exports.fp170_34 = function() {
var document = domino.createDocument(
'<p style="font -family:\'ar";x=expression(xss())/*ial\'"></p>'
);
// Be sure domino re-encodes the entities correctly
// (especially in the future if we parse the CSS values more fully)
document.body.innerHTML.should.equal(
'<p style="font -family:\'ar";x=expression(xss())/*ial\'"></p>'
);
};
exports.fp170_35 = function() {
var document = domino.createDocument(
'<img style="font-fa\\22onload\\3dxss\\28\\29\\20mily:\'arial\'" src="test.jpg" />'
);
// Again, ensure domino doesn't decode the backslash escapes
// (especially in the future if we parse the CSS values more fully)
document.body.innerHTML.should.equal(
'<img style="font-fa\\22onload\\3dxss\\28\\29\\20mily:\'arial\'" src="test.jpg">'
);
};
exports.fp170_36 = function() {
var document = domino.createDocument(
'<style>*{font-family:\'ar<img src="test.jpg" onload="xss()"/>ial\'}</style>'
);
// Ensure that HTML entities are properly encoded inside <style>
document.head.innerHTML.should.equal(
'<style>*{font-family:\'ar<img src="test.jpg" onload="xss()"/>ial\'}</style>'
);
};
exports.fp170_37 = function() {
var document = domino.createDocument(
'<p><svg><style>*{font-family:\'</style><img/src=x	onerror=xss()//\'}</style></svg></p>'
);
// Ensure that HTML entities are properly encoded inside <style>
document.body.innerHTML.should.equal(
'<p><svg><style>*{font-family:\'</style><img/src=x\tonerror=xss()//\'}</style></svg></p>'
);
};
exports.escapeAngleBracketsInDivAttr = function() {
var document = domino.createDocument(
`<div>You don't have JS! Click<a href="#" title="Search for </div><script>alert(1)</script> without JS">here</a> to go to the no-js website.</div>`
);
document.body.innerHTML.should.equal(
`<div>You don't have JS! Click<a href="#" title="Search for </div><script>alert(1)</script> without JS">here</a> to go to the no-js website.</div>`
);
};
exports.escapeAngleBracketsInNoScriptAttr = function() {
var document = domino.createDocument(
`<div><noscript>You don't have JS! Click<a href="#" title="Search for </noscript><script>alert(1)</script> without JS">here</a> to go to the no-js website.</noscript></div>`
);
document.body.innerHTML.should.equal(
`<div><noscript>You don't have JS! Click<a href="#" title="Search for </noscript><script>alert(1)</script> without JS">here</a> to go to the no-js website.</noscript></div>`
);
};
exports.styleMatchingClosingTagInRawText = function() {
const document = domino.createDocument('');
const style = document.createElement("style");
style.textContent = "abc</style><script>alert(1)</script>";
document.body.appendChild(style);
// Ensure that HTML entities are properly encoded inside <style>
document.body.serialize().should.equal(
'<style>abc</style><script>alert(1)</script></style>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};
exports.styleMatchingClosingTagSkipsInsideCommentedContent = function() {
const document = domino.createDocument('');
const style = document.createElement("style");
style.textContent = "abc<!--</style>--><script>alert(1)</script>";
document.body.appendChild(style);
document.body.serialize().should.equal(
'<style>abc<!--</style>--><script>alert(1)</script></style>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};
exports.styleMatchingClosingTagAfterClosingComment = function() {
const document = domino.createDocument('');
const style = document.createElement("style");
style.textContent = "abc--></style><script>alert(1)</script>";
document.body.appendChild(style);
// Ensure that HTML entities are properly encoded inside <style>
document.body.serialize().should.equal(
'<style>abc--></style><script>alert(1)</script></style>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};
exports.styleMatchingClosingTagSkipsUnclosedCommentedContent = function() {
const document = domino.createDocument('');
const style = document.createElement("style");
style.textContent = "abc<!--</style><script>alert(1)</script>";
document.body.appendChild(style);
document.body.serialize().should.equal(
'<style>abc<!--</style><script>alert(1)</script></style>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};
exports.scriptMatchingClosingTagInRawText = function() {
const document = domino.createDocument('');
const script = document.createElement("script");
script.textContent = "abc</script><script>alert(1)</script>";
document.body.appendChild(script);
// Ensure that HTML entities are properly encoded inside <script>
// Note: the `</script>` is encoded in both places.
document.body.serialize().should.equal(
'<script>abc</script><script>alert(1)</script></script>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};
exports.oneRawTextTagInsideAnotherOne = function() {
const document = domino.createDocument('');
const xmp = document.createElement("xmp");
const style = document.createElement("style");
xmp.textContent = "</style><script>alert(1)</script>";
style.appendChild(xmp);
document.body.appendChild(style);
document.body.serialize().should.equal(
'<style><xmp></style><script>alert(1)</script></xmp></style>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.xssInAttributeInsideRawTextTag = function() {
const document = domino.createDocument('');
const xmp = document.createElement("xmp");
const div = document.createElement("div");
div.title = "</xmp><script>alert(1)</script>";
xmp.appendChild(div);
document.body.appendChild(xmp);
document.body.serialize().should.equal(
'<xmp><div title="</xmp><script>alert(1)</script>"></div></xmp>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.commentNodeInsideRawTextTag = function() {
const document = domino.createDocument('');
const xmp = document.createElement("xmp");
const comment = document.createComment('</xmp><script>alert(1)</script>');
xmp.appendChild(comment);
document.body.appendChild(xmp);
document.body.serialize().should.equal(
'<xmp><!--</xmp><script>alert(1)</script>--></xmp>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.alternativeEndTagForRawTextTag = function() {
const document = domino.createDocument('');
const style = document.createElement("style");
style.textContent = "</style /foobar><script>alert(1)</script>";
document.body.appendChild(style);
document.body.serialize().should.equal(
'<style></style /foobar><script>alert(1)</script></style>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.badCommentNode = function() {
const document = domino.createDocument('');
const comment = document.createComment('--><script>alert(1)</script>');
document.body.appendChild(comment);
document.body.serialize().should.equal(
'<!----><script>alert(1)</script>-->'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.anotherBadCommentNode = function() {
const document = domino.createDocument('');
const comment = document.createComment('--!><script>alert(1)</script>');
document.body.appendChild(comment);
document.body.serialize().should.equal(
'<!----!><script>alert(1)</script>-->'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.badProcessingInstruction = function() {
const document = domino.createDocument('');
const pi = document.createProcessingInstruction("bad", "><script>alert(1)</script>");
document.body.appendChild(pi);
document.body.serialize().should.equal(
'<?bad ><script>alert(1)</script>?>'
);
const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
}
exports.verifyEscapeMatchingClosingTag = function() {
const cases = [
['', 'style', ''], // no artifacts while processing an empty string
['abc', 'script', 'abc'], // no artifacts while processing a string without closing tags
['</style /foobar>abc', 'style', '</style /foobar>abc'],
['</xmp><script>alert(1)</script>', 'xmp', '</xmp><script>alert(1)</script>'],
['"</xmp>"', 'xmp', '"</xmp>"'],
// Raw content element inside another raw content element.
['<xmp></style><script>alert(1)</script></xmp>', 'style',
'<xmp></style><script>alert(1)</script></xmp>'],
['abc</script><script>alert(1)</script>', 'script',
'abc</script><script>alert(1)</script>'],
// No changes to the content in case there are no matching closing tags.
['<xmp></style><script>alert(1)</script></xmp>', 'iframe',
'<xmp></style><script>alert(1)</script></xmp>'],
];
for (const [rawContent, parentTag, expected] of cases) {
NodeUtils.ɵescapeMatchingClosingTag(rawContent, parentTag).should.equal(expected);
}
}
exports.verifyEscapeClosingCommentTag = function() {
const cases = [
['', ''], // no artifacts while processing an empty string
['abc', 'abc'], // no artifacts while processing a string without closing tags
['a-->bc-->', 'a-->bc-->'],
['a--!>bc--!>', 'a--!>bc--!>'],
['a- -> b c - ->', 'a- -> b c - ->'],
['a- -!> b c - -!>', 'a- -!> b c - -!>'],
['<!--a--!> <!--b--!>', '<!--a--!> <!--b--!>'],
['<!--a--> <!--b-->', '<!--a--> <!--b-->'],
['<!--a--< <!--b--<', '<!--a--< <!--b--<'],
];
for (const [rawContent, expected] of cases) {
NodeUtils.ɵescapeClosingCommentTag(rawContent).should.equal(expected);
}
}
exports.verifyEscapeProcessingInstructionContent = function() {
const cases = [
['', ''], // no artifacts while processing an empty string
['abc', 'abc'], // no artifacts while processing a string without `>` chars
['>>>', '>>>'],
['<<<', '<<<'],
['><script>alert(1)</script>', '><script>alert(1)</script>'],
['<!--a-->', '<!--a-->'],
['">"', '">"'],
];
for (const [rawContent, expected] of cases) {
NodeUtils.ɵescapeProcessingInstructionContent(rawContent).should.equal(expected);
}
}