Skip to content

Commit

Permalink
Close connection to server on connect errors
Browse files Browse the repository at this point in the history
  • Loading branch information
luddd3 committed Sep 18, 2021
1 parent fe53e18 commit 940ca2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ function connect(url, socketOptions, openCallback) {
if (timeout) sock.setTimeout(0);
if (err === null) {
openCallback(null, c);
} else {
// The connection isn't closed by the server on e.g. wrong password
sock.end();
sock.destroy();
openCallback(err);
}
else openCallback(err);
});
}

Expand Down
48 changes: 47 additions & 1 deletion test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

var connect = require('../lib/connect').connect;
var credentialsFromUrl = require('../lib/connect').credentialsFromUrl;
var defs = require('../lib/defs');
var assert = require('assert');
var util = require('./util');
var net = require('net');
var fail = util.fail, succeed = util.succeed,
var fail = util.fail, succeed = util.succeed, latch = util.latch,
kCallback = util.kCallback,
succeedIfAttributeEquals = util.succeedIfAttributeEquals;
var format = require('util').format;
Expand Down Expand Up @@ -147,5 +148,50 @@ suite("Connect API", function() {
else done();
});
});
});

suite('Errors on connect', function() {
var server
afterEach(function() {
if (server) {
server.close();
}
})

test("closes underlying connection on authentication error", function(done) {
var bothDone = latch(2, done);
server = net.createServer(function(socket) {
socket.once('data', function(protocolHeader) {
assert.deepStrictEqual(
protocolHeader,
Buffer.from("AMQP" + String.fromCharCode(0,0,9,1))
);
util.runServer(socket, function(send, wait) {
send(defs.ConnectionStart,
{versionMajor: 0,
versionMinor: 9,
serverProperties: {},
mechanisms: Buffer.from('PLAIN'),
locales: Buffer.from('en_US')});
wait(defs.ConnectionStartOk)().then(function() {
send(defs.ConnectionClose,
{replyCode: 403,
replyText: 'ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN',
classId: 0,
methodId: 0});
});
});
});

// Wait for the connection to be closed after the authentication error
socket.once('end', function() {
bothDone();
});
}).listen(0);

connect('amqp://localhost:' + server.address().port, {}, function(err) {
if (!err) bothDone(new Error('Expected authentication error'));
bothDone();
});
});
});

0 comments on commit 940ca2f

Please sign in to comment.