Home > Article > Backend Development > Discussing the security of PHP data filtering_PHP Tutorial
In at the beginning of the guide, we said that data filtering is the cornerstone of WEB application security in any language and on any platform. This includes verifying the data input to the application and the data output from the application, and a good software design can help developers do:
Ensure that PHP data filtering cannot be bypassed and ensure that illegal information cannot be bypassed. Affect legitimate information and identify the source of the data.
There are various views on how to ensure that data filtering cannot be bypassed, and two of them are more general than others and provide a higher level of assurance.
PHP data filtering scheduling method
This method is scheduled with a single PHP script (via URL). Any other operations are included using include or require when necessary. This approach generally requires that each URL be passed a separate GET variable for dispatch. This GET variable can be thought of as a more simplified design that replaces the script name. For example:
http://example.org/dispatch.php?task=print_formdispatch.php is the only root file (Document root). It allows developers to do two very important things:
Implement some global security processing at the beginning of dispatch.php, and ensure that these processing cannot be bypassed. It is easy to determine where necessary to perform data filtering, especially for some special-purpose control flow operations. Look at the following example for further discussion of the dispatch.php script:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>/* 全局安全处理 */ </span></li><li class="alt"><span>switch ($_GET['task']){case <br />'print_form':include '/inc/<br />presentation/form.inc'; </span></li><li><span>break; </span></li><li class="alt"><span>case 'process_form':$</span><span class="attribute">form_valid</span><span> = </span><span class="attribute-value">false</span><span>; </span></li><li><span>include '/inc/logic/process.inc'; </span></li><li class="alt"><span>if ($form_valid){include '/inc/<br />presentation/end.inc';}else{include <br />'/inc/presentation/form.inc';}<br />break;default:include '/inc/presentation<br />/index.inc'; </span></li><li><span>break; </span></li><li class="alt"><span>} </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
If this is the only publicly accessible PHP script, you can be sure that the program is designed to ensure that it is Global security handling cannot be bypassed. It also makes it easy for developers to see the control flow of specific tasks. For example, it is easy to know without browsing the entire code: when $form_valid is true, end.inc is the only one displayed to the user; since it is before process.inc is included and has just been initialized to false, it can be determined that The internal logic of process.inc will set it to true; otherwise the form will be displayed again (possibly with an associated error message).
Things to note when filtering PHP data
If you use a directory-directed file such as index.php (instead of dispatch.php), you can use the URL address like this : http://example.org/?task=print_form.
You can also use ApacheForceType redirection or mod_rewrite to adjust the URL address: http://example.org/app/print-form.
Inclusion method of PHP data filtering
Another way is to use a single module, which is responsible for all security processing. This module is included at the front (or very front) of all public PHP scripts. Refer to the following script security.inc
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>switch ($_POST['form']) </span></li><li class="alt"><span>{case 'login': </span></li><li><span>$</span><span class="attribute">allowed</span><span> = </span><span class="attribute-value">array</span><span>(); </span></li><li class="alt"><span>$allowed[] = 'form'; </span></li><li><span>$allowed[] = 'username'; </span></li><li class="alt"><span>$allowed[] = 'password'; </span></li><li><span>$</span><span class="attribute">sent</span><span> = </span><span class="attribute-value">array_keys</span><span>($_POST); </span></li><li class="alt"><span>if ($</span><span class="attribute">allowed</span><span> == $sent) </span></li><li><span>{include '/inc/logic/<br />process.inc';} </span></li><li class="alt"><span>break; </span></li><li><span>} </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
In this PHP data filtering example, each submitted form is considered to contain the unique verification value of form, and security.inc independently processes the 0 required in the form Filtered data. The HTML form that implements this requirement looks like this:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">form.</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"/receive.php"</span><span> <br /></span><span class="attribute">method</span><span>=</span><span class="attribute-value">"POST"</span><span class="tag">></span><span> </span></span></li> <li> <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"hidden"</span><span> <br /></span><span class="attribute">name</span><span>=</span><span class="attribute-value">"form"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"login"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">p</span><span class="tag">></span><span>Username: </span> </li> <li> <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"username"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /p</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> </span><span class="tag-name">p</span><span class="tag">></span><span>Password:</span><span class="tag"><</span><span class="tag-name">input</span><span> <br /></span><span class="attribute">type</span><span>=</span><span class="attribute-value">"password"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"password"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /p</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span> </li> </ol>
The array called $allowed is used to check which form variables are allowed. This list should be consistent before the form is processed. Process control decides what to execute, and process.inc is where the actual filtered data arrives.
Note
A better way to ensure that security.inc is always included at the beginning of every script is to use the auto_prepend_file setting.
Example of PHP data filtering
Establishing a whitelist is very important for PHP data filtering. Since it's impossible to give examples for every type of form data you may encounter, some examples can help you get a general understanding.
The following code validates the email address:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">clean</span><span> = </span><span class="attribute-value">array</span><span>(); </span></li><li class="alt"><span>$</span><span class="attribute">email_pattern</span><span> = </span><span class="attribute-value">'<br />/^[^@s<&>]+@([-a-z0-9]+.)<br>+[a-z]{2,}$/i'</span><span>; </span></span></li> <li><span>if (preg_match($email_<br>pattern, $_POST['email'])) </span></li> <li class="alt"><span>{$clean['email'] = $_POST<br>['email'];} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
The following PHP data filtering code ensures that the content of $_POST['color'] is red, green, or blue:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">clean</span><span> = </span><span class="attribute-value">array</span><span>(); </span></li><li class="alt"><span>switch ($_POST['color'])<br />{case 'red':case 'green':case <br />'blue':$clean['color'] = <br />$_POST['color']; </span></li><li><span>break; </span></li><li class="alt"><span>} </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
The following PHP data filtering code ensures that $_POST['num'] is an integer:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">clean</span><span> = </span><span class="attribute-value">array</span><span>(); </span></li><li class="alt"><span>if ($_POST['num'] == <br />strval(intval($_<br />POST['num']))){$clean<br />['num'] = $_POST['num']; </span></li><li><span>} </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
The following PHP data filtering code ensures that $_POST[' num'] is a floating point number (float):
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">clean</span><span> = </span><span class="attribute-value">array</span><span>(); </span></li><li class="alt"><span>if ($_POST['num'] == <br />strval(floatval($_POST<br />['num']))){$clean['num'] <br />= $_POST['num']; </span></li><li><span>} </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
Name conversion for PHP data filtering
Each of the previous examples used the array $clean. This is a good practice for developers to determine if their data is potentially compromised. Never save data in $_POST or $_GET after validating it. As a developer, you should always be fully suspicious of data saved in super global arrays.
It should be added that using $clean can help to think about what has not been filtered, which is more similar to the role of a whitelist. Can improve the level of security.
If you only save the validated data in $clean, the only risk in data validation is that the array element you reference does not exist, not unfiltered dangerous data.
Timing of PHP data filtering
Once the PHP script starts executing, it means that all HTTP requests have ended. At this point, the user has no chance to send data to the script. Therefore, no data can be entered into the script (even if register_globals is turned on). This is why initializing variables is a very good practice.