davsclaus commented on code in PR #27944: URL: https://github.com/apache/camel/pull/27944#discussion_r4295629848
########## components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerEncodedUriTest.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.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() { Review Comment: Optional: a positive test that a header value with a tab or a Latin-1 character (for example `"café\tau lait"`) is not refused by `checkWriteable` would pin down the allowed set, so a later tightening of the check cannot silently start rejecting legal header values. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
