Skip to content

fix bug 3860 for no class issue #3861

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

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.common.transport.connector;

import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.ml.common.utils.StringUtils.validateFields;
import static org.opensearch.ml.common.utils.Validator.validateFields;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.common.transport.connector;

import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.ml.common.utils.StringUtils.validateFields;
import static org.opensearch.ml.common.utils.Validator.validateFields;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.common.transport.model;

import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.ml.common.utils.StringUtils.validateFields;
import static org.opensearch.ml.common.utils.Validator.validateFields;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.common.transport.model_group;

import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.ml.common.utils.StringUtils.validateFields;
import static org.opensearch.ml.common.utils.Validator.validateFields;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.common.transport.model_group;

import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.ml.common.utils.StringUtils.validateFields;
import static org.opensearch.ml.common.utils.Validator.validateFields;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.common.transport.register;

import static org.opensearch.action.ValidateActions.addValidationError;
import static org.opensearch.ml.common.utils.StringUtils.validateFields;
import static org.opensearch.ml.common.utils.Validator.validateFields;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package org.opensearch.ml.common.utils;

import static org.opensearch.action.ValidateActions.addValidationError;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
Expand All @@ -30,7 +28,6 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.opensearch.OpenSearchParseException;
import org.opensearch.action.ActionRequestValidationException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -63,11 +60,6 @@ public class StringUtils {
+ " return input;"
+ "\n }\n";

// Regex allows letters, digits, spaces, hyphens, underscores, and dots.
private static final Pattern SAFE_INPUT_PATTERN = Pattern.compile("^[\\p{L}\\p{N}\\s.,!?():@\\-_/'\"]*$");

public static final String SAFE_INPUT_DESCRIPTION = "can only contain letters, numbers, spaces, and basic punctuation (.,!?():@-_'/\")";

public static final Gson gson;

static {
Expand Down Expand Up @@ -505,51 +497,4 @@ public static String hashString(String input) {
}
}

/**
* Validates a map of fields to ensure that their values only contain allowed characters.
* <p>
* Allowed characters are: letters, digits, spaces, underscores (_), hyphens (-), dots (.), and colons (:).
* If a value does not comply, a validation error is added.
*
* @param fields A map where the key is the field name (used for error messages) and the value is the text to validate.
* @return An {@link ActionRequestValidationException} containing all validation errors, or {@code null} if all fields are valid.
*/
public static ActionRequestValidationException validateFields(Map<String, FieldDescriptor> fields) {
ActionRequestValidationException exception = null;

for (Map.Entry<String, FieldDescriptor> entry : fields.entrySet()) {
String key = entry.getKey();
FieldDescriptor descriptor = entry.getValue();
String value = descriptor.getValue();

if (descriptor.isRequired()) {
if (!isSafeText(value)) {
String reason = (value == null || value.isBlank()) ? "is required and cannot be null or blank" : SAFE_INPUT_DESCRIPTION;
exception = addValidationError(key + " " + reason, exception);
}
} else {
if (value != null && !value.isBlank() && !matchesSafePattern(value)) {
exception = addValidationError(key + " " + SAFE_INPUT_DESCRIPTION, exception);
}
}
}

return exception;
}

/**
* Checks if the input is safe (non-null, non-blank, matches safe character set).
*
* @param value The input string to validate
* @return true if input is safe, false otherwise
*/
public static boolean isSafeText(String value) {
return value != null && !value.isBlank() && matchesSafePattern(value);
}

// Just checks pattern
public static boolean matchesSafePattern(String value) {
return SAFE_INPUT_PATTERN.matcher(value).matches();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.utils;

import static org.opensearch.action.ValidateActions.addValidationError;

import java.util.Map;
import java.util.regex.Pattern;

import org.opensearch.action.ActionRequestValidationException;

public class Validator {

Check warning on line 15 in common/src/main/java/org/opensearch/ml/common/utils/Validator.java

View check run for this annotation

Codecov / codecov/patch

common/src/main/java/org/opensearch/ml/common/utils/Validator.java#L15

Added line #L15 was not covered by tests
public static final String SAFE_INPUT_DESCRIPTION = "can only contain letters, numbers, spaces, and basic punctuation (.,!?():@-_'/\")";
// Regex allows letters, digits, spaces, hyphens, underscores, and dots.
static final Pattern SAFE_INPUT_PATTERN = Pattern.compile("^[\\p{L}\\p{N}\\s.,!?():@\\-_/'\"]*$");

/**
* Validates a map of fields to ensure that their values only contain allowed characters.
* <p>
* Allowed characters are: letters, digits, spaces, underscores (_), hyphens (-), dots (.), and colons (:).
* If a value does not comply, a validation error is added.
*
* @param fields A map where the key is the field name (used for error messages) and the value is the text to validate.
* @return An {@link ActionRequestValidationException} containing all validation errors, or {@code null} if all fields are valid.
*/
public static ActionRequestValidationException validateFields(Map<String, FieldDescriptor> fields) {
ActionRequestValidationException exception = null;

for (Map.Entry<String, FieldDescriptor> entry : fields.entrySet()) {
String key = entry.getKey();
FieldDescriptor descriptor = entry.getValue();
String value = descriptor.getValue();

if (descriptor.isRequired()) {
if (!isSafeText(value)) {
String reason = (value == null || value.isBlank()) ? "is required and cannot be null or blank" : SAFE_INPUT_DESCRIPTION;
exception = addValidationError(key + " " + reason, exception);
}
} else {
if (value != null && !value.isBlank() && !matchesSafePattern(value)) {
exception = addValidationError(key + " " + SAFE_INPUT_DESCRIPTION, exception);
}
}
}

return exception;
}

/**
* Checks if the input is safe (non-null, non-blank, matches safe character set).
*
* @param value The input string to validate
* @return true if input is safe, false otherwise
*/
public static boolean isSafeText(String value) {
return value != null && !value.isBlank() && matchesSafePattern(value);
}

// Just checks pattern
public static boolean matchesSafePattern(String value) {
return SAFE_INPUT_PATTERN.matcher(value).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.opensearch.ml.common.utils.StringUtils.SAFE_INPUT_DESCRIPTION;
import static org.opensearch.ml.common.utils.Validator.SAFE_INPUT_DESCRIPTION;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.opensearch.ml.common.utils.StringUtils.SAFE_INPUT_DESCRIPTION;
import static org.opensearch.ml.common.utils.Validator.SAFE_INPUT_DESCRIPTION;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.opensearch.ml.common.utils.StringUtils.SAFE_INPUT_DESCRIPTION;
import static org.opensearch.ml.common.utils.Validator.SAFE_INPUT_DESCRIPTION;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.opensearch.ml.common.utils.StringUtils.SAFE_INPUT_DESCRIPTION;
import static org.opensearch.ml.common.utils.Validator.SAFE_INPUT_DESCRIPTION;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.opensearch.ml.common.utils.StringUtils.SAFE_INPUT_DESCRIPTION;
import static org.opensearch.ml.common.utils.Validator.SAFE_INPUT_DESCRIPTION;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.opensearch.ml.common.transport.register;

import static org.junit.Assert.*;
import static org.opensearch.ml.common.utils.StringUtils.SAFE_INPUT_DESCRIPTION;
import static org.opensearch.ml.common.utils.Validator.SAFE_INPUT_DESCRIPTION;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,16 +754,16 @@ public void testValidateSchema() throws IOException {

@Test
public void testIsSafeText_ValidInputs() {
assertTrue(StringUtils.isSafeText("Model-Name_1.0"));
assertTrue(StringUtils.isSafeText("This is a description:"));
assertTrue(StringUtils.isSafeText("Name_with-dots.and:colons"));
assertTrue(Validator.isSafeText("Model-Name_1.0"));
assertTrue(Validator.isSafeText("This is a description:"));
assertTrue(Validator.isSafeText("Name_with-dots.and:colons"));
}

@Test
public void testValidateFields_AllValid() {
Map<String, FieldDescriptor> fields = Map
.of("Field1", new FieldDescriptor("Valid Name 1", true), "Field2", new FieldDescriptor("Another_Valid-Field.Name:Here", true));
assertNull(StringUtils.validateFields(fields));
assertNull(Validator.validateFields(fields));
}

@Test
Expand All @@ -777,47 +777,47 @@ public void testValidateFields_OptionalFieldsValidWhenBlank() {
"OptionalField3",
new FieldDescriptor(null, false)
);
assertNull(StringUtils.validateFields(fields));
assertNull(Validator.validateFields(fields));
}

@Test
public void testValidateFields_OptionalFieldInvalidPattern() {
Map<String, FieldDescriptor> fields = Map.of("OptionalField1", new FieldDescriptor("Bad@Value$", false));
ActionRequestValidationException exception = StringUtils.validateFields(fields);
ActionRequestValidationException exception = Validator.validateFields(fields);
assertNotNull(exception);
assertTrue(exception.getMessage().contains("OptionalField1"));
}

@Test
public void testIsSafeText_AdvancedValidInputs() {
// Testing all allowed characters
assertTrue(StringUtils.isSafeText("Hello World")); // spaces
assertTrue(StringUtils.isSafeText("Hello.World")); // period
assertTrue(StringUtils.isSafeText("Hello,World")); // comma
assertTrue(StringUtils.isSafeText("Hello!World")); // exclamation
assertTrue(StringUtils.isSafeText("Hello?World")); // question mark
assertTrue(StringUtils.isSafeText("Hello(World)")); // parentheses
assertTrue(StringUtils.isSafeText("Hello:World")); // colon
assertTrue(StringUtils.isSafeText("Hello@World")); // at sign
assertTrue(StringUtils.isSafeText("Hello-World")); // hyphen
assertTrue(StringUtils.isSafeText("Hello_World")); // underscore
assertTrue(StringUtils.isSafeText("Hello'World")); // single quote
assertTrue(StringUtils.isSafeText("Hello\"World")); // double quote
assertTrue(Validator.isSafeText("Hello World")); // spaces
assertTrue(Validator.isSafeText("Hello.World")); // period
assertTrue(Validator.isSafeText("Hello,World")); // comma
assertTrue(Validator.isSafeText("Hello!World")); // exclamation
assertTrue(Validator.isSafeText("Hello?World")); // question mark
assertTrue(Validator.isSafeText("Hello(World)")); // parentheses
assertTrue(Validator.isSafeText("Hello:World")); // colon
assertTrue(Validator.isSafeText("Hello@World")); // at sign
assertTrue(Validator.isSafeText("Hello-World")); // hyphen
assertTrue(Validator.isSafeText("Hello_World")); // underscore
assertTrue(Validator.isSafeText("Hello'World")); // single quote
assertTrue(Validator.isSafeText("Hello\"World")); // double quote
}

@Test
public void testIsSafeText_AdvancedInvalidInputs() {
// Testing specifically excluded characters
assertFalse(StringUtils.isSafeText("Hello<World")); // less than
assertFalse(StringUtils.isSafeText("Hello>World")); // greater than
assertTrue(StringUtils.isSafeText("Hello/World")); // forward slash
assertFalse(StringUtils.isSafeText("Hello\\World")); // backslash
assertFalse(StringUtils.isSafeText("Hello&World")); // ampersand
assertFalse(StringUtils.isSafeText("Hello+World")); // plus
assertFalse(StringUtils.isSafeText("Hello=World")); // equals
assertFalse(StringUtils.isSafeText("Hello;World")); // semicolon
assertFalse(StringUtils.isSafeText("Hello|World")); // pipe
assertFalse(StringUtils.isSafeText("Hello*World")); // asterisk
assertFalse(Validator.isSafeText("Hello<World")); // less than
assertFalse(Validator.isSafeText("Hello>World")); // greater than
assertTrue(Validator.isSafeText("Hello/World")); // forward slash
assertFalse(Validator.isSafeText("Hello\\World")); // backslash
assertFalse(Validator.isSafeText("Hello&World")); // ampersand
assertFalse(Validator.isSafeText("Hello+World")); // plus
assertFalse(Validator.isSafeText("Hello=World")); // equals
assertFalse(Validator.isSafeText("Hello;World")); // semicolon
assertFalse(Validator.isSafeText("Hello|World")); // pipe
assertFalse(Validator.isSafeText("Hello*World")); // asterisk
}

@Test
Expand All @@ -828,7 +828,7 @@ public void testValidateFields_RequiredFields_MissingOrInvalid() {
fields.put("RequiredField3", new FieldDescriptor("Bad@#Char$", true));
fields.put("RequiredField4", new FieldDescriptor(null, true));

ActionRequestValidationException exception = StringUtils.validateFields(fields);
ActionRequestValidationException exception = Validator.validateFields(fields);
assertNotNull(exception);
String message = exception.getMessage();
assertTrue(message.contains("RequiredField1"));
Expand All @@ -840,20 +840,20 @@ public void testValidateFields_RequiredFields_MissingOrInvalid() {
@Test
public void testValidateFields_EmptyMap() {
Map<String, FieldDescriptor> fields = new HashMap<>();
assertNull(StringUtils.validateFields(fields));
assertNull(Validator.validateFields(fields));
}

@Test
public void testValidateFields_UnicodeLettersAndNumbers() {
Map<String, FieldDescriptor> fields = Map
.of("field1", new FieldDescriptor("Hello世界123", true), "field2", new FieldDescriptor("Café42", true));
assertNull(StringUtils.validateFields(fields));
assertNull(Validator.validateFields(fields));
}

@Test
public void testValidateFields_InvalidCharacterSet() {
Map<String, FieldDescriptor> fields = Map.of("Field1", new FieldDescriptor("Bad#Value$With^Weird*Chars", true));
ActionRequestValidationException exception = StringUtils.validateFields(fields);
ActionRequestValidationException exception = Validator.validateFields(fields);
assertNotNull(exception);
assertTrue(exception.getMessage().contains("Field1"));
}
Expand Down
Loading