From 493161fa213c85b59da05adac7bbff69efb6eefb Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Fri, 29 Sep 2023 16:06:49 +0100 Subject: [PATCH] [v10.1.x] Subpath: Add check for url being same as subpath on stripBaseFromUrl (#75755) Subpath: Add check for url being same as subpath on stripBaseFromUrl (#75670) * Subpath: Add check for url being same as subpath * Better comment (cherry picked from commit 59f0f9a93e3d49fc3303ae02156b4f96d9bb45cf) Co-authored-by: Joao Silva <100691367+JoaoSilvaGrafana@users.noreply.github.com> --- packages/grafana-data/src/utils/location.test.ts | 8 ++++++++ packages/grafana-data/src/utils/location.ts | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/grafana-data/src/utils/location.test.ts b/packages/grafana-data/src/utils/location.test.ts index 1b78b1b785f..897b4678cbb 100644 --- a/packages/grafana-data/src/utils/location.test.ts +++ b/packages/grafana-data/src/utils/location.test.ts @@ -54,6 +54,10 @@ describe('locationUtil', () => { const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl-backup/thisShouldRemain/'); expect(urlWithoutMaster).toBe('/subUrl-backup/thisShouldRemain/'); }); + test('relative url with same url', () => { + const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl'); + expect(urlWithoutMaster).toBe(''); + }); test('absolute url', () => { const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/subUrl/thisShouldRemain/'); expect(urlWithoutMaster).toBe('/thisShouldRemain/'); @@ -74,6 +78,10 @@ describe('locationUtil', () => { ); expect(urlWithoutMaster).toBe('http://www.domain.com:9877/subUrl-backup/thisShouldRemain/'); }); + test('absolute url with same url', () => { + const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/subUrl'); + expect(urlWithoutMaster).toBe(''); + }); }); describe('when appSubUrl not configured', () => { diff --git a/packages/grafana-data/src/utils/location.ts b/packages/grafana-data/src/utils/location.ts index 3b016f2094a..7fb3081ce3e 100644 --- a/packages/grafana-data/src/utils/location.ts +++ b/packages/grafana-data/src/utils/location.ts @@ -43,9 +43,10 @@ const stripBaseFromUrl = (urlOrPath: string): string => { segmentToStrip = `${window.location.origin}${appSubUrl}`; } - // Check if the segment is followed by a '/' so it does not replace incorrect similarly named segments + // Check if the segment is either exactly the same as the url + // or 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 + return urlOrPath.length > 0 && (urlOrPath.indexOf(segmentToStrip + '/') === 0 || urlOrPath === segmentToStrip) ? urlOrPath.slice(segmentToStrip.length - stripExtraChars) : urlOrPath; };