Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import UrlPattern from 'url-pattern';
import { SECURITY_OPTIONS } from '../options/config';
const { TRUSTED_HOSTS } = SECURITY_OPTIONS.fetherNetwork;
// Github
const GITHUB_TRUSTED_HOSTS = TRUSTED_HOSTS.github;
// Blockscout - Only includes those that are available on the Blockscout website
const BLOCKSCOUT_TRUSTED_HOSTS = TRUSTED_HOSTS.blockscout;
const BLOCKSCOUT_CHAINS_SUPPORTED = ['eth', 'etc', 'poa'];
const BLOCKSCOUT_NETWORKS_SUPPORTED = [
'mainnet',
'classic',
'ropsten',
'kovan',
'goerli',
'core',
'dai',
'sokol',
'rinkeby'
];
const BLOCKSCOUT_HASH_KINDS = ['tx', 'address'];
const BLOCKSCOUT_HASH_TRAILERS = [
'coin_balances',
'internal_transactions',
'tokens',
'transactions'
];
const HASH_ADDRESS_LENGTH = 40;
const HASH_TX_LENGTH = 64;
// Url Patterns
const GENERAL_PATTERN = new UrlPattern(
'(https\\://)(:subdomain.):domain.:tld(\\::port)(/*)'
);
const GITHUB_PATTERN_1 = new UrlPattern(
'(https\\://)(:subdomain.):domain.:tld(/)(*)(.png|.jpg)'
);
const GITHUB_PATTERN_2 = new UrlPattern(
'(https\\://)(:subdomain.):domain.:tld(/)atomiclabs/cryptocurrency-icons/(*)(.png|.jpg)'
);
const GITHUB_PATTERN_3 = new UrlPattern(
'(https\\://)(:subdomain.):domain.:tld(/)ethcore/(*)(.png|.jpg)'
);
const BLOCKSCOUT_PATTERN = new UrlPattern(
'(https\\://)(:subdomain.):domain.:tld(/):chain(/):network(/):hashKind(/0x):hash(/:hashTrailer)'
);
function isValidGithubUrl (url) {
const match =
GITHUB_PATTERN_1.match(url) ||
GITHUB_PATTERN_2.match(url) ||
GITHUB_PATTERN_3.match(url);
if (!match) {
return false;
}
return true;
}
function isValidBlockscoutUrl (url) {
const match = BLOCKSCOUT_PATTERN.match(url);
if (!match) {
return false;
}
if (
BLOCKSCOUT_CHAINS_SUPPORTED.includes(match.chain) &&
BLOCKSCOUT_NETWORKS_SUPPORTED.includes(match.network) &&
BLOCKSCOUT_HASH_KINDS.includes(match.hashKind) &&
[HASH_ADDRESS_LENGTH, HASH_TX_LENGTH].includes(match.hash.length) &&
BLOCKSCOUT_HASH_TRAILERS.includes(match.hashTrailer)
) {
return true;
}
return false;
}
/**
* List of trusted subdomains, domain extensions, and ports, as relevant.
* Detect trusted prefix and then call method to validate it.
*/
function isValidUrl (url) {
const match = GENERAL_PATTERN.match(url);
if (!match) {
return false;
}
if (!url.startsWith('https')) {
return false;
}
// Blockscout URL
if (
BLOCKSCOUT_TRUSTED_HOSTS.includes(
(match.subdomain && match.subdomain) +
(match.subdomain && '.') +
(match.domain && match.domain) +
(match.domain && '.') +
(match.tld && match.tld)
) ||
BLOCKSCOUT_TRUSTED_HOSTS.includes(
(match.domain && match.domain) +
(match.domain && '.') +
(match.tld && match.tld)
)
) {
return isValidBlockscoutUrl(url);
}
// Github URL
if (
GITHUB_TRUSTED_HOSTS.includes(
(match.subdomain && match.subdomain) +
(match.subdomain && '.') +
(match.domain && match.domain) +
(match.domain && '.') +
(match.tld && match.tld)
) ||
GITHUB_TRUSTED_HOSTS.includes(
(match.domain && match.domain) +
(match.domain && '.') +
(match.tld && match.tld)
)
) {
return isValidGithubUrl(url);
}
}
export default isValidUrl;