Skip to content

Commit 5368a35

Browse files
committed
Reject invalid BIT column defaults
Require exact BIT and hexadecimal literal forms during normalization. Reject values that cannot be normalized with MySQL error 1067 so arbitrary strings cannot be stored and emitted as SQL.
1 parent fc3a703 commit 5368a35

4 files changed

Lines changed: 116 additions & 15 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7908,6 +7908,14 @@ private function convert_information_schema_exception( WP_SQLite_Information_Sch
79087908
null,
79097909
array( '42000', 1072, $driver_message )
79107910
);
7911+
case WP_SQLite_Information_Schema_Exception::TYPE_INVALID_DEFAULT_VALUE:
7912+
$driver_message = sprintf( "Invalid default value for '%s'", $e->get_data()['column_name'] );
7913+
return $this->new_driver_exception(
7914+
'SQLSTATE[42000]: Syntax error or access violation: 1067 ' . $driver_message,
7915+
'42000',
7916+
null,
7917+
array( '42000', 1067, $driver_message )
7918+
);
79117919
case WP_SQLite_Information_Schema_Exception::TYPE_CONSTRAINT_DOES_NOT_EXIST:
79127920
$driver_message = sprintf( "Constraint '%s' does not exist.", $e->get_data()['name'] );
79137921
return $this->new_driver_exception(

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ private function record_drop_check_constraint(
15071507
private function extract_column_data( string $table_name, string $column_name, WP_Parser_Node $node, int $position ): array {
15081508
list ( $data_type, $column_type ) = $this->get_column_data_types( $node );
15091509

1510-
$default = $this->get_column_default( $node, $data_type );
1510+
$default = $this->get_column_default( $node, $data_type, $column_name );
15111511
$nullable = $this->get_column_nullable( $node );
15121512
$key = $this->get_column_key( $node );
15131513
$extra = $this->get_column_extra( $node );
@@ -2043,11 +2043,12 @@ private function get_table_comment( WP_Parser_Node $node ): string {
20432043
/**
20442044
* Extract column default value from the "columnDefinition" or "fieldDefinition" AST node.
20452045
*
2046-
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2047-
* @param string $data_type The column data type as stored in information schema.
2048-
* @return string|null The column default as stored in information schema.
2046+
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2047+
* @param string $data_type The column data type as stored in information schema.
2048+
* @param string $column_name The column name.
2049+
* @return string|null The column default as stored in information schema.
20492050
*/
2050-
private function get_column_default( WP_Parser_Node $node, string $data_type ): ?string {
2051+
private function get_column_default( WP_Parser_Node $node, string $data_type, string $column_name ): ?string {
20512052
$default_attr = null;
20522053
foreach ( $node->get_descendant_nodes( 'columnAttribute' ) as $attr ) {
20532054
if ( $attr->has_child_token( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2081,7 +2082,7 @@ private function get_column_default( WP_Parser_Node $node, string $data_type ):
20812082
// A signed number, such as "-5", has no "literal" child node.
20822083
return $this->get_value( $signed_literal );
20832084
}
2084-
return $this->get_literal_default( $literal, $data_type );
2085+
return $this->get_literal_default( $literal, $data_type, $column_name );
20852086
}
20862087

20872088
// DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2096,11 +2097,12 @@ private function get_column_default( WP_Parser_Node $node, string $data_type ):
20962097
/**
20972098
* Extract and normalize a literal default value.
20982099
*
2099-
* @param WP_Parser_Node $literal The "literal" AST node.
2100-
* @param string $data_type The column data type as stored in information schema.
2101-
* @return string|null The default value as stored in information schema.
2100+
* @param WP_Parser_Node $literal The "literal" AST node.
2101+
* @param string $data_type The column data type as stored in information schema.
2102+
* @param string $column_name The column name.
2103+
* @return string|null The default value as stored in information schema.
21022104
*/
2103-
private function get_literal_default( WP_Parser_Node $literal, string $data_type ): ?string {
2105+
private function get_literal_default( WP_Parser_Node $literal, string $data_type, string $column_name ): ?string {
21042106
// DEFAULT NULL
21052107
if ( $literal->has_child_node( 'nullLiteral' ) ) {
21062108
return null;
@@ -2116,7 +2118,15 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21162118
$default = $this->get_value( $literal );
21172119

21182120
if ( 'bit' === $data_type ) {
2119-
return $this->get_bit_default( $default ) ?? $default;
2121+
/*
2122+
* @TODO: Validate and normalize defaults from their AST and the full
2123+
* column definition before storing them in the information schema.
2124+
*/
2125+
$bit_default = $this->get_bit_default( $default );
2126+
if ( null === $bit_default ) {
2127+
throw WP_SQLite_Information_Schema_Exception::invalid_default_value( $column_name );
2128+
}
2129+
return $bit_default;
21202130
}
21212131

21222132
/*
@@ -2143,14 +2153,20 @@ private function get_bit_default( string $default_value ): ?string {
21432153
$value = strtolower( $default_value );
21442154

21452155
// Bit literal, e.g. b'101' or 0b101.
2146-
if ( str_starts_with( $value, "b'" ) || str_starts_with( $value, '0b' ) ) {
2147-
$bits = ltrim( rtrim( substr( $value, 2 ), "'" ), '0' );
2156+
if (
2157+
preg_match( "/\Ab'([01]*)'\z/", $value, $matches )
2158+
|| preg_match( '/\A0b([01]+)\z/', $value, $matches )
2159+
) {
2160+
$bits = ltrim( $matches[1], '0' );
21482161
return "b'" . ( '' === $bits ? '0' : $bits ) . "'";
21492162
}
21502163

21512164
// Hex literal, e.g. x'05' or 0x05.
2152-
if ( str_starts_with( $value, "x'" ) || str_starts_with( $value, '0x' ) ) {
2153-
return "b'" . decbin( hexdec( rtrim( substr( $value, 2 ), "'" ) ) ) . "'";
2165+
if (
2166+
preg_match( "/\Ax'([0-9a-f]*)'\z/", $value, $matches )
2167+
|| preg_match( '/\A0x([0-9a-f]+)\z/', $value, $matches )
2168+
) {
2169+
return "b'" . decbin( hexdec( $matches[1] ) ) . "'";
21542170
}
21552171

21562172
// Decimal, e.g. 5, or a numeric string literal such as '0'.

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-exception.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WP_SQLite_Information_Schema_Exception extends Exception {
1616
const TYPE_KEY_COLUMN_NOT_FOUND = 'key-column-not-found';
1717
const TYPE_CONSTRAINT_DOES_NOT_EXIST = 'constraint-does-not-exist';
1818
const TYPE_MULTIPLE_CONSTRAINTS_WITH_NAME = 'multiple-constraints-with-name';
19+
const TYPE_INVALID_DEFAULT_VALUE = 'invalid-default-value';
1920

2021
/**
2122
* The exception type.
@@ -151,4 +152,18 @@ public static function multiple_constraints_with_name( string $name ): WP_SQLite
151152
array( 'name' => $name )
152153
);
153154
}
155+
156+
/**
157+
* Create an invalid default value exception.
158+
*
159+
* @param string $column_name The name of the affected column.
160+
* @return self The exception instance.
161+
*/
162+
public static function invalid_default_value( string $column_name ): WP_SQLite_Information_Schema_Exception {
163+
return new self(
164+
self::TYPE_INVALID_DEFAULT_VALUE,
165+
sprintf( "Invalid default value for '%s'.", $column_name ),
166+
array( 'column_name' => $column_name )
167+
);
168+
}
154169
}

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,68 @@ public function testShowCreateTableWithBitDefaultValues(): void {
748748
);
749749
}
750750

751+
/**
752+
* @dataProvider invalidBitDefaultQueries
753+
*/
754+
public function testRejectsInvalidBitDefaultValues( string $query, string $column_name ): void {
755+
$this->assertQuery( 'CREATE TABLE _tmp_invalid_bit_default (id INT, injected BIT(1))' );
756+
757+
$exception = null;
758+
try {
759+
$this->query( $query );
760+
} catch ( WP_MySQL_On_SQLite_Exception $e ) {
761+
$exception = $e;
762+
}
763+
764+
$driver_message = sprintf( "Invalid default value for '%s'", $column_name );
765+
$this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception );
766+
$this->assertSame(
767+
'SQLSTATE[42000]: Syntax error or access violation: 1067 ' . $driver_message,
768+
$exception->getMessage()
769+
);
770+
$this->assertSame( '42000', $exception->getCode() );
771+
$this->assertSame( array( '42000', 1067, $driver_message ), $exception->errorInfo );
772+
}
773+
774+
public static function invalidBitDefaultQueries(): array {
775+
$payload = "0,\n PRIMARY KEY (`id`)\n); DROP TABLE users; --";
776+
777+
return array(
778+
'CREATE TABLE' => array(
779+
"CREATE TABLE _tmp_created_invalid_bit_default (injected BIT(1) DEFAULT '$payload')",
780+
'injected',
781+
),
782+
'ALTER TABLE ADD' => array(
783+
"ALTER TABLE _tmp_invalid_bit_default ADD COLUMN injected_2 BIT(1) DEFAULT '$payload'",
784+
'injected_2',
785+
),
786+
'ALTER TABLE MODIFY' => array(
787+
"ALTER TABLE _tmp_invalid_bit_default MODIFY COLUMN injected BIT(1) DEFAULT '$payload'",
788+
'injected',
789+
),
790+
'empty string' => array(
791+
"ALTER TABLE _tmp_invalid_bit_default MODIFY COLUMN injected BIT(1) DEFAULT ''",
792+
'injected',
793+
),
794+
'bit-like string' => array(
795+
"ALTER TABLE _tmp_invalid_bit_default MODIFY COLUMN injected BIT(1) DEFAULT 'b''0''; DROP TABLE users; -- '",
796+
'injected',
797+
),
798+
'0b-like string' => array(
799+
"ALTER TABLE _tmp_invalid_bit_default MODIFY COLUMN injected BIT(1) DEFAULT '0b0''; DROP TABLE users; -- '",
800+
'injected',
801+
),
802+
'hex-like string' => array(
803+
"ALTER TABLE _tmp_invalid_bit_default MODIFY COLUMN injected BIT(1) DEFAULT 'x''05''; DROP TABLE users; -- '",
804+
'injected',
805+
),
806+
'0x-like string' => array(
807+
"ALTER TABLE _tmp_invalid_bit_default MODIFY COLUMN injected BIT(1) DEFAULT '0x05; DROP TABLE users; -- '",
808+
'injected',
809+
),
810+
);
811+
}
812+
751813
public function testUpdateWithBitColumnDefaultValue(): void {
752814
$this->assertQuery(
753815
"CREATE TABLE _tmp_bit_defaults (

0 commit comments

Comments
 (0)