当用户在html表单上输入“或\”时出现问题
输入的文本将在html内容和html属性上下文中再次显示给用户
我有以下数据流:
"
或\
)目标是向用户显示与输入完全相同的文本,但要正确转义以避免xss攻击。
现在我得到的第一件事就是$ _POST由于某种原因添加了斜杠。所以我现在首先使用stripslashes。这解决了单引号的所有问题,但如果用户输入“或\仍然会中断。
问题似乎是javascript在json_parse获取数据之前进行了一些解码。它将十六进制转义回到\并“从而杀死json_parse。
那么我想如果在步骤4和5之间我使用htmlspecialchars($ data,NO_QUOTES,'utf-8')我将&符编码为&
,这应该中和javascript解码,但不是。它解码& quot和十六进制编码时,由于某些原因它不解码&
...
我哪里错了? 有没有办法确切地知道javascipt解码和从PHP中和它?
在浪费了半天之后,我现在正在做什么:
我认为在onsuccess处理程序获取数据之前干扰数据可能是一些jQuery的事情。我现在没有时间去挖掘并杀死它,所以我只是用一个黑客偷偷过去,这意味着3个字符串转换只是为了保持字符串不变形,但是嘿,开发时间在这里是一种罕见的商品。
在php中:
// due to a problem with the jQuery callback code which seems to decode html entities and hex entities except for &
// we need to do something to keep our data intact, otherwise parse_json chokes on unescaped backslashes
// and quotes. So we mask the entity by transforming the & into & here and back in js.
// TODO: unit test this to prevent regression
// TODO: debug the jQuery to avoid this workaround
//
// echo json_encode( $response );
echo preg_replace( '/&/u', '&', json_encode( $response ) );
在parse_json之前的js中:
// due to a problem with the jQuery callback code which seems to decode html entities and hex entities except for &
// we need to do something to keep our data intact, otherwise parse_json chokes on unescaped backslashes
// and quotes. So we mask the entity by transforming the & into & here and back in js.
// See function xxxxxx() in file xxxxx.php for the corresponding transformation
//
responseText = responseText.replace( /&/g, '&' );
我现在不能为它编写单元测试而烦恼,但我似乎无法打破它。
真正的问题仍然是如何在获得相同结果的同时淘汰不需要的转变?
答案 0 :(得分:1)
尝试关闭php中的“Magic Quotes”。这样,数据通过$ _POST进入,就像用户键入它一样。请参阅:http://www.php.net/manual/en/security.magicquotes.disabling.php
然后你可以根据自己的需要逃避它。
答案 1 :(得分:0)
我遇到了问题,并使用了utf8_encode()函数。现在它运作良好。你能试试吗?