Symfony+WSSE:为什么nonce缓存文件夹的大小是20GB



我正在处理一个基于Symfony 3.4的项目,该项目使用Symfony文档中描述的WSSE身份验证。

每个随机数作为单独的文件存储在缓存目录myProject/var/cache/prod/security/nonces中。问题是,这个目录的大小变得非常大。该项目已经启动并运行,nonce已经使用近20GB的磁盘空间

$ cd myProject/var/cache/prod/security/
$ du -sch *
19G    nonces
19G    total

这对我来说似乎很重要……我试图弄清楚存储了多少随机数,并使用以下命令对文件进行计数:

$ cd myProject/var/cache/prod/security/nonces
$ find -maxdepth 1 -type f | wc -l
4697417

即使对于470万个文件,19GB似乎也差不多。每个文件的大小大约需要4KB。然而,据我所知,每个文件只有10B。。。

$ cd myProject/var/cache/prod/security/nonces
$ ls -lh
-rw-r----- 1 user nobody 10 Jul 25 16:46 'MWFiYWM5YjAiOTRyOWRmZA=='
-rw-r----- 1 user nobody 10 Jul  1 19:41 'MWFiYWNiYTflNTdhLGYwYQ=='
-rw-r----- 1 user nobody 10 Sep 29 11:05 'MWFiYWNkNzEjZfFlCjM0OQ=='
...

我知道文件大小和占用的磁盘空间是有区别的。然而,du也显示了10B的磁盘空间:

$ du -sb --apparent-size MWFiYWNkNzEjZfFlCjM0OQ==
10

那么,文件如何使用19G的磁盘空间,而每个文件只使用10B?我是不是错过了什么?或者我没有正确使用命令

难道没有更好的方法来存储nonce吗

我当然可以不时删除缓存。然而,这将使随机数几乎毫无用处,不是吗

文件大小

du报告所消耗的磁盘空间大小磁盘空间按块分配。因此,一个文件可以占用的最小空间是1个块。在您的情况下,文件系统的块大小似乎是4kb。因此,约470万个10字节大小的文件消耗4700000*4kb,大约为19gb。

存储随机数的时间

Nonces通常会缓存几分钟。你提到的symfony食谱推荐的时间是5分钟。这是文件的摘录

class WsseProvider implements AuthenticationProviderInterface
{
protected function validateDigest($digest, $nonce, $created, $secret)
{
// Check created time is not in the future
if (strtotime($created) > time()) {
return false;
}
// Expire timestamp after 5 minutes
if (time() - strtotime($created) > 300) {
return false;
}
// Try to fetch the cache item from pool
$cacheItem = $this->cachePool->getItem(md5($nonce));
// Validate that the nonce is *not* in cache
// if it is, this could be a replay attack
if ($cacheItem->isHit()) {
// In a real world application you should throw a custom
// exception extending the AuthenticationException
throw new AuthenticationException('Previously used nonce detected');
}
// Store the item in cache for 5 minutes
$cacheItem->set(null)->expiresAfter(300);
$this->cachePool->save($cacheItem);
// Validate Secret
$expected = base64_encode(sha1(base64_decode($nonce).$created.$secret, true));
return hash_equals($expected, $digest);
}
}

随机数以5分钟的ttl添加到缓存池中。保持nonce长于您认为创建的字段有效的时间(在本例中为if (time() - strtotime($created) > 300)的五分钟(不会增加任何额外的安全性,因为一旦创建日期过期,就会根据创建的时间戳拒绝重放的请求。

最新更新