Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, SQL injection attacks can use an unescaped ORDER BY clause as a vector. There's an explanation of how this can be exploited and how to avoid this problem here:</p> <p><a href="http://josephkeeler.com/2009/05/php-security-sql-injection-in-order-by/" rel="noreferrer">http://josephkeeler.com/2009/05/php-security-sql-injection-in-order-by/</a></p> <p>That blog post recommends using a white list to validate the ORDER BY parameter against, which is almost certainly the safest approach.</p> <hr> <p>To respond to the update, even if the clause is complex, you can still write a routine that validates it against a whitelist, for example:</p> <pre><code>function validate_order_by($order_by_parameter) { $columns = array('first_name', 'last_name', 'zip', 'created_at'); $parts = preg_split("/[\s,]+/", $order_by_parameter); foreach ($parts as $part) { $subparts = preg_split("/\s+/", $part); if (count($subparts) &lt; 0 || count($subparts) &gt; 2) { // Too many or too few parts. return false; } if (!in_array($subparts[0], $columns)) { // Column name is invalid. return false; } if (count($subparts) == 2 &amp;&amp; !in_array(strtoupper($subparts[1]), array('ASC', 'DESC')) { // ASC or DESC is invalid return false; } } return true; } </code></pre> <p>Even if the ORDER BY clause is complex, it's still made only out of values you supply (assuming you're not letting users edit it by hand). You can still validate using a white list.</p> <p>I should also add that I normally don't like to expose my database structure in URLs or other places in the UI and will often alias the stuff in the parameters in the URLs and map it to the real values using a hash. </p>
    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