|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | + |
| 8 | +const http2 = require('http2'); |
| 9 | +const net = require('net'); |
| 10 | + |
| 11 | +// When a peer sends GOAWAY(NO_ERROR), the session starts a graceful close |
| 12 | +// on its own. If nghttp2 later terminates the session internally because of |
| 13 | +// a protocol error (here, an oversized frame) while a stream is still open, |
| 14 | +// the session must still be torn down and the error surfaced, rather than |
| 15 | +// leaving the stream and connection hanging. |
| 16 | + |
| 17 | +const server = http2.createServer(); |
| 18 | + |
| 19 | +server.on('stream', common.mustCall((stream) => { |
| 20 | + stream.on('error', common.expectsError({ |
| 21 | + code: 'ERR_HTTP2_ERROR', |
| 22 | + name: 'Error', |
| 23 | + message: 'Protocol error' |
| 24 | + })); |
| 25 | + stream.on('close', common.mustCall()); |
| 26 | +})); |
| 27 | + |
| 28 | +server.on('session', common.mustCall((session) => { |
| 29 | + session.on('goaway', common.mustCall()); |
| 30 | + session.on('error', common.expectsError({ |
| 31 | + code: 'ERR_HTTP2_ERROR', |
| 32 | + name: 'Error', |
| 33 | + message: 'Protocol error' |
| 34 | + })); |
| 35 | + |
| 36 | + session.on('close', common.mustCall(() => server.close())); |
| 37 | +})); |
| 38 | + |
| 39 | +server.listen(0, common.mustCall(() => { |
| 40 | + const conn = net.connect({ |
| 41 | + port: server.address().port, |
| 42 | + allowHalfOpen: true, |
| 43 | + }); |
| 44 | + |
| 45 | + // HTTP/2 client connection preface. |
| 46 | + conn.write('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'); |
| 47 | + |
| 48 | + // Empty SETTINGS frame. |
| 49 | + const settingsFrame = Buffer.alloc(9); |
| 50 | + settingsFrame[3] = 0x04; // type: SETTINGS |
| 51 | + conn.write(settingsFrame); |
| 52 | + |
| 53 | + let inbuf = Buffer.alloc(0); |
| 54 | + let state = 'settingsHeader'; |
| 55 | + let settingsFrameLength; |
| 56 | + |
| 57 | + conn.on('data', (chunk) => { |
| 58 | + inbuf = Buffer.concat([inbuf, chunk]); |
| 59 | + |
| 60 | + switch (state) { |
| 61 | + case 'settingsHeader': |
| 62 | + if (inbuf.length < 9) return; |
| 63 | + settingsFrameLength = inbuf.readUIntBE(0, 3); |
| 64 | + inbuf = inbuf.slice(9); |
| 65 | + state = 'readingSettings'; |
| 66 | + // Fallthrough |
| 67 | + case 'readingSettings': { |
| 68 | + if (inbuf.length < settingsFrameLength) return; |
| 69 | + inbuf = inbuf.slice(settingsFrameLength); |
| 70 | + state = 'done'; |
| 71 | + |
| 72 | + // ACK the server SETTINGS. |
| 73 | + const ack = Buffer.alloc(9); |
| 74 | + ack[3] = 0x04; // type: SETTINGS |
| 75 | + ack[4] = 0x01; // flag: ACK |
| 76 | + conn.write(ack); |
| 77 | + |
| 78 | + // Open stream 1 without END_STREAM, so it stays open on the server. |
| 79 | + // HPACK: :method GET (0x82), :scheme http (0x86), :path / (0x84), |
| 80 | + // :authority localhost (literal with indexed name 1). |
| 81 | + const block = Buffer.from([ |
| 82 | + 0x82, 0x86, 0x84, 0x41, 0x09, |
| 83 | + ...Buffer.from('localhost'), |
| 84 | + ]); |
| 85 | + const headers = Buffer.alloc(9); |
| 86 | + headers.writeUIntBE(block.length, 0, 3); |
| 87 | + headers[3] = 0x01; // type: HEADERS |
| 88 | + headers[4] = 0x04; // flags: END_HEADERS |
| 89 | + headers.writeUInt32BE(1, 5); // stream id: 1 |
| 90 | + conn.write(Buffer.concat([headers, block])); |
| 91 | + |
| 92 | + // GOAWAY(NO_ERROR), last stream id 0. This makes the server |
| 93 | + // gracefully close its session without any application code. |
| 94 | + const goaway = Buffer.alloc(17); |
| 95 | + goaway.writeUIntBE(8, 0, 3); // length: 8 |
| 96 | + goaway[3] = 0x07; // type: GOAWAY |
| 97 | + goaway.writeUInt32BE(0, 9); // last stream id |
| 98 | + goaway.writeUInt32BE(0, 13); // error code: NO_ERROR |
| 99 | + conn.write(goaway); |
| 100 | + |
| 101 | + // Send a DATA frame header claiming length 16385, which exceeds the |
| 102 | + // default max_frame_size of 16384, so nghttp2 calls |
| 103 | + // nghttp2_session_terminate_session(FRAME_SIZE_ERROR) internally. |
| 104 | + const oversized = Buffer.alloc(9); |
| 105 | + oversized.writeUIntBE(16385, 0, 3); // length: 16385 (one over max) |
| 106 | + oversized[3] = 0x00; // type: DATA |
| 107 | + oversized.writeUInt32BE(1, 5); // stream id: 1 |
| 108 | + conn.write(oversized); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + // The server must close the connection after sending GOAWAY: |
| 114 | + conn.on('end', common.mustCall(() => conn.end())); |
| 115 | + conn.on('close', common.mustCall()); |
| 116 | +})); |
0 commit comments