Skip to content

Unescaped $ sign in pattern prevent pattern matching in ServerRmq #15304

Closed
@getlarge

Description

@getlarge

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

  1. Install dependencies:

    npm install
  2. Start RabbitMQ:

    docker compose up -d
  3. In one terminal, start the microservice:

    npm run start
  4. In another terminal, run the client tests:

    npm run start:client

Expected behavior

The convertRoutingKeyToRegex 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions