symfony 使用 get 方法禁用类形式的 csrf 保护
symfony disable csrf protection for classles form with get method
我有无类表单,使用 GET 方法,无法禁用 csrf 保护。
我想使用 GET 方法,因为用户必须可以选择提供 link 到 his/her 搜索。当用户提交表单时,_token 出现在 URL 中。如果另一个用户尝试使用它,您知道,"The CSRF token is invalid. Please try to resubmit the form." 出现。
这是控制器中的函数:
/**
* @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 注入的可能性。 :-( 我会修复它。
问题是您在数据数组而不是选项数组中添加了 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)
...
我有无类表单,使用 GET 方法,无法禁用 csrf 保护。 我想使用 GET 方法,因为用户必须可以选择提供 link 到 his/her 搜索。当用户提交表单时,_token 出现在 URL 中。如果另一个用户尝试使用它,您知道,"The CSRF token is invalid. Please try to resubmit the form." 出现。
这是控制器中的函数:
/**
* @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 注入的可能性。 :-( 我会修复它。
问题是您在数据数组而不是选项数组中添加了 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)
...