Issues (41)

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/Db/Mapper.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
3
namespace XHGui\Db;
4
5
use DateInterval;
6
use DateTime;
7
use MongoDate;
8
use XHGui\Searcher\SearcherInterface;
9
10
class Mapper
11
{
12
    /**
13
     * Convert request data keys into mongo values.
14
     */
15
    public function convert(array $options): array
16
    {
17
        return [
18
            'conditions' => $this->buildConditions($options['conditions'] ?? []),
19
            'sort' => $this->buildSort($options),
20
            'direction' => $this->buildDirection($options),
21
            'perPage' => $options['perPage'] ?? SearcherInterface::DEFAULT_PER_PAGE,
22
        ];
23
    }
24
25
    /**
26
     * Convert the search parameters into the matching fields.
27
     *
28
     * Keeps the schema details out of the GET parameters.
29
     * String casts are uses to prevent mongo operator injection.
30
     */
31
    private function buildConditions(array $search): array
32
    {
33
        if (!$search) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
            return [];
35
        }
36
37 View Code Duplication
        if (!empty($search['limit_custom']) && $search['limit_custom'][0] === 'P') {
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...
38
            $search['limit'] = $search['limit_custom'];
39
        }
40
        $hasLimit = (!empty($search['limit']) && $search['limit'] != -1);
41
42
        $conditions = [];
43 View Code Duplication
        if (!empty($search['date_start']) && !$hasLimit) {
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...
44
            $conditions['meta.request_date']['$gte'] = (string)$search['date_start'];
45
        }
46 View Code Duplication
        if (!empty($search['date_end']) && !$hasLimit) {
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...
47
            $conditions['meta.request_date']['$lte'] = (string)$search['date_end'];
48
        }
49
        if (isset($search['simple_url'])) {
50
            $conditions['meta.simple_url'] = (string)$search['simple_url'];
51
        }
52 View Code Duplication
        if (!empty($search['request_start'])) {
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...
53
            $conditions['meta.SERVER.REQUEST_TIME']['$gte'] = $this->convertDate($search['request_start']);
54
        }
55 View Code Duplication
        if (!empty($search['request_end'])) {
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...
56
            $conditions['meta.SERVER.REQUEST_TIME']['$lte'] = $this->convertDate($search['request_end']);
57
        }
58
59
        if (!empty($search['remote_addr'])) {
60
            $conditions['meta.SERVER.REMOTE_ADDR'] = (string)$search['remote_addr'];
61
        }
62
        if (isset($search['cookie'])) {
63
            $conditions['meta.SERVER.HTTP_COOKIE'] = (string)$search['cookie'];
64
        }
65
66
        if ($hasLimit && $search['limit'][0] === 'P') {
67
            $date = new DateTime();
68
            try {
69
                $date->sub(new DateInterval($search['limit']));
70
                $conditions['meta.request_ts']['$gte'] = new MongoDate($date->getTimestamp());
71
            } catch (\Exception $e) {
72
                // Match a day in the future so we match nothing, as it's likely an invalid format
73
                $conditions['meta.request_ts']['$gte'] = new MongoDate(time() + 86400);
74
            }
75
        }
76
77
        if (isset($search['url'])) {
78
            // Not sure if letting people use regex here
79
            // is a good idea. Only one way to find out.
80
            $conditions['meta.url'] = [
81
                '$regex' => (string)$search['url'],
82
                '$options' => 'i',
83
            ];
84
        }
85
86
        return $conditions;
87
    }
88
89
    private function convertDate($dateString)
90
    {
91
        if (is_numeric($dateString)) {
92
            return (float) $dateString;
93
        }
94
        $date = DateTime::createFromFormat('Y-m-d H:i:s', $dateString);
95
        if (!$date) {
96
            return $date;
97
        }
98
99
        return $date->getTimestamp();
100
    }
101
102
    private function buildDirection(array $options): string
103
    {
104
        if (empty($options['direction'])) {
105
            return SearcherInterface::DEFAULT_DIRECTION;
106
        }
107
        $valid = ['desc', 'asc'];
108
        if (in_array($options['direction'], $valid, true)) {
109
            return $options['direction'];
110
        }
111
112
        return 'desc';
113
    }
114
115
    /**
116
     * Get sort options for a paginated set.
117
     *
118
     * Whitelists to valid known keys.
119
     *
120
     * @param array $options pagination options including the sort key
121
     * @return array sort field & direction
122
     */
123
    private function buildSort(array $options): array
124
    {
125
        $direction = -1;
126
        if (isset($options['direction']) && $options['direction'] === 'asc') {
127
            $direction = 1;
128
        }
129
130
        $valid = ['time', 'wt', 'mu', 'cpu'];
131
        if (
132
            empty($options['sort']) ||
133
            (isset($options['sort']) && !in_array($options['sort'], $valid))
134
        ) {
135
            return ['meta.SERVER.REQUEST_TIME' => $direction];
136
        }
137
        if ($options['sort'] === 'time') {
138
            return ['meta.SERVER.REQUEST_TIME' => $direction];
139
        } elseif ($options['sort'] === 'wt') {
140
            return ['profile.main().wt' => $direction];
141
        } elseif ($options['sort'] === 'mu') {
142
            return ['profile.main().mu' => $direction];
143
        } elseif ($options['sort'] === 'cpu') {
144
            return ['profile.main().cpu' => $direction];
145
        }
146
    }
147
}
148