Compare commits

..

1 Commits

Author SHA1 Message Date
pfg
5d90ee679f test: add tls async cb after socket end 2025-05-29 17:52:35 -07:00
3 changed files with 89 additions and 41 deletions

View File

@@ -95,6 +95,7 @@ const owner_symbol = Symbol("owner_symbol");
const kServerSocket = Symbol("kServerSocket");
const kBytesWritten = Symbol("kBytesWritten");
const bunTLSConnectOptions = Symbol.for("::buntlsconnectoptions::");
const bunTLSServerSession = Symbol.for("::bunTLSServerSession::");
const kReinitializeHandle = Symbol("kReinitializeHandle");
const kRealListen = Symbol("kRealListen");
@@ -439,6 +440,16 @@ const ServerHandlers: SocketHandler = {
self.servername = socket.getServername();
const server = self.server;
self.alpnProtocol = socket.alpnProtocol;
if (!server[bunTLSServerSession]) {
server[bunTLSServerSession] = true;
if (server.listenerCount("newSession") > 0) {
const done = () => {};
server.emit("newSession", Buffer.alloc(0), self.getSession(), done);
}
} else if (server.listenerCount("resumeSession") > 0) {
const cb = () => {};
server.emit("resumeSession", Buffer.alloc(0), cb);
}
if (self._requestCert || self._rejectUnauthorized) {
if (verifyError) {
self.authorized = false;
@@ -920,9 +931,7 @@ Socket.prototype.connect = function connect(...args) {
process.nextTick(() => {
this.resume();
});
if (!connection && !fd) {
this.connecting = true;
}
this.connecting = true;
}
if (fd) {
return this;
@@ -2096,6 +2105,7 @@ function Server(options?, connectionListener?) {
this[bunSocketServerConnections] = 0;
this[bunSocketServerOptions] = undefined;
this[bunTLSServerSession] = false;
this.allowHalfOpen = allowHalfOpen;
this.keepAlive = keepAlive;
this.keepAliveInitialDelay = keepAliveInitialDelay;

View File

@@ -0,0 +1,76 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
const tls = require('tls');
// Check that TLS1.2 session resumption callbacks don't explode when made after
// the tls socket is destroyed. Disable TLS ticket support to force the legacy
// session resumption mechanism to be used.
// TLS1.2 is the last protocol version to support TLS sessions, after that the
// new and resume session events will never be emitted on the server.
const options = {
secureOptions: SSL_OP_NO_TICKET,
key: fixtures.readKey('rsa_private.pem'),
cert: fixtures.readKey('rsa_cert.crt')
};
const server = tls.createServer(options, common.mustCall());
let sessionCb = null;
let client = null;
server.on('newSession', common.mustCall((key, session, done) => {
done();
}));
server.on('resumeSession', common.mustCall((id, cb) => {
sessionCb = cb;
// Destroy the client and then call the session cb, to check that the cb
// doesn't explode when called after the handle has been destroyed.
next();
}));
server.listen(0, common.mustCall(() => {
const clientOpts = {
// Don't send a TLS1.3/1.2 ClientHello, they contain a fake session_id,
// which triggers a 'resumeSession' event for client1. TLS1.2 ClientHello
// won't have a session_id until client2, which will have a valid session.
maxVersion: 'TLSv1.2',
port: server.address().port,
rejectUnauthorized: false,
session: false
};
const s1 = tls.connect(clientOpts, common.mustCall(() => {
clientOpts.session = s1.getSession();
console.log('1st secure');
s1.destroy();
const s2 = tls.connect(clientOpts, (s) => {
console.log('2nd secure');
s2.destroy();
}).on('connect', common.mustCall(() => {
console.log('2nd connected');
client = s2;
next();
}));
}));
}));
function next() {
if (!client || !sessionCb)
return;
client.destroy();
setTimeout(common.mustCall(() => {
sessionCb();
server.close();
}), 100);
}

View File

@@ -1,38 +0,0 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const { duplexPair } = require('stream');
const assert = require('assert');
const { TLSSocket, connect } = require('tls');
const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');
const ca = fixtures.readKey('ca1-cert.pem');
const [ clientSide, serverSide ] = duplexPair();
const clientTLS = connect({
socket: clientSide,
ca,
host: 'agent1' // Hostname from certificate
});
const serverTLS = new TLSSocket(serverSide, {
isServer: true,
key,
cert,
ca
});
assert.strictEqual(clientTLS.connecting, false);
assert.strictEqual(serverTLS.connecting, false);
clientTLS.on('secureConnect', common.mustCall(() => {
clientTLS.write('foobar', common.mustCall(() => {
assert.strictEqual(serverTLS.read().toString(), 'foobar');
assert.strictEqual(clientTLS._handle.writeQueueSize, 0);
}));
assert.ok(clientTLS._handle.writeQueueSize > 0);
}));