-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handling of bad requests (#29249)
Today we have a few problems with how we handle bad requests: - handling requests with bad encoding - handling requests with invalid value for filter_path/pretty/human - handling requests with a garbage Content-Type header There are two problems: - in every case, we give an empty response to the client - in most cases, we leak the byte buffer backing the request! These problems are caused by a broader problem: poor handling preparing the request for handling, or the channel to write to when the response is ready. This commit addresses these issues by taking a unified approach to all of them that ensures that: - we respond to the client with the exception that blew us up - we do not leak the byte buffer backing the request
- Loading branch information
1 parent
13e19e7
commit 4ef3de4
Showing
11 changed files
with
398 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...s/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4BadRequestTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.http.netty4; | ||
|
||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.common.network.NetworkService; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.common.util.MockBigArrays; | ||
import org.elasticsearch.common.util.MockPageCacheRecycler; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.http.HttpServerTransport; | ||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; | ||
import org.elasticsearch.rest.BytesRestResponse; | ||
import org.elasticsearch.rest.RestChannel; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.threadpool.TestThreadPool; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
public class Netty4BadRequestTests extends ESTestCase { | ||
|
||
private NetworkService networkService; | ||
private MockBigArrays bigArrays; | ||
private ThreadPool threadPool; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
networkService = new NetworkService(Collections.emptyList()); | ||
bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService()); | ||
threadPool = new TestThreadPool("test"); | ||
} | ||
|
||
@After | ||
public void shutdown() throws Exception { | ||
terminate(threadPool); | ||
} | ||
|
||
public void testBadParameterEncoding() throws Exception { | ||
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() { | ||
@Override | ||
public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) { | ||
fail(); | ||
} | ||
|
||
@Override | ||
public void dispatchBadRequest(RestRequest request, RestChannel channel, ThreadContext threadContext, Throwable cause) { | ||
try { | ||
final Exception e = cause instanceof Exception ? (Exception) cause : new ElasticsearchException(cause); | ||
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, e)); | ||
} catch (final IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
}; | ||
|
||
try (HttpServerTransport httpServerTransport = | ||
new Netty4HttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher)) { | ||
httpServerTransport.start(); | ||
final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses()); | ||
|
||
try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) { | ||
final Collection<FullHttpResponse> responses = | ||
nettyHttpClient.get(transportAddress.address(), "/_cluster/settings?pretty=%"); | ||
assertThat(responses, hasSize(1)); | ||
assertThat(responses.iterator().next().status().code(), equalTo(400)); | ||
final Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses); | ||
assertThat(responseBodies, hasSize(1)); | ||
assertThat(responseBodies.iterator().next(), containsString("\"type\":\"bad_parameter_exception\"")); | ||
assertThat( | ||
responseBodies.iterator().next(), | ||
containsString( | ||
"\"reason\":\"java.lang.IllegalArgumentException: unterminated escape sequence at end of string: %\"")); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.