Properly Escaping with MySQLI | query over prepared statements

后端 未结 1 902
囚心锁ツ
囚心锁ツ 2020-12-03 16:43

I have read this:

will help you NOT against injection. Beause escaping is just a string formatting facility, not injection preventer by any means. Go figure. Ho

相关标签:
1条回答
  • 2020-12-03 17:04
    1. http://php.net/manual/en/mysqli-stmt.get-result.php
    2. Yes, but it is very bad practice:
      • it will help you in this case but only in this case and deceive with anything else
      • manual escaping is just silly, better let driver to do it for you
    3. YES, because there is no such thing like SQL injection but improper formatting ONLY

    is that using $mysqli->real_escape_string($Var); does not provide protection against SQL Injection?

    I didn't change my mind: sure, it doesn't.
    It will do only if you enclose the resulting value in quotes (and set proper encoding using mysqli_set_charset() to be strict).

    Look, SQL injection not something essential, existing on it's own, but it's rather mere a consequence. A consequence of improperly formatted query.
    When creating a query, you have to properly format every part of it. Not because of whatever "injection" but for the sake of it. When you're going to insert a string into query, you HAVE to put it into quotes, or you will get a syntax error. When you're going to insert a string into query, you HAVE to escape these quotes were used to delimit this string, or you will get a syntax error. And so on. It is proper formatting that should be your concern, not scaring tales about injection. And as long as you have every dynamic query part properly formatted according to it's type - no injection ever could be possible

    So, the source of variable or it's value should never be your concern. But only it's place in the query:

    • strings have to be enclosed in quotes and have these quotes escaped.
    • numbers have to be cast to it's type.
    • identifiers have to be enclosed in backticks and have these backticks doubled

    When it's going for the static part of the query, hardcoded in the script, we don't use such strict standards - say, we're not enclosing every identifier in backticks.
    But when it's going for the dynamical part of the query, applying formatting rules should be strict rule, as we cannot know variable content for sure.

    By the way, there is another way to format your strings and numbers - prepared statements. It is not as convenient as it should be, but because it is using placeholders to represent your data in the query, it it recommended to use over silly manual formatting.

    0 讨论(0)
提交回复
热议问题