Skip to content

Commit

Permalink
support allow set for PathSanitizer (#1065)
Browse files Browse the repository at this point in the history
This allows an explicit set of allowed segment strings
for some use-cases where there is a small set of known
strings that are desirable to include, but do not match
with the normal rules.
  • Loading branch information
brharrington committed Jun 22, 2023
1 parent 2273095 commit 43d1b3b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 Netflix, Inc.
* Copyright 2014-2023 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,9 @@

import com.netflix.spectator.impl.AsciiSet;

import java.util.Collections;
import java.util.Set;

/**
* Helper for sanitizing a URL path for including as the {@code ipc.endpoint} value. Makes
* a best effort to try and remove segments that tend to be variable like numbers, UUIDs,
Expand All @@ -37,7 +40,22 @@ private PathSanitizer() {

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

/**
* Returns a sanitized path string for use as an endpoint tag value.
*
* @param path
* Path from a URI that should be sanitized.
* @param allowed
* Set of allowed segment strings. This can be used to override the default rules for
* a set of known good values.
* @return
* 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);
}

private static String removeParameters(String path) {
Expand All @@ -49,7 +67,7 @@ private static String removeParameters(String path, char c) {
return i > 0 ? path.substring(0, i) : path;
}

private static String sanitizeSegments(String 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;
Expand All @@ -67,7 +85,7 @@ private static String sanitizeSegments(String path) {
}

if (!segment.isEmpty()) {
if (shouldSuppressSegment(segment))
if (shouldSuppressSegment(segment, allowed))
appendIfSpaceAvailable(builder, "-");
else
appendIfSpaceAvailable(builder, segment);
Expand All @@ -78,10 +96,10 @@ private static String sanitizeSegments(String path) {
return builder.toString();
}

private static boolean shouldSuppressSegment(String segment) {
private static boolean shouldSuppressSegment(String segment, Set<String> allowed) {
// GraphQL is a common endpoint, but hits the consonants check to detect strings
// that are likely to be random. Special case it for now.
if ("graphql".equals(segment)) {
if ("graphql".equals(segment) || allowed.contains(segment)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 Netflix, Inc.
* Copyright 2014-2023 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,9 +22,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

public class PathSanitizerTest {
Expand Down Expand Up @@ -107,6 +109,16 @@ public void graphql() {
Assertions.assertEquals("_graphql", sanitize(path));
}

@Test
public void allowSet() {
String path = "/foo-bar";
Assertions.assertEquals("_-", PathSanitizer.sanitize(path));

Set<String> allowed = Collections.singleton("foo-bar");
Assertions.assertEquals("_foo-bar", PathSanitizer.sanitize(path, allowed));
Assertions.assertEquals("_api_v1_foo-bar", PathSanitizer.sanitize("/api/v1" + path, allowed));
}

@Test
public void randomLookingString() {
String path = "/random/AOTV16LMT2";
Expand Down

0 comments on commit 43d1b3b

Please sign in to comment.