Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#7585] feat: add DB name validation to secure DB creation (#7654)

Reuben George108176462637
diff --git a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/container/MySQLContainer.java b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/container/MySQLContainer.java
index 55f4549434..3cfb6ecc56 100644
--- a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/container/MySQLContainer.java
+++ b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/container/MySQLContainer.java
@@ -132,7 +132,13 @@ public class MySQLContainer extends BaseContainer {
DriverManager.getConnection(mySQLJdbcUrl, USER_NAME, getPassword());
Statement statement = connection.createStatement()) {
- String query = String.format("CREATE DATABASE IF NOT EXISTS %s;", testDatabaseName);
+ // validate database name to ensure it only contains safe characters
+ String databaseName = testDatabaseName.toString();
+ if (!isValidDatabaseName(databaseName)) {
+ throw new IllegalArgumentException("Invalid database name: " + databaseName);
+ }
+
+ String query = String.format("CREATE DATABASE IF NOT EXISTS `%s`;", databaseName);
// FIXME: String, which is used in SQL, can be unsafe
statement.execute(query);
LOG.info(String.format("MySQL container database %s has been created", testDatabaseName));
@@ -141,6 +147,18 @@ public class MySQLContainer extends BaseContainer {
}
}
+ private boolean isValidDatabaseName(String databaseName) {
+ if (databaseName == null || databaseName.isEmpty()) {
+ return false;
+ }
+
+ if (databaseName.length() > 64) {
+ return false;
+ }
+
+ return databaseName.matches("^[a-zA-Z0-9_$]+$");
+ }
+
public String getUsername() {
return USER_NAME;
}