Issues (7)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Auth/SimpleRbacAuthorize.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Burzum\SimpleRbac\Auth;
3
4
use Cake\Auth\BaseAuthorize;
5
use Cake\Core\Configure;
6
use Cake\Network\Request;
7
use Cake\Utility\Inflector;
8
use RuntimeException;
9
10
/**
11
 * Copyright 2011 - 2015, Florian Krämer
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @copyright Copyright 2011 - 2015, Florian Krämer
17
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
18
 */
19
class SimpleRbacAuthorize extends BaseAuthorize {
20
21
	/**
22
	 * Default config for this object.
23
	 *
24
	 * - `roleField` - The name of the role field in the user data array that is passed to authorize()
25
	 *
26
	 * @var array
27
	 */
28
	public $_defaultConfig = array(
29
		'roleField' => 'role',
30
		'allowEmptyActionMap' => false,
31
		'allowEmptyPrefixMap' => true,
32
		'undefinedActionsAreAllowed' => false
33
	);
34
35
	/**
36
	 * Authorize a user based on his roles
37
	 *
38
	 * @param array $user The user to authorize
39
	 * @param Request $request The request needing authorization.
40
	 * @return boolean
41
	 * @throws RuntimeException when the role field does not exist
42
	 */
43
	public function authorize($user, Request $request) {
44
		$roleField = $this->_config['roleField'];
45
46
		if (!isset($user[$roleField])) {
47
			throw new RuntimeException(sprintf('The role field `%s` does not exist!', $roleField));
48
		}
49
50
		if (is_string($user[$roleField])) {
51
			$user[$roleField] = array($user[$roleField]);
52
		}
53
54
		if ($this->authorizeByPrefix($user[$roleField], $request)) {
55
			return true;
56
		}
57
58
		if ($this->authorizeByControllerAndAction($user, $request)) {
59
			return true;
60
		}
61
62
		return false;
63
	}
64
65
	/**
66
	 * Checks if a role is granted access to a controller and action
67
	 *
68
	 * @param array $user
69
	 * @param Request $request
70
	 * @return boolean
71
	 */
72
	public function authorizeByControllerAndAction($user, Request $request) {
73
		$roleField = $this->_config['roleField'];
74
		extract($this->getControllerNameAndAction($request));
0 ignored issues
show
$this->getControllerNameAndAction($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
75
		$actionMap = $this->getActionMap();
76
77 View Code Duplication
		if (isset($actionMap[$name]['*'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
			if ($this->_isAllowedRole($user[$roleField], $actionMap[$name]['*'])) {
79
				return true;
80
			}
81
		}
82
83 View Code Duplication
		if (isset($actionMap[$name][$action])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
			if ($this->_isAllowedRole($user[$roleField], $actionMap[$name][$action])) {
85
				return true;
86
			}
87
		}
88
89
		if ($this->config('undefinedActionsAreAllowed') === true) {
90
			return true;
91
		}
92
93
		return false;
94
	}
95
96
	/**
97
	 * Checks if a role is allowed.
98
	 *
99
	 * @param array|string $userRoles
100
	 * @param array $allowedRoles
101
	 * @return boolean
102
	 */
103
	protected function _isAllowedRole($userRoles, array $allowedRoles) {
104
		if (in_array('*', $allowedRoles)) {
105
			return true;
106
		}
107
		if (is_string($userRoles)) {
108
			$userRoles = [$userRoles];
109
		}
110
		foreach ($userRoles as $userRole) {
111
			if (in_array($userRole, $allowedRoles)) {
112
				return true;
113
			}
114
		}
115
		return false;
116
	}
117
118
	/**
119
	 * Checks if a role is granted access to a prefix route like /admin.
120
	 *
121
	 * @param array $roles
122
	 * @param Request $request
123
	 * @return boolean
124
	 */
125
	public function authorizeByPrefix(array $roles, Request $request) {
126
		$prefixeMap = $this->getPrefixMap();
127
		if (isset($request->params['prefix']) && isset($prefixeMap[$request->params['prefix']])) {
128
			foreach ($roles as $role) {
129
				if (in_array($role, $prefixeMap[$request->params['prefix']])) {
130
					return true;
131
				}
132
			}
133
		}
134
		return false;
135
	}
136
137
	/**
138
	 * Gets the controller and action, prefixes the controller with the plugin if there is one
139
	 *
140
	 * @param Request $request
141
	 * @return array
142
	 */
143
	public function getControllerNameAndAction(Request $request) {
144
		$controller = $this->_registry->getController();
145
		$name = $controller->name;
146
		$action = $request->action;
0 ignored issues
show
The property action does not seem to exist in Cake\Network\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
147
148
		if (!empty($request->params['plugin'])) {
149
			$name = Inflector::camelize($request->params['plugin']) . '.' . $name;
150
		}
151
		return compact('name', 'action');
152
	}
153
154
	/**
155
	 * Can be overridden if inherited with a method to fetch this from anywhere, a database for example.
156
	 *
157
	 * @return array
158
	 * @throws RuntimeException
159
	 */
160 View Code Duplication
	public function getActionMap() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
		$actionMap = (array) Configure::read('SimpleRbac.actionMap');
162
		if (empty($actionMap) && $this->_config['allowEmptyActionMap'] === false) {
163
			throw new \RuntimeException('SimpleRbac.actionMap configuration is empty!');
164
		}
165
		return $actionMap;
166
	}
167
168
	/**
169
	 * Can be overriden if inherited with a method to fetch this from anywhere, a database for exaple
170
	 *
171
	 * @return array
172
	 * @throws \RuntimeException
173
	 */
174 View Code Duplication
	public function getPrefixMap() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
		$prefixMap = (array) Configure::read('SimpleRbac.prefixMap');
176
		if (empty($prefixMap) && $this->_config['allowEmptyPrefixMap'] === false) {
177
			throw new \RuntimeException('SimpleRbac.prefixMap configuration is empty!');
178
		}
179
		return $prefixMap;
180
	}
181
}
182