Skip to content

Commit

Permalink
Add a new initialisation parameter, envHttpHeaders, to the CGI Servle…
Browse files Browse the repository at this point in the history
…t to mitigate httpoxy (CVE-2016-5388) by default and to provide a mechanism that can be used to mitigate any future, similar issues.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc8.5.x/trunk@1756940 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Aug 19, 2016
1 parent fa70000 commit 1b91e91
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
11 changes: 10 additions & 1 deletion conf/web.xml
Expand Up @@ -334,6 +334,15 @@
<!-- executable Name of the executable used to run the -->
<!-- script. [perl] -->
<!-- -->
<!-- envHttpHeaders A regular expression used to select the HTTP -->
<!-- headers passed to the CGI process as -->
<!-- environment variables. Note that headers are -->
<!-- converted to upper case before matching and -->
<!-- that the entire header name must match the -->
<!-- pattern. -->
<!-- [ACCEPT[-0-9A-Z]*|CACHE-CONTROL|COOKIE|HOST| -->
<!-- IF-[-0-9A-Z]*|REFERER|USER-AGENT] -->
<!-- -->
<!-- parameterEncoding Name of parameter encoding to be used with -->
<!-- CGI servlet. -->
<!-- [System.getProperty("file.encoding","UTF-8")] -->
Expand All @@ -353,7 +362,7 @@
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
<load-on-startup>5</load-on-startup>
</servlet>
-->

Expand Down
23 changes: 17 additions & 6 deletions java/org/apache/catalina/servlets/CGIServlet.java
Expand Up @@ -35,6 +35,7 @@
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
Expand Down Expand Up @@ -265,6 +266,16 @@ public final class CGIServlet extends HttpServlet {
*/
private long stderrTimeout = 2000;

/**
* The regular expression used to select HTTP headers to be passed to the
* CGI process as environment variables. The name of the environment
* variable will be the name of the HTTP header converter to upper case,
* prefixed with <code>HTTP_</code> and with all <code>-</code> characters
* converted to <code>_</code>.
*/
private Pattern envHttpHeadersPattern = Pattern.compile(
"ACCEPT[-0-9A-Z]*|CACHE-CONTROL|COOKIE|HOST|IF-[-0-9A-Z]*|REFERER|USER-AGENT");

/** object used to ensure multiple threads don't try to expand same file */
private static final Object expandFileLock = new Object();

Expand Down Expand Up @@ -326,6 +337,10 @@ public void init(ServletConfig config) throws ServletException {
"stderrTimeout"));
}

if (getServletConfig().getInitParameter("envHttpHeaders") != null) {
envHttpHeadersPattern =
Pattern.compile(getServletConfig().getInitParameter("envHttpHeaders"));
}
}


Expand Down Expand Up @@ -963,12 +978,8 @@ protected boolean setCGIEnvironment(HttpServletRequest req) throws IOException {
//REMIND: rewrite multiple headers as if received as single
//REMIND: change character set
//REMIND: I forgot what the previous REMIND means
if ("AUTHORIZATION".equalsIgnoreCase(header) ||
"PROXY_AUTHORIZATION".equalsIgnoreCase(header)) {
//NOOP per CGI specification section 11.2
} else {
envp.put("HTTP_" + header.replace('-', '_'),
req.getHeader(header));
if (envHttpHeadersPattern.matcher(header).matches()) {
envp.put("HTTP_" + header.replace('-', '_'), req.getHeader(header));
}
}

Expand Down
6 changes: 6 additions & 0 deletions webapps/docs/cgi-howto.xml
Expand Up @@ -103,6 +103,12 @@ if your script is itself executable (e.g. an exe file). Default is
<li><strong>executable-arg-1</strong>, <strong>executable-arg-2</strong>,
and so on - additional arguments for the executable. These precede the
CGI script name. By default there are no additional arguments.</li>
<li><strong>envHttpHeaders</strong> - A regular expression used to select the
HTTP headers passed to the CGI process as environment variables. Note that
headers are converted to upper case before matching and that the entire header
name must match the pattern. Default is
<code>ACCEPT[-0-9A-Z]*|CACHE-CONTROL|COOKIE|HOST|IF-[-0-9A-Z]*|REFERER|USER-AGENT</code>
</li>
<li><strong>parameterEncoding</strong> - Name of the parameter encoding
to be used with the CGI servlet. Default is
<code>System.getProperty("file.encoding","UTF-8")</code>. That is the system
Expand Down
7 changes: 7 additions & 0 deletions webapps/docs/changelog.xml
Expand Up @@ -146,6 +146,13 @@
<code>StandardRoot</code> instance now invalidate the cache if caching
is enabled. (markt)
</fix>
<add>
Add a new initialisation parameter, <code>envHttpHeaders</code>, to
the CGI Servlet to mitigate <a href="https://httpoxy.org">httpoxy</a>
(<a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-5388"
>CVE-2016-5388</a>) by default and to provide a mechanism that can be
used to mitigate any future, similar issues. (markt)
</add>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit 1b91e91

Please sign in to comment.