Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSS filtering function in PHP

Tags:

php

xss

filter

Does anyone know of a good function out there for filtering generic input from forms? Zend_Filter_input seems to require prior knowledge of the contents of the input and I'm concerned that using something like HTML Purifier will have a big performance impact.

What about something like : http://snipplr.com/view/1848/php--sacar-xss/

Many thanks for any input.

like image 540
codecowboy Avatar asked Aug 26 '09 18:08

codecowboy


People also ask

What is XSS attack in PHP?

Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user.

What is XSS filtering?

Cross-site scripting (XSS) is a computer security vulnerability that allows malicious attackers to inject client-side script into web pages viewed by other users. You can use the Cross-site Scripting Filter setting to check all HTTP GET requests sent to IBM® OpenPages® with Watson™.

Which PHP function can help prevent cross-site scripting?

Using htmlspecialchars() function – The htmlspecialchars() function converts special characters to HTML entities. For a majority of web-apps, we can use this method and this is one of the most popular methods to prevent XSS.

Does Htmlspecialchars prevent XSS?

Use the PHP htmlspecialchars() function to convert special characters to HTML entities. Always escape a string before displaying it on a webpage using the htmlspecialchars() function to prevent XSS attacks.


1 Answers

Simple way? Use strip_tags():

$str = strip_tags($input); 

You can also use filter_var() for that:

$str = filter_var($input, FILTER_SANITIZE_STRING); 

The advantage of filter_var() is that you can control the behaviour by, for example, stripping or encoding low and high characters.

Here is a list of sanitizing filters.

like image 86
cletus Avatar answered Sep 29 '22 02:09

cletus