Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the comments above I explained why using <code>eval()</code> is a potential security issue:</p> <blockquote> <p>If you receive this data from an Android application that means your data is not secure and could easily be changed by a potential hacker. If you use <code>eval()</code> to execute the received string I could call <strong>every</strong> PHP function by changing the function name. For example, if I change <code>function1</code> in your string to <code>exec("rm -rf /"); function1</code> every file on your server will be deleted, including the operating system (provided you have <code>exec()</code> enabled on your server and your server is running unix). But you can see how dangerous <code>eval()</code> is, so never use it!</p> </blockquote> <hr> <p>But is <code>call_user_func_array()</code> secure?</p> <blockquote> <p>It is safer, but still not safe enough. Using <code>eval()</code> I could specify both the function name and the parameters in one go, because it simply executes a string. And although that is not possible with <code>call_user_func_array()</code>, it still executes any specified function, which means I can still do the same. If I would change your string to <code>exec!!rm -rf /</code> you would still have a problem. Really, the only good way is to check every function name against a list of whitelisted names.</p> </blockquote> <hr> <p>And how can you make this operation safe? You can check for allowed functions using either a <code>switch</code> statement or <code>in_array()</code>. An example using the latter below:</p> <pre><code>&lt;?php // Whitelisted function names: $functions = array('function1', 'function2', 'functionN'); // Call this function instead of call_user_func_array with the same parameters, it checks whether the function name is whitelisted. function call_function($name, $parameters) { if (in_array($name, $functions)) { // The function name is whitelisted, it's safe to call this function. if (!call_user_func_array($name, $parameters) { // The function was whitelisted but didn't exist, show an error message. } } else { // A function was called that was not whitelisted! Write to log. } } ?&gt; </code></pre> <p>I didn't test it, but this should work. Hope this helped!</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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