Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I used to be a friend of centralizing sanitation as much as possible, but extensive discussion on SO (<a href="https://stackoverflow.com/questions/1809843/why-is-filter-input-incomplete"><strong>for example here</strong></a>) has changed my mind. Definitely worth a read.</p> <p>I submit to you the following practice:</p> <p>In a central validation routine, do no sanitation, or just "rough" checks (say, for data type) and size ("$_POST["category_name"] should not be larger than 200 bytes.") </p> <p>Mark incoming variables as <strong>unsafe</strong> (e.g. <code>$unsafe_id = $_POST["category_name"];</code>). Store them in whatever controller / class / construct you have available for it.</p> <p>Sanitize data <strong>where it is used</strong>. If incoming data is used in a <code>exec</code> call for example, do the necessary sanitation directly in front of the call:</p> <pre><code> $safe_category_name = escapeshellargs($unsafe_category_name); exec("external_binary -category_name '$safe_category_name'"); </code></pre> <p>if the same data is then used in a, say, mySQL query, again sanitize it in front of the call:</p> <pre><code> $safe_category_name = mysql_real_escape_string ($unsafe_category_name); mysql_query("SELECT * FROM items WHERE category_name = '$safe_category_name'"); </code></pre> <p>(this is just an example. If starting a project from scratch, you will want to use PDO and prepared statements, which takes away the hassle of escaping incoming data in this context.)</p> <p>if the same data is then output in a web page, again do the sanitation directly in front of the call:</p> <pre><code>$safe_category_name = htmlspecialchars($unsafe_category_name); echo "&lt;span&gt;$safe_category_name&lt;/span&gt;"; </code></pre> <p>This practice </p> <ul> <li><p>Establishes a workflow that assumes there are unsafe variables that need to be dealt with first, which leads to a safer programming style IMO. </p></li> <li><p>Prevents unnecessary conversions.</p></li> <li><p>Helps fight the illusion that there is a one-click method to make input "safe." There isn't. Sanitation depends 100% on context.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload