-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-http-response-splitting.js
74 lines (66 loc) · 1.94 KB
/
test-http-response-splitting.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
'use strict';
const common = require('../common');
const http2 = require('http2');
const url = require('url');
const assert = require('assert');
const Countdown = require('../common/countdown');
// Response splitting example, credit: Amit Klein, Safebreach
const str = '/welcome?lang=bar%c4%8d%c4%8aContentLength:%200%c4%8d%c4%8a%c' +
'4%8d%c4%8aHTTP/1.1%20200%20OK%c4%8d%c4%8aContentLength:%202' +
'0%c4%8d%c4%8aLastModified:%20Mon,%2027%20Oct%202003%2014:50:18' +
'%20GMT%c4%8d%c4%8aContentType:%20text/html%c4%8d%c4%8a%c4%8' +
'd%c4%8a%3chtml%3eGotcha!%3c/html%3e';
// Response splitting example, credit: Сковорода Никита Андреевич (@ChALkeR)
const x = 'fooഊSet-Cookie: foo=barഊഊ<script>alert("Hi!")</script>';
const y = 'foo⠊Set-Cookie: foo=bar';
let count = 0;
let client;
const countdown = new Countdown(3, () => {
client.close();
server.close();
});
function test(res, code, key, value) {
const header = { [key]: value };
common.expectsError(
() => res.writeHead(code, header),
{
code: 'ERR_INVALID_CHAR',
type: TypeError,
message: `Invalid character in header content ["${key}"]`
}
);
}
const server = http2.createServer((req, res) => {
switch (count++) {
case 0:
const loc = url.parse(req.url, true).query.lang;
test(res, 302, 'Location', `/foo?lang=${loc}`);
break;
case 1:
test(res, 200, 'foo', x);
break;
case 2:
test(res, 200, 'foo', y);
break;
default:
assert.fail('should not get to here.');
}
countdown.dec();
res.end('ok');
});
server.listen(0, () => {
client = http2.connect('http://localhost:' + server.address().port);
const send = (path) => {
const req = client.request({
':path': path
});
req.end();
req.resume();
req.on('end', common.mustCall(() => {
req.close();
}));
}
send(str);
send('/');
send('/');
});