Skip to content

Commit 380afb5

Browse files
authored
fix(sdk): percent-encode request param names and values in query strings (#7492)
RequestParamParameterProcessor appended parameter keys and values into the URL verbatim, so reserved characters were interpreted as URL syntax instead of parameter data: a value like "a&admin=true" was sent as one logical parameter becoming two, and spaces, '=' and '%' could produce malformed or differently parsed URLs. Encode both the key and the value with UriUtils.encodeQueryParam before appending. Plain alphanumeric values are unchanged, and the map-valued RequestParam branch is covered as well. Regression tests use values with reserved characters, spaces, and a map with reserved keys and values (red on the previous code, which appended them raw). Fixes #7464 Co-authored-by: Sean-Walker0 <[email protected]>
1 parent 1797bd3 commit 380afb5

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessor.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.collect.Maps;
2121
import java.lang.annotation.Annotation;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.Map;
2324

2425
import org.apache.commons.lang3.StringUtils;
@@ -27,6 +28,7 @@
2728
import static org.apache.shenyu.sdk.core.util.Util.checkState;
2829
import org.apache.shenyu.sdk.spring.factory.AnnotatedParameterProcessor;
2930
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.util.UriUtils;
3032
import org.springframework.web.multipart.MultipartFile;
3133

3234
/**
@@ -61,7 +63,9 @@ public boolean processArgument(final ShenyuRequest shenyuRequest, final Annotati
6163
} else {
6264
urlResult.append("?");
6365
}
64-
urlResult.append(key).append("=").append(value);
66+
urlResult.append(UriUtils.encodeQueryParam(String.valueOf(key), StandardCharsets.UTF_8))
67+
.append('=')
68+
.append(UriUtils.encodeQueryParam(String.valueOf(value), StandardCharsets.UTF_8));
6569
});
6670
shenyuRequest.setUrl(urlResult.toString());
6771
return true;

‎shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/annotation/RequestParamParameterProcessorTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ public void init() throws NoSuchMethodException {
5353
this.method2 = clazz.getMethod("method2", Map.class);
5454
}
5555

56+
@Test
57+
public void processArgumentEncodesReservedCharactersTest() {
58+
RequestTemplate template = new RequestTemplate(Void.class, method1, "method1", "/dev/url/param", "", "/path", ShenyuRequest.HttpMethod.GET, null, null, null);
59+
this.request = ShenyuRequest.create(ShenyuRequest.HttpMethod.POST, template.getUrl() + template.getPath(), Maps.newHashMap(), "", "test", template);
60+
final RequestParam param = spy(RequestParam.class);
61+
when(param.value()).thenReturn("q");
62+
63+
processor.processArgument(request, param, "a&admin=true 100%");
64+
65+
assertTrue(request.getUrl().endsWith("q=a%26admin%3Dtrue%20100%25"), "query param value must be percent-encoded: " + request.getUrl());
66+
}
67+
68+
@Test
69+
public void processArgumentMapValuesAreEncodedTest() {
70+
RequestTemplate template = new RequestTemplate(Void.class, method2, "method2", "/dev/url/param", "", "/path", ShenyuRequest.HttpMethod.GET, null, null, null);
71+
this.request = ShenyuRequest.create(ShenyuRequest.HttpMethod.POST, template.getUrl() + template.getPath(), Maps.newHashMap(), "", "test", template);
72+
final RequestParam param = spy(RequestParam.class);
73+
74+
Map<String, Object> params = Maps.newHashMap();
75+
params.put("k&y", "v=l");
76+
processor.processArgument(request, param, params);
77+
78+
assertTrue(request.getUrl().contains("k%26y=v%3Dl"), "map keys and values must be percent-encoded: " + request.getUrl());
79+
}
80+
5681
@Test
5782
public void processArgumentNullTest() {
5883
RequestTemplate template = new RequestTemplate(Void.class, method1, "method1", "/dev/url/param", "", "/path", ShenyuRequest.HttpMethod.GET, null, null, null);

0 commit comments

Comments
 (0)