|
| 1 | +// Flags: --experimental-quic --no-warnings |
| 2 | + |
| 3 | +// Regression test for an unauthenticated remote crash in the QUIC Version |
| 4 | +// Negotiation path. |
| 5 | +// |
| 6 | +// ngtcp2_pkt_decode_version_cid() does not clamp the connection ID lengths |
| 7 | +// to NGTCP2_MAX_CIDLEN (20) when the packet's version is unsupported -- it |
| 8 | +// returns the raw single-byte length fields from the wire, which can be up |
| 9 | +// to 255. Endpoint::Receive() used to build CID objects (each backed by a |
| 10 | +// fixed 20-byte buffer) directly from those lengths before any bound check, |
| 11 | +// so a single crafted UDP datagram with an oversized DCID/SCID length would |
| 12 | +// overflow the buffer and abort the process before the handshake. |
| 13 | +// |
| 14 | +// This test sends such a datagram directly with node:dgram and asserts the |
| 15 | +// endpoint drops it without crashing, while a well-formed unsupported-version |
| 16 | +// datagram still produces exactly one Version Negotiation response. |
| 17 | + |
| 18 | +import{hasQuic,skip,mustNotCall}from'../common/index.mjs'; |
| 19 | +importassertfrom'node:assert'; |
| 20 | + |
| 21 | +const{ strictEqual }=assert; |
| 22 | + |
| 23 | +if(!hasQuic){ |
| 24 | +skip('QUIC is not enabled'); |
| 25 | +} |
| 26 | + |
| 27 | +const{ createSocket }=awaitimport('node:dgram'); |
| 28 | +const{ listen }=awaitimport('../common/quic.mjs'); |
| 29 | + |
| 30 | +// A long-header QUIC packet must be at least NGTCP2_MAX_UDP_PAYLOAD_SIZE |
| 31 | +// (1200) bytes for an unsupported version to decode as a VN trigger. |
| 32 | +constPACKET_SIZE=1200; |
| 33 | + |
| 34 | +// Build a QUIC long-header packet with an unsupported version and the given |
| 35 | +// DCID/SCID lengths. Lengths greater than 20 are only expressible because the |
| 36 | +// on-wire length field is a single byte (0-255). |
| 37 | +functionbuildLongHeaderPacket(dcidLen,scidLen){ |
| 38 | +constpacket=Buffer.alloc(PACKET_SIZE); |
| 39 | +// Header form bit (0x80) + fixed bit (0x40). |
| 40 | +packet[0]=0xc0; |
| 41 | +// Version 0x0a0a0a0a: nonzero (so it is not a real VN packet) and not a |
| 42 | +// version Node supports, which forces the NGTCP2_ERR_VERSION_NEGOTIATION |
| 43 | +// decode path. |
| 44 | +packet.writeUInt32BE(0x0a0a0a0a,1); |
| 45 | +packet[5]=dcidLen;// DCID length byte |
| 46 | +// DCID bytes occupy [6, 6 + dcidLen); SCID length byte follows them. |
| 47 | +packet[6+dcidLen]=scidLen; |
| 48 | +// Remaining bytes stay zero-filled as padding to reach PACKET_SIZE. |
| 49 | +returnpacket; |
| 50 | +} |
| 51 | + |
| 52 | +// No handshake ever completes: the test only sends raw datagrams, so the |
| 53 | +// session callback must never fire. |
| 54 | +constserverEndpoint=awaitlisten(mustNotCall()); |
| 55 | +const{ address, port }=serverEndpoint.address; |
| 56 | + |
| 57 | +constsocket=createSocket('udp4'); |
| 58 | + |
| 59 | +functionsend(packet){ |
| 60 | +returnnewPromise((resolve,reject)=>{ |
| 61 | +socket.send(packet,port,address,(err)=>{ |
| 62 | +if(err)reject(err); |
| 63 | +elseresolve(); |
| 64 | +}); |
| 65 | +}); |
| 66 | +} |
| 67 | + |
| 68 | +// 1. Oversized DCID (21 > NGTCP2_MAX_CIDLEN). Before the fix this aborted the |
| 69 | +// process. After the fix it must be dropped silently. |
| 70 | +awaitsend(buildLongHeaderPacket(21,0)); |
| 71 | + |
| 72 | +// 2. A well-formed unsupported-version packet with valid (<= 20 byte) CIDs. |
| 73 | +// This must still elicit exactly one Version Negotiation response, proving |
| 74 | +// the fix did not break legitimate version negotiation. |
| 75 | +awaitsend(buildLongHeaderPacket(8,8)); |
| 76 | + |
| 77 | +// Poll until the valid packet has been processed into a VN response. |
| 78 | +constdeadline=Date.now()+2000; |
| 79 | +while(serverEndpoint.stats.versionNegotiationCount===0n){ |
| 80 | +if(Date.now()>deadline)break; |
| 81 | +awaitnewPromise((resolve)=>setTimeout(resolve,25)); |
| 82 | +} |
| 83 | + |
| 84 | +// Exactly one VN response: the oversized packet was dropped (not crashed, not |
| 85 | +// negotiated), the valid packet was negotiated. |
| 86 | +strictEqual(serverEndpoint.stats.versionNegotiationCount,1n); |
| 87 | + |
| 88 | +socket.close(); |
| 89 | +awaitserverEndpoint.close(); |
0 commit comments