A1 – Preventing injection attacks
According to OWASP, the most critical type of vulnerability found in web applications is the injection of some type of code, such as SQL injection, OS command injection, and HTML injection.
These vulnerabilities are usually caused by a poor input validation by the application. In this recipe, we will cover some of the best practices to use when processing user inputs and constructing queries that make use of them.
How to do it...
- The first thing to do in order to prevent injection attacks is to properly validate inputs. On the server side, this can be done by writing your own validation routines, although the best option is using the language's own validation routines, as they are more widely used and tested. A good example is
filter_var
in PHP or the validation helper in ASP.NET. For example, an email validation in PHP would look similar to this:
function isValidEmail($email){ return filter_var($email, FILTER_VALIDATE_EMAIL); }
- On the client side, validation...