1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Error;
use PhpMyAdmin\ErrorReport;
use PhpMyAdmin\Template;
use PhpMyAdmin\Utils\HttpRequest;
use PhpMyAdmin\Version;
use function htmlspecialchars;
use function json_encode;
use function phpversion;
use const ENT_QUOTES;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
/**
* @covers \PhpMyAdmin\ErrorReport
*/
class ErrorReportTest extends AbstractTestCase
{
/** @var ErrorReport $errorReport */
private $errorReport;
protected function setUp(): void
{
parent::setUp();
$GLOBALS['server'] = 1;
$GLOBALS['cfg']['ServerDefault'] = 1;
$GLOBALS['cfg']['ProxyUrl'] = '';
$GLOBALS['cfg']['ProxyUser'] = '';
$GLOBALS['cfg']['ProxyPass'] = '';
$_SERVER['SERVER_SOFTWARE'] = 'SERVER_SOFTWARE';
$_SERVER['HTTP_USER_AGENT'] = 'HTTP_USER_AGENT';
$_COOKIE['pma_lang'] = 'en';
$GLOBALS['config']->set('is_https', false);
$this->errorReport = new ErrorReport(
new HttpRequest(),
new Relation($this->dbi),
new Template(),
$GLOBALS['config']
);
$this->errorReport->setSubmissionUrl('http://localhost');
}
public function testGetData(): void
{
$actual = $this->errorReport->getData('unknown');
$this->assertEquals([], $actual);
$actual = $this->errorReport->getData('php');
$this->assertEquals([], $actual);
$_SESSION['prev_errors'] = [];
$actual = $this->errorReport->getData('php');
$this->assertEquals([], $actual);
$_SESSION['prev_errors'] = [
new Error(0, 'error 0', 'file', 1),
new Error(1, 'error 1', 'file', 2),
];
$report = [
'pma_version' => Version::VERSION,
'browser_name' => $GLOBALS['config']->get('PMA_USR_BROWSER_AGENT'),
'browser_version' => $GLOBALS['config']->get('PMA_USR_BROWSER_VER'),
'user_os' => $GLOBALS['config']->get('PMA_USR_OS'),
'server_software' => $_SERVER['SERVER_SOFTWARE'],
'user_agent_string' => $_SERVER['HTTP_USER_AGENT'],
'locale' => $_COOKIE['pma_lang'],
'configuration_storage' => 'disabled',
'php_version' => phpversion(),
'exception_type' => 'php',
'errors' => [
0 => [
'lineNum' => $_SESSION['prev_errors'][0]->getLine(),
'file' => $_SESSION['prev_errors'][0]->getFile(),
'type' => $_SESSION['prev_errors'][0]->getType(),
'msg' => $_SESSION['prev_errors'][0]->getOnlyMessage(),
'stackTrace' => $_SESSION['prev_errors'][0]->getBacktrace(5),
'stackhash' => $_SESSION['prev_errors'][0]->getHash(),
],
1 => [
'lineNum' => $_SESSION['prev_errors'][1]->getLine(),
'file' => $_SESSION['prev_errors'][1]->getFile(),
'type' => $_SESSION['prev_errors'][1]->getType(),
'msg' => $_SESSION['prev_errors'][1]->getOnlyMessage(),
'stackTrace' => $_SESSION['prev_errors'][1]->getBacktrace(5),
'stackhash' => $_SESSION['prev_errors'][1]->getHash(),
],
],
];
$actual = $this->errorReport->getData('php');
$this->assertEquals($report, $actual);
}
public function testSend(): void
{
$submissionUrl = 'http://localhost';
$report = [];
$return = 'return';
$httpRequest = $this->getMockBuilder(HttpRequest::class)
->onlyMethods(['create'])
->getMock();
$httpRequest->expects($this->once())
->method('create')
->with(
$submissionUrl,
'POST',
false,
json_encode($report),
'Content-Type: application/json'
)
->willReturn($return);
$this->errorReport = new ErrorReport(
$httpRequest,
new Relation($this->dbi),
new Template(),
$GLOBALS['config']
);
$this->errorReport->setSubmissionUrl($submissionUrl);
$this->assertEquals($return, $this->errorReport->send($report));
}
public function testGetForm(): void
{
$_POST['exception'] = [];
$form = $this->errorReport->getForm();
$this->assertStringContainsString('<pre class="pre-scrollable">[]</pre>', $form);
$context = [
'Widget.prototype = {',
' close: function() {',
' if (this.completion.widget != this) return;',
' this.completion.widget = null;',
' this.hints.parentNode.removeChild(this.hints);',
' this.completion.cm.removeKeyMap(this.keyMap);',
'',
' var cm = this.completion.cm;',
' if (this.completion.options.closeOnUnfocus) {',
' cm.off("blur", this.onBlur);',
];
$_POST['exception'] = [
'mode' => 'stack',
'name' => 'TypeError',
'message' => 'Cannot read property \'removeChild\' of null',
'stack' => [
[
'url' => 'http://pma.7.3.local/js/vendor/codemirror/addon/hint/show-hint.js?v=4.8.6-dev',
'func' => 'Widget.close',
'line' => 307,
'column' => 29,
'context' => $context,
],
],
'url' => 'http://pma.7.3.local/index.php?route=/table/sql&db=aaaaa&table=a&server=14',
];
$_POST['description'] = 'description';
$report = [
'pma_version' => Version::VERSION,
'browser_name' => $GLOBALS['config']->get('PMA_USR_BROWSER_AGENT'),
'browser_version' => $GLOBALS['config']->get('PMA_USR_BROWSER_VER'),
'user_os' => $GLOBALS['config']->get('PMA_USR_OS'),
'server_software' => $_SERVER['SERVER_SOFTWARE'],
'user_agent_string' => $_SERVER['HTTP_USER_AGENT'],
'locale' => $_COOKIE['pma_lang'],
'configuration_storage' => 'disabled',
'php_version' => phpversion(),
'script_name' => 'index.php',
'exception_type' => 'js',
'exception' => [
'mode' => 'stack',
'name' => 'TypeError',
'message' => 'Cannot read property \'removeChild\' of null',
'stack' => [
[
'func' => 'Widget.close',
'line' => 307,
'column' => 29,
'context' => $context,
'uri' => 'js/vendor/codemirror/addon/hint/show-hint.js?v=4.8.6-dev',
'scriptname' => 'js/vendor/codemirror/addon/hint/show-hint.js',
],
],
'uri' => 'index.php?route=%2Ftable%2Fsql',
],
'steps' => $_POST['description'],
];
$expectedData = json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$form = $this->errorReport->getForm();
$this->assertStringContainsString(
'<pre class="pre-scrollable">' . htmlspecialchars((string) $expectedData, ENT_QUOTES) . '</pre>',
$form
);
}
public function testTruncateJsTrace(): void
{
$context = [
' success: function (response) {',
' Functions.ajaxRemoveMessage($msgbox);',
' if (response.success) {',
' // Get the column min value.',
' var min = response.column_data.min',
' ? \'(\' + window.Messages.strColumnMin +',
' this.completion.cm.removeKeyMap(this.keyMap);',
' \' \' + response.column_data.min + \')\'',
' : \'\';',
' if (this.completion.options.closeOnUnfocus) {',
' cm.off("blur", this.onBlur);',
];
$data = [
'mode' => 'stack',
'name' => 'TypeError',
'message' => 'Cannot read property \'removeChild\' of null',
'stack' => [
[
'url' => 'http://pma.7.3.local/js/vendor/codemirror/addon/hint/show-hint.js?v=4.8.6-dev',
'func' => 'Widget.close',
'line' => 307,
'column' => 29,
'context' => $context,
],
[
'func' => 'Object.fireWith [as resolveWith]',
'line' => '2',
'column' => '29039',
'context' => [
'/*! jQuery v3.5.1 | (c) JS Foundation and other contributors | jquery.org/license */',
'!function(e,t){"use strict";"object"==typeof module&&'
. '"object"==typeof module.exports?module.exports=e.document?t',
],
'url' => 'js/vendor/jquery/jquery.min.js?v=5.1.0-rc2',
'scriptname' => 'js/vendor/jquery/jquery.min.js',
],
],
'url' => 'http://pma.7.3.local/index.php?route=/table/sql&db=aaaaa&table=a&server=14',
];
$_POST['exception'] = $data;
$actual = $this->errorReport->getData('js');
// Adjust the data
unset($data['stack'][0]['url']);
$data['stack'][0]['uri'] = 'js/vendor/codemirror/addon/hint/show-hint.js?v=4.8.6-dev';
$data['stack'][0]['scriptname'] = 'js/vendor/codemirror/addon/hint/show-hint.js';
unset($data['stack'][1]['url']);
$data['stack'][1]['uri'] = 'js/vendor/jquery/jquery.min.js?v=5.1.0-rc2';
unset($data['url']);
$data['uri'] = 'index.php?route=%2Ftable%2Fsql';
$data['stack'][1]['context'][0] = '/*! jQuery v3.5.1 | (c) JS Foundation'
. ' and other contributors | jquery.org/l//...';
$data['stack'][1]['context'][1] = '!function(e,t){"use strict";"object"='
. '=typeof module&&"object"==typeof modul//...';
$this->assertEquals($data, $actual['exception']);
}
/**
* The urls to be tested for sanitization
*
* @return array[]
*/
public function urlsToSanitize(): array
{
return [
[
'',
[
'index.php?',
'index.php',
],
],
[
'http://localhost/js/vendor/codemirror/addon/hint/show-hint.js?v=4.8.6-dev',
[
'js/vendor/codemirror/addon/hint/show-hint.js?v=4.8.6-dev',
'js/vendor/codemirror/addon/hint/show-hint.js',
],
],
[
'http://pma.7.3.local/tbl_sql.php?db=aaaaa&table=a&server=14',
[
'tbl_sql.php?',
'tbl_sql.php',
],
],
[
'http://pma.7.3.local/tbl_sql.php?db=aaaaa;table=a;server=14',
[
'tbl_sql.php?',
'tbl_sql.php',
],
],
[
'https://pma.7.3.local/tbl_sql.php?db=aaaaa;table=a;server=14',
[
'tbl_sql.php?',
'tbl_sql.php',
],
],
[
'https://pma.7.3.local/fileDotPhp.php?db=aaaaa;table=a;server=14',
[
'fileDotPhp.php?',
'fileDotPhp.php',
],
],
[
'https://pma.7.3.local/secretFolder/fileDotPhp.php?db=aaaaa;table=a;server=14',
[
'fileDotPhp.php?',
'fileDotPhp.php',
],
],
[
'http://7.2.local/@williamdes/theREALphpMyAdminREPO/js/vendor/jquery/jquery-ui.min.js?v=5.0.0-dev',
[
'js/vendor/jquery/jquery-ui.min.js?v=5.0.0-dev',
'js/vendor/jquery/jquery-ui.min.js',
],
],
];
}
/**
* Test the url sanitization
*
* @param string $url The url to test
* @param array $result The result
*
* @dataProvider urlsToSanitize
*/
public function testSanitizeUrl(string $url, array $result): void
{
// $this->errorReport->sanitizeUrl
$this->assertSame(
$result,
$this->callFunction(
$this->errorReport,
ErrorReport::class,
'sanitizeUrl',
[$url]
)
);
}
}
|