|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2025 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.webserver.tests; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.helidon.common.testing.http.junit5.SocketHttpClient; |
| 22 | +import io.helidon.http.HttpPrologue; |
| 23 | +import io.helidon.http.Method; |
| 24 | +import io.helidon.webclient.http1.Http1Client; |
| 25 | +import io.helidon.webserver.http.HttpRouting; |
| 26 | +import io.helidon.webserver.http1.Http1Route; |
| 27 | +import io.helidon.webserver.testing.junit5.ServerTest; |
| 28 | +import io.helidon.webserver.testing.junit5.SetUpRoute; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import static org.hamcrest.CoreMatchers.containsString; |
| 33 | +import static org.hamcrest.CoreMatchers.is; |
| 34 | +import static org.hamcrest.CoreMatchers.not; |
| 35 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Verifies that 400 bad request is returned when a bad prologue is sent. Response |
| 39 | + * entities must be empty given that {@link io.helidon.webserver.ErrorHandling#includeEntity()} |
| 40 | + * is {@code false} by default. |
| 41 | + */ |
| 42 | +@ServerTest |
| 43 | +class BadPrologueNoEntityTest { |
| 44 | + private final Http1Client client; |
| 45 | + private final SocketHttpClient socketClient; |
| 46 | + |
| 47 | + BadPrologueNoEntityTest(Http1Client client, SocketHttpClient socketClient) { |
| 48 | + this.client = client; |
| 49 | + this.socketClient = socketClient; |
| 50 | + } |
| 51 | + |
| 52 | + @SetUpRoute |
| 53 | + static void routing(HttpRouting.Builder builder) { |
| 54 | + builder.route(Http1Route.route(Method.GET, |
| 55 | + "/", |
| 56 | + (req, res) -> { |
| 57 | + HttpPrologue prologue = req.prologue(); |
| 58 | + String fragment = prologue.fragment().hasValue() |
| 59 | + ? prologue.fragment().rawValue() |
| 60 | + : ""; |
| 61 | + res.send("path: " + prologue.uriPath().rawPath() |
| 62 | + + ", query: " + prologue.query().rawValue() |
| 63 | + + ", fragment: " + fragment); |
| 64 | + })); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testOk() { |
| 69 | + String response = client.method(Method.GET) |
| 70 | + .requestEntity(String.class); |
| 71 | + |
| 72 | + assertThat(response, is("path: /, query: , fragment: ")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testBadQuery() { |
| 77 | + String response = socketClient.sendAndReceive(Method.GET, |
| 78 | + "/?a=<a%20href='/bad-uri.com'/>bad</a>", |
| 79 | + null, |
| 80 | + List.of()); |
| 81 | + |
| 82 | + assertThat(response, containsString("400 Bad Request")); |
| 83 | + // beginning of message to the first double quote |
| 84 | + assertThat(response, not(containsString("Query contains invalid char: "))); |
| 85 | + // end of message from double quote, index of bad char, and bad char |
| 86 | + assertThat(response, not(containsString(", index: 2, char: 0x3c"))); |
| 87 | + assertThat(response, not(containsString(">"))); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void testBadQueryCurly() { |
| 92 | + String response = socketClient.sendAndReceive(Method.GET, |
| 93 | + "/?name=test1{{", |
| 94 | + null, |
| 95 | + List.of()); |
| 96 | + |
| 97 | + assertThat(response, containsString("400 Bad Request")); |
| 98 | + // beginning of message to the first double quote |
| 99 | + assertThat(response, not(containsString("Query contains invalid char: "))); |
| 100 | + // end of message from double quote, index of bad char, and bad char |
| 101 | + assertThat(response, not(containsString(", index: 10, char: 0x7b"))); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testBadPath() { |
| 106 | + String response = socketClient.sendAndReceive(Method.GET, |
| 107 | + "/name{{{{{{{Sdsds<Dhttps:--www.example.com", |
| 108 | + null, |
| 109 | + List.of()); |
| 110 | + |
| 111 | + assertThat(response, containsString("400 Bad Request")); |
| 112 | + // for path we are on the safe side, and never return it back (even HTML encoded) |
| 113 | + assertThat(response, not(containsString("Bad request, see server log for more information"))); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void testBadFragment() { |
| 118 | + String response = socketClient.sendAndReceive(Method.GET, |
| 119 | + "/?a=b#invalid-fragment>", |
| 120 | + null, |
| 121 | + List.of()); |
| 122 | + |
| 123 | + assertThat(response, containsString("400 Bad Request")); |
| 124 | + // beginning of message to the first double quote |
| 125 | + assertThat(response, not(containsString("Fragment contains invalid char: "))); |
| 126 | + // end of message from double quote, index of bad char, and bad char |
| 127 | + assertThat(response, not(containsString(", index: 16, char: 0x3e"))); |
| 128 | + assertThat(response, not(containsString(">"))); |
| 129 | + } |
| 130 | +} |
0 commit comments