Skip to content

Commit

Permalink
feat: add validation for amq connection uri (golevelup#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfisk authored Oct 7, 2024
1 parent 788b0cd commit af3ac52
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
25 changes: 24 additions & 1 deletion packages/rabbitmq/src/amqp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function matchesRoutingKey(
routingKey: string,
pattern: string[] | string | undefined
pattern: string[] | string | undefined,
): boolean {
// An empty string is a valid pattern therefore
// we should only exclude null values and empty array
Expand All @@ -27,3 +27,26 @@ export function matchesRoutingKey(

return false;
}

const rabbitMQRegex =
/^amqp:\/\/(([^:]+):([^@]+)@)?([^:/]+)(:[0-9]+)?(\/[^\/]+)?$/;

/**
* Validates a rabbitmq uri
* @see https://www.rabbitmq.com/docs/uri-spec#the-amqps-uri-scheme
* @param uri
* @returns
*/
export const assertRabbitMqUri = (uri: string | string[]) => {
if (Array.isArray(uri)) {
for (const u of uri) {
assertRabbitMqUri(u);
}
return;
}

const valid = rabbitMQRegex.test(uri);
if (!valid) {
throw new Error(`Invalid RabbitMQ connection uri, received: ${uri}`);
}
};
4 changes: 4 additions & 0 deletions packages/rabbitmq/src/rabbitmq.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-cr
import { groupBy } from 'lodash';
import { AmqpConnection } from './amqp/connection';
import { AmqpConnectionManager } from './amqp/connectionManager';
import { assertRabbitMqUri } from './amqp/utils';
import {
RABBIT_CONFIG_TOKEN,
RABBIT_CONTEXT_TYPE_KEY,
Expand Down Expand Up @@ -89,6 +90,9 @@ export class RabbitMQModule
);
return undefined;
}

assertRabbitMqUri(config.uri);

const connection = new AmqpConnection(config);
this.connectionManager.addConnection(connection);
await connection.init();
Expand Down
29 changes: 28 additions & 1 deletion packages/rabbitmq/src/tests/rabbitmq.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { matchesRoutingKey } from '../amqp/utils';
import { assertRabbitMqUri, matchesRoutingKey } from '../amqp/utils';

describe(matchesRoutingKey.name, () => {
const userCreated = 'user.created';
Expand Down Expand Up @@ -58,4 +58,31 @@ describe(matchesRoutingKey.name, () => {
const result = matchesRoutingKey(routingKey, pattern);
expect(result).toBe(expectedResult);
});

describe(assertRabbitMqUri.name, () => {
it('should not throw with valid uris', () => {
expect(() =>
assertRabbitMqUri('amqp://rabbitmq:rabbitmq@localhost:4444'),
).not.toThrowError();

expect(() =>
assertRabbitMqUri([
'amqp://rabbitmq:rabbitmq@localhost:4444',
'amqp://rabbitmq:rabbitmq@localhost:1234',
]),
).not.toThrowError();
});

it('should throw when malformed uris are provided', () => {
expect(() =>
assertRabbitMqUri('xamqp://rabbitmq:rabbitmq@localhost:4444'),
).toThrowError();
expect(() =>
assertRabbitMqUri([
'amqp://rabbitmq:rabbitmq@localhost:hello',
'superbawl://rabbitmq:rabbitmq@localhost:4444',
]),
).toThrowError();
});
});
});

0 comments on commit af3ac52

Please sign in to comment.