Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
where applicable.

## 1.1.17 — 2026-11-16

- Stop failing the boot when another local user pre-creates the predictable temporary cache root (`<tmp>/sympress-kernel-<uid>-<hash>`) as a directory or symlink. The kernel still never adopts such a path; it reuses or atomically creates a randomly suffixed private sibling (mode 0700, owned by the PHP user) and logs the switch.

## 1.1.16 — 2026-11-16

- Keep compiled-container metadata per bundle set in `meta_<bundle-set>.json` instead of one shared `meta.json`. On multisite, sites with different SymPress plugins or themes no longer overwrite each other's metadata, so requests alternating between them stop recompiling the container under the exclusive lock. Cleanup keeps every generation that metadata still references and caps metadata at 32 bundle sets. The legacy `meta.json` is ignored and removed; the first request after the upgrade compiles once per bundle set.
Expand Down
5 changes: 4 additions & 1 deletion docs/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,10 @@ path is shared by CLI and PHP-FPM even with systemd PrivateTmp. If the project i
the webroot or a cold project cannot be written, the implicit cache uses a
project-specific, owner-checked directory under the system temporary directory,
outside DOCUMENT_ROOT/WP_CONTENT_DIR. Pre-created symlinks, other owners and
group/world-writable directories are rejected. Every cache directory, implicit or
group/world-writable directories are never adopted: because the name is predictable,
the kernel then reuses or atomically creates a randomly suffixed private sibling
(mode 0700, owned by the PHP user) and logs it, so another local user cannot block
the boot. Every cache directory, implicit or
configured, must be owned by the PHP process user, and no parent up to the project
(or up to `/` for a cache outside the project) may be group/world-writable unless it
has the sticky bit; compiled containers and discovery manifests must be owned by the
Expand Down
33 changes: 31 additions & 2 deletions src/Kernel/CachePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ private static function privateFallback(string $project, string $environment): s
if (self::replaceableByOthers(fileperms($temporary))) {
throw new \RuntimeException('A private kernel cache requires a trusted temporary parent or APP_CACHE_DIR; shared temporary directories must have the sticky bit.');
}
$root = rtrim($temporary, '/') . '/sympress-kernel-' . $user . '-'
. substr(hash('sha256', self::canonical($project)), 0, 24);
$root = self::temporaryRoot(rtrim($temporary, '/') . '/sympress-kernel-' . $user . '-'
. substr(hash('sha256', self::canonical($project)), 0, 24), $owner);
}
if (self::isPublic($root)) {
throw new \RuntimeException('No private kernel cache is available outside the webroot. Set APP_CACHE_DIR to a durable private directory and warm it as the PHP-FPM user.');
Expand All @@ -135,6 +135,35 @@ private static function privateFallback(string $project, string $environment): s
return $root . '/' . $environment . '/kernel';
}

/**
* The temporary root name is predictable, so another local user can create it first.
* Never adopt such a path and never fail the boot over it: reuse or atomically create
* a randomly named sibling. Only this user can own one, and the sticky temporary
* parent keeps others from swapping it, so ownership is enough to find it again.
*/
private static function temporaryRoot(string $root, int $owner): string
{
if ((!file_exists($root) && !is_link($root)) || self::privateDirectory($root, $owner)) {
return $root;
}
foreach (glob($root . '.*', GLOB_NOESCAPE) ?: [] as $candidate) {
if (self::privateDirectory($candidate, $owner)) {
return $candidate;
}
}
$candidate = $root . '.' . bin2hex(random_bytes(8));
if (!mkdir($candidate, 0700)) {
throw new \RuntimeException('Unable to create a private kernel cache next to ' . $root . '; configure APP_CACHE_DIR for the PHP-FPM identity.');
}
error_log('SymPress found ' . $root . ' not owned privately by the PHP user and uses ' . $candidate . ' as its kernel cache instead. Configure APP_CACHE_DIR for a durable private cache.');
return $candidate;
}

private static function privateDirectory(string $path, int $owner): bool
{
return !is_link($path) && is_dir($path) && fileowner($path) === $owner && (fileperms($path) & 0022) === 0;
}

private static function isPublic(string $path): bool
{
$resolved = self::canonical($path);
Expand Down
48 changes: 41 additions & 7 deletions tests/Kernel/CacheRequestCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function testReadOnlyColdProjectBootsWithPrivateFallback(): void
}
}

public function testTemporaryFallbackRejectsPrecreatedSymlink(): void
public function testTemporaryFallbackAvoidsPrecreatedSymlink(): void
{
$project = $this->tmpPath('public-symlink-cache');
$outside = $this->tmpPath('untrusted-fallback');
Expand All @@ -210,15 +210,49 @@ public function testTemporaryFallbackRejectsPrecreatedSymlink(): void
$root = dirname(CachePath::resolve($project, 'test'), 2);
rmdir($root);
symlink($outside, $root);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('symlinked');
CachePath::resolve($project, 'test');
$this->assertPrivateSiblingOf($root, CachePath::resolve($project, 'test'));
self::assertSame([], glob($outside . '/*'));
} finally {
unset($_SERVER['DOCUMENT_ROOT']);
if ($root !== null && is_link($root)) {
unlink($root);
}
$this->removeTemporaryRoots($root);
}
}

public function testTemporaryFallbackAvoidsPrecreatedReplaceableDirectory(): void
{
$project = $this->tmpPath('public-replaceable-cache');
$_SERVER['DOCUMENT_ROOT'] = $project;
$root = null;
try {
$root = dirname(CachePath::resolve($project, 'test'), 2);
// Stands in for a directory another local user created first.
chmod($root, 0777);
$cache = CachePath::resolve($project, 'test');
$this->assertPrivateSiblingOf($root, $cache);
self::assertSame($cache, CachePath::resolve($project, 'test'), 'The private sibling is reused.');
} finally {
unset($_SERVER['DOCUMENT_ROOT']);
$this->removeTemporaryRoots($root);
}
}

private function assertPrivateSiblingOf(string $root, string $cache): void
{
$sibling = dirname($cache, 2);
self::assertMatchesRegularExpression('/^' . preg_quote($root, '/') . '\\.[0-9a-f]{16}$/', $sibling);
self::assertTrue(CachePath::ownedByProcess($sibling));
self::assertSame(0700, fileperms($sibling) & 0777);
}

private function removeTemporaryRoots(?string $root): void
{
if ($root === null) {
return;
}
if (is_link($root)) {
unlink($root);
}
(new Filesystem())->remove([$root, ...(glob($root . '.*') ?: [])]);
}

public function testPrivateFallbackVerifiesProcessOwnerWithoutPosixExtension(): void
Expand Down
Loading