mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Routing: Fix links to different port from being treated as internal links (#45192)
* Add some failing tests that trigger the bug * start at refactor of stripBaseFromUrl
This commit is contained in:
parent
beed3fd5de
commit
416da59c43
@ -3,7 +3,7 @@ import { locationUtil } from './location';
|
|||||||
describe('locationUtil', () => {
|
describe('locationUtil', () => {
|
||||||
const { location } = window;
|
const { location } = window;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeEach(() => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
delete window.location;
|
delete window.location;
|
||||||
|
|
||||||
@ -21,11 +21,12 @@ describe('locationUtil', () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterEach(() => {
|
||||||
window.location = location;
|
window.location = location;
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('strip base when appSubUrl configured', () => {
|
describe('stripBaseFromUrl', () => {
|
||||||
|
describe('when appSubUrl configured', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
locationUtil.initialize({
|
locationUtil.initialize({
|
||||||
config: { appSubUrl: '/subUrl' } as any,
|
config: { appSubUrl: '/subUrl' } as any,
|
||||||
@ -61,7 +62,7 @@ describe('locationUtil', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('strip base when appSubUrl not configured', () => {
|
describe('when appSubUrl not configured', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
locationUtil.initialize({
|
locationUtil.initialize({
|
||||||
config: {} as any,
|
config: {} as any,
|
||||||
@ -81,6 +82,39 @@ describe('locationUtil', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when origin does not have a port in it', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
window.location = {
|
||||||
|
...location,
|
||||||
|
hash: '#hash',
|
||||||
|
host: 'www.domain.com',
|
||||||
|
hostname: 'www.domain.com',
|
||||||
|
href: 'http://www.domain.com/path/b?search=a&b=c&d#hash',
|
||||||
|
origin: 'http://www.domain.com',
|
||||||
|
pathname: '/path/b',
|
||||||
|
port: '',
|
||||||
|
protocol: 'http:',
|
||||||
|
search: '?search=a&b=c&d',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
test('relative url', () => {
|
||||||
|
const urlWithoutMaster = locationUtil.stripBaseFromUrl('/subUrl/grafana/');
|
||||||
|
expect(urlWithoutMaster).toBe('/subUrl/grafana/');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('URL with same host, different port', () => {
|
||||||
|
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.domain.com:9877/subUrl/grafana/');
|
||||||
|
expect(urlWithoutMaster).toBe('http://www.domain.com:9877/subUrl/grafana/');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('URL of a completely different origin', () => {
|
||||||
|
const urlWithoutMaster = locationUtil.stripBaseFromUrl('http://www.another-domain.com/subUrl/grafana/');
|
||||||
|
expect(urlWithoutMaster).toBe('http://www.another-domain.com/subUrl/grafana/');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('updateSearchParams', () => {
|
describe('updateSearchParams', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
locationUtil.initialize({
|
locationUtil.initialize({
|
||||||
|
@ -7,22 +7,43 @@ let grafanaConfig: GrafanaConfig = { appSubUrl: '' } as any;
|
|||||||
let getTimeRangeUrlParams: () => RawTimeRange;
|
let getTimeRangeUrlParams: () => RawTimeRange;
|
||||||
let getVariablesUrlParams: (scopedVars?: ScopedVars) => UrlQueryMap;
|
let getVariablesUrlParams: (scopedVars?: ScopedVars) => UrlQueryMap;
|
||||||
|
|
||||||
|
const maybeParseUrl = (input: string): URL | undefined => {
|
||||||
|
try {
|
||||||
|
return new URL(input);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
const stripBaseFromUrl = (url: string): string => {
|
const stripBaseFromUrl = (urlOrPath: string): string => {
|
||||||
|
// Will only return a URL object if the input is actually a valid URL
|
||||||
|
const parsedUrl = maybeParseUrl(urlOrPath);
|
||||||
|
if (parsedUrl) {
|
||||||
|
// If the input is a URL, and for a different origin that we're on, just bail
|
||||||
|
// and return it. There's no need to strip anything from it
|
||||||
|
if (parsedUrl.origin !== window.location.origin) {
|
||||||
|
return urlOrPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const appSubUrl = grafanaConfig.appSubUrl ?? '';
|
const appSubUrl = grafanaConfig.appSubUrl ?? '';
|
||||||
const stripExtraChars = appSubUrl.endsWith('/') ? 1 : 0;
|
const stripExtraChars = appSubUrl.endsWith('/') ? 1 : 0;
|
||||||
const isAbsoluteUrl = url.startsWith('http');
|
const isAbsoluteUrl = urlOrPath.startsWith('http');
|
||||||
|
|
||||||
let segmentToStrip = appSubUrl;
|
let segmentToStrip = appSubUrl;
|
||||||
|
|
||||||
if (!url.startsWith('/') || isAbsoluteUrl) {
|
if (!urlOrPath.startsWith('/') || isAbsoluteUrl) {
|
||||||
segmentToStrip = `${window.location.origin}${appSubUrl}`;
|
segmentToStrip = `${window.location.origin}${appSubUrl}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return url.length > 0 && url.indexOf(segmentToStrip) === 0 ? url.slice(segmentToStrip.length - stripExtraChars) : url;
|
return urlOrPath.length > 0 && urlOrPath.indexOf(segmentToStrip) === 0
|
||||||
|
? urlOrPath.slice(segmentToStrip.length - stripExtraChars)
|
||||||
|
: urlOrPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user