diff --git a/docs/Error-Handling.md b/docs/Error-Handling.md index c2eb343..f6bcf62 100644 --- a/docs/Error-Handling.md +++ b/docs/Error-Handling.md @@ -119,10 +119,17 @@ try { $cache = Cache::create(Redis::class, ['host' => 'redis.internal']); $cache->has('ping'); // force the lazy connection now, so we fail fast } catch (CacheException $e) { - $cache = Cache::create(File::class, ['path' => sys_get_temp_dir() . '/cache']); + // A directory that belongs to the application, not a shared one such as + // sys_get_temp_dir(): the File handler unserialize()s what it finds there. + $cache = Cache::create(File::class, ['path' => __DIR__ . '/var/cache']); } ``` +Do not point the `File` fallback at `/tmp` or any other directory other users can +write to. Another local user could pre-create it and plant a file in it, and +the handler would deserialize that file. Keep the directory owned by the +application user (mode `0700`); see [File Handler](File-Handler.md#usage). + ## Next steps - [Keys, TTL & Values](Keys-TTL-and-Values.md) — what makes a key invalid. diff --git a/docs/File-Handler.md b/docs/File-Handler.md index cf4a7a1..05bf3a7 100644 --- a/docs/File-Handler.md +++ b/docs/File-Handler.md @@ -34,12 +34,18 @@ A quick bootstrap: ```php $dir = __DIR__ . '/var/cache'; if (!is_dir($dir)) { - mkdir($dir, 0775, true); + mkdir($dir, 0700, true); } $cache = Cache::create(File::class, ['path' => $dir]); ``` +> **Security:** use a directory that belongs to the application (owned by the +> web/CLI user, mode `0700`), never `/tmp` or another location other users can +> write to. Cache files are PHP-`serialize()`d and read back with `unserialize()`, +> so a file planted by someone else becomes a deserialization input. Keep the +> directory outside the web root as well. + If `path` is missing or empty, any operation that touches the filesystem throws a [`CacheException`](Error-Handling.md). diff --git a/docs/Installation.md b/docs/Installation.md index 3f9701c..f6aa43b 100644 --- a/docs/Installation.md +++ b/docs/Installation.md @@ -50,7 +50,7 @@ use InitPHP\Cache\Cache; use InitPHP\Cache\Handler\File; $dir = __DIR__ . '/var/cache'; -@mkdir($dir, 0775, true); +@mkdir($dir, 0700, true); // private to the application: cache files are unserialized $cache = Cache::create(File::class, ['path' => $dir]); diff --git a/docs/Recipes.md b/docs/Recipes.md index 67d4f73..da34078 100644 --- a/docs/Recipes.md +++ b/docs/Recipes.md @@ -84,15 +84,24 @@ function fetchJson(CacheInterface $cache, string $url, int $ttl = 600): array ## Fragment caching (rendered HTML) +A fragment that depends on the current user must be cached **per user**: put the +user's id in the key. A fixed key such as `frag_nav` would serve the first user's +menu, name and any tokens in it to everyone until the entry expires. + ```php -function renderNav(CacheInterface $cache, callable $render): string +function renderNav(CacheInterface $cache, ?int $userId, callable $render): string { - return remember($cache, 'frag_nav', 600, $render); + // $userId is null for a guest, who gets one shared "guest" fragment: the guest + // fragment must not contain a CSRF token or other per-session data. + return remember($cache, 'frag_nav_' . ($userId ?? 'guest'), 600, $render); } -echo renderNav($cache, fn () => view('partials/nav', ['user' => $user])); +echo renderNav($cache, $user?->id, fn () => view('partials/nav', ['user' => $user])); ``` +Only fragments that are identical for every visitor (a footer, a public +category list) may use a fixed key. + ## Pick a backend per environment Develop on the filesystem, run Redis in production — one place decides: diff --git a/docs/Testing.md b/docs/Testing.md index acd59a1..52000fa 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -79,7 +79,7 @@ final class PriceServiceFileTest extends TestCase protected function setUp(): void { $this->dir = sys_get_temp_dir() . '/cache-test-' . uniqid(); - mkdir($this->dir); + mkdir($this->dir, 0700); $this->cache = Cache::create(File::class, ['path' => $this->dir]); } diff --git a/docs/The-Cache-Factory.md b/docs/The-Cache-Factory.md index ee37473..47721ec 100644 --- a/docs/The-Cache-Factory.md +++ b/docs/The-Cache-Factory.md @@ -69,9 +69,9 @@ config) and when you want a single place that validates the class. Option keys are normalised to lower case, so these are all the same option: ```php -Cache::create(File::class, ['PATH' => '/tmp/cache']); -Cache::create(File::class, ['Path' => '/tmp/cache']); -Cache::create(File::class, ['path' => '/tmp/cache']); +Cache::create(File::class, ['PATH' => __DIR__ . '/var/cache']); +Cache::create(File::class, ['Path' => __DIR__ . '/var/cache']); +Cache::create(File::class, ['path' => __DIR__ . '/var/cache']); ``` Options you don't set fall back to the handler's defaults (and the shared diff --git a/docs/Troubleshooting.md b/docs/Troubleshooting.md index e3769d0..28fd939 100644 --- a/docs/Troubleshooting.md +++ b/docs/Troubleshooting.md @@ -30,7 +30,7 @@ an empty one). ```php $dir = __DIR__ . '/var/cache'; -is_dir($dir) || mkdir($dir, 0775, true); +is_dir($dir) || mkdir($dir, 0700, true); // private to the application $cache = Cache::create(File::class, ['path' => $dir]); ```