Skip to content

Commit f73719a

Browse files
committed
Validate the SQLite busy timeout
Throw InvalidArgumentException when the busy timeout is not an integer from 0 to 2147483647, as with an invalid journal mode or synchronous setting. Before, a non-integer value silently fell back to the 10 second default. The plugin now passes SQLITE_BUSY_TIMEOUT to the driver as-is. The (int) cast turned values like false, null, or an empty string into 0, which disabled the wait instead of reporting the mistake.
1 parent 0e21f38 commit f73719a

6 files changed

Lines changed: 78 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ class WP_MySQL_On_SQLite extends PDO {
801801
* @type string|int|null $sqlite_synchronous Optional. SQLite synchronous setting.
802802
* }
803803
*
804-
* @throws InvalidArgumentException When the MySQL version is invalid.
804+
* @throws InvalidArgumentException When the MySQL version or an SQLite option is invalid.
805805
* @throws WP_MySQL_On_SQLite_Exception When the driver initialization fails.
806806
*/
807807
public function __construct(

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ public function __construct( array $options ) {
107107
// Throw exceptions on error.
108108
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
109109

110-
// Configure SQLite busy timeout.
111-
if ( isset( $options['busy_timeout'] ) && is_int( $options['busy_timeout'] ) ) {
112-
$busy_timeout = $options['busy_timeout'];
113-
} else {
114-
$busy_timeout = self::DEFAULT_SQLITE_BUSY_TIMEOUT;
110+
// Configure SQLite busy timeout. SQLite stores it as a 32-bit signed integer.
111+
$busy_timeout = $options['busy_timeout'] ?? self::DEFAULT_SQLITE_BUSY_TIMEOUT;
112+
if ( ! is_int( $busy_timeout ) || $busy_timeout < 0 || $busy_timeout > 2147483647 ) {
113+
throw new InvalidArgumentException(
114+
'Invalid SQLite busy timeout. It must be an integer from 0 to 2147483647.'
115+
);
115116
}
116117
$this->query( 'PRAGMA busy_timeout = ' . $busy_timeout );
117118

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,18 @@ public function test_busy_timeout_defaults_to_the_connection_default(): void {
404404
);
405405
}
406406

407+
public function test_rejects_invalid_busy_timeout(): void {
408+
$this->expectException( InvalidArgumentException::class );
409+
$this->expectExceptionMessage( 'Invalid SQLite busy timeout. It must be an integer from 0 to 2147483647.' );
410+
411+
new WP_MySQL_On_SQLite(
412+
'mysql-on-sqlite:path=:memory:;dbname=wp',
413+
null,
414+
null,
415+
array( 'sqlite_busy_timeout' => '10000' )
416+
);
417+
}
418+
407419
public function test_query(): void {
408420
$result = $this->driver->query( "SELECT 1, 'abc'" );
409421
$this->assertInstanceOf( WP_MySQL_On_SQLite_Statement::class, $result );

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,59 @@ public function testDriverKeepsConfiguredJournalMode(): void {
189189
$this->assertSame( 'delete', $this->get_journal_mode( $driver->get_connection() ) );
190190
}
191191

192+
public function testDefaultBusyTimeout(): void {
193+
$connection = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
194+
195+
$this->assertSame( '10000', $this->get_busy_timeout( $connection ) );
196+
}
197+
198+
public function testBusyTimeoutAcceptsZero(): void {
199+
$connection = new WP_SQLite_Connection(
200+
array(
201+
'path' => $this->db_path,
202+
'busy_timeout' => 0,
203+
)
204+
);
205+
206+
$this->assertSame( '0', $this->get_busy_timeout( $connection ) );
207+
}
208+
209+
public function testBusyTimeoutAcceptsMaximumValue(): void {
210+
$connection = new WP_SQLite_Connection(
211+
array(
212+
'path' => $this->db_path,
213+
'busy_timeout' => 2147483647,
214+
)
215+
);
216+
217+
$this->assertSame( '2147483647', $this->get_busy_timeout( $connection ) );
218+
}
219+
220+
/**
221+
* @dataProvider provideInvalidBusyTimeouts
222+
*/
223+
public function testInvalidBusyTimeoutThrows( $busy_timeout ): void {
224+
$this->expectException( InvalidArgumentException::class );
225+
$this->expectExceptionMessage( 'Invalid SQLite busy timeout. It must be an integer from 0 to 2147483647.' );
226+
new WP_SQLite_Connection(
227+
array(
228+
'path' => $this->db_path,
229+
'busy_timeout' => $busy_timeout,
230+
)
231+
);
232+
}
233+
234+
public function provideInvalidBusyTimeouts(): array {
235+
return array(
236+
'string' => array( '10000' ),
237+
'float' => array( 10000.0 ),
238+
'boolean' => array( false ),
239+
'array' => array( array( 10000 ) ),
240+
'negative' => array( -1 ),
241+
'above maximum' => array( 2147483648 ),
242+
);
243+
}
244+
192245
/**
193246
* Create the database file first, and then make its directory read-only,
194247
* so that the WAL sidecar files ("-wal", "-shm") cannot be created.
@@ -216,4 +269,8 @@ private function get_journal_mode( WP_SQLite_Connection $connection ): string {
216269
private function get_synchronous( WP_SQLite_Connection $connection ): string {
217270
return (string) $connection->query( 'PRAGMA synchronous' )->fetchColumn();
218271
}
272+
273+
private function get_busy_timeout( WP_SQLite_Connection $connection ): string {
274+
return (string) $connection->query( 'PRAGMA busy_timeout' )->fetchColumn();
275+
}
219276
}

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public function db_connect( $allow_bail = true ) {
453453
try {
454454
$options = array(
455455
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
456-
'sqlite_busy_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
456+
'sqlite_busy_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? SQLITE_BUSY_TIMEOUT : null,
457457
);
458458
$dbh = new WP_MySQL_On_SQLite(
459459
sprintf(

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/install-functions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function sqlite_make_db_sqlite() {
3030
null,
3131
array(
3232
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
33-
'sqlite_busy_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
33+
'sqlite_busy_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? SQLITE_BUSY_TIMEOUT : null,
3434
)
3535
);
3636
$translator->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO

0 commit comments

Comments
 (0)