Skip to content

Commit 60a4d67

Browse files
committed
Handle special characters in TraceableHttpServletRequest
Fixes gh-13273
1 parent 1a0dfa0 commit 60a4d67

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/servlet/TraceableHttpServletRequest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.actuate.web.trace.servlet;
1818

1919
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
import java.nio.charset.StandardCharsets;
2022
import java.util.ArrayList;
2123
import java.util.Enumeration;
2224
import java.util.LinkedHashMap;
@@ -27,6 +29,7 @@
2729

2830
import org.springframework.boot.actuate.trace.http.TraceableRequest;
2931
import org.springframework.util.StringUtils;
32+
import org.springframework.web.util.UriUtils;
3033

3134
/**
3235
* An adapter that exposes an {@link HttpServletRequest} as a {@link TraceableRequest}.
@@ -48,12 +51,26 @@ public String getMethod() {
4851

4952
@Override
5053
public URI getUri() {
51-
StringBuffer urlBuffer = this.request.getRequestURL();
52-
if (StringUtils.hasText(this.request.getQueryString())) {
53-
urlBuffer.append("?");
54-
urlBuffer.append(this.request.getQueryString());
54+
String queryString = this.request.getQueryString();
55+
if (!StringUtils.hasText(queryString)) {
56+
return URI.create(this.request.getRequestURL().toString());
57+
}
58+
try {
59+
StringBuffer urlBuffer = appendQueryString(queryString);
60+
return new URI(urlBuffer.toString());
61+
}
62+
catch (URISyntaxException ex) {
63+
String encoded = UriUtils.encode(queryString, StandardCharsets.UTF_8);
64+
StringBuffer urlBuffer = appendQueryString(encoded);
65+
return URI.create(urlBuffer.toString());
5566
}
56-
return URI.create(urlBuffer.toString());
67+
}
68+
69+
private StringBuffer appendQueryString(String queryString) {
70+
StringBuffer urlBuffer = this.request.getRequestURL();
71+
urlBuffer.append("?");
72+
urlBuffer.append(queryString);
73+
return urlBuffer;
5774
}
5875

5976
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
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+
package org.springframework.boot.actuate.web.trace.servlet;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
21+
import org.springframework.mock.web.MockHttpServletRequest;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link TraceableHttpServletRequest}.
27+
*
28+
* @author Madhura Bhave
29+
*/
30+
public class TraceableHttpServletRequestTests {
31+
32+
private MockHttpServletRequest request;
33+
34+
@Before
35+
public void setup() {
36+
this.request = new MockHttpServletRequest("GET", "/script");
37+
}
38+
39+
@Test
40+
public void getUriWithoutQueryStringShouldReturnUri() {
41+
validate("http://localhost/script");
42+
}
43+
44+
@Test
45+
public void getUriShouldReturnUriWithQueryString() {
46+
this.request.setQueryString("a=b");
47+
validate("http://localhost/script?a=b");
48+
}
49+
50+
@Test
51+
public void getUriWithSpecialCharactersInQueryStringShouldEncode() {
52+
this.request.setQueryString("a=${b}");
53+
validate("http://localhost/script?a%3D%24%7Bb%7D");
54+
}
55+
56+
@Test
57+
public void getUriWithSpecialCharactersEncodedShouldNotDoubleEncode() {
58+
this.request.setQueryString("a%3D%24%7Bb%7D");
59+
validate("http://localhost/script?a%3D%24%7Bb%7D");
60+
}
61+
62+
private void validate(String expectedUri) {
63+
TraceableHttpServletRequest trace = new TraceableHttpServletRequest(this.request);
64+
assertThat(trace.getUri().toString()).isEqualTo(expectedUri);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)