Repository navigation
Commit a8468b5
authored
Add randomized SQLite database storage (#502)
## Summary
> [!NOTE]
> This is an alternative approach to #494. It keeps the same randomized
storage design, but delegates coordination to SQLite's VFS and locking
protocol through a dedicated SQLite database.
This PR adds **randomized storage** for SQLite databases as an
additional layer of protection against direct web access.
- **Randomized paths:** New managed databases are stored in a directory
generated from 128 bits of randomness.
- **Portable discovery:** `db-path.php` records the database location
relative to the managed database root.
- **SQLite-backed locking:** Setup and maintenance are coordinated
through a dedicated SQLite locking database.
- **Database isolation:** Maintenance also acquires an exclusive lock on
an existing database before changing it.
- **Automatic migration:** Existing `.ht.sqlite` and `.ht.sqlite.php`
databases are moved to the randomized layout.
- **Explicit path compatibility:** User-configured database paths and
`:memory:` continue to work unchanged.
- **Retryable setup:** Another request can complete setup after an
interruption.
- **Safer failures:** Filesystem errors do not expose the randomized
database path.
## Managed storage
When no explicit database path is configured, the default layout is:
```text
wp-content/
└── database/
├── .htaccess
├── index.php
├── .ht.sqlite.lock
├── db-path.php
└── .ht.<32-hex-char-random-key>/
├── .htaccess
├── index.php
└── .ht.sqlite
```
The `db-path.php` file returns the database location using `__DIR__`, so
copying or moving the complete `database` directory keeps the reference
valid. If setup is interrupted after the path is published, another
request can finish creating the protected directory and database.
## Storage locking
The storage implements a locking mechanism for initialization and
maintenance. Locking uses a dedicated empty SQLite database and a
maintenance file:
1. An exclusive lock is acquired on the locking database at
".ht.sqlite.lock".
2. A maintenance marker file is created at ".ht.sqlite.maintenance":
- When requests see the file, they use the locking database.
- Its absence provides a fast path for normal traffic.
3. When the lock is released, the maintenance file is automatically
removed.
## Legacy migration
Migration is serialized across concurrent requests. Before moving a
legacy database, the storage manager checkpoints WAL data, switches to
`DELETE` journal mode, and acquires an exclusive SQLite lock. If the
database remains busy or migration otherwise fails, the original
database file stays in place.
## Why
A SQLite database at a predictable location under the document root may
be served directly when the web server does not honor `.htaccess` or
equivalent denial rules. The randomized directory makes accidental
exposure substantially harder while retaining a stable discovery
mechanism for WordPress and external tools.
This is an additional safeguard, not a replacement for private storage.
Keeping the database outside the document root or configuring the web
server to deny access remains the strongest protection.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added managed SQLite storage with randomized and in-memory database
locations.
* Added support for migrating existing SQLite databases.
* Improved database protection and concurrent-access handling, including
stale-lock recovery.
* **Bug Fixes**
* Improved handling of database initialization failures with clearer
failure responses.
* Added safeguards for invalid database paths and storage-operation
failures.
* Improved cleanup of WordPress database files during test environment
resets.
* **Tests**
* Expanded coverage for initialization, migration, permissions, locking,
concurrency, recovery, and in-memory databases.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 085ea60 commit a8468b5
6 files changed
Lines changed: 1480 additions & 81 deletions
File tree
- packages/plugin-sqlite-database-integration
- wp-includes/sqlite
- tests/phpunit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
| 69 | + | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
85 | 88 | | |
86 | 89 | | |
87 | 90 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 48 | + | |
| 49 | + | |
51 | 50 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
432 | 432 | | |
433 | 433 | | |
434 | 434 | | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | 435 | | |
455 | 436 | | |
456 | 437 | | |
| |||
459 | 440 | | |
460 | 441 | | |
461 | 442 | | |
462 | | - | |
463 | | - | |
464 | 443 | | |
465 | 444 | | |
466 | 445 | | |
| |||
785 | 764 | | |
786 | 765 | | |
787 | 766 | | |
788 | | - | |
789 | | - | |
790 | | - | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
796 | | - | |
797 | | - | |
798 | | - | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
803 | | - | |
804 | | - | |
805 | | - | |
806 | | - | |
807 | | - | |
808 | | - | |
809 | | - | |
810 | | - | |
811 | | - | |
812 | | - | |
813 | | - | |
814 | | - | |
815 | | - | |
816 | | - | |
817 | | - | |
818 | | - | |
819 | | - | |
820 | | - | |
821 | | - | |
822 | | - | |
823 | | - | |
824 | | - | |
825 | | - | |
826 | | - | |
827 | | - | |
828 | | - | |
829 | | - | |
830 | | - | |
831 | | - | |
832 | | - | |
833 | | - | |
834 | | - | |
835 | | - | |
836 | 767 | | |
837 | 768 | | |
838 | 769 | | |
| |||
0 commit comments