Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect against XSS and CSRF + some improvements #30

Open
wants to merge 94 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
7e48cf6
csrf_class
metamorfosec Sep 12, 2018
0f90509
HTMLPurifier.autoload
metamorfosec Sep 12, 2018
77c6730
another files in library
metamorfosec Sep 12, 2018
45329db
protect against xss
metamorfosec Sep 12, 2018
522d65d
protect against csrf and xss
metamorfosec Sep 12, 2018
d94f4be
protect against csrf and xss
metamorfosec Sep 12, 2018
50c408b
protect against csrf and xss
metamorfosec Sep 12, 2018
077d9c3
protect against xss and csrf
metamorfosec Sep 12, 2018
ed15eb7
protect against xss and csrf
metamorfosec Sep 12, 2018
597cb9c
protect against xss and csrf
metamorfosec Sep 12, 2018
5d4b551
Bootstrap
metamorfosec Sep 12, 2018
80ae5a4
Another files
metamorfosec Sep 12, 2018
54a6442
Validator
metamorfosec Sep 12, 2018
7e7a1be
Add files via upload
metamorfosec Sep 12, 2018
bee18a4
Create ConfigSchema.php
metamorfosec Sep 12, 2018
5157ae7
Add files via upload
metamorfosec Sep 12, 2018
aef64c5
Add files via upload
metamorfosec Sep 12, 2018
7697de1
Add files via upload
metamorfosec Sep 12, 2018
f88d502
Add files via upload
metamorfosec Sep 15, 2018
adb2bb3
Add files via upload
metamorfosec Sep 15, 2018
d2da28d
Add files via upload
metamorfosec Sep 15, 2018
b62e891
Add files via upload
metamorfosec Sep 15, 2018
1caba3c
Add files via upload
metamorfosec Sep 15, 2018
08b2110
Add files via upload
metamorfosec Sep 15, 2018
9b6426c
Add files via upload
metamorfosec Sep 15, 2018
c23f2c3
Add files via upload
metamorfosec Sep 15, 2018
82a14e2
change name from Token to CSRF_Token
metamorfosec Sep 15, 2018
c4f0602
Add files via upload
metamorfosec Sep 15, 2018
f18b09b
Add files via upload
metamorfosec Sep 15, 2018
e03ceba
Add files via upload
metamorfosec Sep 15, 2018
7a46655
Add files via upload
metamorfosec Sep 15, 2018
be2f8a6
Add files via upload
metamorfosec Sep 15, 2018
ec4324b
Add files via upload
metamorfosec Sep 15, 2018
9f7690a
Add files via upload
metamorfosec Sep 16, 2018
4d8fe70
Add files via upload
metamorfosec Sep 16, 2018
83e6133
Add files via upload
metamorfosec Sep 16, 2018
6ce95f2
Add ClassCSRF and/or HTMLPurifier Paths
metamorfosec Sep 16, 2018
1410568
Add CSRF Token
metamorfosec Sep 16, 2018
04dfc7a
Add files via upload
metamorfosec Sep 16, 2018
734939b
Add files via upload
metamorfosec Sep 16, 2018
a61050c
Add ' ' for TR_FORMAT_*
metamorfosec Sep 16, 2018
9ffec36
Fix HTMLPurifier Path from ../ to ../../
metamorfosec Sep 16, 2018
1d00788
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
67cf691
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
a72a1f6
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
47cb110
Add ' ' for TR_FILESIZE_*
metamorfosec Sep 17, 2018
9e08e60
Protect against xss
metamorfosec Sep 17, 2018
d219fe4
Protect against XSS
metamorfosec Sep 17, 2018
ed91d90
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
18a1b58
Change Token to CSRF_Token
metamorfosec Sep 17, 2018
c7ab14b
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
c5bdb85
Add HTMLPurifer Path
metamorfosec Sep 17, 2018
0b7ddc1
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
c7262d4
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
b9ca748
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
150d7dc
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
be68624
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
c726bbb
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
95c7a52
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
2d3626a
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
c8346af
Change Token to CSRF_Token
metamorfosec Sep 17, 2018
678ef33
Delete change_email.php
metamorfosec Sep 17, 2018
e1b6983
Delete index.php
metamorfosec Sep 17, 2018
fd3ac50
Delete change_password.php
metamorfosec Sep 17, 2018
005339f
add ' ' to unibo
metamorfosec Sep 17, 2018
f16ba9c
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
57940d5
Add HTMLPurifier Path
metamorfosec Sep 17, 2018
205b273
Add ClassCSRF and HTMLPurifier Paths
metamorfosec Sep 17, 2018
573acb3
Update the use of HTMLPurifier
metamorfosec Sep 18, 2018
88491a9
Replace 'login' with 'title'
metamorfosec Sep 18, 2018
3d08b68
Protect against XSS
metamorfosec Sep 18, 2018
47d99e8
Prevent reflected XSS on _cid parameter
metamorfosec Sep 18, 2018
510c8ee
Add HTMLPurifier Path
metamorfosec Sep 19, 2018
8f0d955
Use TABLE_PREFIX to prevent error
metamorfosec Sep 23, 2018
1935676
Remove Folder Protection contains third party class and library
metamorfosec Sep 23, 2018
bf215b5
To prevent CSRF
metamorfosec Sep 23, 2018
5965841
Remove HTMLPurifier Path, add htmlspecialchars...
metamorfosec Sep 23, 2018
022a541
Remove HTMLPurifier Path
metamorfosec Sep 23, 2018
0f85d59
Remove HTMLPurifier Path, add htmlspecialchars...
metamorfosec Sep 23, 2018
a872c9c
Remove HTMLPurifier, add htmlspecialchars...
metamorfosec Sep 23, 2018
2bb3222
Remove HTMLPurifier
metamorfosec Sep 23, 2018
7ee4da2
Remove HTMLPurifier, update class_csrf Path. ...
metamorfosec Sep 23, 2018
c5261d5
Remove HTMLPurifieri Path, add htmlspecialshars...
metamorfosec Sep 23, 2018
f8a34c2
Remove HTMLPurifier Path
metamorfosec Sep 23, 2018
99449c5
Update csrf_class Path
metamorfosec Sep 23, 2018
075b257
Update csrf_class Path
metamorfosec Sep 23, 2018
fd11198
Remove HTMLPurifier Path, add htmlspecialchars...
metamorfosec Sep 23, 2018
7fa1fab
Remove HTMLPurifier, update csrf_class Path,..
metamorfosec Sep 23, 2018
6f2462a
Remove csrf_class
metamorfosec Sep 23, 2018
dd9fcc6
Update csrf_class path
metamorfosec Sep 23, 2018
01e11f1
Remove csrf_class
metamorfosec Sep 23, 2018
f585059
Remove csrf_class
metamorfosec Sep 23, 2018
8de4222
Remove HTMLPurifier, add htmlspecialchars..
metamorfosec Sep 23, 2018
d8007c9
Remove csrf_class, add htmlspecialchars...
metamorfosec Sep 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions class_csrf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Cross Site Request Forgery (CSRF) Class
* @Author Matt Kent (Matt_Kent9)
* @License MIT
*
* session_start(); must be called before this is utilised.
*/
class CSRF_Token
{
// Empty constructor to avoid "Constructor cannot be static" error.
public function __construct() {}

// Used for is_recent() method.
private static $max_elapsed = 60 * 60 * 24; // 1 day

/**
* Generates token for use but doesn't store it.
*/
private static function token()
{
return bin2hex(openssl_random_pseudo_bytes(64));
}

/**
* Generate and store CSRF token in user session.
* Requires session to have been started already.
*/
private static function createToken()
{
$token = self::token();
$_SESSION['token'] = $token;
$_SESSION['token_time'] = time();
return $token;
}

/**
* Destroys a token by removing it from the session.
*/
private static function destroyToken()
{
$_SESSION['token'] = null;
$_SESSION['token_time'] = null;
return true;
}

/**
* Return HTML tag for use in a form.
*/
public static function display()
{
return "<input type=\"hidden\" name=\"token\" value=\"" . self::createToken() . "\" />";
}

/**
* Returns true if user-submitted POST token is
* identical to the previously stored SESSION token.
* Returns false otherwise.
*/
public static function isValid()
{
if (isset($_POST['token']))
{
$user_token = $_POST['token'];
$stored_token = $_SESSION['token'];
return hash_equals($_SESSION['token'], $_POST['token']);
}
else
{
return false;
}
}

/**
* You can simply check the token validity and
* handle the failure yourself, or you can use
* this "stop-everything-on-failure" method.
*/
public static function exitOnFailure()
{
if (!self::isValid())
{
exit('Invalid Security Token.');
}
}

/**
* This doesn't have to be used but it
* checks to see if the token is recent.
*/
public static function isRecent()
{
if (isset($_SESSION['token_time']))
{
$stored_time = $_SESSION['token_time'];
return ($stored_time + self::$max_elapsed) >= time();
}
else
{
self::destroyToken();
return false;
}
}
}
5 changes: 4 additions & 1 deletion documentation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
/************************************************************************/

define('TR_INCLUDE_PATH', '../include/');

include(TR_INCLUDE_PATH.'vitals.inc.php');
include(TR_INCLUDE_PATH.'handbook_pages.inc.php');

global $handbook_pages;

if (isset($_GET['p'])) {
$p = htmlentities($_GET['p']);
// We depend on htmlspecialchars, trim, stripslashes, and strip_tags to prevent Reflected XSS
// for p parameter
$p = htmlspecialchars(trim(stripslashes(strip_tags($_GET['p']))));
} else {
// go to first handbook page defined in $handbook_pages
foreach ($handbook_pages as $page_key => $page_value)
Expand Down
Loading