1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.http.parser;
import java.nio.charset.StandardCharsets;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.http.CookiesWithoutEquals;
import org.apache.tomcat.util.http.ServerCookie;
import org.apache.tomcat.util.http.ServerCookies;
import org.apache.tomcat.util.log.UserDataHelper;
import org.apache.tomcat.util.res.StringManager;
/**
* <p>
* Cookie header parser based on RFC6265
* </p>
* <p>
* The parsing of cookies using RFC6265 is more relaxed that the specification in the following ways:
* </p>
* <ul>
* <li>Values 0x80 to 0xFF are permitted in cookie-octet to support the use of UTF-8 in cookie values as used by HTML
* 5.</li>
* <li>For cookies without a value, the '=' is not required after the name as some browsers do not sent it.</li>
* </ul>
* <p>
* Implementation note:<br>
* This class has been carefully tuned. Before committing any changes, ensure that the TesterCookiePerformance unit test
* continues to give results within 1% for the old and new parsers.
* </p>
*/
public class Cookie {
private static final Log log = LogFactory.getLog(Cookie.class);
private static final UserDataHelper invalidCookieLog = new UserDataHelper(log);
private static final StringManager sm = StringManager.getManager("org.apache.tomcat.util.http.parser");
private static final boolean[] isCookieOctet = new boolean[256];
private static final boolean[] isText = new boolean[256];
private static final byte[] EMPTY_BYTES = new byte[0];
private static final byte TAB_BYTE = (byte) 0x09;
private static final byte SPACE_BYTE = (byte) 0x20;
private static final byte QUOTE_BYTE = (byte) 0x22;
private static final byte COMMA_BYTE = (byte) 0x2C;
private static final byte SEMICOLON_BYTE = (byte) 0x3B;
private static final byte EQUALS_BYTE = (byte) 0x3D;
private static final byte SLASH_BYTE = (byte) 0x5C;
private static final byte DEL_BYTE = (byte) 0x7F;
static {
// %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E (RFC6265)
// %x80 to %xFF (UTF-8)
for (int i = 0; i < 256; i++) {
isCookieOctet[i] = !(i < 0x21 || i == QUOTE_BYTE || i == COMMA_BYTE ||
i == SEMICOLON_BYTE || i == SLASH_BYTE || i == DEL_BYTE);
}
for (int i = 0; i < 256; i++) {
isText[i] = !(i < TAB_BYTE || (i > TAB_BYTE && i < SPACE_BYTE) || i == DEL_BYTE);
}
}
private Cookie() {
// Hide default constructor
}
/**
* Parse byte array as cookie header.
*
* @param bytes Source
* @param offset Start point in array
* @param len Number of bytes to read
* @param serverCookies Structure to store results
*
* @deprecated Unused. This method will be removed in Tomcat 11 onwards.
*/
@Deprecated
public static void parseCookie(byte[] bytes, int offset, int len, ServerCookies serverCookies) {
parseCookie(bytes, offset, len, serverCookies, CookiesWithoutEquals.NAME);
}
/**
* Parse byte array as cookie header.
*
* @param bytes Source
* @param offset Start point in array
* @param len Number of bytes to read
* @param serverCookies Structure to store results
* @param cookiesWithoutEquals How to handle a cookie name-value-pair that does not contain an equals character
*/
public static void parseCookie(byte[] bytes, int offset, int len, ServerCookies serverCookies,
CookiesWithoutEquals cookiesWithoutEquals) {
// ByteBuffer is used throughout this parser as it allows the byte[]
// and position information to be easily passed between parsing methods
ByteBuffer bb = new ByteBuffer(bytes, offset, len);
boolean moreToProcess = true;
while (moreToProcess) {
skipLWS(bb);
int start = bb.position();
ByteBuffer name = readToken(bb);
ByteBuffer value = null;
skipLWS(bb);
SkipResult skipResult = skipByte(bb, EQUALS_BYTE);
if (skipResult == SkipResult.FOUND) {
skipLWS(bb);
value = readCookieValueRfc6265(bb);
if (value == null) {
// Invalid cookie value. Skip to the next semicolon
skipUntilSemiColon(bb);
logInvalidHeader(start, bb);
continue;
}
skipLWS(bb);
}
skipResult = skipByte(bb, SEMICOLON_BYTE);
if (skipResult == SkipResult.FOUND) {
// NO-OP
} else if (skipResult == SkipResult.NOT_FOUND) {
// Invalid cookie. Ignore it and skip to the next semicolon
skipUntilSemiColon(bb);
logInvalidHeader(start, bb);
continue;
} else {
// SkipResult.EOF
moreToProcess = false;
}
if (name.hasRemaining()) {
if (value == null) {
switch (cookiesWithoutEquals) {
case IGNORE: {
// This name-value-pair is a NO-OP
break;
}
case NAME: {
ServerCookie sc = serverCookies.addCookie();
sc.getName().setBytes(name.array(), name.position(), name.remaining());
sc.getValue().setBytes(EMPTY_BYTES, 0, EMPTY_BYTES.length);
break;
}
}
} else {
ServerCookie sc = serverCookies.addCookie();
sc.getName().setBytes(name.array(), name.position(), name.remaining());
sc.getValue().setBytes(value.array(), value.position(), value.remaining());
}
}
}
}
private static void skipLWS(ByteBuffer bb) {
while (bb.hasRemaining()) {
byte b = bb.get();
if (b != TAB_BYTE && b != SPACE_BYTE) {
bb.rewind();
break;
}
}
}
private static void skipUntilSemiColon(ByteBuffer bb) {
while (bb.hasRemaining()) {
if (bb.get() == SEMICOLON_BYTE) {
break;
}
}
}
private static SkipResult skipByte(ByteBuffer bb, byte target) {
if (!bb.hasRemaining()) {
return SkipResult.EOF;
}
if (bb.get() == target) {
return SkipResult.FOUND;
}
bb.rewind();
return SkipResult.NOT_FOUND;
}
/**
* Similar to readCookieValue() but treats a comma as part of an invalid value.
*/
private static ByteBuffer readCookieValueRfc6265(ByteBuffer bb) {
boolean quoted = false;
if (bb.hasRemaining()) {
if (bb.get() == QUOTE_BYTE) {
quoted = true;
} else {
bb.rewind();
}
}
int start = bb.position();
int end = bb.limit();
while (bb.hasRemaining()) {
byte b = bb.get();
if (isCookieOctet[(b & 0xFF)]) {
// NO-OP
} else if (b == SEMICOLON_BYTE || b == SPACE_BYTE || b == TAB_BYTE) {
end = bb.position() - 1;
bb.position(end);
break;
} else if (quoted && b == QUOTE_BYTE) {
end = bb.position() - 1;
break;
} else {
// Invalid cookie
return null;
}
}
return new ByteBuffer(bb.bytes, start, end - start);
}
private static ByteBuffer readToken(ByteBuffer bb) {
final int start = bb.position();
int end = bb.limit();
while (bb.hasRemaining()) {
if (!HttpParser.isToken(bb.get())) {
end = bb.position() - 1;
bb.position(end);
break;
}
}
return new ByteBuffer(bb.bytes, start, end - start);
}
private static void logInvalidHeader(int start, ByteBuffer bb) {
UserDataHelper.Mode logMode = invalidCookieLog.getNextMode();
if (logMode != null) {
String headerValue = new String(bb.array(), start, bb.position() - start, StandardCharsets.UTF_8);
String message = sm.getString("cookie.invalidCookieValue", headerValue);
switch (logMode) {
case INFO_THEN_DEBUG:
message += sm.getString("cookie.fallToDebug");
//$FALL-THROUGH$
case INFO:
log.info(message);
break;
case DEBUG:
log.debug(message);
}
}
}
/**
* Custom implementation that skips many of the safety checks in {@link java.nio.ByteBuffer}.
*/
private static class ByteBuffer {
private final byte[] bytes;
private final int limit;
private int position;
ByteBuffer(byte[] bytes, int offset, int len) {
this.bytes = bytes;
this.position = offset;
this.limit = offset + len;
}
public int position() {
return position;
}
public void position(int position) {
this.position = position;
}
public int limit() {
return limit;
}
public int remaining() {
return limit - position;
}
public boolean hasRemaining() {
return position < limit;
}
public byte get() {
return bytes[position++];
}
public void rewind() {
position--;
}
public byte[] array() {
return bytes;
}
// For debug purposes
@Override
public String toString() {
return "position [" + position + "], limit [" + limit + "]";
}
}
}
|