【发布时间】:2016-04-21 06:17:26
【问题描述】:
我有一个 php 'search' 脚本,它在 MySQL 数据库中查找请求的数据并打印一个表。通过单击图标可以修改或删除该表的每一行。当您单击其中一个图标时,将调用一个显示显示的 javascript 函数。
这是一段代码:
while ($row = mysqli_fetch_row($result)) {
// Define $id
$id = $row[7];
// Sanitize output
$user = htmlentities($row[0]);
$name = htmlentities($row[1]);
$surnames = htmlentities($row[2]);
$email = htmlentities($row[3]);
$role = htmlentities($row[4]);
$access = htmlentities($row[5]);
$center = htmlentities($row[6]);
$message .= "<tr>
<td>" . $user . "</td>" .
"<td>" . $name . "</td>" .
"<td>" . $surnames . "</td>" .
"<td>" . $email . "</td>" .
"<td>" . $role . "</td>" .
"<td>" . $access . "</td>" .
"<td>" . $center . "</td>" .
"<td>" .
"<input type='image' src='../resources/edit.png' id='edit_" . $user . "' class='edit' onclick=edit_user(\"$user\",\"$name\",\"$surnames\",'$email','$role','$access',\"$center\",'$id') title='Editar'></button>" .
"</td>" .
"<td>" .
"<input type='image' src='../resources/delete.png' id='delete_" . $user . "' class='delete' onclick=delete_user(\"$user\",'$role') title='Eliminar'></button>" .
"</td>
</tr>";
}
这只是我生成的表格的一部分。毕竟,我用 json_encode 对表进行编码并回显它。回声由 ajax 函数捕获,该函数将其解码(JSON.parse)并将其放入 div 中。
表格已正确呈现,正常字符一切正常,但我发现如果我有引号、斜杠和其他有意义的字符,我可能会遇到一些问题。字符串在表格中正确显示,所以php没有问题,但是生成的javascript对某些字符串不起作用。
例如,如果我介绍:
</b>5'"
或:
<b>5'6"</b><br><div
作为用户,当我点击编辑或删除图标时,我在 javascript 控制台中遇到了一些错误:
未捕获的 SyntaxError:无效的正则表达式:缺少 / home.php:1 Uncaught SyntaxError: missing ) after argument list
Uncaught SyntaxError: missing ) after argument list
Uncaught SyntaxError: Unexpected token ILLEGAL
我尝试过 addlash、replace、htmlentites、htmlspecialchars 的几种组合...但我找不到合适的组合。
为了避免任何问题,正确的处理方法是什么?
谢谢。
编辑:
我已经对此进行了调查,它似乎有效:
在php中我使用这个函数:
function javascript_escape($str) {
$new_str = '';
$str_len = strlen($str);
for($i = 0; $i < $str_len; $i++) {
$new_str .= '\\x' . dechex(ord(substr($str, $i, 1)));
}
return $new_str;
}
然后我使用类似的东西
$('<textarea />').html(user).text()
在javascript中解码字符串。
这对 XSS 攻击安全吗?
【问题讨论】:
-
至少你需要在 Javascript
onclick代码周围加上引号,否则任何空格都会导致语法错误,并且它是无效的 HTML 没有包含引号。然后,你需要` escape any quotes. Tryonclick="edit_user('$user', '$name', ....); return false"` 和$user,$name,其他变量应该是贯穿addslashes。 -
更好的做法是使用 Javascript 事件处理程序,而不是直接将 onclick= 打印到元素中。见this question and answer
-
你可以使用 ...
id='edit_" . MD5($user) . "' class=... -
@dres010 试过了,我得到 Uncaught SyntaxError: Unexpected token ILLEGAL
-
@IarsAnders 我想我可以添加带有 id 的 eventListeners,但我也有同样的 id 问题。生成不好,双引号完成了 id,虽然我使用了 addsplashes。
标签: javascript php special-characters sanitization