Skip to content

Commit aa9e4ad

Browse files
committed
Reject unterminated comments in the MySQL lexer
MySQL treats a comment without its closing marker as a syntax error. The lexer accepted these, so an open comment could hide the rest of a statement and change what it does. For example, "WHERE a = 7 OR 1=1/*' AND b = 'c'" parsed without its last condition. https://bugs.mysql.com/bug.php?id=28779 https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc#L2026 https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc#L2076
1 parent 4de6b63 commit aa9e4ad

3 files changed

Lines changed: 70 additions & 12 deletions

File tree

‎packages/mysql-parser/src/class-wp-mysql-lexer.php‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,7 @@ private function read_next_token(): ?int {
955955
$type = $this->read_mysql_comment();
956956
} else {
957957
$this->bytes_already_read += 2;
958-
$this->read_comment_content();
959-
$type = self::COMMENT;
958+
$type = $this->read_comment_content();
960959
}
961960
} else {
962961
$this->bytes_already_read += 1;
@@ -1007,7 +1006,8 @@ private function read_next_token(): ?int {
10071006
$type = self::NCHAR_STRING;
10081007
}
10091008
} elseif ( null === $byte ) {
1010-
$type = self::EOF;
1009+
// An unterminated version comment is invalid input.
1010+
$type = $this->in_mysql_comment ? null : self::EOF;
10111011
} elseif ( '$' === $byte ) {
10121012
$type = $this->read_identifier();
10131013

@@ -1433,7 +1433,7 @@ private function read_line_comment(): int {
14331433
return self::COMMENT;
14341434
}
14351435

1436-
private function read_mysql_comment(): int {
1436+
private function read_mysql_comment(): ?int {
14371437
// @TODO: Consider supporting optimizer hints (/*+ ... */) or document
14381438
// that they are not supported.
14391439

@@ -1464,13 +1464,13 @@ private function read_mysql_comment(): int {
14641464
$nested_start = strpos( $this->sql, '/*', $this->bytes_already_read );
14651465
$comment_end = strpos( $this->sql, '*/', $this->bytes_already_read );
14661466
if ( false === $nested_start || ( false !== $comment_end && $comment_end < $nested_start ) ) {
1467-
break;
1467+
return $this->read_comment_content();
14681468
}
14691469
$this->bytes_already_read = $nested_start + 2;
1470-
$this->read_comment_content();
1470+
if ( null === $this->read_comment_content() ) {
1471+
return null; // Invalid input.
1472+
}
14711473
}
1472-
$this->read_comment_content();
1473-
return self::COMMENT;
14741474
} else {
14751475
// Version satisfied or not specified. Treat the content as SQL code.
14761476
$this->bytes_already_read += $version_length; // Skip the version number.
@@ -1479,13 +1479,13 @@ private function read_mysql_comment(): int {
14791479
}
14801480
}
14811481

1482-
private function read_comment_content(): void {
1482+
private function read_comment_content(): ?int {
14831483
$comment_end = strpos( $this->sql, '*/', $this->bytes_already_read );
14841484
if ( false === $comment_end ) {
1485-
$this->bytes_already_read = $this->sql_length;
1486-
} else {
1487-
$this->bytes_already_read = $comment_end + 2;
1485+
return null; // Invalid input.
14881486
}
1487+
$this->bytes_already_read = $comment_end + 2;
1488+
return self::COMMENT;
14891489
}
14901490

14911491
private function determine_identifier_or_keyword_type( string $value ): int {

‎packages/mysql-parser/tests/WP_MySQL_Lexer_Tests.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public function data_version_comments(): array {
167167

168168
// Other comments have their own rules.
169169
'regular comment' => array( 'SELECT 1 /* /* */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
170+
'closed at the end' => array( 'SELECT 1 /* a */', 80410, array( 'SELECT', '1' ) ),
171+
'applied, closed at end' => array( 'SELECT 1 /*!80000 + 2 */', 80410, array( 'SELECT', '1', '+', '2' ) ),
170172
'applied version comment' => array( 'SELECT 1 /*!80000 /* */ + 2 */', 80410, array( 'SELECT', '1', '+', '2' ) ),
171173
);
172174

@@ -349,6 +351,58 @@ public function test_invalid_input_returns_partial_stream_without_end_markers():
349351
$this->assertSame( array( 'SELECT', 'NUM', 'WITH' ), $names );
350352
}
351353

354+
/**
355+
* @dataProvider data_unterminated_comments
356+
*/
357+
public function test_unterminated_comment_is_invalid_input( string $sql, array $expected_bytes ): void {
358+
$read_bytes = static function ( array $tokens ): array {
359+
return array_map(
360+
static function ( WP_MySQL_Token $token ): string {
361+
return $token->get_bytes();
362+
},
363+
$tokens
364+
);
365+
};
366+
$this->assertSame( $expected_bytes, $read_bytes( ( new WP_MySQL_Lexer( $sql ) )->remaining_tokens() ) );
367+
368+
// The pull iterator follows the same path as remaining_tokens().
369+
$lexer = new WP_MySQL_Lexer( $sql );
370+
$tokens = array();
371+
while ( $lexer->next_token() ) {
372+
$tokens[] = $lexer->get_token();
373+
}
374+
$this->assertSame( $expected_bytes, $read_bytes( $tokens ) );
375+
}
376+
377+
public function data_unterminated_comments(): array {
378+
return array(
379+
// A regular comment.
380+
'text' => array( 'SELECT 1 /* a', array( 'SELECT', '1' ) ),
381+
'only an opening marker' => array( 'SELECT 1 /*', array( 'SELECT', '1' ) ),
382+
'overlapping markers' => array( 'SELECT 1 /*/', array( 'SELECT', '1' ) ),
383+
'nothing before the comment' => array( '/* a', array() ),
384+
'an earlier closed comment' => array( 'SELECT /* a */ 1 /* b', array( 'SELECT', '1' ) ),
385+
'a closing marker before it' => array( 'SELECT 1 */ /* a', array( 'SELECT', '1', '*', '/' ) ),
386+
'after WITH' => array( 'SELECT 1 WITH /* a', array( 'SELECT', '1', 'WITH' ) ),
387+
388+
// A version comment that applies.
389+
'applied' => array( 'SELECT 1 /*!80000 + 2', array( 'SELECT', '1', '+', '2' ) ),
390+
'applied, without a version' => array( 'SELECT 1 /*! + 2', array( 'SELECT', '1', '+', '2' ) ),
391+
'applied and empty' => array( 'SELECT 1 /*!80000', array( 'SELECT', '1' ) ),
392+
'applied, after a nested comment' => array( 'SELECT 1 /*!80000 /* a */ + 2', array( 'SELECT', '1', '+', '2' ) ),
393+
'applied, in a nested comment' => array( 'SELECT 1 /*!80000 /* a', array( 'SELECT', '1' ) ),
394+
'applied, a closing marker string' => array( "SELECT 1 /*!80000 + '*/'", array( 'SELECT', '1', '+', "'*/'" ) ),
395+
'applied, a commented-out marker' => array( 'SELECT 1 /*!80000 + 2 -- */', array( 'SELECT', '1', '+', '2' ) ),
396+
397+
// A version comment that doesn't apply.
398+
'skipped' => array( 'SELECT 1 /*!99999 a', array( 'SELECT', '1' ) ),
399+
'skipped and empty' => array( 'SELECT 1 /*!99999', array( 'SELECT', '1' ) ),
400+
'skipped, after a nested comment' => array( 'SELECT 1 /*!99999 /* a */ b', array( 'SELECT', '1' ) ),
401+
'skipped, in a nested comment' => array( 'SELECT 1 /*!99999 /* a', array( 'SELECT', '1' ) ),
402+
'skipped, overlapping markers' => array( 'SELECT 1 /*!99999 /*/ */', array( 'SELECT', '1' ) ),
403+
);
404+
}
405+
352406
public function test_system_variable_name_must_not_be_empty(): void {
353407
// As in MySQL, the name after "@@" can't be empty, and only backticks
354408
// can quote it. Otherwise, the stream ends after the "@@".

‎packages/mysql-parser/tests/WP_MySQL_Parser_Tests.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public function test_rejects_statements_invalid_in_mysql_84(): void {
156156
'SELECT 0.select', // "SELECT" after "0." is a keyword.
157157
'SELECT @@ version', // An empty name after "@@".
158158
"SELECT @@'version'", // A single-quoted name after "@@".
159+
'SELECT 1 /* a', // An unterminated comment.
160+
'SELECT 1 /*!80000 + 2', // An unterminated version comment.
161+
'SELECT 1 /*!99999 a', // An unterminated skipped comment.
162+
"DELETE FROM t WHERE a = 7 OR 1=1/*' AND b = 'c'", // A comment hides the rest.
159163
);
160164
foreach ( $statements as $sql ) {
161165
$this->assertNull( self::parse( $sql ), "Should not parse: $sql" );

0 commit comments

Comments
 (0)