summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-transfer-encoding-smuggling.js
blob: 472b717022d7e17d2458b13802b5d6a443cc7607 (plain)
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
'use strict';

const common = require('../common');
const assert = require('assert');

const http = require('http');
const net = require('net');

{
  const msg = [
    'POST / HTTP/1.1',
    'Host: 127.0.0.1',
    'Transfer-Encoding: chunked',
    'Transfer-Encoding: chunked-false',
    'Connection: upgrade',
    '',
    '1',
    'A',
    '0',
    '',
    'GET /flag HTTP/1.1',
    'Host: 127.0.0.1',
    '',
    '',
  ].join('\r\n');

  const server = http.createServer(common.mustNotCall((req, res) => {
    res.end();
  }, 1));

  server.listen(0, common.mustSucceed(() => {
    const client = net.connect(server.address().port, 'localhost');

    let response = '';

    // Verify that the server listener is never called

    client.on('data', common.mustCall((chunk) => {
      response += chunk;
    }));

    client.setEncoding('utf8');
    client.on('error', common.mustNotCall());
    client.on('end', common.mustCall(() => {
      assert.strictEqual(
        response,
        'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
      );
      server.close();
    }));
    client.write(msg);
    client.resume();
  }));
}

{
  const msg = [
    'POST / HTTP/1.1',
    'Host: 127.0.0.1',
    'Transfer-Encoding: chunked',
    ' , chunked-false',
    'Connection: upgrade',
    '',
    '1',
    'A',
    '0',
    '',
    'GET /flag HTTP/1.1',
    'Host: 127.0.0.1',
    '',
    '',
  ].join('\r\n');

  const server = http.createServer(common.mustCall((request, response) => {
    assert.notStrictEqual(request.url, '/admin');
    response.end('hello world');
  }), 1);

  server.listen(0, common.mustSucceed(() => {
    const client = net.connect(server.address().port, 'localhost');

    client.on('end', common.mustCall(function() {
      server.close();
    }));

    client.write(msg);
    client.resume();
  }));
}