refactor(dsql): quote the rewriter's and bootstrap's literals with PostgresWire.quoteLiteral (#8933)

* refactor(dsql): quote the rewriter's and bootstrap's literals with PostgresWire.quoteLiteral
This commit is contained in:
Joaquín Gamboa authored and GitHub committed 2026-11-18 21:53:39 -04:00
1 parent b5f29c8434
commit f383d774c5
5 files changed
+39 -42

No files matched your search

@@ -11,8 +11,8 @@ import java.nio.charset.StandardCharsets;
* Renders the DSQL bootstrap script for the database, owner role and admin role of one cluster.
* The script's placeholders: {@code {{database}}}, {@code {{owner}}} and {@code {{admin}}} as
* identifiers, always quoted; {@code {{owner_literal}}}, {@code {{admin_literal}}},
* {@code {{connect_role_literal}}} and {@code {{role_prefix}}} as SQL string literals
* ({@code 'dabc_'}, or {@code ''} for no prefix); {@code {{external}}} as the SQL boolean
* {@code {{connect_role_literal}}} and {@code {{role_prefix}}} as SQL escape-string literals
* ({@code E'dabc_'}, or {@code E''} for no prefix); {@code {{external}}} as the SQL boolean
* {@code true} or {@code false}.
*/
final class DsqlBootstrap {
@@ -37,12 +37,12 @@ final class DsqlBootstrap {
}
return template()
.replace("{{database}}", PostgresWire.quoteIdentifier(database))
.replace("{{owner_literal}}", literal(owner))
.replace("{{owner_literal}}", PostgresWire.quoteLiteral(owner))
.replace("{{owner}}", PostgresWire.quoteIdentifier(owner))
.replace("{{admin_literal}}", literal(admin))
.replace("{{admin_literal}}", PostgresWire.quoteLiteral(admin))
.replace("{{admin}}", PostgresWire.quoteIdentifier(admin))
.replace("{{connect_role_literal}}", literal(connectRole))
.replace("{{role_prefix}}", literal(rolePrefix))
.replace("{{connect_role_literal}}", PostgresWire.quoteLiteral(connectRole))
.replace("{{role_prefix}}", PostgresWire.quoteLiteral(rolePrefix))
.replace("{{external}}", Boolean.toString(external));
}
@@ -56,8 +56,4 @@ final class DsqlBootstrap {
throw new UncheckedIOException(e);
}
}
private static String literal(String value) {
return "'" + value.replace("'", "''") + "'";
}
}
@@ -1,5 +1,7 @@
package io.floci.aws.services.dsql.dataplane;
import io.floci.aws.core.common.PostgresWire;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -117,8 +119,8 @@ public final class DsqlStatements {
/** {@code sys._reject} raising {@code state} with {@code message}, as a statement. */
public static String reject(String state, String message, String detail) {
return "SELECT sys._reject(" + literal(state) + ", " + literal(message)
+ (detail == null ? "" : ", " + literal(detail)) + ")";
return "SELECT sys._reject(" + PostgresWire.quoteLiteral(state) + ", " + PostgresWire.quoteLiteral(message)
+ (detail == null ? "" : ", " + PostgresWire.quoteLiteral(detail)) + ")";
}
/**
@@ -348,8 +350,8 @@ public final class DsqlStatements {
if (roleName == null) {
return null;
}
String call = "CALL sys.aws_iam_" + (grant ? "grant" : "revoke") + "(" + literal(roleName) + ", "
+ literal(tokens.get(5).value()) + ")";
String call = "CALL sys.aws_iam_" + (grant ? "grant" : "revoke") + "("
+ PostgresWire.quoteLiteral(roleName) + ", " + PostgresWire.quoteLiteral(tokens.get(5).value()) + ")";
return new Statement(call, grant ? "AWS IAM GRANT" : "AWS IAM REVOKE");
}
@@ -387,7 +389,8 @@ public final class DsqlStatements {
}
String table = text.substring(tokens.get(tableStart).start() - base, tokens.get(at - 1).end() - base);
String call = "SELECT job_id FROM sys.create_index_async(" + dollarQuoted(statement) + ", "
+ (indexName == null ? "NULL" : literal(indexName)) + ", " + literal(table) + ")";
+ (indexName == null ? "NULL" : PostgresWire.quoteLiteral(indexName)) + ", "
+ PostgresWire.quoteLiteral(table) + ")";
return new Statement(call, null);
}
@@ -514,7 +517,7 @@ public final class DsqlStatements {
if (specifier || name.type() != Type.WORD && name.type() != Type.QUOTED_IDENTIFIER) {
return null;
}
names.add(literal(identifier(name)));
names.add(PostgresWire.quoteLiteral(identifier(name)));
if (i + 1 < tokens.size() && !",".equals(tokens.get(i + 1).text())) {
return null;
}
@@ -1216,10 +1219,6 @@ public final class DsqlStatements {
};
}
static String literal(String value) {
return "'" + value.replace("'", "''") + "'";
}
private static String dollarQuoted(String value) {
String tag = "$dsql$";
int n = 0;