Skip to content

Commit a5f9e89

Browse files
authored
Detect existing SQLite databases by file header, not presence (#448)
Backend auto-detection selected the sqlite backend whenever a candidate database path existed. Opening a SQLite path creates an empty file, so a stray zero-byte wp-content/database/.ht.sqlite moved an mdi-native site onto SQLite. Accept an inferred candidate only when it starts with the SQLite format 3 header; an explicit MARKDOWN_DB_BACKEND is unchanged. Fixes #447
1 parent 9bf6fee commit a5f9e89

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

‎db.php‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ static function () use ( $verifier, $report_path ): void {
6464
}
6565
}
6666

67+
if ( ! function_exists( 'markdown_database_integration_path_is_sqlite' ) ) {
68+
/**
69+
* Whether a path holds a SQLite database, judged by its file header.
70+
*
71+
* Opening a SQLite path creates an empty file, so presence alone does not
72+
* prove a database exists. Reads only the 16-byte header; plugin classes
73+
* are not loaded yet when the drop-in runs.
74+
*/
75+
function markdown_database_integration_path_is_sqlite( string $path ): bool {
76+
if ( ! is_file( $path ) || ! is_readable( $path ) ) {
77+
return false;
78+
}
79+
$handle = @fopen( $path, 'rb' );
80+
if ( false === $handle ) {
81+
return false;
82+
}
83+
$header = fread( $handle, 16 );
84+
fclose( $handle );
85+
return "SQLite format 3\0" === $header;
86+
}
87+
}
88+
6789
if ( ! function_exists( 'markdown_database_integration_configured_backend' ) ) {
6890
/**
6991
* Resolve the backend that owns this bootstrap.
@@ -72,29 +94,34 @@ static function () use ( $verifier, $report_path ): void {
7294
* file store is authoritative and mdi-native serves it directly, which is
7395
* why SQLite Database Integration is optional rather than required.
7496
*
75-
* An install that already holds a SQLite database keeps using it, so
76-
* upgrading never moves a running site onto a different query engine
77-
* without the operator asking for it.
97+
* Inferred database paths are accepted only when they are readable regular
98+
* files with the SQLite format 3 header. An install that already holds a
99+
* SQLite database keeps using it, so upgrading never moves a running site
100+
* onto a different query engine without the operator asking for it.
78101
*/
79102
function markdown_database_integration_configured_backend(): string {
80103
if ( defined( 'MARKDOWN_DB_BACKEND' ) ) {
81104
return (string) MARKDOWN_DB_BACKEND;
82105
}
83106
if ( defined( 'DB_PATH' ) || defined( 'FQDB' ) ) {
84107
$path = defined( 'DB_PATH' ) ? DB_PATH : FQDB;
85-
return is_string( $path ) && is_file( $path ) ? 'sqlite' : 'mdi-native';
108+
return is_string( $path ) && markdown_database_integration_path_is_sqlite( $path ) ? 'sqlite' : 'mdi-native';
86109
}
87110

88111
$directory = markdown_database_integration_sqlite_directory();
89112
if ( defined( 'DB_FILE' ) ) {
90-
return is_file( $directory . DB_FILE ) ? 'sqlite' : 'mdi-native';
113+
return markdown_database_integration_path_is_sqlite( $directory . DB_FILE ) ? 'sqlite' : 'mdi-native';
91114
}
92115

93-
// Handle the randomized secret database path.
116+
// db-path.php records a randomized database path as executable PHP.
117+
// Detection never runs it, so its presence alone selects SQLite.
94118
if ( is_file( $directory . 'db-path.php' ) ) {
95119
return 'sqlite';
96120
}
97-
return is_file( $directory . '.ht.sqlite' ) || is_file( $directory . '.ht.sqlite.php' ) ? 'sqlite' : 'mdi-native';
121+
// .ht.sqlite.php is the integration's legacy name for the same raw
122+
// SQLite file; it renames it to .ht.sqlite on first connection.
123+
return markdown_database_integration_path_is_sqlite( $directory . '.ht.sqlite' )
124+
|| markdown_database_integration_path_is_sqlite( $directory . '.ht.sqlite.php' ) ? 'sqlite' : 'mdi-native';
98125
}
99126
}
100127

