From 875809a59baec3f3de4a70193e9c589445dfea57 Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Fri, 4 Jun 2021 21:01:15 -0600 Subject: [PATCH] Fix proxy redirect for base path without slash Signed-off-by: Tim Hansen --- .changeset/wild-wasps-turn.md | 5 +++++ plugins/proxy-backend/src/service/router.test.ts | 8 ++++---- plugins/proxy-backend/src/service/router.ts | 6 +++++- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .changeset/wild-wasps-turn.md diff --git a/.changeset/wild-wasps-turn.md b/.changeset/wild-wasps-turn.md new file mode 100644 index 0000000000..2bdb0d832d --- /dev/null +++ b/.changeset/wild-wasps-turn.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-proxy-backend': patch +--- + +Fixed proxy requests to the base URL of routes without a trailing slash redirecting to the `target` with the full path appended. diff --git a/plugins/proxy-backend/src/service/router.test.ts b/plugins/proxy-backend/src/service/router.test.ts index 571de96ac4..57b87754e2 100644 --- a/plugins/proxy-backend/src/service/router.test.ts +++ b/plugins/proxy-backend/src/service/router.test.ts @@ -74,7 +74,7 @@ describe('buildMiddleware', () => { expect(filter('', { method: 'PATCH', headers: {} })).toBe(true); expect(filter('', { method: 'DELETE', headers: {} })).toBe(true); - expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' }); + expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' }); expect(fullConfig.changeOrigin).toBe(true); expect(fullConfig.logProvider!(logger)).toBe(logger); }); @@ -94,7 +94,7 @@ describe('buildMiddleware', () => { expect(filter('', { method: 'PATCH', headers: {} })).toBe(true); expect(filter('', { method: 'DELETE', headers: {} })).toBe(true); - expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' }); + expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' }); expect(fullConfig.changeOrigin).toBe(true); expect(fullConfig.logProvider!(logger)).toBe(logger); }); @@ -114,7 +114,7 @@ describe('buildMiddleware', () => { expect(filter('', { method: 'PATCH', headers: {} })).toBe(true); expect(filter('', { method: 'DELETE', headers: {} })).toBe(true); - expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' }); + expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' }); expect(fullConfig.changeOrigin).toBe(true); expect(fullConfig.logProvider!(logger)).toBe(logger); }); @@ -137,7 +137,7 @@ describe('buildMiddleware', () => { expect(filter('', { method: 'PATCH', headers: {} })).toBe(false); expect(filter('', { method: 'DELETE', headers: {} })).toBe(true); - expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' }); + expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' }); expect(fullConfig.changeOrigin).toBe(true); expect(fullConfig.logProvider!(logger)).toBe(logger); }); diff --git a/plugins/proxy-backend/src/service/router.ts b/plugins/proxy-backend/src/service/router.ts index 71ed82c23b..c3b1d056db 100644 --- a/plugins/proxy-backend/src/service/router.ts +++ b/plugins/proxy-backend/src/service/router.ts @@ -95,8 +95,12 @@ export function buildMiddleware( routeWithSlash = routeWithSlash.substring(1); } + // The ? makes the slash optional for the rewrite, so that a base path without an ending slash + // will also be matched (e.g. '/sample' and then requesting just '/api/proxy/sample' without an + // ending slash). Otherwise the target gets called with the full '/api/proxy/sample' path + // appended. fullConfig.pathRewrite = { - [`^${pathPrefix}${routeWithSlash}`]: '/', + [`^${pathPrefix}${routeWithSlash}?`]: '/', }; }