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

Replace Pattern with CharMatcher for valid/clean key checks #20528

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions graylog2-server/src/main/java/org/graylog2/plugin/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.codahale.metrics.Meter;
import com.eaio.uuid.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
Expand Down Expand Up @@ -64,9 +65,10 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.google.common.base.CharMatcher.anyOf;
import static com.google.common.base.CharMatcher.inRange;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static org.graylog.schema.GraylogSchemaFields.FIELD_ILLUMINATE_EVENT_CATEGORY;
Expand Down Expand Up @@ -216,10 +218,9 @@ public class Message implements Messages, Indexable, Acknowledgeable {
public static final String FIELD_GL2_SOURCE_RADIO_INPUT = "gl2_source_radio_input";

// Matches whole field names containing a-z, A-Z, 0-9, period char, -, or @.
private static final Pattern VALID_KEY_CHARS = Pattern.compile("^[\\w\\.\\-@]*$");
// Same as above, but matches only the invalid (non-indicated) characters.
// [^ ... ] around the pattern inverts the match.
private static final Pattern INVALID_KEY_CHARS = Pattern.compile("[^\\w\\.\\-@]");
private static final CharMatcher VALID_KEY_CHAR_MATCHER = inRange('a', 'z').or(inRange('A', 'Z')).or(inRange('0', '9')).or(anyOf(".@-_")).precomputed();
private static final CharMatcher INVALID_KEY_CHAR_MATCHER = VALID_KEY_CHAR_MATCHER.negate().precomputed();

private static final char KEY_REPLACEMENT_CHAR = '_';

private static final ImmutableSet<String> GRAYLOG_FIELDS = ImmutableSet.of(
Expand Down Expand Up @@ -652,11 +653,11 @@ public long getSize() {
}

public static boolean validKey(final String key) {
return VALID_KEY_CHARS.matcher(key).matches();
return VALID_KEY_CHAR_MATCHER.matchesAllOf(key);
}

public static String cleanKey(final String key) {
return INVALID_KEY_CHARS.matcher(key).replaceAll(String.valueOf(KEY_REPLACEMENT_CHAR));
return INVALID_KEY_CHAR_MATCHER.replaceFrom(key, KEY_REPLACEMENT_CHAR);
}

public void addFields(final Map<String, Object> fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ public void testCleanKey() throws Exception {
assertEquals("foo_bar", Message.cleanKey("foo{bar"));
assertEquals("foo_bar", Message.cleanKey("foo,bar"));
assertEquals("foo_bar", Message.cleanKey("foo?bar"));
assertEquals("foo___bar", Message.cleanKey("foo +?bar"));
assertEquals("_", Message.cleanKey(" "));
}

Expand Down
Loading