在 CakePHP 2.2 中使用 Form->postLInk 避免 CSRF



我正在尝试使用安全组件以避免CSRF攻击,如果我使用formHelper仅使用postLink创建票证,如下所示,它将失败:

<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $user['User']['id']),  array('class' => 'button mini'), __('Are you sure?', $user['User']['id'])); ?>

我不确定这是否可能,或者 CakePHP 只是使用 formHelper 的 create() 和 end() 方法允许此功能。

CakePHP文档只说必须使用formHelper,但它没有指定更多。

启用安全组件并对所有窗体使用 FormHelper 方法后,您不必担心这一点。您也不必配置任何内容。它开箱即用。

对于 CSRF,您可以使用以下选项:

property SecurityComponent::$csrfCheck
Whether to use CSRF protected forms. Set to false to disable CSRF protection on forms.
property SecurityComponent::$csrfExpires
The duration from when a CSRF token is created that it will expire on. Each form/page request will generate a new token that can only be submitted once unless it expires. Can be any value compatible with strtotime(). The default is +30 minutes.
property SecurityComponent::$csrfUseOnce
Controls whether or not CSRF tokens are use and burn. Set to false to not generate new tokens on each request. One token will be reused until it expires. This reduces the chances of users getting invalid requests because of token consumption. It has the side effect of making CSRF less secure, as tokens are reusable.

如果你有所有的东西,你应该在表单的html中看到CSRF令牌。您可以设置您喜欢的任何其他选项,但它几乎可以开箱即用。

最新更新