Skip to content

Commit eab502d

Browse files
des1roerclaude
andcommitted
Fix TypeError on EXPLAIN FOR CONNECTION without integer ID
ExplainStatement::parse() assigned the token after `FOR CONNECTION` directly to the `int|null $connectionId` property, so any non-integer token (identifier, float, delimiter, end of query) threw an uncaught TypeError instead of producing a parser error. Report "Unexpected token." in that case, as KillStatement does, and drop the now obsolete phpstan/psalm baseline entries. Refs CVE-2026-121262 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: des1roer <des1roer@gmail.com>
1 parent 4a90c3c commit eab502d

12 files changed

Lines changed: 598 additions & 14 deletions

‎phpstan-baseline.neon‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,12 +1230,6 @@ parameters:
12301230
count: 1
12311231
path: src/Statements/ExplainStatement.php
12321232

1233-
-
1234-
message: '#^Cannot access property \$value on PhpMyAdmin\\SqlParser\\Token\|null\.$#'
1235-
identifier: property.nonObject
1236-
count: 1
1237-
path: src/Statements/ExplainStatement.php
1238-
12391233
-
12401234
message: '#^Class PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement has an uninitialized property \$statementAlias\. Give it default value or assign it in the constructor\.$#'
12411235
identifier: property.uninitialized
@@ -1260,12 +1254,6 @@ parameters:
12601254
count: 1
12611255
path: src/Statements/ExplainStatement.php
12621256

1263-
-
1264-
message: '#^Property PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement\:\:\$connectionId \(int\|null\) does not accept bool\|float\|int\|string\.$#'
1265-
identifier: assign.propertyType
1266-
count: 1
1267-
path: src/Statements/ExplainStatement.php
1268-
12691257
-
12701258
message: '#^Property PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement\:\:\$explainedDatabase \(string\|null\) does not accept bool\|float\|int\|string\.$#'
12711259
identifier: assign.propertyType

‎psalm-baseline.xml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,11 @@
704704
<file src="src/Statements/ExplainStatement.php">
705705
<PossiblyNullPropertyFetch>
706706
<code><![CDATA[$nextToken->keyword]]></code>
707-
<code><![CDATA[$nextToken->value]]></code>
708707
</PossiblyNullPropertyFetch>
709708
<PropertyNotSetInConstructor>
710709
<code><![CDATA[$statementAlias]]></code>
711710
</PropertyNotSetInConstructor>
712711
<PropertyTypeCoercion>
713-
<code><![CDATA[$nextToken->value]]></code>
714712
<code><![CDATA[$token->value]]></code>
715713
<code><![CDATA[$token->value]]></code>
716714
</PropertyTypeCoercion>

‎src/Statements/ExplainStatement.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\SqlParser\TokenType;
1313

1414
use function array_slice;
15+
use function is_int;
1516

