I am sure you all know how CakePHP is one of the popular open-source web application framework used by every php development company. It was released in April 2005and was written in php while following model-view-controller architecture. It provides the features to make the application faster, easier and there isn’t much of a need to write long codes.  Many developers love it because of its improved functionality and enhanced security.

Security is usually a major concern for the applications that are written in PHP frameworks. But CakePHP is one of the most secured frameworks. Whenever security issues occur in this framework, the community immediately starts working on it. While developing high and commercial applications, some additional security measures must be taken into consideration.

While developing the applications in CakePHP, we must take the help of data validations and data sanitization. For creating a secure and strong application, CakePHP provides multiple security options such as SQL Injection prevention, CSRF protection, XSS prevention, input validation, form tampering protection etc. The HTTP methods used by developers are also controlled by CakePHP. It also controls the cross controller communication in web applications. CakePHP also offers help for AJAX, HTML forms and JavaScript etc

In the application building process, along with Authentication component and Access controlled lists, we should take a help of Data validation and Data sanitization.

Validations

Validations restrict the users to provide essential data in correct format.  Server side validations are strongly recommended while developing applications. In CakePHP, there are several default validations like required, email address, year, number etc. Along with default validations, you can also add the custom validations by using a regular expressions. For example, you can add regular expression for contact number input field.

Sanitization

Don’t rely on data validations only as it cannot sense the markup language and hence leaves your application open to the XSS (Cross Site Scripting) vulnerability. To avoid this, CakePHP has the Sanitize class. The data coming straight from the (malicious) user can be filtered by sanitize class. It can be directly used in a controller as well as in model as it can be treated as library in CakePHP.

For dealing with the data sent by malicious user, Sanitization class has following methods.

  • Sanitize::html

To save our HTML page from disturbing or inserting new HTML code use Sanitize::html(string $string, array $options = array()) method.

  • Sanitize::clean

With help of Sanitize::clean(mixed $data, mixed $options) method, we can handle the wide range of potential data problems such as handling tricky backslashes, weird spaces, HTML, carriage returns etc.

  • Sanitize::paranoid

If you need to use only alphanumeric characters from user input then use Sanitize::paranoid(string $string, array $allowedChars); function. It strips out the special characters from input.

  • Sanitize::escape

SQLInjection is the serious security issue. By default, it is handled by CakePHP by following its ORM conventions, but in case of custom queries it is highly recommended to use Sanitize::escape(string $string, string $connection)function.

Security Component

The tighter security to your application can be provided by Security Component in CakePHP. By adding only one extra line of code we can make our form quite secure.

var $components = array(‘Security’);

The Security component will create a hash based on the form fields produced by our Form Helper. If someone tampers with the form fields (by changing, by adding or by removing any field), the hash will not match with the expected one and the add() action will fail.

Auth component

The process of identifying users is called as authentication. In CakePHP Auth Component is inbuilt. This is used for user authentication, so we don’t need to do custom code for the same. It checks the request before filter of controller and checks if the page is allowed to display or not.

For example, in a web application, some pages can be browsed only after logging in like post comments, edit profile etc and some pages can be browsed without logging in like contact us, home page, product page etc. User Authentication is used to make sure that visitor is accessing the page which is denied for free access then it redirects it to the login page and after successful login it brings back to the requested page.

POSTED IN: September 29, 2016 Ashvini Hyalij