Issues (85)

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/Fields/FieldParser.php (2 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 namespace Mascame\Artificer\Fields;
2
3
use \Illuminate\Support\Str as Str;
4
5
class FieldParser
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $types = [];
11
12
    /**
13
     * @var array
14
     */
15
    public $typeReason = [];
16
17
    public function __construct(array $types)
18
    {
19
        $this->types = $types;
20
    }
21
22
    /**
23
     * @return array
24
     */
25
    public function getTypes()
26
    {
27
        return $this->types;
28
    }
29
30
    /**
31
     * @param $field
32
     * @return bool|int|mixed|string
33
     */
34
    public function parse($field)
35
    {
36
        $type = $this->detectType($field);
37
38
        if (isset($this->types[$type]['onParse'])) {
39
            $this->types[$type]['onParse']($field, $type);
40
        }
41
42
        return $type;
43
    }
44
45
    /**
46
     * @param $name
47
     * @param $types
48
     * @return bool
49
     */
50
    public function isTypeEqual($name, $types)
51
    {
52
        if (in_array(snake_case($name), array_keys($types))
53
            || in_array(strtolower($name), array_keys($types))
54
        ) {
55
            $this->setTypeReason($name, 'equal');
56
57
            return true;
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * @param $fields
65
     * @param $name
66
     * @param $type
67
     * @return int
68
     */
69
    public function getSimilarityPoints($fields, $name, $type)
70
    {
71
        $points = 0;
72
73
        if ($this->isSimilar($name, $type)) {
74
            // Gives more importance to similar TYPE than field
75
            $points = +2;
76
        }
77
78
        foreach ($fields as $field) {
79
            if ($this->isSimilar($name, $field)) {
80
                $points++;
81
            }
82
        }
83
84
        return $points;
85
    }
86
87
    /**
88
     * @param $name
89
     * @param $types
90
     * @return bool|mixed
91
     */
92
    public function isTypeSimilar($name, $types)
93
    {
94
        $points = array();
95
96 View Code Duplication
        foreach ($types as $type => $data) {
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...
97
            if (!isset($data['autodetect'])) {
98
                continue;
99
            }
100
101
            $points[$type] = $this->getSimilarityPoints($data['autodetect'], $name, $type);
102
        }
103
104
        if (max($points) > 0) {
105
            $this->setTypeReason($name, 'similar to one in admin.fields');
106
107
            return array_search(max($points), $points);
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * @param $haystack
115
     * @param $needle
116
     * @return bool
117
     */
118
    public function isSimilar($haystack, $needle)
119
    {
120
        return Str::startsWith($haystack, $needle)
121
        || Str::endsWith($haystack, $needle)
122
        || Str::contains($haystack, $needle) ? true : false;
123
    }
124
125
    /**
126
     * @param $name
127
     * @param $types
128
     * @return bool|int|string
129
     */
130
    public function isUserType($name, $types)
131
    {
132
//        if (!isset($types['autodetect']) || empty($types['autodetect'])) return false;
133
134 View Code Duplication
        foreach ($types as $type => $data) {
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...
135
            if (!isset($data['autodetect'])) {
136
                continue;
137
            }
138
139
            if (in_array($name, $data['autodetect'])) {
140
                $this->setTypeReason($name, 'set by user in admin.fields');
141
142
                return $type;
143
            }
144
        }
145
146
        return false;
147
    }
148
149
    /**
150
     * @param $name
151
     * @param $types
152
     * @return bool|int|string
153
     */
154
    public function matchesRegex($name, $types)
155
    {
156
        foreach ($types as $type => $data) {
157
            if (!isset($data['regex'])) {
158
                continue;
159
            }
160
161
            if (preg_match($data['regex'], $name, $matches)) {
162
                $this->setTypeReason($name, "matched regex '{$data['regex']}'");
163
164
                return $type;
165
            }
166
        }
167
168
        return false;
169
    }
170
171
    /**
172
     * @param $name
173
     * @param $types
174
     * @return bool|int|mixed|string
175
     */
176
    public function detectType($name)
177
    {
178
        if ($type = $this->matchesRegex($name, $this->types)) {
179
            return $type;
180
        }
181
182
        if ($this->isTypeEqual($name, $this->types)) {
183
            return $name;
184
        }
185
186
        if ($type = $this->isUserType($name, $this->types)) {
187
            return $type;
188
        }
189
190
        if ($type = $this->isTypeSimilar($name, $this->types)) {
191
            return $type;
192
        }
193
194
        $this->setTypeReason($name, 'default');
195
196
        return $this->types['default']['type'];
197
    }
198
199
    /**
200
     * @param $name
201
     * @param $value
202
     */
203
    protected function setTypeReason($name, $value)
204
    {
205
        $this->typeReason[$name] = $value;
206
    }
207
208
}