1617
/**
1718
* `EXPLAIN` statement.
@@ -161,6 +162,11 @@ public function parse(Parser $parser, TokensList $list): void
161162
$list->idx++; // Ignore the current token
162163
$list->getNext(); // CONNECTION
163164
$nextToken = $list->getNext(); // Identifier
165+
if ($nextToken === null || $nextToken->type !== TokenType::Number || ! is_int($nextToken->value)) {
166+
$parser->error('Unexpected token.', $nextToken);
167+
break;
168+
}
169+
164170
$this->connectionId = $nextToken->value;
165171
break;
166172
}

‎tests/Parser/ExplainStatementTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ public static function explainProvider(): array
3434
['parser/parseExplain12'],
3535
['parser/parseExplain13'],
3636
['parser/parseExplain14'],
37+
['parser/parseExplain15'],
3738
['parser/parseExplainErr'],
3839
['parser/parseExplainErr1'],
3940
['parser/parseExplainErr2'],
4041
['parser/parseExplainErr3'],
42+
['parser/parseExplainErr4'],
43+
['parser/parseExplainErr5'],
44+
['parser/parseExplainErr6'],
4145
];
4246
}
4347
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXPLAIN FOR CONNECTION 458
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"query": "EXPLAIN FOR CONNECTION 458",
3+
"lexer": {
4+
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5+
"strict": false,
6+
"errors": [],
7+
"str": "EXPLAIN FOR CONNECTION 458",
8+
"len": 26,
9+
"last": 26,
10+
"list": {
11+
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
12+
"count": 8,
13+
"idx": 8,
14+
"tokens": [
15+
{
16+
"@type": "PhpMyAdmin\\SqlParser\\Token",
17+
"token": "EXPLAIN",
18+
"value": "EXPLAIN",
19+
"keyword": "EXPLAIN",
20+
"type": {
21+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
22+
"name": "Keyword",
23+
"value": 1
24+
},
25+
"flags": 3,
26+
"position": 0
27+
},
28+
{
29+
"@type": "PhpMyAdmin\\SqlParser\\Token",
30+
"token": " ",
31+
"value": " ",
32+
"keyword": null,
33+
"type": {
34+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
35+
"name": "Whitespace",
36+
"value": 3
37+
},
38+
"flags": 0,
39+
"position": 7
40+
},
41+
{
42+
"@type": "PhpMyAdmin\\SqlParser\\Token",
43+
"token": "FOR",
44+
"value": "FOR",
45+
"keyword": "FOR",
46+
"type": {
47+
"@type": "@3"
48+
},
49+
"flags": 3,
50+
"position": 8
51+
},
52+
{
53+
"@type": "PhpMyAdmin\\SqlParser\\Token",
54+
"token": " ",
55+
"value": " ",
56+
"keyword": null,
57+
"type": {
58+
"@type": "@5"
59+
},
60+
"flags": 0,
61+
"position": 11
62+
},
63+
{
64+
"@type": "PhpMyAdmin\\SqlParser\\Token",
65+
"token": "CONNECTION",
66+
"value": "CONNECTION",
67+
"keyword": "CONNECTION",
68+
"type": {
69+
"@type": "@3"
70+
},
71+
"flags": 1,
72+
"position": 12
73+
},
74+
{
75+
"@type": "PhpMyAdmin\\SqlParser\\Token",
76+
"token": " ",
77+
"value": " ",
78+
"keyword": null,
79+
"type": {
80+
"@type": "@5"
81+
},
82+
"flags": 0,
83+
"position": 22
84+
},
85+
{
86+
"@type": "PhpMyAdmin\\SqlParser\\Token",
87+
"token": "458",
88+
"value": 458,
89+
"keyword": null,
90+
"type": {
91+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
92+
"name": "Number",
93+
"value": 6
94+
},
95+
"flags": 0,
96+
"position": 23
97+
},
98+
{
99+
"@type": "PhpMyAdmin\\SqlParser\\Token",
100+
"token": "",
101+
"value": "",
102+
"keyword": null,
103+
"type": {
104+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
105+
"name": "Delimiter",
106+
"value": 9
107+
},
108+
"flags": 0,
109+
"position": null
110+
}
111+
]
112+
},
113+
"delimiter": ";",
114+
"delimiterLen": 1
115+
},
116+
"parser": {
117+
"@type": "PhpMyAdmin\\SqlParser\\Parser",
118+
"strict": false,
119+
"errors": [],
120+
"list": {
121+
"@type": "@1"
122+
},
123+
"statements": [
124+
{
125+
"@type": "PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement",
126+
"bodyParser": null,
127+
"statementAlias": "EXPLAIN",
128+
"connectionId": 458,
129+
"explainedDatabase": null,
130+
"explainedTable": null,
131+
"explainedColumn": null,
132+
"options": {
133+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
134+
"options": []
135+
},
136+
"first": 0,
137+
"last": 7
138+
}
139+
],
140+
"brackets": 0
141+
},
142+
"errors": {
143+
"lexer": [],
144+
"parser": []
145+
}
146+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXPLAIN FOR CONNECTION
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"query": "EXPLAIN FOR CONNECTION",
3+
"lexer": {
4+
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5+
"strict": false,
6+
"errors": [],
7+
"str": "EXPLAIN FOR CONNECTION",
8+
"len": 22,
9+
"last": 22,
10+
"list": {
11+
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
12+
"count": 6,
13+
"idx": 7,
14+
"tokens": [
15+
{
16+
"@type": "PhpMyAdmin\\SqlParser\\Token",
17+
"token": "EXPLAIN",
18+
"value": "EXPLAIN",
19+
"keyword": "EXPLAIN",
20+
"type": {
21+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
22+
"name": "Keyword",
23+
"value": 1
24+
},
25+
"flags": 3,
26+
"position": 0
27+
},
28+
{
29+
"@type": "PhpMyAdmin\\SqlParser\\Token",
30+
"token": " ",
31+
"value": " ",
32+
"keyword": null,
33+
"type": {
34+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
35+
"name": "Whitespace",
36+
"value": 3
37+
},
38+
"flags": 0,
39+
"position": 7
40+
},
41+
{
42+
"@type": "PhpMyAdmin\\SqlParser\\Token",
43+
"token": "FOR",
44+
"value": "FOR",
45+
"keyword": "FOR",
46+
"type": {
47+
"@type": "@3"
48+
},
49+
"flags": 3,
50+
"position": 8
51+
},
52+
{
53+
"@type": "PhpMyAdmin\\SqlParser\\Token",
54+
"token": " ",
55+
"value": " ",
56+
"keyword": null,
57+
"type": {
58+
"@type": "@5"
59+
},
60+
"flags": 0,
61+
"position": 11
62+
},
63+
{
64+
"@type": "PhpMyAdmin\\SqlParser\\Token",
65+
"token": "CONNECTION",
66+
"value": "CONNECTION",
67+
"keyword": "CONNECTION",
68+
"type": {
69+
"@type": "@3"
70+
},
71+
"flags": 1,
72+
"position": 12
73+
},
74+
{
75+
"@type": "PhpMyAdmin\\SqlParser\\Token",
76+
"token": "",
77+
"value": "",
78+
"keyword": null,
79+
"type": {
80+
"@type": "PhpMyAdmin\\SqlParser\\TokenType",
81+
"name": "Delimiter",
82+
"value": 9
83+
},
84+
"flags": 0,
85+
"position": null
86+
}
87+
]
88+
},
89+
"delimiter": ";",
90+
"delimiterLen": 1
91+
},
92+
"parser": {
93+
"@type": "PhpMyAdmin\\SqlParser\\Parser",
94+
"strict": false,
95+
"errors": [],
96+
"list": {
97+
"@type": "@1"
98+
},
99+
"statements": [
100+
{
101+
"@type": "PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement",
102+
"bodyParser": null,
103+
"statementAlias": "EXPLAIN",
104+
"connectionId": null,
105+
"explainedDatabase": null,
106+
"explainedTable": null,
107+
"explainedColumn": null,
108+
"options": {
109+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
110+
"options": []
111+
},
112+
"first": 0,
113+
"last": 6
114+
}
115+
],
116+
"brackets": 0
117+
},
118+
"errors": {
119+
"lexer": [],
120+
"parser": [
121+
[
122+
"Unexpected token.",
123+
{
124+
"@type": "@9"
125+
},
126+
0
127+
]
128+
]
129+
}
130+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXPLAIN FOR CONNECTION abc

0 commit comments

Comments
 (0)