I keep getting emails from Beyond Security Support website telling me of all my site security issues. First on the list is the word 'username' in my register.php script. I can post the result of that error if needed. I would at some point like to change it to secure login using https instead of http but for now i just want to solve this issue. It tells me to use prepared statments which it already does as you can see from the code. It would be nice to make it secure from sql injection and made rather simple. this is the username section of the code in php.
if(isset($_POST['submit'])){
//very basic validation
if (!preg_match("/^[a-zA-Z0-9_-]*$/",$_POST['username'])) {
$error[] = 'Usernames can only be numbers, letters, and characters _ -';
}
if(strlen($_POST['username']) < 3){
$error[] = 'Username is too short,3 chars min.';
} elseif (strlen($_POST['username']) > 25){
$error[] = 'Username is too long, 25 chars max.';
}
else
{
$stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
$stmt->execute(array(':username' => $_POST['username']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!empty($row['username'])){
$error[] = 'Username provided is already in use.';
}
}
and this the login form below
<form role="form" method="post" action="" autocomplete="off">
<h2>Please Sign Up</h2>
<p>Already a member? <a href='/login.php'>Login</a></p>
<hr>
<?php
//check for any errors
if(isset($error)){
foreach($error as $error){
echo '<p class="bg-danger">'.$error.'</p>';
}
}
//if action is joined show sucess
if(isset($_GET['action']) && $_GET['action'] == 'joined'){
echo "<h2 class='bg-success'>Registration successful, please check your email to activate your account.<br />Do not forget to check in your junk mail!.</h2>";
}
?>
<div class="form-group">
<input type="text" name="username" id="username" class="form-control input-lg" placeholder="User Name" value="<?php if(isset($error)){ echo $_POST['username']; } ?>" tabindex="1">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" value="<?php if(isset($error)){ echo $_POST['email']; } ?>" tabindex="2">
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="3">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control input-lg" placeholder="Confirm Password" tabindex="4">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="question" id="question" class="form-control input-lg" placeholder="Fill in the missing word. A bird likes to ???? loudly!" value="<?php if(isset($error)){ echo $_POST['question']; } ?>" tabindex="5">
</div>
<div class="row">
<div class="col-xs-6 col-md-6"><input type="submit" name="submit" value="Register" class="btn btn-primary btn-block btn-lg" tabindex="6"></div>
</div>
</form>
I wasnt sure if the whole form code was needed but thought i'd post it all 'just in case'. this is the report they sent me below.
We discovered vulnerabilities in the scripts listed below. Next to each script, there is a description of the type of attack that is possible, and the way to recreate the attack. If the attack is a simple HTTP GET request, you can usually paste it into your browser to see how it works. If it's a POST attack, the parameters for the POST request will be listed in square parenthesis.
Cross Site Scripting
URL: http://www.finchkeeper.com/register.php
Affected Parameter: username
Vector Used: ">alert('foo');
Pattern found: ">alert('foo');
Complete Attack: http://www.finchkeeper.com/register.php [username=">alert('foo'); &email= &password= &passwordConfirm= &question= &submit=Register]