ctors/pledge-symfony-routing

为 Symfony 路由添加 OpenBSD pledge/unveil 支持

安装: 5

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.0.0 2023-11-22 18:08 UTC

This package is auto-updated.

Last update: 2024-09-22 19:49:46 UTC


README

此包添加了 PHP 属性 #[Pledge]#[Unveil],这样您可以将这些属性添加到路由中。您可以将这些属性添加到路由类、方法中,并且可以添加多个属性。

安装

composer require ctors/pledge-symfony-routing

此包需要 ext/pledge 扩展,该扩展作为 OpenBSD 端口和软件包提供。有关安装说明,请参阅 pecl-pledge

cd /usr/ports/www/pecl-pledge
env FLAVOR="php82" make install

pkg_add pecl82-pledge-2.1.0

使用

#[Unveil('/', 'r')]
#[Unveil('/htdocs/var/log', 'rwc')]
#[Unveil('/htdocs/var/cache', 'rwc')]
class DnsLookupController extends AbstractController
{
    #[Route('/hello', name: 'hello')]
    #[Unveil] // Disallow future unveil calls
    #[Pledge('stdio rpath wpath cpath fattr flock')]
    public function index(): Response
    {
        return $this->render('hello/index.html.twig');
    }
}

如果您想在附加目录中编写代码,可以按照以下方式修改

#[Unveil('/', 'r')]
#[Unveil('/htdocs/var/log', 'rwc')]
#[Unveil('/htdocs/var/cache', 'rwc')]
class DnsLookupController extends AbstractController
{
    #[Route('/hello', name: 'hello')]
    #[Unveil('/htdocs/src/Controller', 'rwc')]
    #[Unveil] // Disallow future unveil calls
    #[Pledge('stdio rpath wpath cpath fattr flock')]
    public function index(): Response
    {
        file_put_contents(__DIR__.'/test', 'ohai');

        return $this->render('hello/index.html.twig');
    }
}

如果您想通过 TCP/IP 连接到 MariaDB 数据库,请添加 inet pledge

#[Unveil('/', 'r')]
#[Unveil('/htdocs/var/log', 'rwc')]
#[Unveil('/htdocs/var/cache', 'rwc')]
class DnsLookupController extends AbstractController
{
    public function __construct(
        private UserRepository $userRepository,
    ) {
    }

    #[Route('/hello', name: 'hello')]
    #[Unveil] // Disallow future unveil calls
    #[Pledge('stdio rpath wpath cpath fattr flock inet')]
    public function index(): Response
    {
        return $this->render(
            'hello/index.html.twig',
            [
                'users' => $this->userRepository->findAll(),
            ]
        );
    }
}

有关在 FPM 池级别配置此内容的说明,请参阅 pecl-pledge

注意

请确保设置 PHP-FPM 的 pm.max_requests = 1,这样您就不会重复使用已 pledge/unveil 的进程。