Skip to content

Commit

Permalink
fix: disable null as key of req/resp headers and args (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzssangglass committed Dec 31, 2021
1 parent f0ef4b7 commit 1fcc377
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Expand Up @@ -33,6 +33,7 @@
import org.springframework.test.util.ReflectionTestUtils;

import java.nio.ByteBuffer;
import java.util.HashMap;

@DisplayName("test encode data")
class PayloadEncoderTest {
Expand Down Expand Up @@ -285,4 +286,20 @@ void testDoFilterWithoutSetStatusCode() {
}
}
}

@Test
@DisplayName("test set the parameter of the rewrite request to null")
void testHttpResponseNPE() {
HttpResponse httpResponse = new HttpResponse(0L);
// HashMap accepts null as key and value, but we want to disable null as key
httpResponse.setArg(null, null);
httpResponse.setReqHeader(null, null);
httpResponse.setHeader(null, null);
HashMap<String, String> reqHeaders = (HashMap<String, String>) ReflectionTestUtils.getField(httpResponse, "reqHeaders");
HashMap<String, String> args = (HashMap<String, String>) ReflectionTestUtils.getField(httpResponse, "args");
HashMap<String, String> respHeaders = (HashMap<String, String>) ReflectionTestUtils.getField(httpResponse, "respHeaders");
Assertions.assertNull(reqHeaders);
Assertions.assertNull(args);
Assertions.assertNull(respHeaders);
}
}
Expand Up @@ -67,6 +67,12 @@ public long getRequestId() {
}

public void setReqHeader(String headerKey, String headerValue) {
// key is null will cause the request to block
if (headerKey == null) {
logger.warn("headerKey is null, ignore it");
return;
}

actionType = ActionType.Rewrite;
if (Objects.isNull(reqHeaders)) {
reqHeaders = new HashMap<>();
Expand All @@ -81,6 +87,10 @@ public void setReqHeader(String headerKey, String headerValue) {
* @param argValue the arg value
*/
public void setArg(String argKey, String argValue) {
if (argKey == null) {
logger.warn("argKey is null, ignore it");
return;
}
actionType = ActionType.Rewrite;
if (Objects.isNull(args)) {
args = new HashMap<>();
Expand All @@ -105,6 +115,11 @@ public void setPath(String path) {
* @param headerValue the header value
*/
public void setHeader(String headerKey, String headerValue) {
if (headerKey == null) {
logger.warn("headerKey is null, ignore it");
return;
}

actionType = ActionType.Stop;
if (Objects.isNull(respHeaders)) {
respHeaders = new HashMap<>();
Expand Down

0 comments on commit 1fcc377

Please sign in to comment.