Skip to content

Commit

Permalink
Support http headers (#76)
Browse files Browse the repository at this point in the history
* Support http headers

* Remove ignored and excluded headers
  • Loading branch information
pc9795 authored Jun 22, 2024
1 parent 36b2979 commit 6409f2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class RequestInfo extends Model {
@Builder.Default Map<String, String> cookies = new HashMap<>();
Object postData;
@Builder.Default Map<String, List<String>> queryString = new HashMap<>();
@Builder.Default Map<String, List<String>> headers = new HashMap<>();

public Boolean isSecure() {
return secure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@
import java.net.URI;
import java.net.UnknownHostException;
import java.net.http.HttpRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class RequestInfoPlugin implements EventPluginIF {
private static final Integer DEFAULT_PRIORITY = 70;
private static final Set<String> IGNORED_HEADERS =
Set.of(
"Authorization",
"Cookie",
"Host",
"Method",
"Path",
"Proxy-Authorization",
"Referer",
"User-Agent");

@Builder
public RequestInfoPlugin() {}
Expand All @@ -31,8 +42,7 @@ public int getPriority() {
}

@Override
public void run(
EventPluginContext eventPluginContext, Configuration configuration) {
public void run(EventPluginContext eventPluginContext, Configuration configuration) {
Event event = eventPluginContext.getEvent();
if (event.getRequestInfo().isPresent()) {
return;
Expand All @@ -47,10 +57,8 @@ public void run(
RequestInfoGetArgs.builder()
.exclusions(configuration.getDataExclusions())
.includeCookies(configuration.getPrivateInformationInclusions().getCookies())
.includeIpAddress(
configuration.getPrivateInformationInclusions().getIpAddress())
.includePostData(
configuration.getPrivateInformationInclusions().getPostData())
.includeIpAddress(configuration.getPrivateInformationInclusions().getIpAddress())
.includePostData(configuration.getPrivateInformationInclusions().getPostData())
.includeQueryString(
configuration.getPrivateInformationInclusions().getQueryString())
.build());
Expand All @@ -73,7 +81,8 @@ private RequestInfo getRequestInfo(HttpRequest request, RequestInfoGetArgs args)
.httpMethod(request.method())
.host(request.uri().getHost())
.path(request.uri().getPath())
.port(request.uri().getPort());
.port(request.uri().getPort())
.headers(getHeaders(request, args));

if (args.isIncludeIpAddress()) {
try {
Expand All @@ -94,11 +103,17 @@ private RequestInfo getRequestInfo(HttpRequest request, RequestInfoGetArgs args)
filterExclusions(Utils.getQueryParams(request.uri()), args.getExclusions()));
}

// todo get post data from request.

return builder.build();
}

private Map<String, List<String>> getHeaders(HttpRequest request, RequestInfoGetArgs args) {
Map<String, List<String>> headers =
filterExclusions(request.headers().map(), args.getExclusions());
return headers.entrySet().stream()
.filter(entry -> !IGNORED_HEADERS.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private <X> Map<String, X> filterExclusions(Map<String, X> map, Set<String> exclusions) {
return map.entrySet().stream()
.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ public void itShouldCancelEventIfUserAgentIsABotPattern() {
@Test
public void itShouldAddRequestInfoToEvent() {
HttpRequest httpRequest =
HttpRequest.newBuilder().uri(URI.create("http://localhost:5000/test-path")).GET().build();
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:5000/test-path"))
.header("test-header", "test-header-value")
.header("test-header", "test-header-value-2")
.GET()
.build();
context =
EventPluginContext.builder()
.event(Event.builder().build())
Expand All @@ -108,6 +113,8 @@ public void itShouldAddRequestInfoToEvent() {
assertThat(requestInfo.getHost()).isEqualTo("localhost");
assertThat(requestInfo.getPath()).isEqualTo("/test-path");
assertThat(requestInfo.getPort()).isEqualTo(5000);
assertThat(requestInfo.getHeaders())
.isEqualTo(Map.of("test-header", List.of("test-header-value", "test-header-value-2")));
}

@Test
Expand Down Expand Up @@ -163,8 +170,7 @@ public void itCanGetIpAddressCookiesAndQueryStringFromAHttpRequest() {
.context(PluginContext.builder().request(httpRequest).build())
.build();

PrivateInformationInclusions inclusions =
configuration.getPrivateInformationInclusions();
PrivateInformationInclusions inclusions = configuration.getPrivateInformationInclusions();
inclusions.setIpAddress(true);
inclusions.setCookies(true);
inclusions.setQueryString(true);
Expand All @@ -187,7 +193,15 @@ public void itCanExcludeData() {
.uri(
URI.create(
"https://localhost:5000/test-path?query-param-key=query-param-value&exclude-query-param=exclude-value"))
.header("Cookie", "cookie1=value1;cookie2=value2;exclude-cookie=exclude-value")
.headers(
"Cookie",
"cookie1=value1;cookie2=value2;exclude-cookie=exclude-value",
"test-header",
"test-value",
"Authorization",
"test-auth",
"exclude-header",
"exclude-value")
.GET()
.build();

Expand All @@ -196,9 +210,8 @@ public void itCanExcludeData() {
.event(Event.builder().build())
.context(PluginContext.builder().request(httpRequest).build())
.build();
configuration.addDataExclusions("exclude-query-param", "exclude-cookie");
PrivateInformationInclusions inclusions =
configuration.getPrivateInformationInclusions();
configuration.addDataExclusions("exclude-query-param", "exclude-cookie", "exclude-header");
PrivateInformationInclusions inclusions = configuration.getPrivateInformationInclusions();
inclusions.setIpAddress(true);
inclusions.setCookies(true);
inclusions.setQueryString(true);
Expand All @@ -212,5 +225,6 @@ public void itCanExcludeData() {
.isEqualTo(Map.of("cookie1", "value1", "cookie2", "value2"));
assertThat(requestInfo.getQueryString())
.isEqualTo(Map.of("query-param-key", List.of("query-param-value")));
assertThat(requestInfo.getHeaders()).isEqualTo(Map.of("test-header", List.of("test-value")));
}
}

0 comments on commit 6409f2a

Please sign in to comment.