Can't use dynamic table name in es6 literal
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?
Unfortunately, there is no way to achieve both sql injection protection and dynamic t-sql.
@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?
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 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) ) } )`)
So that cast can accept an array?
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.
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...
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
actually I think we can already achieve this using PreparedStatement.
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...
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.
Glad it's now working. Let's leave the academic discussion for another place. Locking.