@@ -71,6 +71,24 @@ const UpstreamProxyDropdown = styled(Select)`
7171 margin-right: 10px;
7272` ;
7373
74+ const HOST_PATTERN = '[A-Za-z0-9\\-.]+' ;
75+ const PORT_PATTERN = '\\d+' ;
76+ // Disallow "@", "/" and ":" in username/password
77+ const CRED_PATTERN = '[^@/:]+' ;
78+
79+ const PROXY_HOST_REGEXES = [
80+ // 0) host or host:port
81+ new RegExp ( `^(${ HOST_PATTERN } )(:${ PORT_PATTERN } )?$` ) ,
82+ // 1) user:pass@host:port
83+ new RegExp ( `^(${ CRED_PATTERN } ):(${ CRED_PATTERN } )@(${ HOST_PATTERN } ):(${ PORT_PATTERN } )$` ) ,
84+ // 2) host:port@user:pass
85+ new RegExp ( `^(${ HOST_PATTERN } ):(${ PORT_PATTERN } )@(${ CRED_PATTERN } ):(${ CRED_PATTERN } )$` ) ,
86+ // 3) host:port:user:pass
87+ new RegExp ( `^(${ HOST_PATTERN } ):(${ PORT_PATTERN } ):(${ CRED_PATTERN } ):(${ CRED_PATTERN } )$` ) ,
88+ // 4) user:pass:host:port
89+ new RegExp ( `^(${ CRED_PATTERN } ):(${ CRED_PATTERN } ):(${ HOST_PATTERN } ):(${ PORT_PATTERN } )$` ) ,
90+ ] ;
91+
7492const isValidClientCertHost = ( input : string ) : boolean =>
7593 isValidHost ( input ) || input === '*' ;
7694
@@ -88,6 +106,25 @@ const validateProxyHost = inputValidation(isValidProxyHost,
88106 "Should be a plain hostname, optionally with a specific port and/or username:password"
89107) ;
90108
109+ // Translates the proxy host input into standard form if needed
110+ const normalizeProxyHost = ( host : string ) : string => {
111+ const idx = PROXY_HOST_REGEXES . findIndex ( regex => regex . test ( host ) ) ;
112+ if ( idx === - 1 ) return host ; // Should not occur
113+
114+ const groups = host . match ( PROXY_HOST_REGEXES [ idx ] ) ! ;
115+
116+ switch ( idx ) {
117+ case 2 : // host:port@user :pass
118+ return `${ groups [ 3 ] } :${ groups [ 4 ] } @${ groups [ 1 ] } :${ groups [ 2 ] } ` ;
119+ case 3 : // host:port:user:pass
120+ return `${ groups [ 3 ] } :${ groups [ 4 ] } @${ groups [ 1 ] } :${ groups [ 2 ] } ` ;
121+ case 4 : // user:pass:host:port
122+ return `${ groups [ 1 ] } :${ groups [ 2 ] } @${ groups [ 3 ] } :${ groups [ 4 ] } ` ;
123+ default : // Does not include auth or already in standard format
124+ return host ;
125+ }
126+ } ;
127+
91128@observer
92129class UpstreamProxyConfig extends React . Component < { rulesStore : RulesStore } > {
93130
0 commit comments