【问题标题】:Symfony + WSSE: Why is the nonce cache folder 20GB in size?Symfony + WSSE:为什么 nonce 缓存文件夹大小为 20GB?
【发布时间】:2021-02-09 21:05:15
【问题描述】:

我正在处理基于Symfony 3.4 的项目,该项目使用WSSE authentication as described in the Symfony docs

每个 nonce 都作为单独的文件存储在缓存目录 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呢?我错过了什么吗?还是我没有正确使用命令?

难道没有更好的存储随机数的方法吗?

当然我可以不时删除缓存。但是,这会使随机数变得毫无用处,不是吗?

【问题讨论】:

  • 我不了解 WSSE,但磁盘上的大小可能取决于您的磁盘块大小。也许您可以将随机数存储在数据库中?
  • @FabienPapet 谢谢。如问题中所述,我知道由于块大小,文件大小和磁盘空间之间存在差异。但是,使用du 应该会显示消耗的磁盘空间,不是吗?
  • 我不知道du的用法,你可以在超级用户网站上试试,他们会有答案
  • Yes du 报告已消耗的磁盘空间。在您的情况下,每个文件 4k,因为 4k 是文件系统的块大小。

标签: symfony caching nonce wsse


【解决方案1】:

文件大小

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

随机数存储多长时间

Nonce 通常会缓存几分钟。您提到的 symfony 食谱建议使用 5 分钟的 nonce ttl。这是该文档的摘录

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);
    }
}

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

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2017-08-25
    • 2016-12-13
    • 2013-04-12
    • 2021-11-16
    相关资源
    最近更新 更多