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/PdoRepository.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
2
3
namespace XHGui\Db;
4
5
use Generator;
6
use PDO;
7
use RuntimeException;
8
9
class PdoRepository
10
{
11
    /** @var PDO */
12
    private $pdo;
13
14
    /** @var string */
15
    private $table;
16
17
    /**
18
     * @param PDO $pdo An open database connection
19
     * @param string $table Table name where Xhgui profiles are stored
20
     */
21
    public function __construct(PDO $pdo, string $table)
22
    {
23
        $this->pdo = $pdo;
24
        $this->table = sprintf('"%s"', $table);
25
        $this->initSchema();
26
    }
27
28 View Code Duplication
    public function getLatest(): array
0 ignored issues
show
This method seems to be duplicated in 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...
29
    {
30
        $query = sprintf('
31
          SELECT
32
            "id",
33
            "profile",
34
            "url",
35
            "SERVER",
36
            "GET",
37
            "ENV",
38
            "simple_url",
39
            "request_ts",
40
            "request_ts_micro,"
41
            "request_date"
42
          FROM %s
43
          ORDER BY "request_date" ASC
44
          LIMIT 1',
45
            $this->table
46
        );
47
        $stmt = $this->pdo->query($query);
48
49
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
50
        if ($row === false) {
51
            throw new RuntimeException('No profile available yet.');
52
        }
53
54
        return $row;
55
    }
56
57 View Code Duplication
    public function getById(string $id): array
0 ignored issues
show
This method seems to be duplicated in 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...
58
    {
59
        $query = sprintf('
60
          SELECT
61
            "profile",
62
            "url",
63
            "SERVER",
64
            "GET",
65
            "ENV",
66
            "simple_url",
67
            "request_ts",
68
            "request_ts_micro",
69
            "request_date"
70
          FROM %s
71
          WHERE id = :id
72
        ', $this->table);
73
        $stmt = $this->pdo->prepare($query);
74
        $stmt->execute(['id' => $id]);
75
76
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
77
        if ($row === false) {
78
            throw new RuntimeException('No profile data found.');
79
        }
80
81
        return $row;
82
    }
83
84
    public function countByUrl(string $url): int
85
    {
86
        $query = sprintf('
87
          SELECT COUNT(*) AS count
88
          FROM %s
89
          WHERE "simple_url" LIKE :url
90
        ', $this->table);
91
        $stmt = $this->pdo->prepare($query);
92
        $stmt->execute(['url' => '%' . $url . '%']);
93
94
        return (int)$stmt->fetchColumn();
95
    }
96
97
    public function findByUrl(string $url, string $direction, int $skip, int $perPage): Generator
98
    {
99
        $query = sprintf('
100
          SELECT
101
            "id",
102
            "url",
103
            "SERVER",
104
            "GET",
105
            "ENV",
106
            "simple_url",
107
            "request_ts",
108
            "request_ts_micro",
109
            "request_date",
110
            "main_wt",
111
            "main_ct",
112
            "main_cpu",
113
            "main_mu",
114
            "main_pmu"
115
          FROM %s
116
          WHERE "simple_url" LIKE :url
117
          ORDER BY "request_ts" %s
118
          LIMIT %d OFFSET %d',
119
            $this->table,
120
            $direction,
121
            $perPage,
122
            $skip
123
        );
124
        $stmt = $this->pdo->prepare($query);
125
        $stmt->execute(['url' => '%' . $url . '%']);
126
127
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
128
            yield $row;
129
        }
130
    }
131
132
    public function deleteById(string $id): void
133
    {
134
        $stmt = $this->pdo->prepare(sprintf('
135
          DELETE FROM %s
136
          WHERE id = :id
137
        ', $this->table));
138
139
        $stmt->execute(['id' => $id]);
140
    }
141
142
    public function deleteAll()
143
    {
144
        return is_int(
145
            $this->pdo->exec(sprintf('DELETE FROM %s', $this->table))
146
        );
147
    }
148
149
    public function getStatistics()
150
    {
151
        $stmt = $this->pdo->query(
152
            sprintf(
153
                '
154
          SELECT
155
            COUNT(*) AS profiles,
156
            MAX("request_ts") AS latest,
157
            SUM(LENGTH("profile")) AS bytes
158
          FROM %s',
159
                $this->table
160
            ),
161
            PDO::FETCH_ASSOC
162
        );
163
164
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
165
166
        return $row ?: null;
167
    }
168
169
    public function initSchema(): void
170
    {
171
        $this->pdo->exec(sprintf('
172
            CREATE TABLE IF NOT EXISTS %s (
173
              "id"               CHAR(24) PRIMARY KEY,
174
              "profile"          TEXT           NOT NULL,
175
              "url"              TEXT           NULL,
176
              "SERVER"           TEXT           NULL,
177
              "GET"              TEXT           NULL,
178
              "ENV"              TEXT           NULL,
179
              "simple_url"       TEXT           NULL,
180
              "request_ts"       INTEGER        NOT NULL,
181
              "request_ts_micro" NUMERIC(15, 4) NOT NULL,
182
              "request_date"     DATE           NOT NULL,
183
              "main_wt"          INTEGER        NOT NULL,
184
              "main_ct"          INTEGER        NOT NULL,
185
              "main_cpu"         INTEGER        NOT NULL,
186
              "main_mu"          INTEGER        NOT NULL,
187
              "main_pmu"         INTEGER        NOT NULL
188
            )
189
        ', $this->table));
190
    }
191
192
    public function saveProfile(array $data): void
193
    {
194
        $stmt = $this->pdo->prepare(sprintf('
195
            INSERT INTO %s (
196
              "id",
197
              "profile",
198
              "url",
199
              "SERVER",
200
              "GET",
201
              "ENV",
202
              "simple_url",
203
              "request_ts",
204
              "request_ts_micro",
205
              "request_date",
206
              "main_wt",
207
              "main_ct",
208
              "main_cpu",
209
              "main_mu",
210
              "main_pmu"
211
            ) VALUES (
212
              :id,
213
              :profile,
214
              :url,
215
              :SERVER,
216
              :GET,
217
              :ENV,
218
              :simple_url,
219
              :request_ts,
220
              :request_ts_micro,
221
              :request_date,
222
              :main_wt,
223
              :main_ct,
224
              :main_cpu,
225
              :main_mu,
226
              :main_pmu
227
            )
228
        ', $this->table));
229
        $stmt->execute($data);
230
    }
231
}
232