mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(rds-data): refuse a database name that would change the JDBC URL (#8927)
This commit is contained in:
1 parent
563de77b1e
commit
fa965882ca
5 files changed
+145
-3
No files matched your search
@@ -4,6 +4,8 @@ import io.floci.aws.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.floci.aws.services.rds.model.DatabaseEngine;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
@@ -31,11 +33,22 @@ class RdsDataConnectionFactory {
|
||||
return DriverManager.getConnection(url, props);
|
||||
}
|
||||
|
||||
/** The longest database name the RDS Data API accepts ({@code DbName}, at most 64 characters). */
|
||||
static final int MAX_DATABASE_NAME_LENGTH = 64;
|
||||
|
||||
/**
|
||||
* The JDBC URL for {@code database}, the name percent-encoded so that both drivers read back exactly
|
||||
* the name given and nothing in it can start, add or override a connection property, as AWS passes
|
||||
* the name to the engine as an identifier. A name the engine does not know is then the engine's
|
||||
* error. {@link URLEncoder} writes a literal {@code +} as {@code %2B} and a space as {@code +}, which
|
||||
* is replaced by {@code %20}: both drivers decode a bare {@code +} in the path as a space.
|
||||
*/
|
||||
static String buildUrl(DatabaseEngine engine, String host, int port, String database) {
|
||||
String path = URLEncoder.encode(database, StandardCharsets.UTF_8).replace("+", "%20");
|
||||
return switch (engine) {
|
||||
case MYSQL, MARIADB -> "jdbc:mysql://" + host + ":" + port + "/" + database
|
||||
case MYSQL, MARIADB -> "jdbc:mysql://" + host + ":" + port + "/" + path
|
||||
+ "?useSSL=false&allowPublicKeyRetrieval=true";
|
||||
case POSTGRES -> "jdbc:postgresql://" + host + ":" + port + "/" + database
|
||||
case POSTGRES -> "jdbc:postgresql://" + host + ":" + port + "/" + path
|
||||
+ "?sslmode=disable";
|
||||
case SQLSERVER -> throw new IllegalArgumentException(
|
||||
"RDS Data API is not supported for SQL Server");
|
||||
|
||||
@@ -190,6 +190,7 @@ public class RdsDataService implements Resettable {
|
||||
}
|
||||
|
||||
public ObjectNode executeStatement(JsonNode request, String region) {
|
||||
validateRequestDatabase(request);
|
||||
rejectUnsupportedOptions(request);
|
||||
|
||||
String sql = requiredText(request, "sql");
|
||||
@@ -205,6 +206,7 @@ public class RdsDataService implements Resettable {
|
||||
}
|
||||
|
||||
public ObjectNode batchExecuteStatement(JsonNode request, String region) {
|
||||
validateRequestDatabase(request);
|
||||
String sql = requiredText(request, "sql");
|
||||
List<Map<String, JsonNode>> parameterSets = parseParameterSets(request);
|
||||
|
||||
@@ -217,6 +219,7 @@ public class RdsDataService implements Resettable {
|
||||
}
|
||||
|
||||
public ObjectNode beginTransaction(JsonNode request, String region) {
|
||||
validateRequestDatabase(request);
|
||||
cleanupExpiredTransactions();
|
||||
String resourceArn = requiredText(request, "resourceArn");
|
||||
requiredText(request, "secretArn");
|
||||
@@ -699,7 +702,7 @@ public class RdsDataService implements Resettable {
|
||||
|
||||
private String databaseName(JsonNode request, RdsDataResourceResolver.DatabaseTarget target) {
|
||||
String database = textOrNull(request, "database");
|
||||
if (database != null && !database.isBlank()) {
|
||||
if (database != null && !database.isEmpty()) {
|
||||
return database;
|
||||
}
|
||||
if (target.databaseName() != null && !target.databaseName().isBlank()) {
|
||||
@@ -708,6 +711,23 @@ public class RdsDataService implements Resettable {
|
||||
throw new AwsException("BadRequestException", "database is required.", 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a request's {@code database} longer than the model's {@code DbName} allows. Runs first, as
|
||||
* AWS checks the request's shape before it looks up the resource. Any other name is the engine's to
|
||||
* judge: {@link RdsDataConnectionFactory#buildUrl} encodes it so it cannot change the connection.
|
||||
*/
|
||||
private static void validateRequestDatabase(JsonNode request) {
|
||||
String database = textOrNull(request, "database");
|
||||
// Only an absent or empty name means "the resource's"; one of spaces is checked like any other.
|
||||
if (database == null || database.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (database.length() > RdsDataConnectionFactory.MAX_DATABASE_NAME_LENGTH) {
|
||||
throw new AwsException("BadRequestException", "database must be at most "
|
||||
+ RdsDataConnectionFactory.MAX_DATABASE_NAME_LENGTH + " characters.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private TransactionContext transaction(String transactionId) {
|
||||
cleanupExpiredTransactions();
|
||||
TransactionContext tx = transactions.get(transactionId);
|
||||
|
||||
Reference in new issue
Block a user