Skip to content

Move $queryRaw() to $dangerouslyQueryRaw() #7095

Closed

Description

@sachinraja

Problem

It can be confusing for newer developers that these two are not the same:

// value is escaped
queryRaw`SELECT * FROM table WHERE col=${value}` 

// open to injection
queryRaw(`SELECT * FROM table WHERE col=${value}`)

The same is true for $executeRaw. This is quite a dangerous mistake to make and so these functions should not be so close together, under the same name.

Suggested solution

Keep the tagged template literal and move $queryRaw() and executeRaw() to $dangerouslyQueryRaw() and $dangerouslyExecuteRaw() (as React has done with dangerouslySetInnerHTML).

Alternatives

Another thing worth considering is to remove the tagged template literal queryRaw because the "raw" already implies it is unsanitized. The sanitized tagged template literal could perhaps be moved under sql instead. The truly raw one could stay under queryRaw() or move to $dangerouslyQueryRaw().

Sidenotes

I know that queryRaw() is just the implementation of the tagged template literal, but it could still be moved to dangerouslyQueryRaw() and that could be the one recommended in the docs. The default implementation could also just return an error if used without any interpolation (although this would prevent people from using it even if they didn't have values to be sanitized).

Activity

  1. ae42faf7 commented on May 15, 2021

    @ae42faf7

    Holy 💩, I had no idea queryRaw() was unsafe.
    I like the suggested solution.

  2. Karl-EdwardJeanMehu commented on May 16, 2021

    @Karl-EdwardJeanMehu

    I agree with @cloudagon, However, renaming $queryRaw to $sql or sql can also cause some confusion considering that all the RDBMSs currently supported or will be supported has some variant of SQL. I believe simply rename it to $query is the best choice, especially considering that MongoDB support will hopefully be available and it does not use SQL whatsoever.

  3. pantharshit00 commented on May 18, 2021

    @pantharshit00
    Contributor

    Looks like @matthewmueller opened a general issue on this topic so I referenced this there: #7142

  4. added
    topic: raw
    on May 18, 2021
  5. matthewmueller commented on Oct 6, 2021

    @matthewmueller
    Contributor

    This is done now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: rawtopic: security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions