I need to run LISTEN channel_name
against a PostgreSQL database using JDBC, where channel_name
is supplied by a user in a web app.
channel_name
can be any PostgreSQL identifier.
I don't believe I can use parameterization, and there is no white-list to check against.
How can I allow a user to do so safely?
I considered regexp, but I was wondering if there was anything pre-built, as I don't want to make a mistake.
Current code (doesn't support quoted identifiers or non-ascii chars):
public String checkIdentifier(String value) {
if (!value.matches("(?i)^[a-z_][a-z_0-9\\$]{0,63}$")) {
throw new RuntimeException("Not a valid SQL identifier.");
}
return value;
}