When writing PHP code it is very important to keep the following security vulnerabilities in mind to avoid writing insecure code. These are the common vulnerabilities you'll encounter when writing PHP code. We'll discuss a few in further depth below. Now let's look at some common vulnerabilities in more detail. Session Hijacking is a vulnerability caused by an attacker gaining access to a
user’s session identifier and being able to use another user’s account impersonating them. This is often used to gain access to an administrative user’s account. To defend against Session Hijacking attacks you need to check the current user’s browser and location information against information stored about the session. Below is an example implementation that
can help mitigate the effects of a session hijacking attack. It checks the IP Address, User Agent, and if the Session Expired removing a session before it’s resumed.
Cross Site ScriptingCross Site Scripting is a type of vulnerability in a web application caused by the programmer not sanitizing input before outputting the input to the web browser (for example a comment on a blog). It is commonly used to run malicious javascript in the web browser to do attacks such as stealing session cookies among other malicious actions to gain higher level privileges in the web application. Example Cross Site Scripting AttackA blog allows users to style their comments with HTML tags, however the script powering the blog does not strip out
Defending your website from cross site scripting attacks in PHPIn PHP there are two primary functions, The
The other approach is the strip_tags() code example
Setting the X-XSS-Protection Header: In PHP you can send the
Writing your own sanitization function Another option, if you would like more control over how the sanitization works, is to write your own HTML Sanitization function, this is not recommended for PHP Beginners as a mistake would make your website vulnerable. Defending your website from cross site scripting attacks with a Content Security PolicyAn effective approach to preventing cross site scripting attacks, which may require a lot of adjustments to your web application’s design and code base, is to use a content security policy. Set a Content Security Policy as an HTTP HeaderThe most common way of setting a Content Security Policy is by setting it directly in the HTTP Header. This can be done by the web server by editing it’s configuration or by sending it through PHP. Example of a Content Security Policy set in a HTTP Header
Set a Content Security Policy as a Meta tagsYou can include your Content Security Policy in the page’s HTML and set on a page by page basis. This method requires you to set on every page or you lose the benefit of the policy. Example of a Content Security Policy set in a HTML Meta Tag
SQL InjectionSQL injection is a vulnerability in the application caused by the programmer not sanitizing input before including it into a query into the database. This leads to the attacker having full read and more often than not write access to the database. With this type of access an attacker can do very bad things. Example SQL Injection attackThe below PHP Script runs an SQL Statement to get a user’s email by ID. However the input is not sanitized making it vulnerable to SQL Injection
So with the above the input is not type casted (I.e. casting the input with (int) so only a number is allowed) nor escaped allowing someone to perform an SQL Injection attack - for example the URL Defending your website from sql injection attacks in PHPThere are a few approaches to defend your website from SQL Injection Attacks. These approaches are Whitelisting, Type Casting, and Character Escaping Whitelisting: The whitelisting approach is used in cases where only a few inputs are expected. You can list each expected input in a PHP Switch and then have a default for invalid input. You do not have to worry about a type casting issue or a character escape bypass but the allowed input is extreamly limited. It remains an option, see the example below.
Type Casting: The type casting approach is commonly used for an application using numeric input. Simply cast the input with Character Escaping: The character escaping approach will escape characters such as quotes and slashes provided by the user to prevent an attack. If you are using MySQL Server and the MySQLi library to access
your database, the More on PHP:
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |