Processing of external data submission for security rules that cannot be violated by PHP development

Source: Internet
Author: User
Tags form post tainted
Rule 1: never trust external data or enter information about Web application security. The first thing that must be realized is that you should not trust external data. External data includes any data that is not directly input by programmers in PHP code. Before you take measures to ensure security, you can GET the variable from any other source (such as GET variable, form POST, "> <LINKhref =" http :/

Rule 1: never trust external data or input

The first thing that must be realized about Web application security is that external data should not be trusted. External data includes any data that is not directly input by programmers in PHP code. Before taking measures to ensure security, any data from any other source (such as GET variables, form POST, database, configuration file, session variables, or cookies) is untrusted.

For example, the following data elements can be considered safe because they are set in PHP.

  Listing 1. safe and flawless code

 

 
 
  1. Reference content is as follows:
  2. $ MyUsername = 'tmyer ';
  3. $ ArrayUsers = array ('tmyer ', 'Tom', 'Tommy ');
  4. Define ("GREETING", 'Hello There'. $ myUsername );
  5. ?>

However, the following data elements are flawed.

List 2. insecure and defective code

 

 
 
  1. Reference content is as follows:
  2. $ MyUsername = $ _ POST ['username']; // tainted!
  3. $ ArrayUsers = array ($ myUsername, 'Tom ', 'Tommy'); // tainted!
  4. Define ("GREETING", 'Hello There'. $ myUsername); // tainted!
  5. ?>

Why is the first variable $ myUsername defective? Because it is directly from form POST. You can enter any strings in this input field, including malicious commands used to clear files or run previously uploaded files. You might ask, "isn't it possible to avoid this risk using a client that only accepts letters of A-Z (Javascr into pt) form validation script ?" Yes, this is always a good step, but as you will see later, anyone can download any form to their machine and modify it, then resubmit any content they need.

The solution is simple: you must run the cleanup code on $ _ POST ['username. Otherwise, $ myUsername may be contaminated at any other time (such as in an array or constant.

A simple method for clearing user input is to use a regular expression to process it. In this example, only letters are allowed. It may be a good idea to limit a string to a specific number of characters, or to require that all letters be in lowercase.

Listing 3. making user input secure

 

 
 
  1. Reference content is as follows:
  2. $ MyUsername = cleanInput ($ _ POST ['username']); // clean!
  3. $ ArrayUsers = array ($ myUsername, 'Tom ', 'Tommy'); // clean!
  4. Define ("GREETING", 'Hello There'. $ myUsername); // clean!
  5. Function cleanInput ($ input) {$ clean = strtolower ($ input );
  6. $ Clean = preg_replace ("/[^ a-z]/", "", $ clean );
  7. $ Clean = substr ($ clean, 0, 12); return $ clean;
  8. }
  9. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.