Skip to content

Replacements break with unrelated user provided data #13817

Closed

Description

@pedroassis

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

  1. pedroassis commented on Dec 23, 2021

    @pedroassis
    Author

    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.

  2. github-actions commented on Jan 7, 2022

    @github-actions
    Contributor

    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. 🙂

  3. added
    type: feature
    and removed on Jan 7, 2022
  4. ephys commented on Jan 7, 2022

    @ephys
    Member

    This is definitely a bug. PR welcome if you can :)

    In the meantime, you could try using bind instead or replacements

  5. added
    type: bug
    and removed
    type: feature
    on Jan 7, 2022
  6. pedroassis commented on Jan 25, 2022

    @pedroassis
    Author

    @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 PR

  7. self-assigned this
    on Apr 16, 2022
  8. pedroassis commented on Apr 19, 2022

    @pedroassis
    Author

    @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?

  9. pedroassis commented on Apr 19, 2022

    @pedroassis
    Author

    @ephys I have updated my example showing how to use this to do a SQL Injection.

    I think this needs some attention

      // 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
        },
        replacements: {
          foo: ` or name = ' or ' = `,
        },
      };
    

    This will return everything in the table.

  10. ephys commented on Apr 19, 2022

    @ephys
    Member

    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:

    User.findAll({
      where: and(
        literal('soundex("firstName") = soundex(:firstName)'),
        { lastName: lastName },
      ),
      replacements: { firstName },
    })

    Do this:

    User.findAll({
      where: and(
        literal('soundex("firstName") = soundex(:firstName)'),
        { lastName: literal(':lastName') },
      ),
      replacements: { firstName, lastName }, // <- *all* user provided data here
    })
  11. moved this to Doing in Roadmapon Apr 21, 2022
  12. ephys commented on Apr 28, 2022

    @ephys
    Member

    Fixed in Sequelize v7 (pending release). Just need to backport the non-breaking change part of that PR now.

  13. pedroassis commented on Apr 29, 2022

    @pedroassis
    Author

    @ephys cool, thank you for taking care of it.

    Do you know to which versions this will get back ported into?

  14. ephys commented on Apr 29, 2022

    @ephys
    Member

    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

  15. pedroassis commented on Apr 29, 2022

    @pedroassis
    Author

    @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.

  16. ephys commented on Apr 29, 2022

    @ephys
    Member

    @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 🙂

  17. ephys commented on Apr 29, 2022

    @ephys
    Member

    And here it is #14472

  18. pedroassis commented on Apr 29, 2022

    @pedroassis
    Author

    @ephys wow, clever fix. I'll try to play with it this weekend and let you know what I get.

  19. github-actions commented on May 17, 2022

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 6.19.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  20. ephys commented on May 18, 2022

    @ephys
    Member

    I think we can close this issue now :)

  21. Repository owner moved this from Doing to Done in Roadmapon May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

releasedtype: bug

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions