Proper insertion of table name

前端 未结 3 1808
太阳男子
太阳男子 2020-12-20 13:46

How does one correctly provide the table name if the name can be dynamically determined and still prevent SQL injection attacks? I am using node-postgres.

For example

3条回答
  •  执笔经年
    2020-12-20 14:23

    Any good library should provide proper escaping for SQL names, which include:

    • schema name
    • table name
    • column name

    For example, within pg-promise you would use it like this:

    db.query("INSERT INTO $1~ VALUES ($2, $3, $4)", [table_name, value_a, value_b, value_c])
    

    i.e. you get your table name properly escaped by appending the variable with ~, which in turn makes it safe from SQL injection.

    From here, a simple escaping for table names executed by the library:

    return '"' + name.replace(/"/g, '""') + '"';
    

    See also: SQL Names

提交回复
热议问题