node-mssql icon indicating copy to clipboard operation
node-mssql copied to clipboard

Can't use dynamic table name in es6 literal

Open priedthirdeye opened this issue 9 years ago • 12 comments

This code:

var maxRecPerPage = 5;
var tblname= 'sometable';
new sql.ConnectionPool(config).connect().then(pool => {
    return pool.query`select TOP (${maxRecPerPage}) * from ${tblname}`;
}).then(result => {
    console.dir(result)
}).catch(err => {
    // ... error checks
})

Produces the error:

Must declare the table variable "@param2".

How can I use a dynamic tablename while still benefiting from the sql injection protection provided by the es6 literal syntax?

priedthirdeye avatar Apr 06 '17 15:04 priedthirdeye

Unfortunately, there is no way to achieve both sql injection protection and dynamic t-sql.

patriksimek avatar Apr 22 '17 21:04 patriksimek

@patriksimek not sure if it's ok to revive a close issue, but:

what if we don't care about sql injection?

it's great if we need to sanitize user input, but template literals are useful in other cases where we might not care about sql injection (e.g. we're not taking using input)

what I want to be able to do is this:

sql.query`insert into ${table.name} values ( ${table.rows.map(cell => cell)} )`

which isn't possible without using the tagged template literal syntax.

for example (note parens):

sql.query(`insert into ${table.name} values ( ${table.rows.map(cell => cell)} )`)

returns an error as per https://github.com/tediousjs/node-mssql/issues/754#issuecomment-440779629

could there be a configuration option to forsake the sql injection protection in favor of being allowed to set the table name?

j-oshb avatar Nov 22 '18 13:11 j-oshb

The point is you can't parameterise a table name and the tagged templates convert the variables into parameters.

I don't see any value in allowing people to turn that off and run non-parameterised queries this way it's just a vulnerability waiting to be exploited

dhensby avatar Nov 22 '18 13:11 dhensby

@dhensby ok, I take your point.

how about exporting the function to used parameterise variables so we can mix and match?

e.g. used something like:

sql.query(`insert into ${table.name} values ( ${ sql.cast( table.rows.map(cell => cell) ) } )`)

j-oshb avatar Nov 22 '18 13:11 j-oshb

So that cast can accept an array?

dhensby avatar Nov 22 '18 13:11 dhensby

it doesn't have to, it could be used on each value inside the map.

my main point is that the library could export the cast function.

j-oshb avatar Nov 22 '18 13:11 j-oshb

ok so I've read a bit more about this and I didn't appreciate how prepared statements/parameterized queries work, I thought it was just sanitizing the data, I see now that it sends two queries, so it's a bit more than just using cast on the parameters.

I still think it would be possible/useful to be able to do this while using the table name as a variable.

I'll come back with a pull request at some point...

j-oshb avatar Nov 22 '18 14:11 j-oshb

Using variables for parts that can't be parameterised is going to be dangerous and mis-used by people if it's there... I'd proceed with great caution around this

dhensby avatar Nov 22 '18 14:11 dhensby

actually I think we can already achieve this using PreparedStatement.

j-oshb avatar Nov 22 '18 14:11 j-oshb

ok I realise I'm borderline spamming this thread now, but I found a solution to this:

sql.query(
  [`INSERT INTO ${table.name} VALUES ( `, ' )'],
  row.map(cell ⇒ cell),
);

it's a 'fake' tagged template literal, with an array of strings as the first argument and parameters in the following arguments. it gets properly prepared/parameterized/etc.

it's not beautiful but it works for me...

j-oshb avatar Nov 22 '18 17:11 j-oshb

TBH, if you're using variables for table names you're doing something wrong; there shouldn't really ever be a case where that is needed. You should know your schema, you shouldn't be allowing arbitrary values as table names and you shouldn't be performing such an elaborate hack just to use a variable for a table name when you should be using a string.

dhensby avatar Nov 22 '18 17:11 dhensby

Glad it's now working. Let's leave the academic discussion for another place. Locking.

willmorgan avatar Nov 24 '18 12:11 willmorgan