Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close connection to server on connect errors #647

Merged
merged 1 commit into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});