Description
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
When using RabbitMQ microservice with wildcards enabled and routing keys containing the $ character, pattern matching fails because the $ character is not escaped in the ServerRMQ.convertRoutingKeyToRegex
method. This causes the regex to interpret $ as an end-of-string anchor instead of a literal character (I believe)
For example, a routing key pattern like $internal.plugin.*.status
generates the regex /^$internal\.plugin\.[^.]+\.status$/
, which will never match the intended message pattern $internal.plugin.0.status
because the unescaped $ creates an invalid regex pattern (looking for start of string followed immediately by end of string).
Minimum reproduction code
https://github.com/getlarge/nestjs-rmq-unescaped-patterns
Steps to reproduce
-
Install dependencies:
npm install
-
Start RabbitMQ:
docker compose up -d
-
In one terminal, start the microservice:
npm run start
-
In another terminal, run the client tests:
npm run start:client
Expected behavior
The convertRoutingKeyToRege
x method should escape special regex characters including $ before creating the RegExp object. The routing key $internal.plugin.*.status
should be converted to the regex /^\$internal\.plugin\.[^.]+\.status$/
, which would correctly match messages with patterns like $internal.plugin.0.status
.
Package
- I don't know. Or some 3rd-party package
-
@nestjs/common
-
@nestjs/core
-
@nestjs/microservices
-
@nestjs/platform-express
-
@nestjs/platform-fastify
-
@nestjs/platform-socket.io
-
@nestjs/platform-ws
-
@nestjs/testing
-
@nestjs/websockets
- Other (see below)
Other package
No response
NestJS version
11.1.2
Packages versions
"@nestjs/axios": "4.0.0",
"@nestjs/bullmq": "11.0.2",
"@nestjs/cache-manager": "3.0.0",
"@nestjs/common": "11.1.2",
"@nestjs/config": "4.0.2",
"@nestjs/core": "11.1.2",
"@nestjs/devtools-integration": "0.2.0",
"@nestjs/event-emitter": "3.0.1",
"@nestjs/mapped-types": "2.1.0",
"@nestjs/microservices": "11.1.2",
"@nestjs/mongoose": "11.0.1",
"@nestjs/platform-fastify": "11.1.2",
"@nestjs/schedule": "5.0.1",
"@nestjs/swagger": "11.0.5",
"@nestjs/throttler": "6.4.0",
Node.js version
22.15.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
I'm willing to fix it. Probably by using the same logic as the MQTT server, example here