Skip to content

Commit

Permalink
influxdata#684: add tag checking for invalid characters
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentonPoke committed Aug 1, 2020
1 parent 125a9ca commit 1d4016b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/org/influxdb/InfluxDBException.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public boolean isRetryWorth() {
static final String USER_NOT_AUTHORIZED_ERROR = "user is not authorized to write to database";
static final String AUTHORIZATION_FAILED_ERROR = "authorization failed";
static final String USERNAME_REQUIRED_ERROR = "username required";
static final String TAGGING_INVALID_ERROR = "tag name or value failed regex check";

public static final class TaggingInvalidException extends InfluxDBException{
private TaggingInvalidException(final String message){
super(message);
}
public boolean isRetryWorth() {
return false;
}
}

public static final class DatabaseNotFoundException extends InfluxDBException {
private DatabaseNotFoundException(final String message) {
Expand Down Expand Up @@ -152,6 +162,8 @@ private static InfluxDBException buildExceptionFromErrorMessage(final String err
if (errorMessage.contains(CACHE_MAX_MEMORY_SIZE_EXCEEDED_ERROR)) {
return new CacheMaxMemorySizeExceededException(errorMessage);
}
if (errorMessage.contains(TAGGING_INVALID_ERROR))
return new TaggingInvalidException(errorMessage);
if (errorMessage.contains(USER_REQUIRED_ERROR)
|| errorMessage.contains(USER_NOT_AUTHORIZED_ERROR)
|| errorMessage.contains(AUTHORIZATION_FAILED_ERROR)
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/influxdb/dto/BatchPoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDB.ConsistencyLevel;
import org.influxdb.InfluxDBException;
import org.influxdb.dto.utils.CheckTags;

import static org.influxdb.dto.utils.CheckTags.*;

/**
* {Purpose of This Type}.
Expand All @@ -29,6 +33,7 @@ public class BatchPoints {

BatchPoints() {
// Only visible in the Builder

}

/**
Expand Down Expand Up @@ -90,7 +95,16 @@ public Builder retentionPolicy(final String policy) {
* @return the Builder instance.
*/
public Builder tag(final String tagName, final String value) {
Objects.requireNonNull(tagName, "tagName");
Objects.requireNonNull(value, "value");
if (!tagName.isEmpty()
&& !value.isEmpty()
&& CheckTags.isTagNameLegal(tagName)
&& CheckTags.isTagValueLegal(value))
this.tags.put(tagName, value);
else {
throw InfluxDBException.buildExceptionForErrorState("tag name or value failed regex check");
}
return this;
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.influxdb.BuilderException;
import org.influxdb.InfluxDBException;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import org.influxdb.annotation.TimeColumn;
import org.influxdb.dto.utils.CheckTags;
import org.influxdb.impl.Preconditions;

/**
Expand Down Expand Up @@ -116,9 +118,15 @@ public static final class Builder {
public Builder tag(final String tagName, final String value) {
Objects.requireNonNull(tagName, "tagName");
Objects.requireNonNull(value, "value");
if (!tagName.isEmpty() && !value.isEmpty()) {
if (!tagName.isEmpty()
&& !value.isEmpty()
&& CheckTags.isTagNameLegal(tagName)
&& CheckTags.isTagValueLegal(value)) {
tags.put(tagName, value);
}
else {
throw InfluxDBException.buildExceptionForErrorState("tag name or value failed regex check");
}
return this;
}

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/influxdb/dto/utils/CheckTags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.influxdb.dto.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* {Purpose of This Type}.
*
* {Other Notes Relating to This Type (Optional)}
*
* @author BrentonPoke
*
*/
public final class CheckTags {
static final String nameRegex = "([a-zA-Z0-9-_]+)";
static final String valueRegex = "[[:ascii:]]+";
static final Pattern namePattern = Pattern.compile(nameRegex, Pattern.MULTILINE);
static final Pattern valuePattern = Pattern.compile(valueRegex, Pattern.MULTILINE);

/**
* Check a single tag's name according to the corresponding regular expression.
*
* @param name
* the tag name
* @return Boolean indicating that the tag name is legal
*
*/

public static Boolean isTagNameLegal(String name){
final Matcher matcher = namePattern.matcher(name);
return matcher.groupCount() == 1 && matcher.matches();
}

/**
* Check a single tag's name according to the corresponding regular expression.
*
* @param value
* the tag value
* @return Boolean indicating that the tag value is legal
*
*/
public static Boolean isTagValueLegal(String value){
final Matcher matcher = valuePattern.matcher(value);
return matcher.matches();
}

}

0 comments on commit 1d4016b

Please sign in to comment.