Apart from checking that the attributeId is in a list of predefined strings – possible, but in this particular case costly – is there any way to re-write this in a less 'injectable' fashion (since the use of '${attributeId}'
is actually a field, and not strictly a parameter...):
const { tablename, attributeId } = req.params;
let stmt = `SELECT DISTINCT row_data->'${attributeId}' as ${attributeId} FROM csv_data WHERE source = '${tablename}'`;
let result;
try {
await pool.connect();
result = await pool.query(stmt);
} catch (err) {
console.error(err);
}
...
return res.status(200).json({
rows: result.rows.map((elt, i, array) => {
return elt[attributeId];
}),
rowCount: result.rowCount,
});