Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.uid2</groupId>
<artifactId>uid2-shared</artifactId>
<version>11.3.4-alpha-333-SNAPSHOT</version>
<version>11.3.4-alpha-334-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Library for all the shared uid2 operations</description>
<url>https://github.com/IABTechLab/uid2docs</url>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/uid2/shared/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.uid2.shared.util.Mapper;
import io.vertx.core.http.impl.HttpUtils;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
Expand Down Expand Up @@ -190,4 +191,20 @@ public static MessageDigest createMessageDigestSHA512() {
throw new RuntimeException(e);
}
}

public static String getNormalizedHttpPath(String path) {
String normalized = HttpUtils.normalizePath(path);

int splitIndex = normalized.indexOf('?');
if (splitIndex != -1) {
normalized = normalized.substring(0, splitIndex);
}

if (normalized.charAt(normalized.length() - 1) == '/') {
normalized = normalized.substring(0, normalized.length() - 1);
}
normalized = normalized.toLowerCase();

return normalized;
}
}
22 changes: 16 additions & 6 deletions src/main/java/com/uid2/shared/util/HTTPPathMetricFilter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.uid2.shared.util;

import io.vertx.core.http.impl.HttpUtils;
import com.uid2.shared.Utils;
import java.util.Set;

public class HTTPPathMetricFilter {
public static String filterPath(String actualPath, Set<String> pathSet) {
try {
String normalized = HttpUtils.normalizePath(actualPath).split("\\?")[0];
if (normalized.charAt(normalized.length() - 1) == '/') {
normalized = normalized.substring(0, normalized.length() - 1);
}
normalized = normalized.toLowerCase();
String normalized = Utils.getNormalizedHttpPath(actualPath);

if (pathSet == null || pathSet.isEmpty()) { return normalized; }

Expand All @@ -25,4 +21,18 @@ public static String filterPath(String actualPath, Set<String> pathSet) {
return "/parsing_error";
}
}

public static String filterPathWithoutPathParameters(String actualPath, Set<String> pathSet) {
try {
String normalized = Utils.getNormalizedHttpPath(actualPath);

if (pathSet == null || pathSet.isEmpty()) { return normalized; }

if (pathSet.contains(normalized)) { return normalized; }

return "/unknown";
} catch (IllegalArgumentException e) {
return "/parsing_error";
}
}
}
48 changes: 47 additions & 1 deletion src/test/java/com/uid2/shared/util/HTTPPathMetricFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

public class HTTPPathMetricFilterTest {
public class HTTPPathMetricFilterTest {
/* filterPathTests */
final Set<String> pathSet = Set.of("/v1/identity/map", "/token/refresh", "/list", "/list/:siteId/:keyId");

@ParameterizedTest
Expand Down Expand Up @@ -54,4 +55,49 @@ void testPathFiltering_ValidPaths_KnownEndpoints(String actualPath, String expec
String filteredPath = HTTPPathMetricFilter.filterPath(actualPath, pathSet);
assertEquals(expectedFilteredPath, filteredPath);
}

/* filterPathWithoutPathParameters tests */
final Set<String> pathSetWithoutParams = Set.of("/v1/identity/map", "/token/refresh", "/list");

@ParameterizedTest
@ValueSource(strings = {
"",
"/",
"/unknown-path",
"../",
"/v1/identity/map%55",
"/list/123",
})
void testPathFilteringWithoutPathParameters_InvalidPaths_Unknown(String actualPath) {
String filteredPath = HTTPPathMetricFilter.filterPathWithoutPathParameters(actualPath, pathSetWithoutParams);
assertEquals("/unknown", filteredPath);
}

@ParameterizedTest
@ValueSource(strings = {
"v1/identity/map?id=bad-escape-code%2",
"token/refresh?refresh_token=SOME_TOKEN<%=7485*4353%>",
"list/12%4/5435"
})
void testPathFilteringWithoutPathParameters_InvalidPaths_ParsingError(String actualPath) {
String filteredPath = HTTPPathMetricFilter.filterPathWithoutPathParameters(actualPath, pathSetWithoutParams);
assertEquals("/parsing_error", filteredPath);
}

@ParameterizedTest
@CsvSource(value = {
"/v1/identity/map, /v1/identity/map",
"v1/identity/map, /v1/identity/map",
"V1/IdenTity/mAp, /v1/identity/map",
"./v1//identity//map/, /v1/identity/map",
"../v1/identity/./map, /v1/identity/map",
"/v1/identity/new/path/../../map, /v1/identity/map",
"token/refresh?refresh_token=123%20%23, /token/refresh",
"v1/identity/map?identity/../map/, /v1/identity/map",
"/list, /list"
})
void testPathFilteringWithoutPathParameters_ValidPaths_KnownEndpoints(String actualPath, String expectedFilteredPath) {
String filteredPath = HTTPPathMetricFilter.filterPathWithoutPathParameters(actualPath, pathSetWithoutParams);
assertEquals(expectedFilteredPath, filteredPath);
}
}
Loading