trustedDomains.test.ts 3.4 KB
Newer Older
P
Pine Wu 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';

P
Pine Wu 已提交
8
import { isURLDomainTrusted } from 'vs/workbench/contrib/url/common/trustedDomainsValidator';
P
Pine Wu 已提交
9 10
import { URI } from 'vs/base/common/uri';

P
Pine Wu 已提交
11 12
function linkAllowedByRules(link: string, rules: string[]) {
	assert.ok(isURLDomainTrusted(URI.parse(link), rules), `Link\n${link}\n should be protected by rules\n${JSON.stringify(rules)}`);
P
Pine Wu 已提交
13
}
P
Pine Wu 已提交
14 15
function linkNotAllowedByRules(link: string, rules: string[]) {
	assert.ok(!isURLDomainTrusted(URI.parse(link), rules), `Link\n${link}\n should NOT be protected by rules\n${JSON.stringify(rules)}`);
P
Pine Wu 已提交
16
}
P
Pine Wu 已提交
17

P
Pine Wu 已提交
18
suite('Link protection domain matching', () => {
P
Pine Wu 已提交
19
	test('simple', () => {
P
Pine Wu 已提交
20
		linkNotAllowedByRules('https://x.org', []);
P
Pine Wu 已提交
21

P
Pine Wu 已提交
22 23
		linkAllowedByRules('https://x.org', ['https://x.org']);
		linkAllowedByRules('https://x.org/foo', ['https://x.org']);
P
Pine Wu 已提交
24

P
Pine Wu 已提交
25 26
		linkNotAllowedByRules('https://x.org', ['http://x.org']);
		linkNotAllowedByRules('http://x.org', ['https://x.org']);
P
Pine Wu 已提交
27

P
Pine Wu 已提交
28
		linkNotAllowedByRules('https://www.x.org', ['https://x.org']);
P
Pine Wu 已提交
29

P
Pine Wu 已提交
30
		linkAllowedByRules('https://www.x.org', ['https://www.x.org', 'https://y.org']);
P
Pine Wu 已提交
31 32 33
	});

	test('localhost', () => {
P
Pine Wu 已提交
34 35 36 37
		linkAllowedByRules('https://127.0.0.1', []);
		linkAllowedByRules('https://127.0.0.1:3000', []);
		linkAllowedByRules('https://localhost', []);
		linkAllowedByRules('https://localhost:3000', []);
P
Pine Wu 已提交
38 39 40
	});

	test('* star', () => {
P
Pine Wu 已提交
41 42 43 44 45 46 47
		linkAllowedByRules('https://a.x.org', ['https://*.x.org']);
		linkAllowedByRules('https://a.b.x.org', ['https://*.x.org']);
		linkAllowedByRules('https://a.x.org', ['https://a.x.*']);
		linkAllowedByRules('https://a.x.org', ['https://a.*.org']);
		linkAllowedByRules('https://a.x.org', ['https://*.*.org']);
		linkAllowedByRules('https://a.b.x.org', ['https://*.b.*.org']);
		linkAllowedByRules('https://a.a.b.x.org', ['https://*.b.*.org']);
P
Pine Wu 已提交
48
	});
P
Pine Wu 已提交
49 50

	test('no scheme', () => {
P
Pine Wu 已提交
51 52 53 54 55 56 57 58
		linkAllowedByRules('https://a.x.org', ['a.x.org']);
		linkAllowedByRules('https://a.x.org', ['*.x.org']);
		linkAllowedByRules('https://a.b.x.org', ['*.x.org']);
		linkAllowedByRules('https://x.org', ['*.x.org']);
	});

	test('sub paths', () => {
		linkAllowedByRules('https://x.org/foo', ['https://x.org/foo']);
P
Pine Wu 已提交
59 60 61 62 63
		linkAllowedByRules('https://x.org/foo/bar', ['https://x.org/foo']);

		linkAllowedByRules('https://x.org/foo', ['https://x.org/foo/']);
		linkAllowedByRules('https://x.org/foo/bar', ['https://x.org/foo/']);

P
Pine Wu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77
		linkAllowedByRules('https://x.org/foo', ['x.org/foo']);
		linkAllowedByRules('https://x.org/foo', ['*.org/foo']);

		linkNotAllowedByRules('https://x.org/bar', ['https://x.org/foo']);
		linkNotAllowedByRules('https://x.org/bar', ['x.org/foo']);
		linkNotAllowedByRules('https://x.org/bar', ['*.org/foo']);

		linkAllowedByRules('https://x.org/foo/bar', ['https://x.org/foo']);
		linkNotAllowedByRules('https://x.org/foo2', ['https://x.org/foo']);

		linkNotAllowedByRules('https://www.x.org/foo', ['https://x.org/foo']);

		linkNotAllowedByRules('https://a.x.org/bar', ['https://*.x.org/foo']);
		linkNotAllowedByRules('https://a.b.x.org/bar', ['https://*.x.org/foo']);
P
Pine Wu 已提交
78 79

		linkAllowedByRules('https://github.com', ['https://github.com/foo/bar', 'https://github.com']);
P
Pine Wu 已提交
80
	});
P
Pine Wu 已提交
81
});