Skip to content

Commit 444e687

Browse files
huebsbshaffer
authored andcommitted
Enable custom umask for cache files
While #617 is well intentioned for its security concerns, it has an annoying side effect when the cache is shared between an application's CLI and Web Interface. Unless you use the same username for Apache or PHP-FPM to access the CLI (which you shouldn't), a cache file will only be readable by whichever interface created it. My solution is to add a third constructor argument to `Google_Cache_File` for setting a custom umask. I've assigned it the default value `077` so that default permissions for files and directories will remain `0600` and `0700` respectively.
1 parent bc39060 commit 444e687

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Google/Cache/File.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ class Google_Cache_File implements CacheInterface
3030
{
3131
const MAX_LOCK_RETRIES = 10;
3232
private $path;
33+
private $umask;
3334
private $fh;
3435

3536
/**
3637
* @var use Psr\Log\LoggerInterface logger
3738
*/
3839
private $logger;
3940

40-
public function __construct($path, LoggerInterface $logger = null)
41+
public function __construct($path, LoggerInterface $logger = null, $umask = 077)
4142
{
4243
$this->path = $path;
4344
$this->logger = $logger;
45+
$this->umask = $umask;
4446
}
4547

4648
public function get($key, $expiration = false)
@@ -155,7 +157,7 @@ private function getCacheDir($file, $forWrite)
155157
// trim the directory separator from the path to prevent double separators
156158
$storageDir = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dirHash;
157159
if ($forWrite && ! is_dir($storageDir)) {
158-
if (! mkdir($storageDir, 0700, true)) {
160+
if (! mkdir($storageDir, 0770 & ~$this->umask, true)) {
159161
$this->log(
160162
'error',
161163
'File cache creation failed',
@@ -199,7 +201,7 @@ private function acquireLock($type, $storageFile)
199201
return false;
200202
}
201203
if ($type == LOCK_EX) {
202-
chmod($storageFile, 0600);
204+
chmod($storageFile, 0660 & ~$this->umask);
203205
}
204206
$count = 0;
205207
while (!flock($this->fh, $type | LOCK_NB)) {

tests/Google/CacheTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ public function testFileWithTrailingSlash()
4040
$this->getSetDelete($cache);
4141
}
4242

43+
public function testFileWithDefaultMask()
44+
{
45+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
46+
$cache = new Google_Cache_File($dir);
47+
$cache->set('foo', 'bar');
48+
49+
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
50+
$method->setAccessible(true);
51+
$filename = $method->invoke($cache, 'foo');
52+
$stat = stat($filename);
53+
54+
$this->assertEquals($stat['mode'] & 0777, 0600);
55+
}
56+
57+
public function testFileWithCustomMask()
58+
{
59+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
60+
$cache = new Google_Cache_File($dir, null, 07);
61+
$cache->set('foo', 'bar');
62+
63+
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
64+
$method->setAccessible(true);
65+
$filename = $method->invoke($cache, 'foo');
66+
$stat = stat($filename);
67+
68+
$this->assertEquals($stat['mode'] & 0777, 0660);
69+
}
70+
4371
public function testNull()
4472
{
4573
$cache = new Google_Cache_Null();

0 commit comments

Comments
 (0)