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

ipc: avoid exception for empty paths #1092

Merged
merged 1 commit into from
Nov 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private PathSanitizer() {

/** Returns a sanitized path string for use as an endpoint tag value. */
public static String sanitize(String path) {
return sanitizeSegments(removeParameters(path), Collections.emptySet());
return sanitize(path, Collections.emptySet());
}

/**
Expand All @@ -55,7 +55,8 @@ public static String sanitize(String path) {
* Sanitized path that can be used as an endpoint tag value.
*/
public static String sanitize(String path, Set<String> allowed) {
return sanitizeSegments(removeParameters(path), allowed);
String tmp = sanitizeSegments(removeParameters(path), allowed);
return tmp.isEmpty() ? "none" : tmp;
}

private static String removeParameters(String path) {
Expand All @@ -64,13 +65,13 @@ private static String removeParameters(String path) {

private static String removeParameters(String path, char c) {
int i = path.indexOf(c);
return i > 0 ? path.substring(0, i) : path;
return i >= 0 ? path.substring(0, i) : path;
}

private static String sanitizeSegments(String path, Set<String> allowed) {
StringBuilder builder = new StringBuilder();
int length = path.length();
int pos = path.charAt(0) == '/' ? 1 : 0;
int pos = path.isEmpty() || path.charAt(0) != '/' ? 0 : 1;
int segmentsAdded = 0;

while (pos < length && segmentsAdded < 5) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ private String sanitize(String path) {
return PathSanitizer.sanitize(path);
}

@Test
public void emptyPath() {
Assertions.assertEquals("none", sanitize("/"));
Assertions.assertEquals("none", sanitize(""));
Assertions.assertEquals("none", sanitize("/?a=1"));
Assertions.assertEquals("none", sanitize("?a=1"));
}

@Test
public void uuids() {
String path = "/api/v1/foo/" + UUID.randomUUID();
Expand Down