-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a different config setting for inbound and outbound HTTP1 head…
…er (#7362) * Create a different config setting for inbound and outbound HTTP1 header validation for both client and server Description =========== Currently, Helidon 4 only support validate-headers config to validate headers for both inbound and outbound headers on both client and server. This change will bring separation on the configuration for both inbound and outbound headers on both client and server. The change will also remove performance impact for a singular config that would validate both outbound and inbound headers by default. By separating the configuration, only necessary headers will be validated by default. For example, the request headers on the client and the response headers on the server will not be validated by default because the user has control on their creation. On the other hand, the response headers on the client and the request headers on the server will be validated by default because the values they contain are unpredictable upon receipt and may contain malicious data. Following are new config options: 1. Client a. validate-response-header (inbound) - Validates the client response headers and will default to true because the content is unpredictable. b. validate-request-header (outbound) - Validates the client request headers and will default to false because the user has control on header creation. 2. Server b. validate-request-header (inbound) - Validates the server request headers and will default to true because the content is unpredictable.. a. validate-response-header (outbound) - Validates the client request headers and will default to false because the user has control on header creation. Details of the change: ===================== 1. Webserver a. Http1ConfigBlueprint - replace validateHeaders() with validateRequestHeaders() and validateResponseHeaders() to allow separate validation control of request headers and response headers, respectively. b. Http1Connection and HttpServerResponse - will process validateRequestHeaders() and validateResponseHeaders() on their corresponding areas of validation. 2. Webclient a. Http1ClientProtocolConfigBlueprint = replace validateHeaders() with validateRequestHeaders() and validateResponseHeaders() to allow separate validation control of request headers and response headers, respectively. b. Http1CallChainBase, Http1CallOutputStreamChain and Http1CallEntityChain - will process validateRequestHeaders() and validateResponseHeaders() on their corresponding areas of validation. c. Http1ClientTest - Header validation related Unit test are adjusted to use validateRequestHeaders() and validateResponseHeaders() instead of their predecessor validateHeaders() 3. Integration tests for Webserver - all files changed will perform various scenarios to validate request or response headers on the client side. 4. Integration tests for Webclient - all files changed will perform various scenarios to validate request or response headers on the client side. Documentation ============= This is a configuration change so documentation will be collected from javadoc comments in Http1ConfigBlueprint.java for the Webserver and Http1ClientProtocolConfigBlueprint.java for Http1 Webclient. Examples: 1. Http1 WebServer ``` ServerConnectionSelector http1 = Http1ConnectionSelector.builder() .config(Http1Config.builder() .validateRequestHeaders(true) .validateResponseHeaders(false) .build()) .build(); server.addConnectionSelector(http1); ``` 2. Http1 WebClient ``` Http1Client client = Http1Client.create(clientConfig -> clientConfig.baseUri(baseURI) .protocolConfig(it -> { it.validateRequestHeaders(true); it.validateResponseHeaders(false); }) ); ``` * Rename ClientRequestImplTest class to Http1ClientTest as this is not focused to ClientRequestImplTest anymore * Update ValidateHeadersTest in tests/integration/webclient/webclient to use the new Headers and HeaderNames apis * Fix inadvertent removal of http2 webclient dependency in the webclient integration test
- Loading branch information
Showing
17 changed files
with
770 additions
and
44 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
229 changes: 229 additions & 0 deletions
229
...ebclient/webclient/src/test/java/io/helidon/nima/webclient/http1/ValidateHeadersTest.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,229 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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 io.helidon.nima.webclient.http1; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.UncheckedIOException; | ||
import java.net.SocketTimeoutException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.helidon.common.GenericType; | ||
import io.helidon.common.http.Headers; | ||
import io.helidon.common.http.Http; | ||
import io.helidon.common.http.Http.HeaderName; | ||
import io.helidon.common.http.ServerRequestHeaders; | ||
import io.helidon.nima.http.media.EntityReader; | ||
import io.helidon.nima.http.media.EntityWriter; | ||
import io.helidon.nima.http.media.MediaContext; | ||
import io.helidon.nima.http.media.MediaContextConfig; | ||
import io.helidon.nima.testing.junit5.webserver.ServerTest; | ||
import io.helidon.nima.testing.junit5.webserver.SetUpRoute; | ||
import io.helidon.nima.webclient.api.ClientConnection; | ||
import io.helidon.nima.webclient.api.ClientResponseTyped; | ||
import io.helidon.nima.webclient.api.HttpClientRequest; | ||
import io.helidon.nima.webclient.api.HttpClientResponse; | ||
import io.helidon.nima.webclient.api.Proxy; | ||
import io.helidon.nima.webclient.api.WebClient; | ||
import io.helidon.nima.webserver.WebServer; | ||
import io.helidon.nima.webserver.WebServerConfig; | ||
import io.helidon.nima.webserver.http.HttpRules; | ||
import io.helidon.nima.webserver.http.ServerRequest; | ||
import io.helidon.nima.webserver.http.ServerResponse; | ||
import io.helidon.nima.webserver.http1.Http1Config; | ||
import io.helidon.nima.webserver.http1.Http1ConnectionSelector; | ||
import io.helidon.nima.webserver.spi.ServerConnectionSelector; | ||
|
||
import io.helidon.nima.testing.junit5.webserver.SetUpServer; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import static io.helidon.common.testing.http.junit5.HttpHeaderMatcher.hasHeader; | ||
import static io.helidon.common.testing.http.junit5.HttpHeaderMatcher.noHeader; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.IsNot.not; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Test for validating client side outbound/inbound headers (request/response headers) | ||
*/ | ||
@ServerTest | ||
class ValidateHeadersTest { | ||
public static final String VALID_HEADER_NAME = "Valid-Header-Name"; | ||
public static final String VALID_HEADER_VALUE = "Valid-Header-Value"; | ||
private final String baseURI; | ||
|
||
ValidateHeadersTest(WebServer webServer) { | ||
baseURI = "http://localhost:" + webServer.port(); | ||
} | ||
|
||
@SetUpServer | ||
static void server(WebServerConfig.Builder server) { | ||
ServerConnectionSelector http1 = Http1ConnectionSelector.builder() | ||
.config(Http1Config.builder() | ||
.validateRequestHeaders(false) | ||
.validateResponseHeaders(false) | ||
.build()) | ||
.build(); | ||
|
||
server.addConnectionSelector(http1); | ||
} | ||
|
||
@SetUpRoute | ||
static void routing(HttpRules rules) { | ||
rules.put("/test", ValidateHeadersTest::headerValidationHandler); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("customHeaders") | ||
void testRequestHeaders(String headerName, String headerValue, boolean expectsValid) { | ||
Http1Client client = Http1Client.create(clientConfig -> clientConfig.baseUri(baseURI) | ||
.protocolConfig(it -> { | ||
it.validateRequestHeaders(true); | ||
it.validateResponseHeaders(false); | ||
}) | ||
); | ||
Http1ClientRequest request = client.put(baseURI + "/test"); | ||
request.header(Http.Headers.create(Http.HeaderNames.create(headerName), headerValue)); | ||
if (expectsValid) { | ||
HttpClientResponse response = request.request(); | ||
assertThat(response.status(), is(Http.Status.OK_200)); | ||
} else { | ||
assertThrows(IllegalArgumentException.class, () -> request.request()); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("customHeaders") | ||
void testResponsetHeaders(String headerName, String headerValue, boolean expectsValid) { | ||
Http1Client client = Http1Client.create(clientConfig -> clientConfig.baseUri(baseURI) | ||
.protocolConfig(it -> { | ||
it.validateRequestHeaders(false); | ||
it.validateResponseHeaders(true); | ||
}) | ||
); | ||
Http1ClientRequest request = client.put(baseURI + "/test"); | ||
request.header(Http.Headers.create(Http.HeaderNames.create(headerName), headerValue)); | ||
if (expectsValid) { | ||
HttpClientResponse response = request.request(); | ||
assertThat(response.status(), is(Http.Status.OK_200)); | ||
String responseHeaderValue = response.headers().get(Http.HeaderNames.create(headerName)).values(); | ||
assertThat(responseHeaderValue, is(headerValue.trim())); | ||
} else { | ||
assertThrows(IllegalArgumentException.class, () -> request.request()); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("customHeaders") | ||
void testOutputStreamResponsetHeaders(String headerName, String headerValue, boolean expectsValid) { | ||
Http1Client client = Http1Client.create(clientConfig -> clientConfig.baseUri(baseURI) | ||
.protocolConfig(it -> { | ||
it.validateRequestHeaders(false); | ||
it.validateResponseHeaders(true); | ||
}) | ||
.sendExpectContinue(false) | ||
); | ||
Http1ClientRequest request = client.put(baseURI + "/test"); | ||
request.header(Http.Headers.create(Http.HeaderNames.create(headerName), headerValue)); | ||
if (expectsValid) { | ||
HttpClientResponse response = request.outputStream(it -> { | ||
it.write("Foo Bar".getBytes(StandardCharsets.UTF_8)); | ||
it.close(); | ||
}); | ||
assertThat(response.status(), is(Http.Status.OK_200)); | ||
String responseHeaderValue = response.headers().get(Http.HeaderNames.create(headerName)).values(); | ||
assertThat(responseHeaderValue, is(headerValue.trim())); | ||
} else { | ||
assertThrows( | ||
IllegalArgumentException.class, () -> request.outputStream(it -> { | ||
it.write("Foo Bar".getBytes(StandardCharsets.UTF_8)); | ||
it.close(); | ||
}) | ||
); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("customHeaders") | ||
void testDisableHeaderValidation(String headerName, String headerValue, boolean expectsValid) { | ||
Http1Client client = Http1Client.create(clientConfig -> clientConfig.baseUri(baseURI) | ||
.protocolConfig(it -> { | ||
it.validateRequestHeaders(false); | ||
it.validateResponseHeaders(false); | ||
}) | ||
); | ||
Http1ClientRequest request = client.put(baseURI + "/test"); | ||
request.header(Http.Headers.create(Http.HeaderNames.create(headerName), headerValue)); | ||
HttpClientResponse response = request.request(); | ||
assertThat(response.status(), is(Http.Status.OK_200)); | ||
String responseHeaderValue = response.headers().get(Http.HeaderNames.create(headerName)).values(); | ||
assertThat(responseHeaderValue, is(headerValue.trim())); | ||
} | ||
|
||
private static void headerValidationHandler(ServerRequest request, ServerResponse response) { | ||
ServerRequestHeaders headers = request.headers(); | ||
request.headers().toMap().forEach((k, v) -> { | ||
if (k.contains("Header")) { | ||
response.headers().add(Http.Headers.create(Http.HeaderNames.create(k), v)); | ||
} | ||
}); | ||
response.send("any"); | ||
} | ||
|
||
private static Stream<Arguments> customHeaders() { | ||
return Stream.of( | ||
// Invalid header names | ||
arguments("Header\u001aName", VALID_HEADER_VALUE, false), | ||
arguments("Header\u000EName", VALID_HEADER_VALUE, false), | ||
arguments("<Header?Name>", VALID_HEADER_VALUE, false), | ||
arguments("{Header=Name}", VALID_HEADER_VALUE, false), | ||
arguments("\"HeaderName\"", VALID_HEADER_VALUE, false), | ||
arguments("[\\HeaderName]", VALID_HEADER_VALUE, false), | ||
arguments("@Header,Name;", VALID_HEADER_VALUE, false), | ||
// Valid header names | ||
arguments("!#$Custom~%&\'*Header+^`|", VALID_HEADER_VALUE, true), | ||
arguments("Custom_0-9_a-z_A-Z_Header", VALID_HEADER_VALUE, true), | ||
// Valid header values | ||
arguments(VALID_HEADER_NAME, "Header Value", true), | ||
arguments(VALID_HEADER_NAME, "HeaderValue1\u0009, Header=Value2", true), | ||
arguments(VALID_HEADER_NAME, "Header\tValue", true), | ||
// Invalid header values | ||
arguments(VALID_HEADER_NAME, "HeaderV\u001calue1", false), | ||
arguments(VALID_HEADER_NAME, "HeaderValue1, Header\u007fValue", false), | ||
arguments(VALID_HEADER_NAME, "HeaderValue1\u001f, HeaderValue2", false) | ||
); | ||
} | ||
} |
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.