Sequelize doesn't replace variable inside array value input in query raw
Issue Creation Checklist
- [x] I understand that my issue will be automatically closed if I don't fill in the requested information
- [x] I have read the contribution guidelines
Bug Description
If you try to use query raw with next syntax "SELECT * FROM table WHERE id = ANY(\'{:ids}\')" and try to replace ids - it doesn't works. You will recive the same query string (without slahes). Other way everything works fine with ARRAY[:ids].
Doesn't replace:
const result = await sequelize.query("SELECT * FROM table WHERE id = ANY(\'{:ids}\');", {
replacements: {
ids: [639, 495]
}
});
Result query:
SELECT * FROM table WHERE id = ANY('{:ids}');
Replace fine:
const result = await sequelize.query("SELECT * FROM table WHERE id = ANY(ARRAY[:ids])", {
replacements: {
ids: [639, 495]
}
});
Result query:
SELECT * FROM table WHERE id = ANY(ARRAY[639, 495]);
Reproducible Example
Here is the link to the SSCCE for this issue: https://github.com/fpavlik/sequelize-sscce
What do you expect to happen?
Variables should replace correctly
What is actually happening?
Variables didn't replace
Environment
- Sequelize version: 6.25.2
- Node.js version: 16.15.0
- If TypeScript related: TypeScript version: No
- Database & Version: PostgreSQL 12.11
- Connector library & Version: "pg": "^8.8.0", "pg-hstore": "^2.3.4",
Would you be willing to resolve this issue by submitting a Pull Request?
- [ ] Yes, I have the time and I know how to start.
- [ ] Yes, I have the time but I will need guidance.
- [ ] No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- [x] No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
What did you expect that the first result query would be?
Hi @WikiRik ! I expect this:
SELECT * FROM table WHERE id = ANY('{639, 495}');
wow cool thanks 🥰🥰🥰🥰🥰
Thanks for the quick reponse! I'll mark this as a feature request.
As you've already shown the easiest workaround for now is just to use ARRAY[:ids]
If you really want to use an array value, this could be a workaround;
const result = await sequelize.query(`SELECT * FROM table WHERE id = ANY(:ids);`, {
replacements: {
ids: `{639, 495}`
}
});
Will result into;
SELECT * FROM table WHERE id = ANY('{639, 495}');
Hey @WikiRik can I start looking into this! If I am able to find something would post here and possibly raise a PR!
This is the intended behavior, '{:ids}' is a string and our replacements won't replace anything in strings. It would make the code vulnerable to SQL injections. Similarly, bind parameters wouldn't work here either.
Using the ARRAY[] syntax is a workaround we support because it's safe, but the proper solution would be https://github.com/sequelize/sequelize/issues/14410
As @WikiRik also stated, this is also a perfectly valid solution: https://github.com/sequelize/sequelize/issues/15214#issuecomment-1298559443
I'll close as there isn't anything we can fix