Skip to content

Commit b93e47b

Browse files
committed
fix: resolve recorded db-path.php secret path for SQLite file operations
sqlite-database-integration 3.1 stores the database at a randomized path recorded in FQDBDIR/db-path.php when nothing explicit is configured. Teach get_sqlite_db_path() to consult that file after the explicit constants, so file operations target the real database instead of a legacy path. An explicitly configured DB_FILE takes precedence over the recorded path, matching the drop-in, and a recorded :memory: database is refused like the other in-memory configurations. Add a Behat scenario covering the recorded secret path with a legacy decoy database.
1 parent d640db8 commit b93e47b

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎features/db-sqlite-path.feature‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,45 @@ Feature: Select the configured SQLite database for file operations
6161
| | selected.sqlite | | | |
6262
| | | | selected.sqlite | / |
6363

64+
Scenario: Resolve the recorded secret path from db-path.php
65+
Given a WP install
66+
When I run `wp eval 'mkdir( "recorded" ); copy( defined( "DB_PATH" ) ? DB_PATH : FQDB, "recorded/selected.sqlite" ); copy( "recorded/selected.sqlite", ".ht.sqlite" ); file_put_contents( "recorded/db-path.php", "<?php return " . var_export( getcwd() . "/recorded/selected.sqlite", true ) . ";" );'`
67+
And I run `sqlite3 recorded/selected.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('selected');"`
68+
And I run `sqlite3 .ht.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('other');"`
69+
70+
Given a paths.php file:
71+
"""
72+
<?php
73+
define( 'FQDBDIR', __DIR__ . '/recorded/' );
74+
"""
75+
When I run `wp config set path_constants "require __DIR__ . '/paths.php'" --type=variable --raw`
76+
And I try `wp eval 'echo "ready";'`
77+
Then the return code should be 0
78+
79+
When I run `wp eval 'echo md5_file( ".ht.sqlite" );' --skip-wordpress`
80+
Then save STDOUT as {OTHER_HASH}
81+
82+
When I try `wp db export selected.sql`
83+
Then the return code should be 0
84+
And the selected.sql file should contain:
85+
"""
86+
INSERT INTO path_marker VALUES('selected');
87+
"""
88+
And the selected.sql file should not contain:
89+
"""
90+
INSERT INTO path_marker VALUES('other');
91+
"""
92+
93+
When I try `wp db drop --yes`
94+
Then the return code should be 0
95+
And the recorded/selected.sqlite file should not exist
96+
97+
When I run `wp eval 'echo md5_file( ".ht.sqlite" );' --skip-wordpress`
98+
Then STDOUT should be:
99+
"""
100+
{OTHER_HASH}
101+
"""
102+
64103
Scenario Outline: Reject file operations on an in-memory SQLite database
65104
Given a WP install
66105
And an input.sql file:

‎src/DB_Command_SQLite.php‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,28 @@ protected function get_sqlite_db_path() {
8383
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
8484

8585
// Check for FQDBDIR and construct path.
86-
$db_dir = defined( 'FQDBDIR' ) ? FQDBDIR : $wp_content_dir . '/database';
87-
$db_file = defined( 'DB_FILE' ) ? DB_FILE : '.ht.sqlite';
86+
$db_dir = defined( 'FQDBDIR' ) ? FQDBDIR : $wp_content_dir . '/database';
8887

89-
$db_path = rtrim( $db_dir, '/' ) . '/' . ltrim( $db_file, '/' );
88+
// DB_FILE selects a file inside FQDBDIR (sqlite-database-integration 3.1+).
89+
if ( defined( 'DB_FILE' ) ) {
90+
return rtrim( $db_dir, '/' ) . '/' . ltrim( DB_FILE, '/' );
91+
}
92+
93+
// Check for the recorded secret path (sqlite-database-integration 3.1+).
94+
$path_file = rtrim( $db_dir, '/\\' ) . '/db-path.php';
95+
if ( file_exists( $path_file ) ) {
96+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Failure is checked below.
97+
$recorded_path = @include $path_file;
98+
if ( is_string( $recorded_path ) && '' !== $recorded_path ) {
99+
if ( ':memory:' === $recorded_path ) {
100+
WP_CLI::error( 'This command does not support in-memory SQLite databases.' );
101+
}
102+
103+
return $recorded_path;
104+
}
105+
}
106+
107+
$db_path = rtrim( $db_dir, '/' ) . '/.ht.sqlite';
90108

91109
// If the file exists, return it.
92110
if ( file_exists( $db_path ) ) {

0 commit comments

Comments
 (0)