diff -up java/org/apache/coyote/http11/AbstractHttp11Processor.java.orig java/org/apache/coyote/http11/AbstractHttp11Processor.java
--- java/org/apache/coyote/http11/AbstractHttp11Processor.java.orig 2017-03-09 08:51:40.000000000 -0500
+++ java/org/apache/coyote/http11/AbstractHttp11Processor.java 2019-03-05 14:58:20.285295932 -0500
@@ -48,6 +48,7 @@ import org.apache.tomcat.util.buf.HexUti
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.FastHttpDateFormat;
import org.apache.tomcat.util.http.MimeHeaders;
+import org.apache.tomcat.util.http.parser.HttpParser;
import org.apache.tomcat.util.log.UserDataHelper;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
@@ -262,6 +263,9 @@ public abstract class AbstractHttp11Proc
protected org.apache.coyote.http11.upgrade.UpgradeInbound upgradeInbound = null;
+ protected HttpParser httpParser;
+
+
/**
* Instance of the new protocol to use after the HTTP connection has been
* upgraded using the Servlet 3.1 based upgrade process.
@@ -1301,33 +1305,62 @@ public abstract class AbstractHttp11Proc
}
}
- // Check for a full URI (including protocol://host:port/)
+ // Check for an absolute-URI less the query string which has already
+ // been removed during the parsing of the request line
ByteChunk uriBC = request.requestURI().getByteChunk();
+ byte[] uriB = uriBC.getBytes();
if (uriBC.startsWithIgnoreCase("http", 0)) {
- int pos = uriBC.indexOf("://", 0, 3, 4);
- int uriBCStart = uriBC.getStart();
- int slashPos = -1;
- if (pos != -1) {
- byte[] uriB = uriBC.getBytes();
- slashPos = uriBC.indexOf('/', pos + 3);
+ int pos = 4;
+ // Check for https
+ if (uriBC.startsWithIgnoreCase("s", pos)) {
+ pos++;
+ }
+ // Next 3 characters must be "://"
+ if (uriBC.startsWith("://", pos)) {
+ int uriBCStart = uriBC.getStart();
+
+ // '/' does not appear in the authority so use the first
+ // instance to split the authority and the path segments
+ int slashPos = uriBC.indexOf('/', pos);
+ // '@' in the authority delimits the userinfo
+
if (slashPos == -1) {
slashPos = uriBC.getLength();
- // Set URI as "/"
- request.requestURI().setBytes
- (uriB, uriBCStart + pos + 1, 1);
+ // Set URI as "/". Use 6 as it will always be a '/'.
+ // 01234567
+ // http://
+ // https://
+ request.requestURI().setBytes(uriB, uriBCStart + 6, 1);
} else {
- request.requestURI().setBytes
- (uriB, uriBCStart + slashPos,
- uriBC.getLength() - slashPos);
+ request.requestURI().setBytes(uriB, uriBCStart + slashPos, uriBC.getLength() - slashPos);
}
MessageBytes hostMB = headers.setValue("host");
hostMB.setBytes(uriB, uriBCStart + pos + 3,
slashPos - pos - 3);
+ } else {
+ response.setStatus(400);
+ setErrorState(ErrorState.CLOSE_CLEAN, null);
+ if (getLog().isDebugEnabled()) {
+ getLog().debug(sm.getString("http11processor.request.invalidScheme"));
+ }
}
}
+ // Validate the characters in the URI. %nn decoding will be checked at
+ // the point of decoding.
+ for (int i = uriBC.getStart(); i < uriBC.getEnd(); i++) {
+ if (!httpParser.isAbsolutePathRelaxed(uriB[i])) {
+ response.setStatus(400);
+ setErrorState(ErrorState.CLOSE_CLEAN, null);
+ if (getLog().isDebugEnabled()) {
+ getLog().debug(sm.getString("http11processor.request.invalidUri"));
+ }
+ break;
+ }
+ }
+
// Input filter setup
InputFilter[] inputFilters = getInputBuffer().getFilters();
@@ -1364,8 +1397,7 @@ public abstract class AbstractHttp11Proc
headers.removeHeader("content-length");
request.setContentLength(-1);
} else {
- getInputBuffer().addActiveFilter
- (inputFilters[Constants.IDENTITY_FILTER]);
+ getInputBuffer().addActiveFilter(inputFilters[Constants.IDENTITY_FILTER]);
contentDelimitation = true;
}
}
@@ -1383,14 +1415,14 @@ public abstract class AbstractHttp11Proc
}
}
+ // Validate host name and extract port if present
parseHost(valueMB);
if (!contentDelimitation) {
// If there's no content length
// (broken HTTP/1.0 or HTTP/1.1), assume
// the client is not broken and didn't send a body
- getInputBuffer().addActiveFilter
- (inputFilters[Constants.VOID_FILTER]);
+ getInputBuffer().addActiveFilter(inputFilters[Constants.VOID_FILTER]);
contentDelimitation = true;
}
diff -up java/org/apache/coyote/http11/AbstractHttp11Protocol.java.orig java/org/apache/coyote/http11/AbstractHttp11Protocol.java
--- java/org/apache/coyote/http11/AbstractHttp11Protocol.java.orig 2019-03-05 12:14:08.096279991 -0500
+++ java/org/apache/coyote/http11/AbstractHttp11Protocol.java 2019-03-05 14:03:32.186921274 -0500
@@ -45,6 +45,23 @@ public abstract class AbstractHttp11Prot
// ------------------------------------------------ HTTP specific properties
// ------------------------------------------ managed in the ProtocolHandler
+ private String relaxedPathChars = null;
+ public String getRelaxedPathChars() {
+ return relaxedPathChars;
+ }
+ public void setRelaxedPathChars(String relaxedPathChars) {
+ this.relaxedPathChars = relaxedPathChars;
+ }
+
+
+ private String relaxedQueryChars = null;
+ public String getRelaxedQueryChars() {
+ return relaxedQueryChars;
+ }
+ public void setRelaxedQueryChars(String relaxedQueryChars) {
+ this.relaxedQueryChars = relaxedQueryChars;
+ }
+
private int socketBuffer = 9000;
public int getSocketBuffer() { return socketBuffer; }
public void setSocketBuffer(int socketBuffer) {
diff -up java/org/apache/coyote/http11/AbstractInputBuffer.java.orig java/org/apache/coyote/http11/AbstractInputBuffer.java
--- java/org/apache/coyote/http11/AbstractInputBuffer.java.orig 2017-03-09 08:51:40.000000000 -0500
+++ java/org/apache/coyote/http11/AbstractInputBuffer.java 2019-03-05 12:14:08.096279991 -0500
@@ -22,6 +22,7 @@ import org.apache.coyote.InputBuffer;
import org.apache.coyote.Request;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.http.MimeHeaders;
+import org.apache.tomcat.util.http.parser.HttpParser;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.SocketWrapper;
import org.apache.tomcat.util.res.StringManager;
@@ -108,6 +109,9 @@ public abstract class AbstractInputBuffe
protected int lastActiveFilter;
+ protected HttpParser httpParser;
+
+
// ------------------------------------------------------------- Properties
diff -up java/org/apache/coyote/http11/Http11AprProcessor.java.orig java/org/apache/coyote/http11/Http11AprProcessor.java
--- java/org/apache/coyote/http11/Http11AprProcessor.java.orig 2019-03-05 12:13:47.032344988 -0500
+++ java/org/apache/coyote/http11/Http11AprProcessor.java 2019-03-05 14:58:20.298295897 -0500
@@ -35,6 +35,7 @@ import org.apache.tomcat.jni.SSLSocket;
import org.apache.tomcat.jni.Sockaddr;
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.http.parser.HttpParser;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.AprEndpoint;
import org.apache.tomcat.util.net.SSLSupport;
@@ -61,11 +62,15 @@ public class Http11AprProcessor extends
public Http11AprProcessor(int headerBufferSize, AprEndpoint endpoint, int maxTrailerSize,
- Settrue
if the start matches
+ */
public boolean startsWithIgnoreCase(String s, int pos) {
byte[] b = buff;
int len = s.length();
diff -up java/org/apache/tomcat/util/http/parser/HttpParser.java.orig java/org/apache/tomcat/util/http/parser/HttpParser.java
--- java/org/apache/tomcat/util/http/parser/HttpParser.java.orig 2017-03-09 08:51:41.000000000 -0500
+++ java/org/apache/tomcat/util/http/parser/HttpParser.java 2019-03-05 14:58:20.291295916 -0500
@@ -67,9 +67,17 @@ public class HttpParser {
private static final boolean[] IS_SEPARATOR = new boolean[ARRAY_SIZE];
private static final boolean[] IS_TOKEN = new boolean[ARRAY_SIZE];
private static final boolean[] IS_HEX = new boolean[ARRAY_SIZE];
- private static final boolean[] IS_NOT_REQUEST_TARGET = new boolean[ARRAY_SIZE];
private static final boolean[] IS_HTTP_PROTOCOL = new boolean[ARRAY_SIZE];
+ private static final boolean[] IS_ALPHA = new boolean[ARRAY_SIZE];
+ private static final boolean[] IS_NUMERIC = new boolean[ARRAY_SIZE];
private static final boolean[] REQUEST_TARGET_ALLOW = new boolean[ARRAY_SIZE];
+ private static final boolean[] IS_UNRESERVED = new boolean[ARRAY_SIZE];
+ private static final boolean[] IS_SUBDELIM = new boolean[ARRAY_SIZE];
+ private static final boolean[] IS_USERINFO = new boolean[ARRAY_SIZE];
+ private static final boolean[] IS_RELAXABLE = new boolean[ARRAY_SIZE];
+
+ private static final HttpParser DEFAULT;
+
static {
// Digest field types.
@@ -91,19 +99,6 @@ public class HttpParser {
// RFC2617 says nc is 8LHEX. <">8LHEX<"> will also be accepted
fieldTypes.put("nc", FIELD_TYPE_LHEX);
- String prop = System.getProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow");
- if (prop != null) {
- for (int i = 0; i < prop.length(); i++) {
- char c = prop.charAt(i);
- if (c == '{' || c == '}' || c == '|') {
- REQUEST_TARGET_ALLOW[c] = true;
- } else {
- log.warn(sm.getString("httpparser.invalidRequestTargetCharacter",
- Character.valueOf(c)));
- }
- }
- }
-
for (int i = 0; i < ARRAY_SIZE; i++) {
// Control> 0-31, 127
if (i < 32 || i == 127) {
@@ -128,6 +123,67 @@ public class HttpParser {
IS_HEX[i] = true;
}
+ // Not valid for HTTP protocol
+ // "HTTP/" DIGIT "." DIGIT
+ if (i == 'H' || i == 'T' || i == 'P' || i == '/' || i == '.' || (i >= '0' && i <= '9')) {
+ IS_HTTP_PROTOCOL[i] = true;
+ }
+
+ if (i >= '0' && i <= '9') {
+ IS_NUMERIC[i] = true;
+ }
+
+ if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z') {
+ IS_ALPHA[i] = true;
+ }
+
+ if (IS_ALPHA[i] || IS_NUMERIC[i] || i == '-' || i == '.' || i == '_' || i == '~') {
+ IS_UNRESERVED[i] = true;
+ }
+
+ if (i == '!' || i == '$' || i == '&' || i == '\'' || i == '(' || i == ')' || i == '*' ||
+ i == '+' || i == ',' || i == ';' || i == '=') {
+ IS_SUBDELIM[i] = true;
+ }
+
+ // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
+ if (IS_UNRESERVED[i] || i == '%' || IS_SUBDELIM[i] || i == ':') {
+ IS_USERINFO[i] = true;
+ }
+
+ // The characters that are normally not permitted for which the
+ // restrictions may be relaxed when used in the path and/or query
+ // string
+ if (i == '\"' || i == '<' || i == '>' || i == '[' || i == '\\' || i == ']' ||
+ i == '^' || i == '`' || i == '{' || i == '|' || i == '}') {
+ IS_RELAXABLE[i] = true;
+ }
+ }
+
+ String prop = System.getProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow");
+ if (prop != null) {
+ for (int i = 0; i < prop.length(); i++) {
+ char c = prop.charAt(i);
+ if (c == '{' || c == '}' || c == '|') {
+ REQUEST_TARGET_ALLOW[c] = true;
+ } else {
+ log.warn(sm.getString("http.invalidRequestTargetCharacter",
+ Character.valueOf(c)));
+ }
+ }
+ }
+
+ DEFAULT = new HttpParser(null, null);
+ }
+
+
+ private final boolean[] IS_NOT_REQUEST_TARGET = new boolean[ARRAY_SIZE];
+ private final boolean[] IS_ABSOLUTEPATH_RELAXED = new boolean[ARRAY_SIZE];
+ private final boolean[] IS_QUERY_RELAXED = new boolean[ARRAY_SIZE];
+
+
+ public HttpParser(String relaxedPathChars, String relaxedQueryChars) {
+ for (int i = 0; i < ARRAY_SIZE; i++) {
// Not valid for request target.
// Combination of multiple rules from RFC7230 and RFC 3986. Must be
// ASCII, no controls plus a few additional characters excluded
@@ -139,12 +195,29 @@ public class HttpParser {
}
}
- // Not valid for HTTP protocol
- // "HTTP/" DIGIT "." DIGIT
- if (i == 'H' || i == 'T' || i == 'P' || i == '/' || i == '.' || (i >= '0' && i <= '9')) {
- IS_HTTP_PROTOCOL[i] = true;
+ /*
+ * absolute-path = 1*( "/" segment )
+ * segment = *pchar
+ * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
+ *
+ * Note pchar allows everything userinfo allows plus "@"
+ */
+ if (IS_USERINFO[i] || i == '@' || i == '/' || REQUEST_TARGET_ALLOW[i]) {
+ IS_ABSOLUTEPATH_RELAXED[i] = true;
+ }
+
+ /*
+ * query = *( pchar / "/" / "?" )
+ *
+ * Note query allows everything absolute-path allows plus "?"
+ */
+ if (IS_ABSOLUTEPATH_RELAXED[i] || i == '?' || REQUEST_TARGET_ALLOW[i]) {
+ IS_QUERY_RELAXED[i] = true;
}
}
+
+ relax(IS_ABSOLUTEPATH_RELAXED, relaxedPathChars);
+ relax(IS_QUERY_RELAXED, relaxedQueryChars);
}
/**
@@ -277,6 +350,39 @@ public class HttpParser {
}
+ public boolean isNotRequestTargetRelaxed(int c) {
+ // Fast for valid request target characters, slower for some incorrect
+ // ones
+ try {
+ return IS_NOT_REQUEST_TARGET[c];
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ return true;
+ }
+ }
+
+
+ public boolean isAbsolutePathRelaxed(int c) {
+ // Fast for valid user info characters, slower for some incorrect
+ // ones
+ try {
+ return IS_ABSOLUTEPATH_RELAXED[c];
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ return false;
+ }
+ }
+
+
+ public boolean isQueryRelaxed(int c) {
+ // Fast for valid user info characters, slower for some incorrect
+ // ones
+ try {
+ return IS_QUERY_RELAXED[c];
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ return false;
+ }
+ }
+
+
public static String unquote(String input) {
if (input == null || input.length() < 2) {
return input;
@@ -329,27 +435,53 @@ public class HttpParser {
public static boolean isNotRequestTarget(int c) {
- // Fast for valid request target characters, slower for some incorrect
+ return DEFAULT.isNotRequestTargetRelaxed(c);
+ }
+
+
+ public static boolean isHttpProtocol(int c) {
+ // Fast for valid HTTP protocol characters, slower for some incorrect
// ones
try {
- return IS_NOT_REQUEST_TARGET[c];
+ return IS_HTTP_PROTOCOL[c];
} catch (ArrayIndexOutOfBoundsException ex) {
- return true;
+ return false;
}
}
- public static boolean isHttpProtocol(int c) {
- // Fast for valid HTTP protocol characters, slower for some incorrect
+ public static boolean isUserInfo(int c) {
+ // Fast for valid user info characters, slower for some incorrect
// ones
try {
- return IS_HTTP_PROTOCOL[c];
+ return IS_USERINFO[c];
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ return false;
+ }
+ }
+
+
+ private static boolean isRelaxable(int c) {
+ // Fast for valid user info characters, slower for some incorrect
+ // ones
+ try {
+ return IS_RELAXABLE[c];
} catch (ArrayIndexOutOfBoundsException ex) {
return false;
}
}
+ public static boolean isAbsolutePath(int c) {
+ return DEFAULT.isAbsolutePathRelaxed(c);
+ }
+
+
+ public static boolean isQuery(int c) {
+ return DEFAULT.isQueryRelaxed(c);
+ }
+
+
// Skip any LWS and return the next char
private static int skipLws(StringReader input, boolean withReset)
throws IOException {
@@ -579,6 +711,18 @@ public class HttpParser {
}
}
+ private void relax(boolean[] flags, String relaxedChars) {
+ if (relaxedChars != null && relaxedChars.length() > 0) {
+ char[] chars = relaxedChars.toCharArray();
+ for (char c : chars) {
+ if (isRelaxable(c)) {
+ flags[c] = true;
+ IS_NOT_REQUEST_TARGET[c] = false;
+ }
+ }
+ }
+ }
+
private static enum SkipConstantResult {
FOUND,
NOT_FOUND,
diff -up conf/catalina.properties.orig conf/catalina.properties
--- conf/catalina.properties.orig 2019-03-05 12:13:51.934329862 -0500
+++ conf/catalina.properties 2019-03-05 12:14:08.094279997 -0500
@@ -132,6 +132,9 @@ tomcat.util.buf.StringCache.byte.enabled
#tomcat.util.buf.StringCache.trainThreshold=500000
#tomcat.util.buf.StringCache.cacheSize=5000
+# This system property is deprecated. Use the relaxedPathChars relaxedQueryChars
+# attributes of the Connector instead. These attributes permit a wider range of
+# characters to be configured as valid.
# Allow for changes to HTTP request validation
-# WARNING: Using this option will expose the server to CVE-2016-6816
+# WARNING: Using this option may expose the server to CVE-2016-6816
#tomcat.util.http.parser.HttpParser.requestTargetAllow=|
diff -up webapps/docs/changelog.xml.orig webapps/docs/changelog.xml
--- webapps/docs/changelog.xml.orig 2019-03-05 12:13:47.068344877 -0500
+++ webapps/docs/changelog.xml 2019-03-05 12:14:08.103279970 -0500
@@ -108,6 +108,12 @@
Improve handing of overflow in the UTF-8 decoder with supplementary
characters. (markt)
+
The HTTP/1.1
+ specification requires that certain characters are %nn encoded when
+ used in URI paths. Unfortunately, many user agents including all the major
+ browsers are not compliant with this specification and use these
+ characters in unencoded form. To prevent Tomcat rejecting such requests,
+ this attribute may be used to specify the additional characters to allow.
+ If not specified, no addtional characters will be allowed. The value may
+ be any combination of the following characters:
+ " < > [ \ ] ^ ` { | }
. Any other characters
+ present in the value will be ignored.
The HTTP/1.1
+ specification requires that certain characters are %nn encoded when
+ used in URI query strings. Unfortunately, many user agents including all
+ the major browsers are not compliant with this specification and use these
+ characters in unencoded form. To prevent Tomcat rejecting such requests,
+ this attribute may be used to specify the additional characters to allow.
+ If not specified, no addtional characters will be allowed. The value may
+ be any combination of the following characters:
+ " < > [ \ ] ^ ` { | }
. Any other characters
+ present in the value will be ignored.
The value is a regular expression (using This system property is deprecated. Use the
+ A string comprised of characters the server should allow even when they are not encoded.
These characters would normally result in a 400 status. The acceptable characters for this property are: WARNING: Use of this option will expose the server to CVE-2016-6816.
+ WARNING: Use of this option may expose the server to CVE-2016-6816.
If not specified, the default value of java.util.regex
)
matching the user-agent
header of HTTP clients for which
diff -up webapps/docs/config/systemprops.xml.orig webapps/docs/config/systemprops.xml
--- webapps/docs/config/systemprops.xml.orig 2019-03-05 12:14:08.104279967 -0500
+++ webapps/docs/config/systemprops.xml 2019-03-05 12:16:02.075928285 -0500
@@ -709,11 +709,15 @@
relaxedPathChars
and relaxedQueryChars
+ attributes of the Connector instead. These attributes permit a wider range
+ of characters to be configured as valid.|
, {
, and }
null
will be used.
OK
"));