9. Knex security
Example vulnerable endpoint (conceptual)
snippetsRouter.get("/", async (req, res) => {
let query = knexInstance.select("*").from("snippets");
if ("sort" in req.query) {
const orderBy = req.query.sort.toString();
if (orderBy.length > 0) {
query = query.orderByRaw(orderBy); // Vulnerable!
}
}
console.log("SQL", query.toSQL().sql);
try {
const data = await query;
res.json({ data });
} catch (e) {
console.error(e);
res.status(500).json({ error: "Internal server error" });
}
});Safer pattern with validation
Last updated