Search code examples
sqldatabaselanguage-agnosticsanitizationparameterized

Are input sanitization and parameterized queries mutually exclusive?


I'm working updating some legacy code that does not properly handle user input. The code does do a minimal amount of sanitization, but does not cover all known threats.

Our newer code uses parameterized queries. As I understand it, the queries are precompiled, and the input is treated simply as data which cannot be executed. In that case, sanitization is not necessary. Is that right?

To put it another way, if I parameterize the queries in this legacy code, is it OK to eliminate the sanitization that it currently does? Or am I missing some additional benefit of sanitization on top of parameterization?


Solution

  • It's true that SQL query parameters are a good defense against SQL injection. Embedded quotes or other special characters can't make mischief.

    But some components of SQL queries can't be parameterized. E.g. table names, column names, SQL keywords.

    $sql = "SELECT * FROM MyTable ORDER BY {$columnname} {$ASC_or_DESC}";
    

    So there are some examples of dynamic content you may need to validate before interpolating into an SQL query. Whitelisting values is also a good technique.

    Also you could have values that are permitted by the data type of a column but would be nonsensical. For these cases, it's often easier to use application code to validate than to try to validate in SQL constraints.

    • Suppose you store a credit card number. There are valid patterns for credit card numbers, and libraries to recognize a valid one from an invalid one.

    • Or how about when a user defines her password? You may want to ensure sufficient password strength, or validate that the user entered the same string in two password-entry fields.

    • Or if they order a quantity of merchandise, you may need to store the quantity as an integer but you'd want to make sure it's greater than zero and perhaps if it's greater than 1000 you'd want to double-check with the user that they entered it correctly.