我有一个页面,我修改它以接受用户的输入,然后它会清理输入值,然后比较密码。然后它将重新路由到另一个页面。我唯一关心的是输入值中的 XSS 和脚本插入。这是我的代码:
<?php
/**
* ****************************************************************************
* Micro Protector
*
* Version: 1.0
* Release date: 2007-09-10
*
* USAGE:
* Define your requested password below and inset the following code
* at the beginning of your page:
* <?php require_once("microProtector.php"); ?>
*
* See the attached example.php.
*
******************************************************************************/
$Password = 'testpass'; // Set your password here
/******************************************************************************/
if (isset($_POST['submit_pwd'])){
// Clean up the input values
foreach($_POST as $key => $value) {
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
$pass = isset($_POST['passwd']) ? $_POST['passwd'] : '';
if ($pass != $Password) {
showForm("Wrong password");
exit();
}
} else {
showForm();
exit();
}
function showForm($error="LOGIN"){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<title>IMC - Authentication</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<Script>
<!--
function capitalize(form) {
value = form.value;
newValue = '';
value = value.split(' ');
for(var i = 0; i < value.length; i++) {
newValue += value[i].substring(0,1).toUpperCase() +
value[i].substring(1,value[i].length) + '';
}
newValue = newValue.replace(/(<([^>]+)>)/ig,"");
form.value = newValue;
}
-->
</Script>
</head>
<body>
<center><a href="http://www.test.com"><img src="http://www.test.com/theImages/topLogo.png" border=0 /></a></center>
<br><br><br>
<div id="main">
<div class="caption"><?php echo $error; ?></div>
<div id="icon"> </div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">
Your Name:
<table>
<tr><td><input class="text" name="name" onBlur="capitalize(this);" maxlength=12 type="text" /></td></tr>
</table>
Password:
<table>
<tr><td><input class="text" name="passwd" maxlength=8 type="password" /></td></tr>
<tr><td align="center"><br/>
<input class="text" type="submit" name="submit_pwd" value="Login" />
</td></tr>
</table>
</form>
</div>
</body>
</html>
<?php
}
?>
问题:上述 php 页面是否足够安全以确保用户无法插入任何标签或执行 XSS 攻击?
编辑: $_POST[$key] = stripslashes($_POST[$key]); 和$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 可以一起包含吗?