I'm trying to execute sql queries against a vertica db. that works so far. but to prevent sql injection, I want to use parameterized queries. looks like vertica supports parameters as ? (compared to postgres' $1, $2, ...)
so the parameters work, BUT NOT if the parameter is an array of values (to use in IN (...) conditions)
any idea how to fix this?
let's say I have a list of user ids and a name:
const userIds = [1, 2, 3];
const name = 'robert'
postgres db (working!)
using pg package:
const pool = new pg.Pool({ /* config */ });
const client = await pool.connect();
const { rows } = client.query(`
SELECT * FROM users WHERE first_name = $1 AND user_id = ANY($2);
`, [name, userIds]);
using postgres:
const sql = postgres({ /* postgres db config */ });
const rows = await sql`
SELECT * FROM users WHERE first_name = ${name} AND user_id = ANY(${userIds});
`;
vertica db (NOT working)
only works if
userIdsis passed as a single value, not an array of 1+ values
using vertica-nodejs:
import Vertica from 'vertica-nodejs';
const { Pool } = Vertica;
const pool = new Pool({ /* vertica db config */ });
const res = await pool.query(`
SELECT * FROM users WHERE first_name = ? AND user_id IN (?);
`, [name, userIds]);
// -> Invalid input syntax for integer: "{"1","2","3"}"
using vertica:
doesn't seem to support parameters at all, just provides a function (quote) to sanitize them before string interpolation.
using pg:
const pool = new pg.Pool({ /* vertica db config */ });
const client = await pool.connect();
const { rows } = client.query(`
SELECT * FROM users WHERE first_name = ? AND user_id IN (?);
`, [name, userIds]);
// -> Invalid input syntax for integer: "{"1","2","3"}"
using postgres:
(doesn't seem to support connecting to a vertica db at all)
const sql = postgres({ /* vertica db config */ });
const rows = await sql`
SELECT * FROM users;
`;
// -> Schema "pg_catalog" does not exist
I also tried those variations instead of user_id IN (?):
user_id IN (?::int[])-> Operator does not exist: int = array[int]user_id = ANY (?)-> Type "Int8Array1D" does not existuser_id = ANY (?::int[])-> Type "Int8Array1D" does not exist
Try
ANY (ARRAY [$1]). I don't know aboutnode.jsor SQL injection, but in a bash shell it seems to work: