Skip to content

Commit 83bf89c

Browse files
committed
Added support for custom loggers and more changes
Changes in this commit - Support for custom loggers which implements LoggerInterface interface - Actions redefined in separate class - All classes moved to separate files - Added tests for custom and default loggers, which is still file based
1 parent 9eebbe3 commit 83bf89c

8 files changed

+925
-672
lines changed

libs/csrf/LoggerInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* This file has implementation for LoggerInterface interface
4+
*/
5+
6+
if (!defined('__CSRF_PROTECTOR_loggerInterface__')) {
7+
// to avoid multiple declaration errors
8+
define('__CSRF_PROTECTOR_loggerInterface__', true);
9+
10+
/**
11+
* Interface for logger class
12+
*/
13+
interface LoggerInterface {
14+
/**
15+
* logging method
16+
*
17+
* Parameters:
18+
* $message - the log message
19+
* $context - context array
20+
*
21+
* Return:
22+
* void
23+
*/
24+
public function log($message, $context = array());
25+
}
26+
}

libs/csrf/csrfpAction.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* This file has implementation for csrfpAction class
4+
*/
5+
6+
if (!defined('__CSRF_PROTECTOR_csrfpAction__')) {
7+
// to avoid multiple declaration errors
8+
define('__CSRF_PROTECTOR_csrfpAction__', true);
9+
10+
/**
11+
* Enumerator for actions
12+
*/
13+
abstract class csrfpAction {
14+
/**
15+
* Variable: ForbiddenResponseAction
16+
* Action of sending back 403 response code
17+
* @var int
18+
*/
19+
const ForbiddenResponseAction = 0;
20+
21+
/**
22+
* Variable: ClearParametersAction
23+
* Action of clearning all request parameters
24+
* @var int
25+
*/
26+
const ClearParametersAction = 1;
27+
28+
/**
29+
* Variable: RedirectAction
30+
* Action of redirecting users to another location
31+
* @var int
32+
*/
33+
const RedirectAction = 2;
34+
35+
/**
36+
* Variable: CustomErrorMessageAction
37+
* Action of sending back a custom message
38+
* @var int
39+
*/
40+
const CustomErrorMessageAction = 3;
41+
42+
/**
43+
* Variable: InternalServerErrorResponseAction
44+
* Action of sending back 5XX response code
45+
* @var int
46+
*/
47+
const InternalServerErrorResponseAction = 4;
48+
}
49+
}

libs/csrf/csrfpCookieConfig.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* This file has implementation for csrfpCookieConfig class
4+
*/
5+
6+
if (!defined('__CSRF_PROTECTOR_csrfpCookieConfig__')) {
7+
// to avoid multiple declaration errors
8+
define('__CSRF_PROTECTOR_csrfpCookieConfig__', true);
9+
10+
/**
11+
* Cookie config class
12+
*/
13+
class csrfpCookieConfig
14+
{
15+
/**
16+
* Variable: $path
17+
* path parameter for setcookie method
18+
* @var string
19+
*/
20+
public $path = '';
21+
22+
/**
23+
* Variable: $domain
24+
* domain parameter for setcookie method
25+
* @var string
26+
*/
27+
public $domain = '';
28+
29+
/**
30+
* Variable: $secure
31+
* secure parameter for setcookie method
32+
* @var bool
33+
*/
34+
public $secure = false;
35+
36+
/**
37+
* Variable: $expire
38+
* expiry parameter in seconds from now for setcookie method, default is 30 minutes
39+
* @var int
40+
*/
41+
public $expire = 1800;
42+
43+
/**
44+
* Function: constructor
45+
*
46+
* Parameters:
47+
* @param $cfg - config array loaded from config file;
48+
*/
49+
function __construct($cfg) {
50+
if ($cfg !== null) {
51+
if (isset($cfg['path'])) $this->path = $cfg['path'];
52+
if (isset($cfg['domain'])) $this->domain = $cfg['domain'];
53+
if (isset($cfg['secure'])) $this->secure = (bool) $cfg['secure'];
54+
if (isset($cfg['expire']) && $cfg['expire']) $this->expire = (int)$cfg['expire'];
55+
}
56+
}
57+
}
58+
}

libs/csrf/csrfpDefaultLogger.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* This file has implementation for csrfpDefaultLogger class
4+
*/
5+
include __DIR__ ."/LoggerInterface.php";
6+
7+
if (!defined('__CSRF_PROTECTOR_csrfpDefaultLogger_')) {
8+
// to avoid multiple declaration errors
9+
define('__CSRF_PROTECTOR_csrfpDefaultLogger_', true);
10+
11+
class logDirectoryNotFoundException extends \exception {};
12+
class logFileWriteError extends \exception {};
13+
14+
/**
15+
* Default logger class for CSRF Protector
16+
* This is a file based logger class
17+
*/
18+
class csrfpDefaultLogger implements LoggerInterface {
19+
/**
20+
* Variable: $logDirectory
21+
* directory for file based logging
22+
*/
23+
private $logDirectory;
24+
25+
/**
26+
* Constructor
27+
*
28+
* Parameters:
29+
* $path - the path for logs to be stored (relative or absolute)
30+
*
31+
* Returns:
32+
* void
33+
*
34+
* Throws:
35+
* logDirectoryNotFoundException - if log directory is not found
36+
*/
37+
function __construct($path) {
38+
//// Check for relative path
39+
$this->logDirectory = __DIR__ . "/../" . $path;
40+
41+
42+
//// If the relative log directory path does not
43+
//// exist try as an absolute path
44+
if (!is_dir($this->logDirectory)) {
45+
$this->logDirectory = $path;
46+
}
47+
48+
if (!is_dir($this->logDirectory)) {
49+
throw new logDirectoryNotFoundException("OWASP CSRFProtector: Log Directory Not Found!");
50+
}
51+
}
52+
53+
/**
54+
* logging method
55+
*
56+
* Parameters:
57+
* $message - the log message
58+
* $context - context array
59+
*
60+
* Return:
61+
* void
62+
*
63+
* Throws:
64+
* logFileWriteError - if unable to log an attack
65+
*/
66+
public function log($message, $context = array()) {
67+
// Append to the log file, or create it if it does not exist create
68+
$logFile = fopen($this->logDirectory ."/" . date("m-20y") . ".log", "a+");
69+
70+
//throw exception if above fopen fails
71+
if (!$logFile) {
72+
throw new logFileWriteError("OWASP CSRFProtector: Unable to write to the log file");
73+
}
74+
75+
$context['timestamp'] = time();
76+
$context['message'] = $message;
77+
78+
//convert log array to JSON format to be logged
79+
$context = json_encode($context) .PHP_EOL;
80+
81+
//append log to the file
82+
fwrite($logFile, $context);
83+
84+
//close the file handler
85+
fclose($logFile);
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)