Links: Fix links to other apps outside Grafana when under sub path (#36498)

This commit is contained in:
Torkel Ödegaard 2021-07-07 15:25:59 +02:00 committed by GitHub
parent 3e95c3826a
commit eed1f36613
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 17 deletions

View File

@ -57,7 +57,7 @@ describe('locationUtil', () => {
});
test('absolute url with subdirectory subUrl', () => {
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/thisShouldRemain/subUrl/');
expect(urlWithoutMaster).toBe('/thisShouldRemain/subUrl/');
expect(urlWithoutMaster).toBe('http://www.domain.com:9877/thisShouldRemain/subUrl/');
});
});

View File

@ -17,16 +17,10 @@ const stripBaseFromUrl = (url: string): string => {
const isAbsoluteUrl = url.startsWith('http');
let segmentToStrip = appSubUrl;
if (!url.startsWith('/')) {
if (!url.startsWith('/') || isAbsoluteUrl) {
segmentToStrip = `${window.location.origin}${appSubUrl}`;
}
if (isAbsoluteUrl) {
segmentToStrip = url.startsWith(`${window.location.origin}${appSubUrl}`)
? `${window.location.origin}${appSubUrl}`
: `${window.location.origin}`;
}
return url.length > 0 && url.indexOf(segmentToStrip) === 0 ? url.slice(segmentToStrip.length - stripExtraChars) : url;
};

View File

@ -18,20 +18,18 @@ export function interceptLinkClicks(e: MouseEvent) {
e.preventDefault();
href = locationUtil.stripBaseFromUrl(href);
// Ensure old angular urls with no starting '/' are handled the same as before
// Make sure external links are handled correctly
// That is they where seen as being absolute from app root
if (href[0] !== '/') {
try {
const external = new URL(href);
if (external.origin !== window.location.origin) {
window.location.href = external.toString();
return;
}
} catch (e) {
console.warn(e);
// if still contains protocol it's an absolute link to another domain or web application
if (href.indexOf('://')) {
window.location.href = href;
return;
} else {
href = `/${href}`;
}
href = `/${href}`;
}
locationService.push(href);
}