【问题标题】:symfony disable csrf protection for classles form with get methodsymfony 使用 get 方法禁用类表单的 csrf 保护
【发布时间】:2018-09-14 13:46:57
【问题描述】:

我有无类表单,使用 GET 方法并且不能禁用 csrf 保护。 我想使用 GET 方法,因为用户必须可以选择提供指向他/她的搜索的链接。当用户提交表单时,_token 会出现在 URL 中。如果其他用户尝试使用它,您会知道,“CSRF 令牌无效。请尝试重新提交表单。”出现。

这是控制器中的函数:

 /**
 * @Route("/tips/all", name="all_tips")
 */
public function listAction(Request $request)
{
    $period = 0;
    $sport = array('3', '4', '15', '19', '20', '29', '33');
    $defaultData = array(
        'csrf_protection' => false,
        'period' => 0,
        'sport' => $sport,
    );
    $form = $this->createFormBuilder($defaultData)
        ->add('period', 'choice',
            array('choices' => array(
                '0' => "All time",
                '1' => "Last month",
                '2' => "Last 3 months",
                '3' => "Last year",
            ),
                'expanded' => false,
                'multiple' => false,
            )
        )
        ->add('sport', 'choice',
            array('choices' => array(
                '3' => 'Baseball',
                '4' => 'Basketball',
                '15' => 'Football',
                '19' => 'Hockey',
                '29' => 'Soccer',
                '33' => 'Tennis',
            ),
                'expanded' => true,
                'multiple' => true,
                'data' => $sport,
            ))
        ->add('send', 'submit')
        ->setMethod('GET')
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();
        $period = $data['period'];
        $sport = $data['sport'];
    }

    $em = $this->getDoctrine()->getManager();
    $addSport = !empty($sport) ? '  p.sport in (' . implode(',', $sport) . ')' : '';
    $dql = "
        SELECT
            p
        FROM
            AppBundle:Predictions p
        WHERE " .
        $addSport .
        $this->fromTo($period, 'dql');
    $query = $em->createQuery($dql);
    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query, $request->query->getInt('page', 1), 50, array(
            'defaultSortFieldName' => 'p.predictDate',
            'defaultSortDirection' => 'DESC',
        )
    );
    return $this->render('AppBundle:Tips:all.html.twig', array(
        'pagination' => $pagination,
        'form' => $form->createView(),
    ));
}

这是twig中的代码

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

我的 Symfony 版本是 v2.8.45。

我尝试在默认选项中传递 'csrf_protection' => false,,但似乎不起作用。

我做错了什么?

编辑:嗯,我刚刚看到了 SQL 注入的可能性。 :-( 我会解决的。

【问题讨论】:

    标签: forms symfony get csrf


    【解决方案1】:

    问题是您在数据数组中添加了 csrf_protection 选项,而不是选项数组。 createFormBuilder 方法的声明为:

    protected function createFormBuilder($data = null, array $options = array())
    

    所以你必须像这样改变你的代码:

    $defaultData = array(
        'period' => 0,
        'sport' => $sport,
    );
    
    $options = array('csrf_protection' => false);
    
    $form = $this->createFormBuilder($defaultData, $options)
            ...
    

    【讨论】:

    • 哦,我真傻。我读了很多答案两次或更多次,但我想不出答案。谢谢!
    猜你喜欢
    • 2017-03-19
    • 2012-04-08
    • 2011-05-18
    • 1970-01-01
    • 2011-10-26
    • 2012-04-10
    • 2017-01-01
    • 2016-01-21
    • 2015-10-15
    相关资源
    最近更新 更多