This is an automated email from the ASF dual-hosted git repository.
tzssangglass pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/apisix-java-plugin-runner.git
The following commit(s) were added to refs/heads/main by this push:
new 1fcc377 fix: disable null as key of req/resp headers and args (#105)
1fcc377 is described below
commit 1fcc377f14926c2270109a3b79afbc902af9bb0f
Author: tzssangglass <[email protected]>
AuthorDate: Fri Dec 31 20:25:00 2021 +0800
fix: disable null as key of req/resp headers and args (#105)
---
.../plugin/runner/codec/impl/PayloadEncoderTest.java | 17 +++++++++++++++++
.../org/apache/apisix/plugin/runner/HttpResponse.java | 15 +++++++++++++++
2 files changed, 32 insertions(+)
diff --git
a/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/PayloadEncoderTest.java
b/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/PayloadEncoderTest.java
index bde4d21..4231e9c 100644
---
a/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/PayloadEncoderTest.java
+++
b/runner-core/src/test/java/org/apache/apisix/plugin/runner/codec/impl/PayloadEncoderTest.java
@@ -33,6 +33,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import java.nio.ByteBuffer;
+import java.util.HashMap;
@DisplayName("test encode data")
class PayloadEncoderTest {
@@ -285,4 +286,20 @@ class PayloadEncoderTest {
}
}
}
+
+ @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);
+ }
}
diff --git
a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
index 9aa2e62..410715e 100644
---
a/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
+++
b/runner-plugin-sdk/src/main/java/org/apache/apisix/plugin/runner/HttpResponse.java
@@ -67,6 +67,12 @@ public class HttpResponse implements A6Response {
}
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<>();
@@ -81,6 +87,10 @@ public class HttpResponse implements A6Response {
* @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<>();
@@ -105,6 +115,11 @@ public class HttpResponse implements A6Response {
* @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<>();