2018-12-02 01:46:11 -05:00
|
|
|
import * as urlUtils from '~/lib/utils/url_utility';
|
2018-06-07 09:47:09 -04:00
|
|
|
|
2019-06-04 18:26:46 -04:00
|
|
|
const setWindowLocation = value => {
|
|
|
|
Object.defineProperty(window, 'location', {
|
|
|
|
writable: true,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-07 09:47:09 -04:00
|
|
|
describe('URL utility', () => {
|
|
|
|
describe('webIDEUrl', () => {
|
2018-06-07 10:11:10 -04:00
|
|
|
afterEach(() => {
|
|
|
|
gon.relative_url_root = '';
|
|
|
|
});
|
|
|
|
|
2018-06-07 09:47:09 -04:00
|
|
|
describe('without relative_url_root', () => {
|
|
|
|
it('returns IDE path with route', () => {
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(urlUtils.webIDEUrl('/gitlab-org/gitlab-ce/merge_requests/1')).toBe(
|
2018-06-07 09:47:09 -04:00
|
|
|
'/-/ide/project/gitlab-org/gitlab-ce/merge_requests/1',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with relative_url_root', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
gon.relative_url_root = '/gitlab';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns IDE path with route', () => {
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(urlUtils.webIDEUrl('/gitlab/gitlab-org/gitlab-ce/merge_requests/1')).toBe(
|
2018-06-07 09:47:09 -04:00
|
|
|
'/gitlab/-/ide/project/gitlab-org/gitlab-ce/merge_requests/1',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-30 04:49:22 -05:00
|
|
|
|
|
|
|
describe('mergeUrlParams', () => {
|
|
|
|
it('adds w', () => {
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(urlUtils.mergeUrlParams({ w: 1 }, '#frag')).toBe('?w=1#frag');
|
|
|
|
expect(urlUtils.mergeUrlParams({ w: 1 }, '/path#frag')).toBe('/path?w=1#frag');
|
|
|
|
expect(urlUtils.mergeUrlParams({ w: 1 }, 'https://host/path')).toBe('https://host/path?w=1');
|
2018-11-27 08:16:24 -05:00
|
|
|
expect(urlUtils.mergeUrlParams({ w: 1 }, 'https://host/path#frag')).toBe(
|
|
|
|
'https://host/path?w=1#frag',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(urlUtils.mergeUrlParams({ w: 1 }, 'https://h/p?k1=v1#frag')).toBe(
|
|
|
|
'https://h/p?k1=v1&w=1#frag',
|
|
|
|
);
|
2018-11-30 04:49:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates w', () => {
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(urlUtils.mergeUrlParams({ w: 1 }, '?k1=v1&w=0#frag')).toBe('?k1=v1&w=1#frag');
|
2018-11-30 04:49:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds multiple params', () => {
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(urlUtils.mergeUrlParams({ a: 1, b: 2, c: 3 }, '#frag')).toBe('?a=1&b=2&c=3#frag');
|
2018-11-30 04:49:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds and updates encoded params', () => {
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(urlUtils.mergeUrlParams({ a: '&', q: '?' }, '?a=%23#frag')).toBe('?a=%26&q=%3F#frag');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeParams', () => {
|
|
|
|
describe('when url is passed', () => {
|
|
|
|
it('removes query param with encoded ampersand', () => {
|
|
|
|
const url = urlUtils.removeParams(['filter'], '/mail?filter=n%3Djoe%26l%3Dhome');
|
|
|
|
|
|
|
|
expect(url).toBe('/mail');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove param when url has no other params', () => {
|
|
|
|
const url = urlUtils.removeParams(['size'], '/feature/home?size=5');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/feature/home');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove param when url has other params', () => {
|
|
|
|
const url = urlUtils.removeParams(['size'], '/feature/home?q=1&size=5&f=html');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/feature/home?q=1&f=html');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove param and preserve fragment', () => {
|
|
|
|
const url = urlUtils.removeParams(['size'], '/feature/home?size=5#H2');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/feature/home#H2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove multiple params', () => {
|
|
|
|
const url = urlUtils.removeParams(['z', 'a'], '/home?z=11111&l=en_US&a=true#H2');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/home?l=en_US#H2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setUrlFragment', () => {
|
|
|
|
it('should set fragment when url has no fragment', () => {
|
|
|
|
const url = urlUtils.setUrlFragment('/home/feature', 'usage');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/home/feature#usage');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set fragment when url has existing fragment', () => {
|
|
|
|
const url = urlUtils.setUrlFragment('/home/feature#overview', 'usage');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/home/feature#usage');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set fragment when given fragment includes #', () => {
|
|
|
|
const url = urlUtils.setUrlFragment('/home/feature#overview', '#install');
|
2018-11-27 08:16:24 -05:00
|
|
|
|
2018-06-08 00:06:44 -04:00
|
|
|
expect(url).toBe('/home/feature#install');
|
2018-11-30 04:49:22 -05:00
|
|
|
});
|
|
|
|
});
|
2019-05-30 13:40:20 -04:00
|
|
|
|
|
|
|
describe('getBaseURL', () => {
|
|
|
|
beforeEach(() => {
|
2019-06-04 18:26:46 -04:00
|
|
|
setWindowLocation({
|
|
|
|
protocol: 'https:',
|
|
|
|
host: 'gitlab.com',
|
2019-05-30 13:40:20 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns correct base URL', () => {
|
|
|
|
expect(urlUtils.getBaseURL()).toBe('https://gitlab.com');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isAbsoluteOrRootRelative', () => {
|
|
|
|
const validUrls = ['https://gitlab.com/', 'http://gitlab.com/', '/users/sign_in'];
|
|
|
|
|
|
|
|
const invalidUrls = [' https://gitlab.com/', './file/path', 'notanurl', '<a></a>'];
|
|
|
|
|
|
|
|
it.each(validUrls)(`returns true for %s`, url => {
|
|
|
|
expect(urlUtils.isAbsoluteOrRootRelative(url)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(invalidUrls)(`returns false for %s`, url => {
|
|
|
|
expect(urlUtils.isAbsoluteOrRootRelative(url)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isSafeUrl', () => {
|
|
|
|
const absoluteUrls = [
|
|
|
|
'http://example.org',
|
|
|
|
'http://example.org:8080',
|
|
|
|
'https://example.org',
|
|
|
|
'https://example.org:8080',
|
|
|
|
'https://192.168.1.1',
|
|
|
|
];
|
|
|
|
|
|
|
|
const rootRelativeUrls = ['/relative/link'];
|
|
|
|
|
|
|
|
const relativeUrls = ['./relative/link', '../relative/link'];
|
|
|
|
|
|
|
|
const urlsWithoutHost = ['http://', 'https://', 'https:https:https:'];
|
|
|
|
|
|
|
|
/* eslint-disable no-script-url */
|
|
|
|
const nonHttpUrls = [
|
|
|
|
'javascript:',
|
|
|
|
'javascript:alert("XSS")',
|
|
|
|
'jav\tascript:alert("XSS");',
|
|
|
|
'  javascript:alert("XSS");',
|
|
|
|
'ftp://192.168.1.1',
|
|
|
|
'file:///',
|
|
|
|
'file:///etc/hosts',
|
|
|
|
];
|
|
|
|
/* eslint-enable no-script-url */
|
|
|
|
|
|
|
|
// javascript:alert('XSS')
|
|
|
|
const encodedJavaScriptUrls = [
|
|
|
|
'javascript:alert('XSS')',
|
|
|
|
'javascript:alert('XSS')',
|
|
|
|
'javascript:alert('XSS')',
|
|
|
|
'\\u006A\\u0061\\u0076\\u0061\\u0073\\u0063\\u0072\\u0069\\u0070\\u0074\\u003A\\u0061\\u006C\\u0065\\u0072\\u0074\\u0028\\u0027\\u0058\\u0053\\u0053\\u0027\\u0029',
|
|
|
|
];
|
|
|
|
|
|
|
|
const safeUrls = [...absoluteUrls, ...rootRelativeUrls];
|
|
|
|
const unsafeUrls = [
|
|
|
|
...relativeUrls,
|
|
|
|
...urlsWithoutHost,
|
|
|
|
...nonHttpUrls,
|
|
|
|
...encodedJavaScriptUrls,
|
|
|
|
];
|
|
|
|
|
|
|
|
describe('with URL constructor support', () => {
|
|
|
|
it.each(safeUrls)('returns true for %s', url => {
|
|
|
|
expect(urlUtils.isSafeURL(url)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(unsafeUrls)('returns false for %s', url => {
|
|
|
|
expect(urlUtils.isSafeURL(url)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-06-04 18:26:46 -04:00
|
|
|
|
|
|
|
describe('getWebSocketProtocol', () => {
|
|
|
|
it.each`
|
|
|
|
protocol | expectation
|
|
|
|
${'http:'} | ${'ws:'}
|
|
|
|
${'https:'} | ${'wss:'}
|
|
|
|
`('returns "$expectation" with "$protocol" protocol', ({ protocol, expectation }) => {
|
|
|
|
setWindowLocation({
|
|
|
|
protocol,
|
|
|
|
host: 'example.com',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(urlUtils.getWebSocketProtocol()).toEqual(expectation);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getWebSocketUrl', () => {
|
|
|
|
it('joins location host to path', () => {
|
|
|
|
setWindowLocation({
|
|
|
|
protocol: 'http:',
|
|
|
|
host: 'example.com',
|
|
|
|
});
|
|
|
|
|
|
|
|
const path = '/lorem/ipsum?a=bc';
|
|
|
|
|
|
|
|
expect(urlUtils.getWebSocketUrl(path)).toEqual('ws://example.com/lorem/ipsum?a=bc');
|
|
|
|
});
|
|
|
|
});
|
2018-06-07 09:47:09 -04:00
|
|
|
});
|