Skip to content

Commit 123ebef

Browse files
authored
Reject invalid BIT column defaults (#487)
## Summary Reject invalid `BIT` column defaults instead of preserving unnormalized input in the emulated information schema. Unsupported values now return MySQL-compatible error 1067. ## Why The `BIT` default accepted any value beginning with a valid prefix and stored the original value. `SHOW CREATE TABLE` later emitted that value without quoting, allowing untrusted text to become executable SQL in a generated MySQL dump. This is a hotfix that requires valid literal forms and rejects anything that cannot be safely normalized. Broader default validation based on the AST and full column definition should be done as a follow-up, also for other types. This prevents untrusted SQL from being embedded in generated MySQL DDL during migration.
1 parent fedd173 commit 123ebef

4 files changed

Lines changed: 120 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: 36 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
/*
@@ -2142,15 +2152,26 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21422152
private function get_bit_default( string $default_value ): ?string {
21432153
$value = strtolower( $default_value );
21442154

2155+
// An empty string coerces to zero.
2156+
if ( '' === $value ) {
2157+
return "b'0'";
2158+
}
2159+
21452160
// 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' );
2161+
if (
2162+
preg_match( "/\Ab'([01]*)'\z/", $value, $matches )
2163+
|| preg_match( '/\A0b([01]+)\z/', $value, $matches )
2164+
) {
2165+
$bits = ltrim( $matches[1], '0' );
21482166
return "b'" . ( '' === $bits ? '0' : $bits ) . "'";
21492167
}
21502168

21512169
// 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 ), "'" ) ) ) . "'";
2170+
if (
2171+
preg_match( "/\Ax'([0-9a-f]*)'\z/", $value, $matches )
2172+
|| preg_match( '/\A0x([0-9a-f]+)\z/', $value, $matches )
2173+
) {
2174+
return "b'" . decbin( hexdec( $matches[1] ) ) . "'";
21542175
}
21552176

21562177
// 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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ public function testShowCreateTableWithBitDefaultValues(): void {
704704
$this->assertQuery(
705705
"CREATE TABLE _tmp_bit_defaults (
706706
id INT DEFAULT 0,
707+
empty_string BIT(1) DEFAULT '',
707708
quoted_zero BIT(1) DEFAULT '0',
708709
integer_five BIT(4) DEFAULT 5,
709710
bit_literal_five BIT(4) DEFAULT b'0101',
@@ -718,6 +719,7 @@ public function testShowCreateTableWithBitDefaultValues(): void {
718719
$this->assertQuery( 'SHOW CREATE TABLE _tmp_bit_defaults;' );
719720
$results = $this->last_result;
720721
$create_table = $results[0]->{'Create Table'};
722+
$this->assertStringContainsString( "`empty_string` bit(1) DEFAULT b'0'", $create_table );
721723
$this->assertStringContainsString( "`quoted_zero` bit(1) DEFAULT b'0'", $create_table );
722724
$this->assertStringContainsString( "`integer_five` bit(4) DEFAULT b'101'", $create_table );
723725
$this->assertStringContainsString( "`bit_literal_five` bit(4) DEFAULT b'101'", $create_table );
@@ -734,6 +736,7 @@ public function testShowCreateTableWithBitDefaultValues(): void {
734736
array(
735737
(object) array(
736738
'id' => '1',
739+
'empty_string' => '0',
737740
'quoted_zero' => '0',
738741
'integer_five' => '5',
739742
'bit_literal_five' => '5',
@@ -748,6 +751,64 @@ public function testShowCreateTableWithBitDefaultValues(): void {
748751
);
749752
}
750753

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

0 commit comments

Comments
 (0)