Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-value key handling only for form body #13998

Merged
merged 4 commits into from Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,12 +17,13 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.http.HttpConstants;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.multipart.HttpPostBodyUtil.SeekAheadOptimize;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException;
Expand Down Expand Up @@ -471,7 +472,8 @@ private void parseBodyAttributesStandard() {
charset);
currentAttribute = factory.createAttribute(request, key);
firstpos = currentpos;
} else if (read == '&' || (isLastChunk && !undecodedChunk.isReadable())) { // special empty FIELD
} else if (read == '&' ||
(isLastChunk && !undecodedChunk.isReadable() && hasFormBody())) { // special empty FIELD
currentStatus = MultiPartStatus.DISPOSITION;
ampersandpos = read == '&' ? currentpos - 1 : currentpos;
String key = decodeAttribute(
Expand Down Expand Up @@ -597,7 +599,8 @@ private void parseBodyAttributes() {
charset);
currentAttribute = factory.createAttribute(request, key);
firstpos = currentpos;
} else if (read == '&' || (isLastChunk && !undecodedChunk.isReadable())) { // special empty FIELD
} else if (read == '&' ||
(isLastChunk && !undecodedChunk.isReadable() && hasFormBody())) { // special empty FIELD
currentStatus = MultiPartStatus.DISPOSITION;
ampersandpos = read == '&' ? currentpos - 1 : currentpos;
String key = decodeAttribute(
Expand Down Expand Up @@ -783,6 +786,18 @@ public void removeHttpDataFromClean(InterfaceHttpData data) {
factory.removeHttpDataFromClean(request, data);
}

/**
* Check if request has headers indicating that it contains form body
*/
private boolean hasFormBody() {
String contentHeaderValue = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
if (contentHeaderValue == null) {
return false;
}
return HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.contentEquals(contentHeaderValue)
|| HttpHeaderValues.MULTIPART_FORM_DATA.contentEquals(contentHeaderValue);
}

private static final class UrlEncodedDetector implements ByteProcessor {
@Override
public boolean process(byte value) throws Exception {
Expand Down
Expand Up @@ -27,6 +27,7 @@
import io.netty.util.CharsetUtil;
import org.junit.jupiter.api.Test;

import static io.netty.handler.codec.http.DefaultHttpHeadersFactory.headersFactory;
import static org.junit.jupiter.api.Assertions.*;

class HttpPostStandardRequestDecoderTest {
Expand All @@ -52,7 +53,8 @@ void testDecodeAttributes() {
void testDecodeSingleAttributeWithNoValue() {
String requestBody = "key1";

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload",
headersFactory().newHeaders().add("Content-Type", "application/x-www-form-urlencoded"));

HttpPostStandardRequestDecoder decoder = new HttpPostStandardRequestDecoder(httpDiskDataFactory(), request);
ByteBuf buf = Unpooled.wrappedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
Expand All @@ -68,7 +70,8 @@ void testDecodeSingleAttributeWithNoValue() {
void testDecodeSingleAttributeWithNoValueEmptyLast() {
String requestBody = "key1";

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload",
headersFactory().newHeaders().add("Content-Type", "application/x-www-form-urlencoded"));

HttpPostStandardRequestDecoder decoder = new HttpPostStandardRequestDecoder(httpDiskDataFactory(), request);
ByteBuf buf = Unpooled.wrappedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
Expand All @@ -86,7 +89,8 @@ void testDecodeSingleAttributeWithNoValueEmptyLast() {
void testDecodeEndAttributeWithNoValue() {
String requestBody = "key1=value1&key2";

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload",
headersFactory().newHeaders().add("Content-Type", "application/x-www-form-urlencoded"));

HttpPostStandardRequestDecoder decoder = new HttpPostStandardRequestDecoder(httpDiskDataFactory(), request);
ByteBuf buf = Unpooled.wrappedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
Expand All @@ -99,6 +103,37 @@ void testDecodeEndAttributeWithNoValue() {
decoder.destroy();
}

@Test
void testDecodeJsonAttributeAsEmpty() {
String requestBody = "{\"iAm\": \" a JSON!\"}";

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload",
headersFactory().newHeaders().add("Content-Type", "application/json"));

HttpPostStandardRequestDecoder decoder = new HttpPostStandardRequestDecoder(httpDiskDataFactory(), request);
ByteBuf buf = Unpooled.wrappedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
DefaultHttpContent httpContent = new DefaultLastHttpContent(buf);
decoder.offer(httpContent);

assertEquals(0, decoder.getBodyHttpDatas().size());
decoder.destroy();
}

@Test
void testDecodeJsonAttributeAsEmptyAndNoHeaders() {
String requestBody = "{\"iAm\": \" a JSON!\"}";

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");

HttpPostStandardRequestDecoder decoder = new HttpPostStandardRequestDecoder(httpDiskDataFactory(), request);
ByteBuf buf = Unpooled.wrappedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
DefaultHttpContent httpContent = new DefaultLastHttpContent(buf);
decoder.offer(httpContent);

assertEquals(0, decoder.getBodyHttpDatas().size());
decoder.destroy();
}

@Test
void testDecodeStartAttributeWithNoValue() {
String requestBody = "key1&key2=value2";
Expand All @@ -120,7 +155,8 @@ void testDecodeStartAttributeWithNoValue() {
void testDecodeMultipleAttributesWithNoValue() {
String requestBody = "key1&key2&key3";

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload",
headersFactory().newHeaders().add("Content-Type", "application/x-www-form-urlencoded"));

HttpPostStandardRequestDecoder decoder = new HttpPostStandardRequestDecoder(httpDiskDataFactory(), request);
ByteBuf buf = Unpooled.wrappedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
Expand Down