Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test cases for CSEC parent id header #120

Merged
merged 8 commits into from
Nov 23, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.newrelic.agent.security.introspec.SecurityIntrospector;
import com.newrelic.agent.security.introspec.internal.HttpServerRule;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.ServletHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
import com.newrelic.api.agent.security.schema.VulnerabilityCaseType;
import com.newrelic.api.agent.security.schema.operation.SSRFOperation;
Expand All @@ -25,6 +27,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Future;

@RunWith(SecurityInstrumentationTestRunner.class)
Expand All @@ -44,76 +48,128 @@ public static void before() {
}
@Test
public void testExecute() throws Exception {
callExecute();
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
setCSECHeaders(headerValue, introspector);

callExecute();
List<AbstractOperation> operations = introspector.getOperations();
Assert.assertTrue("No operations detected", operations.size() > 0);

SSRFOperation operation = (SSRFOperation) operations.get(0);
Map<String, String> headers = server.getHeaders();
Assert.assertEquals("Invalid executed parameters.", endpoint.toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute1() throws Exception {
callExecute1();
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
setCSECHeaders(headerValue, introspector);

callExecute1();
List<AbstractOperation> operations = introspector.getOperations();
Assert.assertTrue("No operations detected", operations.size() > 0);
SSRFOperation operation = (SSRFOperation) operations.get(0);
Map<String, String> headers = server.getHeaders();
Assert.assertEquals("Invalid executed parameters.", endpoint.toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute2() throws Exception {
callExecute2();
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
setCSECHeaders(headerValue, introspector);

callExecute2();
List<AbstractOperation> operations = introspector.getOperations();
Assert.assertTrue("No operations detected", operations.size() > 0);
SSRFOperation operation = (SSRFOperation) operations.get(0);
Map<String, String> headers = server.getHeaders();
Assert.assertEquals("Invalid executed parameters.", endpoint.toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute3() throws Exception {
callExecute3();
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
setCSECHeaders(headerValue, introspector);

callExecute3();
List<AbstractOperation> operations = introspector.getOperations();
Assert.assertTrue("No operations detected", operations.size() > 0);
SSRFOperation operation = (SSRFOperation) operations.get(0);
Map<String, String> headers = server.getHeaders();
Assert.assertEquals("Invalid executed parameters.", endpoint.toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute4() throws Exception {
callExecute4();
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
setCSECHeaders(headerValue, introspector);

callExecute4();
List<AbstractOperation> operations = introspector.getOperations();
Assert.assertTrue("No operations detected", operations.size() > 0);
SSRFOperation operation = (SSRFOperation) operations.get(0);
Map<String, String> headers = server.getHeaders();
Assert.assertEquals("Invalid executed parameters.", endpoint.toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute5() throws Exception {
callExecute5();
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
setCSECHeaders(headerValue, introspector);

callExecute5();
List<AbstractOperation> operations = introspector.getOperations();
Assert.assertTrue("No operations detected", operations.size() > 0);
SSRFOperation operation = (SSRFOperation) operations.get(0);
Map<String, String> headers = server.getHeaders();
Assert.assertEquals("Invalid executed parameters.", endpoint.toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
verifyHeaders(headerValue, headers);
}

private void setCSECHeaders(String headerValue, SecurityIntrospector introspector) {
introspector.setK2FuzzRequestId(headerValue+"a");
introspector.setK2ParentId(headerValue+"b");
introspector.setK2TracingData(headerValue);
}

private void verifyHeaders(String headerValue, Map<String, String> headers) {
Assert.assertTrue(String.format("Missing K2 header: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headers.containsKey(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertEquals(String.format("Invalid K2 header value for: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headerValue+"a", headers.get(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertTrue(String.format("Missing K2 header: %s", GenericHelper.CSEC_PARENT_ID), headers.containsKey(GenericHelper.CSEC_PARENT_ID));
Assert.assertEquals(String.format("Invalid K2 header value for: %s", GenericHelper.CSEC_PARENT_ID), headerValue+"b", headers.get(GenericHelper.CSEC_PARENT_ID));
Assert.assertTrue(String.format("Missing K2 header: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
Assert.assertEquals(String.format("Invalid K2 header value for: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), String.format("%s;DUMMY_UUID/dummy-api-id/dummy-exec-id;",
headerValue), headers.get(
ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
}

@Trace(dispatcher = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.newrelic.agent.security.introspec.SecurityIntrospector;
import com.newrelic.agent.security.introspec.internal.HttpServerRule;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.ServletHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
import com.newrelic.api.agent.security.schema.VulnerabilityCaseType;
Expand Down Expand Up @@ -35,8 +36,7 @@ public void testExecute() throws URISyntaxException, IOException {
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
introspector.setK2FuzzRequestId(headerValue);
introspector.setK2TracingData(headerValue);
setCSECHeaders(headerValue, introspector);

callExecute();

Expand All @@ -48,20 +48,15 @@ public void testExecute() throws URISyntaxException, IOException {
Assert.assertEquals("Invalid executed parameters.", server.getEndPoint().toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
Assert.assertTrue(String.format("Missing CSEC header: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headers.containsKey(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertEquals(String.format("Invalid CSEC header value for: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headerValue, headers.get(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertTrue(String.format("Missing CSEC header: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
Assert.assertEquals(String.format("Invalid CSEC header value for: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), String.format("%s;DUMMY_UUID/dummy-api-id/dummy-exec-id;", headerValue), headers.get(
ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute1() throws URISyntaxException, IOException {
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
introspector.setK2FuzzRequestId(headerValue);
introspector.setK2TracingData(headerValue);
setCSECHeaders(headerValue, introspector);

callExecute();

Expand All @@ -73,20 +68,15 @@ public void testExecute1() throws URISyntaxException, IOException {
Assert.assertEquals("Invalid executed parameters.", server.getEndPoint().toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
Assert.assertTrue(String.format("Missing CSEC header: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headers.containsKey(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertEquals(String.format("Invalid CSEC header value for: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headerValue, headers.get(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertTrue(String.format("Missing CSEC header: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
Assert.assertEquals(String.format("Invalid CSEC header value for: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), String.format("%s;DUMMY_UUID/dummy-api-id/dummy-exec-id;", headerValue), headers.get(
ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
verifyHeaders(headerValue, headers);
}

@Test
public void testExecute2() throws URISyntaxException, IOException {
String headerValue = String.valueOf(UUID.randomUUID());

SecurityIntrospector introspector = SecurityInstrumentationTestRunner.getIntrospector();
introspector.setK2FuzzRequestId(headerValue);
introspector.setK2TracingData(headerValue);
setCSECHeaders(headerValue, introspector);

callExecute();

Expand All @@ -98,10 +88,23 @@ public void testExecute2() throws URISyntaxException, IOException {
Assert.assertEquals("Invalid executed parameters.", server.getEndPoint().toString(), operation.getArg());
Assert.assertEquals("Invalid event category.", VulnerabilityCaseType.HTTP_REQUEST, operation.getCaseType());
Assert.assertEquals("Invalid executed method name.", "execute", operation.getMethodName());
Assert.assertTrue(String.format("Missing CSEC header: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headers.containsKey(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertEquals(String.format("Invalid CSEC header value for: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headerValue, headers.get(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertTrue(String.format("Missing CSEC header: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
Assert.assertEquals(String.format("Invalid CSEC header value for: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), String.format("%s;DUMMY_UUID/dummy-api-id/dummy-exec-id;", headerValue), headers.get(
verifyHeaders(headerValue, headers);
}

private void setCSECHeaders(String headerValue, SecurityIntrospector introspector) {
introspector.setK2FuzzRequestId(headerValue+"a");
introspector.setK2ParentId(headerValue+"b");
introspector.setK2TracingData(headerValue);
}

private void verifyHeaders(String headerValue, Map<String, String> headers) {
Assert.assertTrue(String.format("Missing K2 header: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headers.containsKey(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertEquals(String.format("Invalid K2 header value for: %s", ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID), headerValue+"a", headers.get(ServletHelper.CSEC_IAST_FUZZ_REQUEST_ID));
Assert.assertTrue(String.format("Missing K2 header: %s", GenericHelper.CSEC_PARENT_ID), headers.containsKey(GenericHelper.CSEC_PARENT_ID));
Assert.assertEquals(String.format("Invalid K2 header value for: %s", GenericHelper.CSEC_PARENT_ID), headerValue+"b", headers.get(GenericHelper.CSEC_PARENT_ID));
Assert.assertTrue(String.format("Missing K2 header: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
Assert.assertEquals(String.format("Invalid K2 header value for: %s", ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER), String.format("%s;DUMMY_UUID/dummy-api-id/dummy-exec-id;",
headerValue), headers.get(
ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase()));
}

Expand Down
Loading
Loading