Repository navigation
Description
Issue Creation Checklist
[x] I have read the contribution guidelines
https://github.com/HandleCollections/sequelize-sscce
Bug Description
When using replacements with user provided data we get errors when users input the colon symbol.
This happens because the user string is injected into the sql which later is parsed by sql-string.js file's formatNamedParameters function.
The formatNamedParameters does not have a way to know where the colon symbols comes from.
SSCCE
Here is the link to the SSCCE for this issue:
https://github.com/HandleCollections/sequelize-sscce
// lib/sql-string.js formatNamedParameters gets the sql with `WHERE name = "some:foo"`
// it will try to replace the ":foo", even tho it is not supposed to get replaced
const query = {
where: {
name: 'some:foo', // this is a user provided string I expect to be sanitized
},
replacements: {
test: 'this is used somewhere', // not used in example for the sake of simplicity
},
};What do you expect to happen?
I want to be able to search for strings containing a colon symbol and use replacements at the same time.
I also expect user provided data to be sanitized and never be treated as anything other than text.
What is actually happening?
sequelize tries to parse user provided data and throws an error.
I think it might be an avenue for sql injection since sequelize is treating an user provided string as parseable text.
Error: Named parameter ":foo" has no value in the given object.
at /Users/pedro/projects/sequelize-sscce/node_modules/sequelize/dist/lib/sql-string.js:103:11
Environment
- Sequelize version: 6.12.2 and 4.4.4
- Node.js version: v14.16.0
Bug Report Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
Would you be willing to resolve this issue by submitting a Pull Request?
It seems to be a very difficult problem to solve correctly, meaning only replacing actual replacements, not just as string with a colon.
I also thought about an ignore replacement error, where it would not replace a string that doesn't match a replacement, but this would still allow for a soft "injection" if the user matched a existing replacement.
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
pedroassis commentedon Dec 23, 2021
We might be able to simply improve the regex not to match anything enclosed by quotes.
I was sure this wouldn't work, but someone more experienced with the sequelize usage might know better.
github-actions commentedon Jan 7, 2022
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
ephys commentedon Jan 7, 2022
This is definitely a bug. PR welcome if you can :)
In the meantime, you could try using
bindinstead orreplacementspedroassis commentedon Jan 25, 2022
@ephys A similar issue happens on bind as well. If you have a
$anywhere it tries to replace it. I'll try to see if I can come up with a PRpedroassis commentedon Apr 19, 2022
@ephys maybe a temporary bandaid could be only replace things in the replacements object.
What would be different is that things that would fail in sequelize will start failing in the db.
Like in my example, sequelize uses the regex to replace :foo which isn't in the replacements object.
Now I don't know how this may impact other queries, and we may also add it behind an option param.
I can could do it, but I feel like I don't know enough to mess with such a critical part of the code.
What do you think?
pedroassis commentedon Apr 19, 2022
@ephys I have updated my example showing how to use this to do a SQL Injection.
I think this needs some attention
This will return everything in the table.
ephys commentedon Apr 19, 2022
Yep :) I deleted my previous comment to avoid bringing too much attention to this issue but I already started working on a fix to resolve this, it is a serious vulnerability (https://github.com/sequelize/sequelize/blob/ephys/fix-13817/test/unit/query-interface/select.test.ts)
Until the fix is complete, my recommendation is that if you use replacements in a query, make sure all user data is only passed through replacements (or bind).
So instead of doing this:
Do this:
ephys commentedon Apr 28, 2022
Fixed in Sequelize v7 (pending release). Just need to backport the non-breaking change part of that PR now.
pedroassis commentedon Apr 29, 2022
@ephys cool, thank you for taking care of it.
Do you know to which versions this will get back ported into?
ephys commentedon Apr 29, 2022
I'll backport to v6 myself. v5 has been EOL for a few months but if someone handles backporting, I'll help merge the PR, and v4 and under are completely archived at this point so no change likely over there
pedroassis commentedon Apr 29, 2022
@ephys I saw a couple PRs but I am not sure which ones need to be merged to get this fix, can you point me into which need to get merged?
I'll see if I can figure it out and merge it into the older versions.
ephys commentedon Apr 29, 2022
@pedroassis the main PR is #14447, but that one includes a few breaking changes aimed at improving bind parameters. I've adapted it for V6 but haven't pushed that branch yet. I should have it ready this weekend. I can ping you if you'd like to review it once I've opened the PR 🙂
ephys commentedon Apr 29, 2022
And here it is #14472
pedroassis commentedon Apr 29, 2022
@ephys wow, clever fix. I'll try to play with it this weekend and let you know what I get.
github-actions commentedon May 17, 2022
🎉 This issue has been resolved in version 6.19.1 🎉
The release is available on:
Your semantic-release bot 📦🚀
ephys commentedon May 18, 2022
I think we can close this issue now :)