@@ -10,16 +10,22 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
10
10
}
11
11
12
12
let error ;
13
- const getProtocol = ( url : string ) => {
13
+ const getUrlParams = ( url : string ) => {
14
+ let protocol : string | undefined ;
15
+ let isLocal = false ;
16
+
14
17
try {
15
18
const urlObj = new URL ( url ) ;
16
- return urlObj . protocol ;
17
- }
18
- catch ( err ) {
19
- return undefined ;
19
+
20
+ protocol = urlObj . protocol ;
21
+ // Basic check against localhost; 127.0.0.1/8 and IPv6 localhost [::1]
22
+ isLocal = / ^ ( l o c a l h o s t | \[ : : 1 \] | 1 2 7 ( .[ 0 - 9 ] { 1 , 3 } ) { 3 } ) / i. test ( urlObj . hostname ) ;
23
+ } catch ( err ) {
20
24
}
25
+
26
+ return { protocol, isLocal } ;
21
27
} ;
22
- const protocol = getProtocol ( url ) ;
28
+ const { protocol, isLocal } = getUrlParams ( url ) ;
23
29
const isSsl = window . location . protocol === "https:" ;
24
30
25
31
if ( ! protocol ) {
@@ -40,7 +46,8 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
40
46
else if (
41
47
protocol &&
42
48
protocol === "http:" &&
43
- window . location . protocol === "https:"
49
+ window . location . protocol === "https:" &&
50
+ ! isLocal
44
51
) {
45
52
error = (
46
53
< SmallError >
@@ -76,10 +83,10 @@ type FieldUrlState = {
76
83
77
84
class FieldUrlInternal extends React . Component < FieldUrlInternalProps , FieldUrlState > {
78
85
static defaultProps = {
79
- onInput : ( ) => { } ,
86
+ onInput : ( ) => { } ,
80
87
}
81
88
82
- constructor ( props : FieldUrlInternalProps ) {
89
+ constructor ( props : FieldUrlInternalProps ) {
83
90
super ( props ) ;
84
91
this . state = {
85
92
error : validate ( props . value , props . t ) ,
@@ -100,7 +107,7 @@ class FieldUrlInternal extends React.Component<FieldUrlInternalProps, FieldUrlSt
100
107
this . props . onChange ( url ) ;
101
108
}
102
109
103
- render ( ) {
110
+ render ( ) {
104
111
return (
105
112
< div >
106
113
< InputString
0 commit comments