|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | + |
| 8 | +// tls.connect() builds a SecureContext per connection and marks it as single |
| 9 | +// use, so that the underlying SSL_CTX is released as soon as the socket |
| 10 | +// closes instead of when the JS wrapper happens to be garbage collected. |
| 11 | +// Regression test for https://github.com/nodejs/node/issues/66002, where the |
| 12 | +// flag was lost on its way to the context and nothing was ever closed. |
| 13 | + |
| 14 | +const assert = require('assert'); |
| 15 | +const tls = require('tls'); |
| 16 | +const fixtures = require('../common/fixtures'); |
| 17 | +const { createMockedLookup } = require('../common/dns'); |
| 18 | + |
| 19 | +const key = fixtures.readKey('agent1-key.pem'); |
| 20 | +const cert = fixtures.readKey('agent1-cert.pem'); |
| 21 | + |
| 22 | +const server = tls.createServer({ key, cert }, (conn) => conn.end()); |
| 23 | + |
| 24 | +// Bound to a single address so that the addresses the handle-swap case |
| 25 | +// retries through are refused rather than answered by this server. |
| 26 | +server.listen(0, '127.0.0.1', common.mustCall(() => { |
| 27 | + connectWithOwnContext(common.mustCall(() => { |
| 28 | + connectWithSharedContext(common.mustCall(() => { |
| 29 | + connectAcrossHandleSwaps(common.mustCall(() => { |
| 30 | + connectWithKeepAlive(common.mustCall(() => server.close())); |
| 31 | + })); |
| 32 | + })); |
| 33 | + })); |
| 34 | +})); |
| 35 | + |
| 36 | +// _destroySSL() runs from the immediate queue, after the 'close' event. |
| 37 | +function afterDestroySSL(socket, fn) { |
| 38 | + socket.on('close', common.mustCall(() => setImmediate(fn))); |
| 39 | +} |
| 40 | + |
| 41 | +function connectWithOwnContext(done) { |
| 42 | + const socket = tls.connect({ |
| 43 | + host: '127.0.0.1', |
| 44 | + port: server.address().port, |
| 45 | + rejectUnauthorized: false, |
| 46 | + }, common.mustCall(() => { |
| 47 | + const secureContext = socket.ssl._secureContext; |
| 48 | + assert.strictEqual(secureContext.singleUse, true); |
| 49 | + assert.notStrictEqual(secureContext.context, null); |
| 50 | + |
| 51 | + afterDestroySSL(socket, common.mustCall(() => { |
| 52 | + assert.strictEqual(secureContext.context, null); |
| 53 | + done(); |
| 54 | + })); |
| 55 | + })); |
| 56 | +} |
| 57 | + |
| 58 | +function connectWithSharedContext(done) { |
| 59 | + // A context passed in by the user may outlive the connection, so it must |
| 60 | + // not be marked single use, and it must still work for the next socket. |
| 61 | + const secureContext = tls.createSecureContext(); |
| 62 | + let remaining = 2; |
| 63 | + |
| 64 | + (function connectOnce() { |
| 65 | + const socket = tls.connect({ |
| 66 | + host: '127.0.0.1', |
| 67 | + port: server.address().port, |
| 68 | + rejectUnauthorized: false, |
| 69 | + secureContext, |
| 70 | + }, common.mustCall(() => { |
| 71 | + assert.strictEqual(socket.ssl._secureContext, secureContext); |
| 72 | + assert.strictEqual(secureContext.singleUse, undefined); |
| 73 | + |
| 74 | + afterDestroySSL(socket, common.mustCall(() => { |
| 75 | + assert.notStrictEqual(secureContext.context, null); |
| 76 | + if (--remaining === 0) done(); |
| 77 | + else connectOnce(); |
| 78 | + })); |
| 79 | + })); |
| 80 | + })(); |
| 81 | +} |
| 82 | + |
| 83 | +function connectAcrossHandleSwaps(done) { |
| 84 | + // autoSelectFamily reinitializes the handle on every failed attempt, and the |
| 85 | + // successive TLSWraps share the socket's context. Releasing it with the old |
| 86 | + // handle leaves the next attempt without a context. Two failing addresses |
| 87 | + // are needed: the close happens on the first swap, and the next swap is what |
| 88 | + // trips over it. |
| 89 | + const socket = tls.connect({ |
| 90 | + host: 'example.org', |
| 91 | + port: server.address().port, |
| 92 | + rejectUnauthorized: false, |
| 93 | + autoSelectFamily: true, |
| 94 | + autoSelectFamilyAttemptTimeout: |
| 95 | + common.defaultAutoSelectFamilyAttemptTimeout, |
| 96 | + lookup: createMockedLookup('::1', '127.0.0.2', '127.0.0.1'), |
| 97 | + }, common.mustCall(() => { |
| 98 | + // `ssl` is cleared while the handle is swapped, so read the context from |
| 99 | + // the handle the socket ended up with. |
| 100 | + const secureContext = socket._handle._secureContext; |
| 101 | + assert.strictEqual(secureContext.singleUse, true); |
| 102 | + assert.notStrictEqual(secureContext.context, null); |
| 103 | + |
| 104 | + afterDestroySSL(socket, common.mustCall(() => { |
| 105 | + assert.strictEqual(secureContext.context, null); |
| 106 | + done(); |
| 107 | + })); |
| 108 | + })); |
| 109 | +} |
| 110 | + |
| 111 | +function connectWithKeepAlive(done) { |
| 112 | + // tls.connect() only asks for a single-use context when TCP keepalive is |
| 113 | + // off, so a client that enables it leaves its context to the garbage |
| 114 | + // collector. Pinned here as the current behaviour: whether the context |
| 115 | + // should be released early in this case too is the open question in the |
| 116 | + // issue, and is deliberately not decided by this change. |
| 117 | + const socket = tls.connect({ |
| 118 | + host: '127.0.0.1', |
| 119 | + port: server.address().port, |
| 120 | + rejectUnauthorized: false, |
| 121 | + keepAlive: true, |
| 122 | + }, common.mustCall(() => { |
| 123 | + const secureContext = socket.ssl._secureContext; |
| 124 | + assert.strictEqual(secureContext.singleUse, undefined); |
| 125 | + assert.notStrictEqual(secureContext.context, null); |
| 126 | + |
| 127 | + afterDestroySSL(socket, common.mustCall(() => { |
| 128 | + assert.notStrictEqual(secureContext.context, null); |
| 129 | + done(); |
| 130 | + })); |
| 131 | + })); |
| 132 | +} |
0 commit comments