Skip to content

Commit 7324200

Browse files
committed
use hashed username in cache dir
1 parent e2fae18 commit 7324200

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/Google/Cache/File.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,14 @@ private function getCacheDir($file, $forWrite)
151151
// use the first 2 characters of the hash as a directory prefix
152152
// this should prevent slowdowns due to huge directory listings
153153
// and thus give some basic amount of scalability
154-
$dirHash = substr(md5($file), 0, 2);
154+
$fileHash = substr(md5($file), 0, 2);
155+
$userHash = md5(get_current_user());
156+
$dirHash = $fileHash . DIRECTORY_SEPARATOR . $userHash;
157+
155158
// trim the directory separator from the path to prevent double separators
156159
$storageDir = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dirHash;
157160
if ($forWrite && ! is_dir($storageDir)) {
158-
if (!mkdir($storageDir, 0777, true)) {
161+
if (!mkdir($storageDir, 0700, true)) {
159162
$this->log(
160163
'error',
161164
'File cache creation failed',
@@ -199,7 +202,7 @@ private function acquireLock($type, $storageFile)
199202
return false;
200203
}
201204
if ($type == LOCK_EX) {
202-
chmod($storageFile, 0666 & ~umask());
205+
chmod($storageFile, 0600);
203206
}
204207
$count = 0;
205208
while (!flock($this->fh, $type | LOCK_NB)) {

tests/Google/CacheTest.php

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

43-
public function testFileDirectoryPermissions()
43+
public function testCacheDirectoryPermissions()
4444
{
4545
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/' . rand();
4646
$cache = new Google_Cache_File($dir);
@@ -51,10 +51,10 @@ public function testFileDirectoryPermissions()
5151
$filename = $method->invoke($cache, 'foo');
5252
$stat = stat($dir);
5353

54-
$this->assertEquals(0777 & ~umask(), $stat['mode'] & 0777);
54+
$this->assertEquals(0700, $stat['mode'] & 0777);
5555
}
5656

57-
public function testFileWithDefaultMask()
57+
public function testCacheFilePermissions()
5858
{
5959
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
6060
$cache = new Google_Cache_File($dir);
@@ -65,21 +65,7 @@ public function testFileWithDefaultMask()
6565
$filename = $method->invoke($cache, 'foo');
6666
$stat = stat($filename);
6767

68-
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
69-
}
70-
71-
public function testFileWithCustomMask()
72-
{
73-
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
74-
$cache = new Google_Cache_File($dir, null);
75-
$cache->set('foo', 'bar');
76-
77-
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
78-
$method->setAccessible(true);
79-
$filename = $method->invoke($cache, 'foo');
80-
$stat = stat($filename);
81-
82-
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
68+
$this->assertEquals(0600, $stat['mode'] & 0777);
8369
}
8470

8571
public function testNull()

0 commit comments

Comments
 (0)