为什么我的代码容易受到xss攻击? [英] Why my code vulnerable to xss attack?

查看:76
本文介绍了为什么我的代码容易受到xss攻击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的PHP代码

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

我使用 htmlentities 来防止XSS攻击,但我仍然易受上述字符串的影响。
为什么我的代码容易受到XSS攻击?我怎样才能保护我的代码呢?

解决方案

使用 htmlspecialchars()代替:

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

演示


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?

解决方案

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

这篇关于为什么我的代码容易受到xss攻击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