Repository navigation
[backport camel-4.18.x] CAMEL-25704: camel-undertow - refuse a request target or header value with control characters #28064
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
113 changes: 113 additions & 0 deletions
113
...tow/src/test/java/org/apache/camel/component/undertow/UndertowProducerEncodedUriTest.java
This file contains hidden or 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,113 @@ | ||
| /* | ||
| * 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.camel.component.undertow; | ||
|
|
||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * The undertow producer must not write a request path, query or header value that it cannot put on the wire as it is: a | ||
| * percent-encoded CR/LF in {@code CamelHttpPath} or {@code CamelHttpQuery}, or a CR/LF in a header value, is refused | ||
| * rather than written into the request line or a header line. An ordinary query is still sent. | ||
| */ | ||
| class UndertowProducerEncodedUriTest extends BaseUndertowTest { | ||
|
|
||
| @Test | ||
| void rejectsEncodedCrlfInQuery() { | ||
| Exchange out = template.request("undertow:http://localhost:{{port}}/echo", | ||
| e -> e.getIn().setHeader(Exchange.HTTP_QUERY, "q=x%0D%0AX-Injected:%201")); | ||
| assertRejected(out, "request target"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsEncodedCrlfInPath() { | ||
| Exchange out = template.request("undertow:http://localhost:{{port}}/echo", | ||
| e -> e.getIn().setHeader(Exchange.HTTP_PATH, "a%0D%0Ab")); | ||
| assertRejected(out, "request target"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsNarrowingCharacterInQuery() { | ||
| // U+010A is above 0xFF; the client would narrow it to the single byte 0x0A (LF). It is refused before that. | ||
| Exchange out = template.request("undertow:http://localhost:{{port}}/echo", | ||
| e -> e.getIn().setHeader(Exchange.HTTP_QUERY, "q=Ċ")); | ||
| assertRejected(out, "request target"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsHeaderValueWithCrlf() { | ||
| Exchange out = template.request("undertow:http://localhost:{{port}}/echo", | ||
| e -> e.getIn().setHeader("MyHeader", "value\r\nX-Injected: 1")); | ||
| assertRejected(out, "HTTP header"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsCrlfInPreservedHostHeader() { | ||
| // with preserveHostHeader the caller's Host header is written verbatim, so it is validated too | ||
| Exchange out = template.request("undertow:http://localhost:{{port}}/echo?preserveHostHeader=true", | ||
| e -> e.getIn().setHeader("Host", "localhost\r\nX-Injected: 1")); | ||
| assertRejected(out, "Host header"); | ||
| } | ||
|
|
||
| @Test | ||
| void sendsAnOrdinaryQuery() { | ||
| String out = template.requestBodyAndHeader("undertow:http://localhost:{{port}}/echo", | ||
| null, Exchange.HTTP_QUERY, "q=hello", String.class); | ||
| assertEquals("q=hello", out); | ||
| } | ||
|
|
||
| @Test | ||
| void sendsAHeaderWithTabAndLatin1() { | ||
| // a tab, a space and a Latin-1 (obs-text) character do not change the request framing, so they are NOT refused | ||
| Exchange out = template.request("undertow:http://localhost:{{port}}/echo", | ||
| e -> e.getIn().setHeader("MyHeader", "café\tau lait")); | ||
| assertFalse(out.isFailed(), "a header value with a tab and a Latin-1 character must not be refused"); | ||
| } | ||
|
|
||
| private static void assertRejected(Exchange out, String expectedInMessage) { | ||
| assertTrue(out.isFailed(), "the exchange should have failed"); | ||
| Throwable cause = out.getException(); | ||
| assertNotNull(cause); | ||
| boolean found = false; | ||
| while (cause != null) { | ||
| if (cause instanceof IllegalArgumentException && cause.getMessage() != null | ||
| && cause.getMessage().contains(expectedInMessage)) { | ||
| found = true; | ||
| break; | ||
| } | ||
| cause = cause.getCause(); | ||
| } | ||
| assertTrue(found, "expected an IllegalArgumentException mentioning '" + expectedInMessage + "'"); | ||
| } | ||
|
|
||
| @Override | ||
| protected RouteBuilder createRouteBuilder() { | ||
| return new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("undertow:http://localhost:{{port}}/echo") | ||
| .setBody(simple("${header.CamelHttpQuery}")); | ||
| } | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lands under "Upgrading Camel 4.17 to 4.18", the oldest section of the file. The note belongs under "Upgrading from 4.18.5 to 4.18.6", in the guide on
mainrather than oncamel-4.18.x.