Skip to content

Commit

Permalink
fix(defaultSubjects): throw error on invalid subjects
Browse files Browse the repository at this point in the history
Its possible (and perhaps very likely) for default subjects to be
specified using a variable which might be undefined, and therefore
coerced to a string value as such. As a result, the user will
likely be using an unexpected link unknowingly. Instead, we will
now throw an error on these invalid cases.
  • Loading branch information
mbroadst committed Aug 24, 2016
1 parent bf311ae commit 314b495
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/amqp_client.js
Expand Up @@ -176,6 +176,10 @@ AMQPClient.prototype.createSender = function(address, policyOverrides) {
}, policyOverrides, this.policy.senderLink);

if (!!address.subject && this.policy.defaultSubjects) {
if (address.subject === 'undefined' || address.subject === 'null') {
throw new errors.InvalidSubjectError(address.subject);
}

linkPolicy.defaultSubject = address.subject;
}

Expand Down Expand Up @@ -241,6 +245,10 @@ AMQPClient.prototype.createReceiver = function(address, policyOverrides) {
// if the policy supports the defaultSubjects feature, and a subject has been
// provided then automatically set up a filter to match on that subject.
if (this.policy.defaultSubjects && !!address.subject) {
if (address.subject === 'undefined' || address.subject === 'null') {
throw new errors.InvalidSubjectError(address.subject);
}

var filterSymbol = (address.subject.indexOf('*') || address.subject.indexOf('#')) ?
'apache.org:legacy-amqp-topic-binding:string' :
'apache.org:legacy-amqp-direct-binding:string';
Expand Down
13 changes: 13 additions & 0 deletions lib/errors.js
Expand Up @@ -176,3 +176,16 @@ errors.VersionError = function(msg) {
this.name = 'AmqpVersionError';
};
util.inherits(errors.VersionError, errors.BaseError);

/**
* Invalid subject specified for receiver or sender link creation.
*
* @param subject the subject specified
* @extends BaseError
* @constructor
*/
errors.InvalidSubjectError = function(subject) {
errors.BaseError.call(this, 'Invalid subject: ' + subject);
this.name = 'AmqpInvalidSubjectError';
};
util.inherits(errors.InvalidSubjectError, errors.BaseError);
24 changes: 20 additions & 4 deletions test/integration/qpid/client.test.js
@@ -1,13 +1,15 @@
'use strict';
var Promise = require('bluebird'),
AMQPClient = require('../../..').Client,
Policy = require('../../..').Policy,
amqp = require('../../..'),
AMQPClient = amqp.Client,
Policy = amqp.Policy,
Errors = amqp.Errors,
config = require('./config'),
expect = require('chai').expect;
chai = require('chai'),
expect = chai.expect;

var test = {};
describe('QPID', function() {

describe('Client', function() {
beforeEach(function() {
if (!!test.client) test.client = undefined;
Expand Down Expand Up @@ -246,5 +248,19 @@ describe('Client', function() {
});
});

it('should throw an error when `undefined` or `null` are used for default subjects', function() {
return test.client.connect(config.address)
.then(function() {
[ null, undefined ].forEach(function(value) {
expect(function() {
test.client.createReceiver(config.defaultLink + '/' + value);
}).to.throw(Errors.InvalidSubjectError);
expect(function() {
test.client.createSender(config.defaultLink + '/' + value);
}).to.throw(Errors.InvalidSubjectError);
});
});
});

});
});

0 comments on commit 314b495

Please sign in to comment.