简体   繁体   English

安全主题-TWIG中的is_granted(ROLE_ADMIN)是否足够?

[英]Security Topic - IS is_granted(ROLE_ADMIN) in TWIG enough?

I wanted to know if doing only a is_granted(ROLE_ADMIN) in twig is enough to secure a form. 我想知道在树枝中仅执行is_granted(ROLE_ADMIN)是否足以保护表单。

I mean the form is available to anyone: 我的意思是任何人都可以使用该表格:
- some widget with choice selection are available to anyone to change. -带有选择选项的一些小部件可供任何人更改。
- other widgets (widget about administration in the same form) are only displayed if ROLE_ADMIN is granted. -仅在授予ROLE_ADMIN的情况下,才会显示其他窗口小部件(有关管理的窗口小部件)。

Is it enough to make it safe for administration stuff in this form to be only changed by the ROLE_ADMIN ? 仅通过ROLE_ADMIN更改这种形式的管理内容就足够安全了吗? Or should I secure as well the controller. 还是我也应该固定控制器。

(securing the controller would make me write much more, because right now I just have to do a flush(). then I would have to make many more tests for all the POST I got through the form request) (确保控制器安全将使我编写更多内容,因为现在只需要执行flush()即可。然后,我将对通过表单请求获得的所有POST进行更多测试)

General recommendations 一般建议

It depends on what you mean by security . 这取决于您所说的安全性 And, it depends on your definition of secure enough as well. 而且,这也取决于您对安全性的定义是否足够

Following questions might help: 以下问题可能会有所帮助:

  • Is it an internal-use application (such as a company-internal calendaring app where no sensitive data is stored or exposed) 它是内部使用的应用程序(例如公司内部的日历应用程序,其中没有存储或公开敏感数据)
  • Is it important to ensure security against purposely malformed requests (which I'd consider everytime the answer for the first question is 'yes'). 确保针对故意格式错误的请求的安全性是否重要(我会在每次第一个问题的答案为“是”时都会考虑)。 But even if the answer to the former question is 'no', sometimes you can have malformed requests even in an internal calendaring app... it all depends on what you consider a security threat and what not. 但是,即使前一个问题的答案为“否”,有时即使在内部日历应用程序中,您也会有格式错误的请求……这全都取决于您认为什么是安全威胁,而哪些不是。 And, on your users. 并且,对您的用户。

If you don't secure it in the controller, then a user might submit an arbitrary POST request with the fields that are not displayed there, if she guesses the names of the fields correctly, or finds them somewhere. 如果您没有在控制器中保护它,则用户可能会提交一个任意的POST请求,其中包含未在其中显示的字段,如果她正确猜测了字段的名称或在某个地方找到了它们。 So, in my opinion, you should always double check for sanity. 因此,我认为,您应该始终仔细检查其是否理智。

You could: 你可以:

  • ('pure' solution, more time-consuming) Separate the admin widgets by dynamically generating the form type depending on the users' privileges. (“纯”解决方案,更耗时)通过根据用户权限动态生成表单类型来分隔管理窗口小部件。 Then you can always check for their existence in Twig, prior to the HTML generation. 然后,您始终可以在生成HTML之前在Twig中检查它们的存在。 If you don't use the FormType, then I'd suggest you to do so - but you can dynamically add fields even if you build the whole form in the controller. 如果您不使用FormType,那么我建议您这样做-但即使您在控制器中构建整个表单,也可以动态添加字段。
  • ('dirty' solution, but easy and fast to implement) Check for that role when handling the form submission in the controller (which you suggested). (“肮脏”的解决方案,但易于实现且易于实现)在控制器中处理表单提交时(您建议)检查该角色。

And as an answer to your question in comments: "how can I simulate a POST to check if the token is enough to block POST field which are not actually display in my page." 并在评论中回答您的问题:“如何模拟POST以检查令牌是否足以阻止POST字段,而这些字段实际上并未显示在我的页面中。” I can recommend you the Chrome Advanced Rest Client extension, if you use Google Chrome for development. 如果您使用Google Chrome进行开发,我可以向您推荐Chrome Advanced Rest Client扩展。 I use it to test my forms with great success. 我用它来成功测试我的表格。

So, to sum it up, I think that the question is not clearly stated and cannot be answered generally. 因此,总而言之,我认为这个问题并未明确提出,也无法普遍回答。 It all depends. 一切取决于。 But if you want a simple answer: it's not secure enough. 但是,如果您想得到一个简单的答案:它不够安全。 Hiding something from one's eyes doesn't mean it's secure. 隐藏某人的东西并不表示它是安全的。 Therefore... 因此...

Specific recommendations - a solution with a form event listener 具体建议-带有表单事件监听器的解决方案

This is how I would do it for maximum security (I use PHP 5.5 here, so ensure you have it or it needs some syntax tweaking). 这就是我为达到最大安全性的方式(我在这里使用PHP 5.5,因此请确保您拥有它,或者需要进行一些语法调整)。

In your FormType , register an event listener like such: 在您的FormType ,注册一个事件侦听器,例如:

(...)

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('product_id', 'hidden');

    (...)

    $dynamicFieldCreatorFunc = function (FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        if ($this->getContainer()->get('security.context')->isGranted('ROLE_ADMIN'))
        {
            $form->add('admin_field', 'choice', [
                'choices' => [1 => 'Choice 1', 2 => 'Choice 2'],
            ]);
        }
    };

    $builder->addEventListener(FormEvents::PRE_SET_DATA, $dynamicFieldCreatorFunc);
    $builder->addEventListener(FormEvents::PRE_SUBMIT, $dynamicFieldCreatorFunc);
}

Then, if a user is not an admin one, your form will not have these fields generated. 然后,如果用户不是管理员,则您的表单将不会生成这些字段。 Of course in Twig, if you render your widgets one by one, you should check for the field's existence prior to rendering them: 当然,在Twig中,如果您逐一渲染小部件,则应在渲染它们之前检查该字段的存在:

{% if form.admin_field is defined %}
    {{ form_widget(form.admin_field) }}
{% endif %}

Or, using a shorter syntax with the newest Twig 或者,在最新的Twig中使用较短的语法

{% form.admin_field is defined ? form_widget(form.admin_field) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM