-
Notifications
You must be signed in to change notification settings - Fork 156
add validation for name and description for model model group and connector resources #3805
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
base: main
Are you sure you want to change the base?
Changes from all commits
a6923be
989edf5
3f0f81a
460af00
5dfb719
2bed532
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
|
||
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; | ||
|
@@ -28,6 +30,7 @@ | |
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; | ||
|
@@ -60,6 +63,9 @@ public class StringUtils { | |
+ " return input;" | ||
+ "\n }\n"; | ||
|
||
// Regex allows letters, digits, spaces, hyphens, underscores, and dots. | ||
private static final String SAFE_INPUT_REGEX = "^[a-zA-Z0-9 _\\-\\.:,'()]+$"; | ||
|
||
public static final Gson gson; | ||
|
||
static { | ||
|
@@ -497,4 +503,44 @@ public static String hashString(String input) { | |
} | ||
} | ||
|
||
/** | ||
* 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) { | ||
if (value == null || value.isBlank()) { | ||
return false; | ||
} | ||
return value.matches(SAFE_INPUT_REGEX); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pattern SAFE_TEXT_PATTERN = Pattern.compile(SAFE_INPUT_REGEX); Using Pattern matcher is more efficient as it only Compile pattern once rather than Compiles pattern every time in String.matches(): |
||
} | ||
|
||
/** | ||
* 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, String> fields) { | ||
ActionRequestValidationException exception = null; | ||
|
||
for (Map.Entry<String, String> entry : fields.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
|
||
if (value != null && !isSafeText(value)) { | ||
exception = addValidationError( | ||
key + " can only contain letters, digits, spaces, underscores (_), hyphens (-), dots (.), and colons (:)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve the performance, using StringBuilder msgBuilder = new StringBuilder() for error messages will reduce the memory usage. |
||
exception | ||
); | ||
} | ||
} | ||
|
||
return exception; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious how we determined the regex is this a standard regex generally used? I believe we want to prevent
<, >, ;, ", /, \, =
.Since we are using this for description, maybe we need to allow
!, @
etc?