Issues (24)

Security Analysis    no request data  

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/Manager/ComparisonManager.php (1 issue)

Labels
Severity

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
3
namespace PhpAbac\Manager;
4
5
use PhpAbac\Model\PolicyRuleAttribute;
6
7
use PhpAbac\Comparison\{
8
    ArrayComparison,
9
    BooleanComparison,
10
    DatetimeComparison,
11
    NumericComparison,
12
    ObjectComparison,
13
    UserComparison,
14
    StringComparison
15
};
16
17
class ComparisonManager implements ComparisonManagerInterface
18
{
19
    /** @var AttributeManager **/
20
    protected $attributeManager;
21
    /** @var array **/
22
    protected $comparisons = [
23
        'array' => ArrayComparison::class,
24
        'boolean' => BooleanComparison::class,
25
        'datetime' => DatetimeComparison::class,
26
        'numeric' => NumericComparison::class,
27
        'object' => ObjectComparison::class,
28
        'user' => UserComparison::class,
29
        'string' => StringComparison::class,
30
    ];
31
    /** @var array **/
32
    protected $rejectedAttributes = [];
33
34 10
    public function __construct(AttributeManager $manager)
35
    {
36 10
        $this->attributeManager = $manager;
37 10
    }
38
39
    /**
40
     * This method retrieve the comparison class, instanciate it,
41
     * and then perform the configured comparison
42
     * It does return a control value for special operations,
43
     * but the real check is at the end of the enforce() method,
44
     * when the rejected attributes are counted.
45
     *
46
     * If the second parameter is set to true, compare will not report errors.
47
     * This is used to test a bunch of comparisons expecting not all of them true to return a granted access.
48
     * In fact, this parameter is used in comparisons which need to perform comparisons on their own.
49
     */
50 8
    public function compare(PolicyRuleAttribute $pra, bool $subComparing = false): bool
51
    {
52 8
        $attribute = $pra->getAttribute();
53
        // The expected value can be set in the configuration as dynamic
54
        // In this case, we retrieve the expected value in the passed options
55
        $praValue =
56 8
            ($pra->getValue() === 'dynamic')
57 1
            ? $this->getDynamicAttribute($attribute->getSlug())
58 8
            : $pra->getValue()
59
        ;
60
        // Checking that the configured comparison type is available
61 8
        if (!isset($this->comparisons[$pra->getComparisonType()])) {
62 1
            throw new \InvalidArgumentException('The requested comparison class does not exist');
63
        }
64
        // The comparison class will perform the attribute check with the configured method
65
        // For more complex comparisons, the comparison manager is injected
66 7
        $comparison = new $this->comparisons[$pra->getComparisonType()]($this);
67 7
        if (!method_exists($comparison, $pra->getComparison())) {
68 1
            throw new \InvalidArgumentException('The requested comparison method does not exist');
69
        }
70
        // Then the comparison is performed with needed
71 6
        $result = $comparison->{$pra->getComparison()}($praValue, $attribute->getValue(), $pra->getExtraData());
72
        // If the checked attribute is not valid, the attribute slug is marked as rejected
73
        // The rejected attributes will be returned instead of the expected true boolean
74 6
        if ($result !== true) {
75
            // In case of sub comparing, the error reporting is disabled
76 5
            if (!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) {
77 5
                $this->rejectedAttributes[] = $attribute->getSlug();
78
            }
79 5
            return false;
80
        }
81 5
        return true;
82
    }
83
84 2
    public function setDynamicAttributes(array $dynamicAttributes)
85
    {
86 2
        $this->dynamicAttributes = $dynamicAttributes;
0 ignored issues
show
The property dynamicAttributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87 2
    }
88
89
    /**
90
     * A dynamic attribute is a value given by the user code as an option
91
     * If a policy rule attribute is dynamic,
92
     * we check that the developer has given a dynamic value in the options
93
     *
94
     * Dynamic attributes are given with slugs as key
95
     *
96
     * @param string $attributeSlug
97
     * @return mixed
98
     * @throws \InvalidArgumentException
99
     */
100 3
    public function getDynamicAttribute(string $attributeSlug)
101
    {
102 3
        if (!isset($this->dynamicAttributes[$attributeSlug])) {
103 1
            throw new \InvalidArgumentException("The dynamic value for attribute $attributeSlug was not given");
104
        }
105 2
        return $this->dynamicAttributes[$attributeSlug];
106
    }
107
108
    public function addComparison(string $type, string $class)
109
    {
110
        $this->comparisons[$type] = $class;
111
    }
112
113 3
    public function getAttributeManager(): AttributeManager
114
    {
115 3
        return $this->attributeManager;
116
    }
117
118
    /**
119
     * This method is called when all the policy rule attributes are checked
120
     * All along the comparisons, the failing attributes slugs are stored
121
     * If the rejected attributes array is not empty, it means that the rule is not enforced
122
     */
123 6
    public function getResult(): array
124
    {
125
        $result =
126 6
            (count($this->rejectedAttributes) > 0)
127 5
            ? $this->rejectedAttributes
128 6
            : []
129
        ;
130 6
        $this->rejectedAttributes = [];
131 6
        return $result;
132
    }
133
}
134