Skip to content

Bug in rewrite prefix when having query params and url params in the URL #322

@noa-frontegg

Description

@noa-frontegg

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.0-rc.2

Plugin version

9.2.0

Node.js version

18

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.4.1

Description

When registering routes that includes path parameter in the route, and configuring rewrite prefix, when sending the request with query parameters, there is a bug and the rewrite Prefix is not working.
For example:
registering path: /pref/types/:type/example
with rewritePrefix of: /types/:type/example
The server sends the request with the /pref prefix that should have been removed, when calling this URL with qs, for example:
when calling with: http://localhost:8080/pref/types/sometype/example?data=123
The server proxy the request to the target server with the full URL, instead of sending it without the prefix.
After debugging, the bug is in the line: (index.js line 330)
dest = dest.replace(prefixPathWithVariables, rewritePrefixWithVariables)
When the value of "prefixPathWithVariables" includes the querystring, but it should not include it (the replace doesn't work)

Steps to Reproduce

register route with path parameter (taken from the example):

// /rest-api/123/endpoint will be proxied to http://my-rest-api.example.com/123/endpoint
server.register(proxy, {
  upstream: 'http://my-rest-api.example.com',
  prefix: '/rest-api/:id/endpoint', // optional
  rewritePrefix: '/:id/endpoint', // optional
  http2: false, // optional
});

Call the API with qs:
http://localhost:9090/rest-api/123/endpoint?data=1234

Expected Behavior

The proxy should call: http://my-rest-api.example.com/123/endpoint?data=1234
But it calls: http://my-rest-api.example.com/rest-api/123/endpoint?data=1234

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions