Repository: golevelup/nestjs

Issue:
Wrong URI validation not allowing to pass query params for amqplib
We have this RegEx validation for RabbitMQ URI here:
https://github.com/golevelup/nestjs/blob/a57a7508be863b326e26175f688df7dd40a67fdf/packages/rabbitmq/src/amqp/utils.ts#L40-L52

It should allow to pass query params like, for example, `frameMax`.

Hints from issue comments:

Solution patch:
diff --git a/packages/rabbitmq/src/amqp/utils.ts b/packages/rabbitmq/src/amqp/utils.ts
index 792608f..9738a01 100644
--- a/packages/rabbitmq/src/amqp/utils.ts
+++ b/packages/rabbitmq/src/amqp/utils.ts
@@ -28,9 +28,6 @@ export function matchesRoutingKey(
   return false;
 }
 
-const rabbitMQRegex =
-  /^amqps?:\/\/(([^:]+):([^@]+)@)?([^:/]+)(:[0-9]+)?(\/.*)?$/;
-
 /**
  * Validates a rabbitmq uri
  * @see https://www.rabbitmq.com/docs/uri-spec#the-amqps-uri-scheme
@@ -45,8 +42,9 @@ export const assertRabbitMqUri = (uri: string | string[]) => {
     return;
   }
 
-  const valid = rabbitMQRegex.test(uri);
-  if (!valid) {
-    throw new Error(`Invalid RabbitMQ connection uri, received: ${uri}`);
+  const rmqUri = new URL(uri);
+
+  if (!rmqUri.protocol.startsWith('amqp')) {
+    throw new Error('RabbitMQ URI protocol must start with amqp or amqps');
   }
 };



Test patch:
diff --git a/packages/rabbitmq/src/tests/rabbitmq.utils.spec.ts b/packages/rabbitmq/src/tests/rabbitmq.utils.spec.ts
index 3f6f3e3..c01e9af 100644
--- a/packages/rabbitmq/src/tests/rabbitmq.utils.spec.ts
+++ b/packages/rabbitmq/src/tests/rabbitmq.utils.spec.ts
@@ -72,8 +72,14 @@ describe(matchesRoutingKey.name, () => {
           'amqps://rabbitmq:rabbitmq@localhost:2345',
           'amqp://rabbitmq:rabbitmq@localhost:3456/',
           'amqps://rabbitmq:rabbitmq@localhost:4567/',
+          // with virtual host
           'amqps://rabbitmq:rabbitmq@localhost:4567/vhost',
           'amqps://rabbitmq:rabbitmq@localhost:4567/v/h(o&s*t_',
+          // With query parameters
+          'amqp://rabbitmq:rabbitmq@localhost:5672?vhost=%2F&frameMax=131072&heartbeat=60',
+          'amqps://rabbitmq:rabbitmq@localhost:5671/vhost?frameMax=131072&heartbeat=30',
+          'amqps://rabbitmq:rabbitmq@localhost:5671/%2F?heartbeat=10',
+          'amqps://user:pass@rabbit.example.com:5671/vhost?frameMax=65536&channelMax=2047',
         ]),
       ).not.toThrowError();
     });