Skip to content

Commit 1d5c74e

Browse files
committed
Add CSRF and clickjacking protection
This header file included in all PHP files as first item. CSRF checks happen on GET or POST instances Security related headers also added to prevent clickjacking
1 parent c6bb782 commit 1d5c74e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/headers.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// Start a session if we haven't already
3+
if(!isset($_SESSION)) {@session_start();}
4+
5+
// CSRF synchronizer token pattern, 32 chars
6+
if (!isset($_SESSION["csrf"])) {
7+
$_SESSION["csrf"] = md5(uniqid(mt_rand(), true));
8+
}
9+
if ($_REQUEST && $_REQUEST["csrf"] !== $_SESSION["csrf"]) {
10+
echo '<script>alert("Bad CSRF token. Please press F12, view the console and report the error, including file & line number, so it can be fixed. Many thanks!");</script>';
11+
echo '<script>console.log("CSRF issue: REQUEST: "+$_REQUEST["csrf"]+", SESSION: "+$_SESSION["csrf"]);</script>';
12+
die('Bad CSRF token');
13+
}
14+
15+
// Set our security related headers, prevents clickjacking
16+
header("frame-options: SAMEORIGIN");
17+
header("XSS-Protection: 1; mode=block");
18+
?>

0 commit comments

Comments
 (0)