Skip to content

Commit cb493f9

Browse files
TunnelProxyServlet: validate proxy configuration
1 parent a307a62 commit cb493f9

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

src/main/java/com/testingbot/tunnel/proxy/TunnelProxyServlet.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ protected void addProxyHeaders(HttpServletRequest clientRequest, Request proxyRe
103103
proxyRequest.header(HttpHeader.PROXY_AUTHORIZATION, proxyAuthHeaderValue);
104104
}
105105

106-
if (getServletContext().getAttribute("extra_headers") != null) {
107-
HashMap<String, String> headers = (HashMap<String, String>) getServletContext().getAttribute("extra_headers");
106+
Object extraHeadersAttr = getServletContext().getAttribute("extra_headers");
107+
if (extraHeadersAttr instanceof Map) {
108+
@SuppressWarnings("unchecked")
109+
Map<String, String> headers = (Map<String, String>) extraHeadersAttr;
108110
for (Map.Entry<String, String> entry : headers.entrySet()) {
109111
String key = entry.getKey();
110112
String value = entry.getValue();
@@ -120,18 +122,24 @@ protected HttpClient newHttpClient() {
120122

121123
final String proxy = getServletConfig().getInitParameter("proxy");
122124
if (proxy != null && !proxy.isEmpty()) {
123-
String[] splitted = proxy.split(":");
124-
ProxyConfiguration proxyConfig = client.getProxyConfiguration();
125-
HttpProxy httpProxy = new HttpProxy(splitted[0], Integer.parseInt(splitted[1]));
126-
proxyConfig.getProxies().add(httpProxy);
127-
128-
String proxyAuth = getServletConfig().getInitParameter("proxyAuth");
129-
if (proxyAuth != null && !proxyAuth.isEmpty()) {
130-
String[] credentials = proxyAuth.split(":");
131-
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.INFO, "Proxy auth {0} : {1}", new Object[]{credentials[0], credentials[1]});
132-
133-
String userPass = credentials[0] + ":" + credentials[1];
134-
proxyAuthHeaderValue = "Basic " + java.util.Base64.getEncoder().encodeToString(userPass.getBytes());
125+
String[] splitted = proxy.split(":", 2);
126+
if (splitted.length < 2) {
127+
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.WARNING, "Invalid proxy format, expected host:port");
128+
} else {
129+
ProxyConfiguration proxyConfig = client.getProxyConfiguration();
130+
HttpProxy httpProxy = new HttpProxy(splitted[0], Integer.parseInt(splitted[1]));
131+
proxyConfig.getProxies().add(httpProxy);
132+
133+
String proxyAuth = getServletConfig().getInitParameter("proxyAuth");
134+
if (proxyAuth != null && !proxyAuth.isEmpty()) {
135+
String[] credentials = proxyAuth.split(":", 2);
136+
if (credentials.length >= 2) {
137+
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.INFO, "Proxy authentication configured");
138+
139+
String userPass = credentials[0] + ":" + credentials[1];
140+
proxyAuthHeaderValue = "Basic " + java.util.Base64.getEncoder().encodeToString(userPass.getBytes(java.nio.charset.StandardCharsets.UTF_8));
141+
}
142+
}
135143
}
136144
}
137145

@@ -140,12 +148,16 @@ protected HttpClient newHttpClient() {
140148
final String[] basicAuth = basicAuthString.split(",");
141149
for (String authCredentials : basicAuth) {
142150
String[] credentials = authCredentials.split(":");
151+
if (credentials.length < 4) {
152+
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.WARNING, "Invalid basic auth format, expected host:port:user:password");
153+
continue;
154+
}
143155
auth = client.getAuthenticationStore();
144-
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.INFO, "Adding Basic Auth for {0} : {1} : {2}", new Object[]{credentials[0] + ":" + credentials[1], credentials[2], credentials[3]});
156+
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.INFO, "Adding Basic Auth for {0}:{1}", new Object[]{credentials[0], credentials[1]});
145157
try {
146158
auth.addAuthentication(new BasicAuthentication(new URI("http://" + credentials[0] + ":" + credentials[1]), Authentication.ANY_REALM, credentials[2], credentials[3]));
147159
} catch (URISyntaxException ex) {
148-
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.SEVERE, null, ex);
160+
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.SEVERE, "Invalid URI for basic auth", ex);
149161
}
150162
}
151163
}

0 commit comments

Comments
 (0)