Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my code vulnerable to xss attack?

Tags:

html

php

xss

I have php code like this

<?php
    $input_from_user = "w' onclick = 'alert(document.cookie);'";
    $i_am_barcelona_fan = htmlentities($input_from_user);
?>
<input type = 'text' name = 'messi_fan' value ='<?php echo $i_am_barcelona_fan;?>' />

I am using htmlentities to protect from XSS attack, but still I am vulnerable to the above string. Why is my code vulnerable to XSS attack? How can I protect from my code from it?

like image 446
open source guy Avatar asked May 01 '26 14:05

open source guy


1 Answers

You're not telling PHP to escape quotes as well, and you should use htmlspecialchars() instead:

<input type = 'text' name = 'messi_fan' value ='<?php echo htmlspecialchars($input_from_user, ENT_QUOTES, 'UTF-8'); ?>' />

Demo

like image 132
Ja͢ck Avatar answered May 03 '26 02:05

Ja͢ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!