-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Table and column identifiers are not properly escaped. #98
Comments
(1) Don't do this (relevant documentation). |
OK, I see your pull request. I need to remind myself why we check if a string is already quoted before we quote it there, since on the face of it that does seem like a bit of a code smell. But it remains the case that this quoting is not designed as a security measure, but only to protect JS-style camelCase identifiers from being downcased by Postgres. |
|
But if The right way to write this query is probably: db.sql`SELECT * FROM ${'users'} WHERE ${{ id: req.body.id }}`.compile(); or equivalently const { id } = req.body;
db.sql`SELECT * FROM ${'users'} WHERE ${{ id }}`.compile(); Fundamentally I'm not convinced it's Zapatos' job to stop you (or other libraries you use) from lying to the type system. I'm not sure it's even possible, in the general case. But as I said, I will look at that quoting mechanism at some point. |
Yes, in this issue the developer can definitely be blamed for not validating input. E.g. router.post('/example', (req, res, next) => {
// Junior developer not validating input
const filter = req.body as { id: string };
console.log(zp.sql`SELECT * FROM ${'users'} WHERE ${filter}`.compile());
}); But zapatos could provide a higher level of robustness for free, like I propose in #99. Zapatos is already quoting identifiers, so why not escape them? My fear is that junior developers get comfortable with typescript very quickly. They think that runtime values are always constrained to types, but that assumption is dangerous when it comes to security. Solving this issue lowers significantly the cost of mistakes. |
A malicious user could send the following input.
This would result in SQL injection.
On the one hand, developers should never use column or table identifiers from input fields. But on the other hand, zapatos should escape correctly table and column identifiers.
The text was updated successfully, but these errors were encountered: