I have this php code and my CMS security auto-test says it’s a XSS attack. Why and How can I fix this?
$url = "news.php";
if (isset($_GET['id']))
$url .= "?id=".$_GET["id"];
echo "<a href='{$url}'>News</a>";
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s XSS (cross site scripting) as someone could call your thing like this:
Essentially turning your code into
Now whenever someone would visit this site, it’d load and run the javascript
alert('xss');which might as well be a redirector or a cookie stealer.As many others have mentioned, you can fix this by using
filter_varorintval(if it’s a number). If you want to be more advanced, you could also use regex to match your string.Imagine you accept a-z A-Z and 0-9. This would work:
filter_inputeven has a manual entry doing exactly what you want (sanitizing your input into a link):