From 2acf4e46d7f96b34b48fbcdf547fc3dd106bbb62 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Thu, 28 Sep 2023 10:37:35 +0100 Subject: [PATCH] [v10.1.x] Subpath: On stripBaseFromUrl, check if the segmentToStrip is followed by a '/' otherwise dont replace it (#75617) --- packages/grafana-data/src/utils/location.test.ts | 10 ++++++++++ packages/grafana-data/src/utils/location.ts | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/grafana-data/src/utils/location.test.ts b/packages/grafana-data/src/utils/location.test.ts index 33717820fb1..1b78b1b785f 100644 --- a/packages/grafana-data/src/utils/location.test.ts +++ b/packages/grafana-data/src/utils/location.test.ts @@ -50,6 +50,10 @@ describe('locationUtil', () => { const urlWithoutMaster = locationUtil.stripBaseFromUrl('/thisShouldRemain/subUrl/'); expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/'); }); + test('relative url with similar suburl', () => { + const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl-backup/thisShouldRemain/'); + expect(urlWithoutMaster).toBe('/subUrl-backup/thisShouldRemain/'); + }); test('absolute url', () => { const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/subUrl/thisShouldRemain/'); expect(urlWithoutMaster).toBe('/thisShouldRemain/'); @@ -64,6 +68,12 @@ describe('locationUtil', () => { const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/thisShouldRemain/subUrl/'); expect(urlWithoutMaster).toBe('http://www.domain.com:9877/thisShouldRemain/subUrl/'); }); + test('absolute url with similar suburl', () => { + const urlWithoutMaster = locationUtil.stripBaseFromUrl( + 'http://www.domain.com:9877/subUrl-backup/thisShouldRemain/' + ); + expect(urlWithoutMaster).toBe('http://www.domain.com:9877/subUrl-backup/thisShouldRemain/'); + }); }); describe('when appSubUrl not configured', () => { diff --git a/packages/grafana-data/src/utils/location.ts b/packages/grafana-data/src/utils/location.ts index a0d70396f19..3b016f2094a 100644 --- a/packages/grafana-data/src/utils/location.ts +++ b/packages/grafana-data/src/utils/location.ts @@ -43,7 +43,9 @@ const stripBaseFromUrl = (urlOrPath: string): string => { segmentToStrip = `${window.location.origin}${appSubUrl}`; } - return urlOrPath.length > 0 && urlOrPath.indexOf(segmentToStrip) === 0 + // Check if the segment is followed by a '/' so it does not replace incorrect similarly named segments + // i.e. /grafana should not replace /grafanadashboards + return urlOrPath.length > 0 && urlOrPath.indexOf(segmentToStrip + '/') === 0 ? urlOrPath.slice(segmentToStrip.length - stripExtraChars) : urlOrPath; };