Issues (41)

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/Parser/Helper/GetData.php (8 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
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Parser\Helper;
5
6
use BrowscapPHP\Cache\BrowscapCacheInterface;
7
use BrowscapPHP\Data\PropertyFormatter;
8
use BrowscapPHP\Data\PropertyHolder;
9
use BrowscapPHP\Helper\QuoterInterface;
10
use ExceptionalJSON\DecodeErrorException;
11
use Psr\Log\LoggerInterface;
12
use Psr\SimpleCache\InvalidArgumentException;
13
14
/**
15
 * extracts the data and the data for theses pattern from the ini content, optimized for PHP 5.5+
16
 */
17
final class GetData implements GetDataInterface
18
{
19
    /**
20
     * The cache instance
21
     *
22
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
23
     */
24
    private $cache;
25
26
    /**
27
     * a logger instance
28
     *
29
     * @var \Psr\Log\LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var \BrowscapPHP\Helper\QuoterInterface
35
     */
36
    private $quoter;
37
38
    /**
39
     * class contsructor
40
     *
41
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
42
     * @param \Psr\Log\LoggerInterface                  $logger
43
     * @param \BrowscapPHP\Helper\QuoterInterface       $quoter
44
     */
45
    public function __construct(BrowscapCacheInterface $cache, LoggerInterface $logger, QuoterInterface $quoter)
46
    {
47
        $this->cache = $cache;
48
        $this->logger = $logger;
49
        $this->quoter = $quoter;
50
    }
51
52
    /**
53
     * Gets the settings for a given pattern (method calls itself to
54
     * get the data from the parent patterns)
55
     *
56
     * @param  string $pattern
57
     * @param  array  $settings
58
     *
59
     * @throws \UnexpectedValueException
60
     *
61
     * @return array
62
     */
63
    public function getSettings(string $pattern, array $settings = []) : array
64
    {
65
        // The pattern has been pre-quoted on generation to speed up the pattern search,
66
        // but for this check we need the unquoted version
67
        $unquotedPattern = $this->quoter->pregUnQuote($pattern);
68
69
        // Try to get settings for the pattern
70
        $addedSettings = $this->getIniPart($unquotedPattern);
71
72
        // set some additional data
73
        if (0 === count($settings)) {
74
            // The optimization with replaced digits get can now result in setting searches, for which we
75
            // won't find a result - so only add the pattern information, is settings have been found.
76
            //
77
            // If not an empty array will be returned and the calling function can easily check if a pattern
78
            // has been found.
79
            if (0 < count($addedSettings)) {
80
                $settings['browser_name_regex'] = '/^' . $pattern . '$/';
81
                $settings['browser_name_pattern'] = $unquotedPattern;
82
            }
83
        }
84
85
        // check if parent pattern set, only keep the first one
86
        $parentPattern = null;
87
88
        if (isset($addedSettings['Parent'])) {
89
            $parentPattern = $addedSettings['Parent'];
90
91
            if (isset($settings['Parent'])) {
92
                unset($addedSettings['Parent']);
93
            }
94
        }
95
96
        // merge settings
97
        $settings += $addedSettings;
98
99
        if (is_string($parentPattern)) {
100
            return $this->getSettings($this->quoter->pregQuote($parentPattern), $settings);
101
        }
102
103
        return $settings;
104
    }
105
106
    /**
107
     * Gets the relevant part (array of settings) of the ini file for a given pattern.
108
     *
109
     * @param  string $pattern
110
     *
111
     * @return array
112
     */
113
    private function getIniPart(string $pattern) : array
114
    {
115
        $pattern = strtolower($pattern);
116
        $patternhash = Pattern::getHashForParts($pattern);
117
        $subkey = SubKey::getIniPartCacheSubKey($patternhash);
118
119
        try {
120
            if (! $this->cache->hasItem('browscap.iniparts.' . $subkey, true)) {
121
                $this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" not found');
122
123
                return [];
124
            }
125
        } catch (InvalidArgumentException $e) {
0 ignored issues
show
The class Psr\SimpleCache\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
126
            $this->logger->error(new \InvalidArgumentException('an error occured while checking a inipart in the cache', 0, $e));
127
128
            return [];
129
        }
130
131
        $success = null;
132
133
        try {
134
            $file = $this->cache->getItem('browscap.iniparts.' . $subkey, true, $success);
135
        } catch (InvalidArgumentException $e) {
0 ignored issues
show
The class Psr\SimpleCache\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
136
            $this->logger->error(new \InvalidArgumentException('an error occured while reading a inipart from the cache', 0, $e));
137
138
            return [];
139
        }
140
141
        if (! $success) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $success of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
142
            $this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" not found');
143
144
            return [];
145
        }
146
147
        if (! is_array($file) || ! count($file)) {
148
            $this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" was empty');
149
150
            return [];
151
        }
152
153
        $propertyFormatter = new PropertyFormatter(new PropertyHolder());
154
        $return = [];
155
156
        foreach ($file as $buffer) {
157
            [$tmpBuffer, $patterns] = explode("\t", $buffer, 2);
0 ignored issues
show
The variable $tmpBuffer does not exist. Did you mean $buffer?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $patterns does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
158
159
            if ($tmpBuffer === $patternhash) {
0 ignored issues
show
The variable $tmpBuffer does not exist. Did you mean $buffer?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
160
                try {
161
                    $return = \ExceptionalJSON\decode($patterns, true);
0 ignored issues
show
The variable $patterns does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
162
                } catch (DecodeErrorException $e) {
0 ignored issues
show
The class ExceptionalJSON\DecodeErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
163
                    $this->logger->error('data for cache key "browscap.iniparts.' . $subkey . '" are not valid json');
164
165
                    return [];
166
                }
167
168
                foreach (array_keys($return) as $property) {
169
                    $return[$property] = $propertyFormatter->formatPropertyValue(
170
                        $return[$property],
171
                        (string) $property
172
                    );
173
                }
174
175
                break;
176
            }
177
        }
178
179
        return $return;
180
    }
181
}
182