Skip to content

Commit 0ec0fbb

Browse files
committed
security fixes
1 parent 363cc15 commit 0ec0fbb

5 files changed

Lines changed: 26 additions & 12 deletions

File tree

connect.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
}
1919

2020
// Connect to session
21-
session_start();
21+
session_start();
22+
session_regenerate_id(true);
2223

2324
// Check Login
2425
function logged_in() {

guestbook.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
2-
2+
echo '<pre>';
3+
var_dump($_SESSION);
4+
echo '</pre>';
35
print('<div class="container">');
46
print('<div class="row">
57
<div class="col-lg-12 text-center">
@@ -13,13 +15,15 @@
1315

1416
if(isset($_POST['entry']) && $_POST['entry'] != "") {
1517
$id = $_SESSION['userid'];
16-
$entry = $_POST['entry'];
18+
$entry = filter_input(INPUT_POST, 'entry', FILTER_SANITIZE_SPECIAL_CHARS); //$_POST['entry'];
1719
$query = "select * from `users` where `id` = '$id' LIMIT 1";
1820
$result = $db->query($query);
1921
if ($row = $result->fetch_assoc()) {
2022
$username = $row['username'];
2123
}
22-
$query = "INSERT INTO `guestbook` (`username`, `entry`) VALUES ('$username', '$entry');";
24+
25+
26+
$query = "INSERT INTO `guestbook` (`username`, `entry`) VALUES ('" . $db->real_escape_string($username) . "', '" . $db->real_escape_string($entry) . "');";
2327
$result = $db->query($query);
2428
$db->commit();
2529
print('<div class="row">

index.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
echo("<html><head>");
43

54
include "connect.php";
5+
6+
7+
echo("<html><head>");
68
include "head.php";
79

810
echo("</head><body>");
@@ -25,10 +27,10 @@
2527
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
2628
<ul class="nav navbar-nav">
2729
<li>
28-
<a href="?site=guestbook.php">Guestbook</a>
30+
<a href="?site=guestbook">Guestbook</a>
2931
</li>
3032
<li>
31-
<a href="?site=login.php">Private</a>
33+
<a href="?site=login">Private</a>
3234
</li>
3335
</ul>
3436
</div>
@@ -37,8 +39,14 @@
3739
<!-- /.container -->
3840
</nav>');
3941

40-
if (isset($_GET['site']) && $_GET['site'] != "") {
41-
include $_GET['site'];
42+
$allowed_pages = [
43+
'guestbook',
44+
'login',
45+
'logout'
46+
];
47+
48+
if (isset($_GET['site']) && $_GET['site'] != "" && in_array( $_GET['site'], $allowed_pages) ) {
49+
require $_GET['site'] . '.php';
4250
} else {
4351
$description = nl2br(file_get_contents("README.md"));
4452
echo(' <!-- Page Content -->

login.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
function login($username, $password) {
44
global $db;
5-
$query = "select `id` from `users` where `username` = '$username' AND `password` = '$password'";
5+
$query = "select `id` from `users` where `username` = '" . $db->real_escape_string($username) . "' AND `password` = '" . $db->real_escape_string($password) . "';";
6+
67
$result = $db->query($query);
78
if ($result->num_rows > 0 && $row = $result->fetch_assoc()) {
89
$_SESSION['logged_in'] = true;
@@ -32,7 +33,7 @@ function login($username, $password) {
3233
<h1>Private Area</h1>
3334
Hey ' . $username . '. Nice to have you here!
3435
<p>
35-
<a class="btn btn-danger" href="?site=logout.php">Logout</a>
36+
<a class="btn btn-danger" href="?site=logout">Logout</a>
3637
</p>
3738
</div>
3839
</div>

setup/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
echo "Setting up database"
44
mysql --user=che --password=che < create_db.sql
5-
5+
rm create_db.sql
66
echo "Finished..."

0 commit comments

Comments
 (0)