如何将数组作为参数从node.js传递给Vertica查询?

问题描述 投票:0回答:2

我正在尝试对 vertica 数据库执行 sql 查询。到目前为止有效。但为了防止sql注入,我想使用参数化查询。看起来 vertica 支持参数为

?
(与 postgres 的
$1, $2, ...
相比)

所以参数有效,但不是如果参数是值数组(在

IN (...)
条件下使用)

知道如何解决这个问题吗?


假设我有一个用户 ID 和名称列表:

const userIds = [1, 2, 3];
const name = 'robert'

postgres 数据库(工作!)

使用

pg
包:

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]);

使用

postgres

const sql = postgres({ /* postgres db config */ });
const rows = await sql`
  SELECT * FROM users WHERE first_name = ${name} AND user_id = ANY(${userIds});
`;

vertica 数据库(不工作)

仅当

userIds
作为单个值传递时才有效,而不是 1+ 值的数组

使用

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"}"

使用

vertica

似乎根本不支持参数,只是提供了一个函数(
quote
)来在字符串插值之前清理它们。

使用

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"}"

使用

postgres

(似乎根本不支持连接到vertica数据库)

const sql = postgres({ /* vertica db config */ });
const rows = await sql`
  SELECT * FROM users;
`;

// -> Schema "pg_catalog" does not exist

我也尝试了这些变体而不是

user_id IN (?)

  • user_id IN (?::int[])
    -> 运算符不存在:int = array[int]
  • user_id = ANY (?)
    -> 类型“Int8Array1D”不存在
  • user_id = ANY (?::int[])
    -> 类型“Int8Array1D”不存在
node.js postgresql sql-injection vertica node-postgres
2个回答
1
投票

尝试

ANY (ARRAY [$1])
。 我不知道
node.js
或 SQL 注入,但在 bash shell 中它似乎可以工作:

marco ~/1/Vertica/supp $ cat test.sh      
#!/usr/bin/env zsh
vsql -c "
SELECT cust_id,cust_from_dt,cust_fname,cust_lname 
FROM scd.d_customer_scd 
WHERE cust_id = ANY(ARRAY[$1]) 
ORDER BY 1,2"
marco ~/1/Vertica/supp $ ./test.sh 1,2,3
 cust_id | cust_from_dt | cust_fname | cust_lname 
---------+--------------+------------+------------
       1 | 2021-12-05   | Arthur     | Dent
       1 | 2021-12-15   | Arthur     | Dent
       1 | 2021-12-22   | Arthur     | Dent
       1 | 2021-12-29   | Arthur     | Dent
       2 | 2021-12-05   | Ford       | Prefect
       3 | 2021-11-05   | Zaphod     | Beeblebrox
       3 | 2021-12-15   | Zaphod     | Beeblebrox
       3 | 2021-12-22   | Zaphod     | Beeblebrox
       3 | 2021-12-29   | Zaphod     | Beeblebrox
(9 rows)

marco ~/1/Vertica/supp $ 

0
投票

类似的东西

const res = await pool.query(`
  SELECT * FROM users WHERE first_name = ? AND user_id IN (${'?,'.repeat(userIds.length).slice(0, -1)});
`, [name, ...userIds]);

应该有用😂

© www.soinside.com 2019 - 2024. All rights reserved.