Issues (17)

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/CheckHelper.php (4 issues)

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
/*
4
 *  @copyright (c) 2019 Mendel <[email protected]>
5
 *  @license see license.txt
6
 */
7
8
namespace drycart\data;
9
10
/**
11
 * Helper for flexible conditions 
12
 *
13
 * @author mendel
14
 */
15
class CheckHelper
16
{
17
    protected static $allRules = [];
18
    
19
    /**
20
     * Check if data satisfies the condition
21
     * 
22
     * @param mixed $data
23
     * @param array $conditions
24
     * @return bool
25
     */
26
    public static function check($data, array $conditions) : bool
27
    {
28
        if(empty($conditions)) {
29
            return true;
30
        }
31
        $args = self::tryPrepareSimpleRules($conditions);
32
        $type = array_shift($args);
33
        switch (strtolower($type)) {
34
            case 'and':
35
                return self::checkAnd($data, $args);
36
            case 'or':
37
                return self::checkOr($data, $args);
38
            case 'not':
39
                return !self::check($data, $args[0]);
40
            default:
41
                return self::checkField($data, $type, $args[0], $args[1]);
42
        }
43
    }
44
    
45
    /**
46
     * If array of rules is in "simple format"
47
     * convert it to full format
48
     * 
49
     * @param array $rules
50
     * @return array
51
     */
52
    protected static function tryPrepareSimpleRules(array $rules) : array
53
    {
54
        self::initAllRules();
55
        if(empty($rules) or isset($rules[0])) {
56
            return $rules;
57
        }
58
        $result = ['and'];
59
        foreach($rules as $fieldRule=>$arg2) {
60
            [$rule, $arg1] = StrHelper::findPrefix($fieldRule, static::$allRules, '=');
0 ignored issues
show
The variable $rule does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $arg1 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
            $result[] = [$rule, $arg1, $arg2];
62
        }
63
        return $result;
64
    }
65
66
    /**
67
     * Init list of rules if not initialized
68
     * 
69
     * @return void
70
     */
71
    public static function initAllRules() : void
72
    {
73
        if(empty(static::$allRules)) {
74
            foreach(CompareHelper::RULES as $rule) {
75
                static::$allRules[] = '*'.$rule;
76
                static::$allRules[] = $rule;
77
            }
78
            foreach(array_keys(CompareHelper::RULES_ALIASES) as $rule) {
79
                static::$allRules[] = '*'.$rule;
80
                static::$allRules[] = $rule;
81
            }
82
            // Sort by lenght
83
            usort(static::$allRules, function(string $a, string $b) : int {
84
                return strlen($b) <=> strlen($a); // for reversal result
85
            });
86
        }
87
    }
88
    
89
    /**
90
     * Check AND condition
91
     * 
92
     * @param mixed $data
93
     * @param array $conditions
94
     * @return bool
95
     */
96
    protected static function checkAnd($data, array $conditions) : bool
97
    {
98
        foreach($conditions as $line) {
99
            if(!self::check($data,$line)) {
100
                return false;
101
            }
102
        }
103
        return true;
104
    }
105
106
    /**
107
     * Check OR condition
108
     * 
109
     * @param mixed $data
110
     * @param array $conditions
111
     * @return bool
112
     */
113
    protected static function checkOr($data, array $conditions) : bool
114
    {
115
        foreach($conditions as $line) {
116
            if(self::check($data,$line)) {
117
                return true;
118
            }
119
        }
120
        return false;
121
    }
122
123
    /**
124
     * Check/compare some field by rule and some value
125
     * 
126
     * @param mixed $data
127
     * @param string $staredRule
128
     * @param mixed $arg1
129
     * @param mixed $arg2
130
     * @return bool
131
     */
132
    protected static function checkField($data, string $staredRule, $arg1, $arg2) : bool
133
    {
134
        [$rulePrefix, $rule] = StrHelper::findPrefix($staredRule, ['*']);
0 ignored issues
show
The variable $rulePrefix does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $rule does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
135
        $value1 = $data->$arg1;
136
        if($rulePrefix == '*') {
137
            $value2 = $data->$arg2;
138
        } else {
139
            $value2 = $arg2;
140
        }
141
        return CompareHelper::compareByRule($rule, $value1, $value2);
142
    }
143
}
144