‎tests/smoke-dropin-backend-selection.php‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@
124124
if ( 'default-native' !== $scenario ) {
125125
mkdir( $database_dir, 0755, true );
126126
}
127-
if ( in_array( $scenario, array( 'default-legacy-sqlite', 'default-legacy-php', 'default-db-dir', 'default-fqdbdir', 'default-missing-db-path', 'default-missing-fqdb', 'default-missing-db-file', 'default-interrupted-migration', 'mdi-native', 'mysql-content', 'mysql-full' ), true ) ) {
128-
file_put_contents( $database_dir . ( 'default-legacy-php' === $scenario ? '/.ht.sqlite.php' : '/.ht.sqlite' ), 'legacy database' );
127+
if ( in_array( $scenario, array( 'default-legacy-sqlite', 'default-db-dir', 'default-fqdbdir', 'default-missing-db-path', 'default-missing-fqdb', 'default-missing-db-file', 'default-interrupted-migration', 'mdi-native', 'mysql-content', 'mysql-full' ), true ) ) {
128+
file_put_contents( $database_dir . '/.ht.sqlite', "SQLite format 3\0" . str_repeat( "\0", 16 ) );
129+
$expected_backend = 'sqlite';
130+
}
131+
if ( 'default-legacy-php' === $scenario ) {
132+
file_put_contents( $database_dir . '/.ht.sqlite.php', "SQLite format 3\0" . str_repeat( "\0", 16 ) );
129133
$expected_backend = 'sqlite';
130134
}
131135
if ( in_array( $scenario, array( 'default-randomized', 'default-missing-recorded', 'default-interrupted-migration' ), true ) ) {
@@ -137,19 +141,22 @@
137141
file_put_contents( $recorded, 'managed database' );
138142
}
139143
}
144+
if ( 'default-empty-ht' === $scenario || 'default-nonsqlite-ht' === $scenario ) {
145+
file_put_contents( $database_dir . '/.ht.sqlite', 'default-empty-ht' === $scenario ? '' : 'not sqlite' );
146+
}
140147
if ( 'default-unread-metadata' === $scenario ) {
141148
file_put_contents( $database_dir . '/db-path.php', '<?php throw new RuntimeException( "Detection read metadata." );' );
142149
$expected_backend = 'sqlite';
143150
}
144-
if ( in_array( $scenario, array( 'default-db-path', 'default-conflicting-paths', 'default-fqdb', 'default-db-file' ), true ) ) {
151+
if ( in_array( $scenario, array( 'default-db-path', 'default-conflicting-paths', 'default-fqdb', 'default-db-file', 'default-empty-fqdb' ), true ) ) {
145152
$constant = match ( $scenario ) {
146-
'default-fqdb' => 'FQDB',
153+
'default-fqdb', 'default-empty-fqdb' => 'FQDB',
147154
'default-db-file' => 'DB_FILE',
148155
default => 'DB_PATH',
149156
};
150157
define( $constant, 'DB_FILE' === $constant ? 'custom.sqlite' : $database_dir . '/custom.sqlite' );
151-
file_put_contents( $database_dir . '/custom.sqlite', 'configured database' );
152-
$expected_backend = 'sqlite';
158+
file_put_contents( $database_dir . '/custom.sqlite', 'default-empty-fqdb' === $scenario ? '' : "SQLite format 3\0" . str_repeat( "\0", 16 ) );
159+
$expected_backend = 'default-empty-fqdb' === $scenario ? 'mdi-native' : 'sqlite';
153160
if ( 'default-conflicting-paths' === $scenario ) {
154161
define( 'FQDB', $database_dir . '/missing.sqlite' );
155162
}
@@ -179,7 +186,11 @@
179186
if ( 'mysql-content' === $scenario ) {
180187
define( 'MARKDOWN_DB_BACKEND', 'mysql-content' );
181188
}
182-
if ( 'unknown' === $scenario ) {
189+
if ( 'explicit-empty-sqlite' === $scenario ) {
190+
define( 'MARKDOWN_DB_BACKEND', 'sqlite' );
191+
file_put_contents( $database_dir . '/.ht.sqlite', '' );
192+
$storage_before = $snapshot();
193+
} elseif ( 'unknown' === $scenario ) {
183194
define( 'MARKDOWN_DB_BACKEND', 'unknown' );
184195
} elseif ( 'incomplete' === $scenario ) {
185196
define( 'MARKDOWN_DB_BACKEND', 'incomplete' );
@@ -297,7 +308,7 @@ class wpdb { public string $prefix = 'wp_'; public string $base_prefix = 'wp_';
297308
echo "PASS: {$scenario} explicitly falls back to stock wpdb.\n";
298309
exit( 0 );
299310
}
300-
if ( 'sqlite' !== $scenario || ! isset( $GLOBALS['wpdb'] ) ) {
311+
if ( ! in_array( $scenario, array( 'sqlite', 'explicit-empty-sqlite' ), true ) || ! isset( $GLOBALS['wpdb'] ) ) {
301312
throw new RuntimeException( 'Expected backend bootstrap failure.' );
302313
}
303314
echo "PASS: an explicitly configured SQLite backend keeps the SQLite drop-in path.\n";
@@ -317,7 +328,7 @@ class wpdb { public string $prefix = 'wp_'; public string $base_prefix = 'wp_';
317328
}
318329

319330
$failed = 0;
320-
foreach ( array( 'sqlite', 'primary-cold-index', 'primary-warm-index', 'default-native', 'default-legacy-sqlite', 'default-legacy-php', 'default-db-dir', 'default-fqdbdir', 'default-randomized', 'default-missing-recorded', 'default-interrupted-migration', 'default-unread-metadata', 'default-db-path', 'default-fqdb', 'default-db-file', 'default-conflicting-paths', 'default-missing-db-path', 'default-missing-fqdb', 'default-missing-db-file', 'mdi-native', 'mysql-content', 'mysql-full', 'mysql-full-shadow', 'mysql-full-no-sink', 'mysql-full-prior-outbox', 'mysql-full-incompatible', 'mysql-full-outbox-incompatible', 'unknown', 'incomplete' ) as $scenario ) {
331+
foreach ( array( 'sqlite', 'explicit-empty-sqlite', 'default-empty-ht', 'default-nonsqlite-ht', 'default-empty-fqdb', 'primary-cold-index', 'primary-warm-index', 'default-native', 'default-legacy-sqlite', 'default-legacy-php', 'default-db-dir', 'default-fqdbdir', 'default-randomized', 'default-missing-recorded', 'default-interrupted-migration', 'default-unread-metadata', 'default-db-path', 'default-fqdb', 'default-db-file', 'default-conflicting-paths', 'default-missing-db-path', 'default-missing-fqdb', 'default-missing-db-file', 'mdi-native', 'mysql-content', 'mysql-full', 'mysql-full-shadow', 'mysql-full-no-sink', 'mysql-full-prior-outbox', 'mysql-full-incompatible', 'mysql-full-outbox-incompatible', 'unknown', 'incomplete' ) as $scenario ) {
321332
passthru( escapeshellarg( PHP_BINARY ) . ' ' . escapeshellarg( __FILE__ ) . ' ' . escapeshellarg( $scenario ), $status );
322333
if ( 0 !== $status ) {
323334
++$failed;

0 commit comments

Comments
 (0)