Skip to content

Commit b9039ad

Browse files
Prevent Cross-Site Request Forgery
1 parent 51c5425 commit b9039ad

File tree

7 files changed

+24
-0
lines changed

7 files changed

+24
-0
lines changed

edit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
?>
146146
<h2><?php echo $edit ? 'Edit' : 'Add'?></h2>
147147
<form action="<?php echo format_html(getRelativePath('edit.php'))?>" method="post">
148+
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
148149

149150
<p>
150151
<label for="type">Type:</label>

export.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ function export_json($key) {
187187
<h2>Export <?php echo isset($_GET['key']) ? format_html($_GET['key']) : ''?></h2>
188188

189189
<form action="<?php echo format_html(getRelativePath('export.php'))?>" method="post">
190+
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
190191

191192
<p>
192193
<label for="type">Type:</label>

import.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
?>
9393
<h2>Import</h2>
9494
<form action="<?php echo format_html(getRelativePath('import.php'))?>" method="post">
95+
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
9596

9697
<p>
9798
<label for="commands">Commands:<br>

includes/common.inc.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
define('PHPREDIS_ADMIN_PATH', dirname(__DIR__));
55

66

7+
if (session_status() !== PHP_SESSION_DISABLED) {
8+
session_start();
9+
10+
if (isset($_SESSION['phpredisadmin_csrf'])) {
11+
$csrfToken = $_SESSION['phpredisadmin_csrf'];
12+
} else {
13+
$csrfToken = bin2hex(random_bytes(16));
14+
$_SESSION['phpredisadmin_csrf'] = $csrfToken;
15+
}
16+
} else {
17+
$csrfToken = 'nosession';
18+
}
19+
20+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
21+
if ($_POST['csrf'] !== $csrfToken) {
22+
die('bad csrf token');
23+
}
24+
}
725

826

927
// These includes are needed by each script.

login.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<h1 class="logo">phpRedisAdmin</h1>
1414

1515
<form class="form-signin" method="post" action="login.php">
16+
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
1617
<h2 class="form-signin-heading">Please log in</h2>
1718

1819
<?php if (isset($_POST['username']) || isset($_POST['password'])): ?>

rename.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
?>
3737
<h2>Edit Name of <?php echo format_html($_GET['key'])?></h2>
3838
<form action="<?php echo format_html(getRelativePath('rename.php'))?>" method="post">
39+
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
3940

4041
<input type="hidden" name="old" value="<?php echo format_html($_GET['key'])?>">
4142

ttl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
?>
2828
<h2>Edit TTL</h2>
2929
<form action="<?php echo format_html(getRelativePath('ttl.php'))?>" method="post">
30+
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
3031

3132
<p>
3233
<label for="key">Key:</label>

0 commit comments

Comments
 (0)