Skip to content

feat: increase raw query length limit #20

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

Merged
merged 4 commits into from
Nov 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/com/topcoder/dal/DBAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private Row rawQueryMapper(ResultSet rs, int rowNum) throws SQLException {
case java.sql.Types.BIGINT -> valueBuilder.setLongValue(rs.getLong(i + 1));
case java.sql.Types.FLOAT -> valueBuilder.setFloatValue(rs.getFloat(i + 1));
case java.sql.Types.DOUBLE -> valueBuilder.setDoubleValue(rs.getDouble(i + 1));
case java.sql.Types.VARCHAR ->
case java.sql.Types.VARCHAR, java.sql.Types.CHAR ->
valueBuilder.setStringValue(Objects.requireNonNullElse(rs.getString(i + 1), ""));
case java.sql.Types.BOOLEAN -> valueBuilder.setBooleanValue(rs.getBoolean(i + 1));
case java.sql.Types.DATE, java.sql.Types.TIMESTAMP -> valueBuilder
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/topcoder/dal/util/QueryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,24 @@ public static String sanitizeSQLStatement(String sql) {
}

// Limit the length of the SQL statement to prevent very long strings
if (sql.length() > 1000) {
if (sql.length() > 2000) {
throw new IllegalArgumentException("SQL statement length exceeds the allowed limit");
}

// Whitelist characters
StringBuilder safeSQL = new StringBuilder();
for (char c : sql.toCharArray()) {
if (Character.isLetterOrDigit(c) || c == ' ' || c == ',' || c == '(' || c == ')' || c == '=' || c == '<'
|| c == '>' || c == '_' || c == ':' || c == '.' || c == '-' || c == '+' || c == '*' || c == '\'') {
|| c == '>' || c == '_' || c == ':' || c == '.' || c == '-' || c == '+' || c == '*' || c == '\''
|| c == '!') {
safeSQL.append(c);
}
}
sql = safeSQL.toString();

// replace single quotes with two single quotes to prevent SQL injection through
// strings
sql = sql.replace("'", "''");
// sql = sql.replace("'", "''");

return sql;
}
Expand Down