dragon321723 2015-07-23 10:27
浏览 15
已采纳

防止Symfony内的“叠加表格”

Overposting a form is a common way of manipulating data / hacking a site. The cause for that possible security problem is the Form/Model Binder, that automatically binds a Form to an object. Within ASP.NET, I know how to protect against these sort of attacks.

We have a User Model, with the following fields: ID, Firstname, Lastname, Password. I want the user to be able to change his Firstname and Lastname. As we know, this happens within Symfony (and ASP.NET MVC) with a Form/Model Binder, that takes the "names" of the forms and maps these values to the corresponding object fields.

Solution in ASP.NET, using the [Bind] expression on each "Post-Controller":

public async Task<ActionResult> Create([Bind(Include="FirstName,Lastname")] Employee employee)

How can I prevent this kind of attack within a Symfony application? How to tell the Model/Form binder, which post data should only be accepted / expected?

@Edit: This question intended to know how to solve this kind of problem when using a FormType for multiple usecases, e.g. for Creation and Edit of an employee. I know that in general, Symfony Form Component already checks if there are any additional fields.

  • 写回答

2条回答 默认 最新

  • dpvr49226 2015-07-23 17:52
    关注
    class FooType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            if ($options['type'] === 'edit') {
                $builder->add('editMe');
                //More edit me fields
            }
    
            $builder->add('createMe');
            //more create me fields            
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setRequired(array(
                'type'
            ));
    
            $resolver->setDefaults(array(
                'type' => 'create'
            ));
        }
    
        //For consistency 
        public function getName() 
        {
            return 'foo';
        }
    }
    

    There is no need for extra events since it would be an overkill.

    Controller:

    public function createFooAction(Request $request)
    {
        $form = $this->createForm(new FooType(), new Foo());
    
        $form->handleRequest($request);
        if ($form->isValid() && $form->submitted()) {
            //flush form
        }
    
        return $this->render("AppBundle:Foo:create.html.twig", array(
            'form' => $form
        ));
    }
    
    public function editFooAction(Request $request, $id)
    {
        $foo = ... //find($id)
    
        $form = $this->createForm(new FooType(), $foo, array(
            'type' => 'edit'
        ));
    
        $form->handleRequest($request);
        if ($form->isValid() && $form->submitted()) {
            //flush form
        }
    
        return $this->render("AppBundle:Foo:edit.html.twig", array(
            'form' => $form
        ));
    }
    

    Bonus

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