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

Bug fix: Enhanced SQL statement validation with word boundary matching #2324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -32,6 +32,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DDBQueryPassthrough implements QueryPassthroughSignature
{
Expand Down Expand Up @@ -86,10 +88,22 @@ public void customConnectorVerifications(Map<String, String> engineQptArguments)
// List of disallowed keywords
Set<String> disallowedKeywords = ImmutableSet.of("INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "ALTER");

// Check if the statement contains any disallowed keywords
for (String keyword : disallowedKeywords) {
if (upperCaseStatement.contains(keyword)) {
throw new AthenaConnectorException("Unaccepted operation; only SELECT statements are allowed. Found: " + keyword, ErrorDetails.builder().errorCode(FederationSourceErrorCode.OPERATION_NOT_SUPPORTED_EXCEPTION.toString()).build());

// Regular expression pattern to match one or more word characters
Pattern WORD_PATTERN = Pattern.compile("\\w+");

// Create a Matcher object to find all word matches in the SQL statement
Matcher matcher = WORD_PATTERN.matcher(uppercaseStatement);

// Iterate through all the word matches found by the Matcher
while (matcher.find()) {
// Get the matched word
String word = matcher.group();

// Check if the matched word is present in the disallowed keywords set
if (disallowedKeywords.contains(word)) {
// If a disallowed keyword is found, throw exception
throw new AthenaConnectorException("Unaccepted operation; only SELECT statements are allowed. Found: " + word, new ErrorDetails().withErrorCode(FederationSourceErrorCode.OperationNotSupportedException.toString()));
}
}
}
Expand Down
Loading