【发布时间】:2021-10-09 22:05:27
【问题描述】:
我正在使用返回数组的 PHP 文件作为一种配置方式。
为了处理这些配置文件,我创建了一个类来搜索提供的配置目录中的 PHP 文件,并使用 include() 将它们的值存储在配置容器中。
现在我只是想知道这种方法的安全性。 如果有人将恶意代码放入其中一个文件中怎么办?
在包含这些文件并可能触发不必要的副作用之前,评估这些文件的最安全方法是什么?
我正在考虑使用file_get_contents() 将文件作为字符串加载并查找任何函数调用,但我不想限制用户使用函数来解析条件配置值等。
这是当前代码,只是为了了解工作原理:
public function load(): void
{
$iterator = $this->createFinder()->getIterator();
foreach ($iterator as $file) {
$config = include $file;
if (! is_array($config)) {
throw new \RuntimeException("Invalid config \"{$file->getRealPath()}\", Config files should return an array.");
}
$this->config[$file->getBasename()] = $config;
}
}
private function createFinder(): Finder
{
$this->finder = (new Finder())
->in($this->directories)
->files()
->name('*.php');
return $this->finder;
}
【问题讨论】:
标签: php symfony security include config