在CakePHP 2.6中将表单提交给不同的操作会导致黑洞

ubby3x7f  于 2022-11-12  发布在  PHP
关注(0)|答案(2)|浏览(140)

我在我的CakePHP 2.6应用程序中有一个表单,它有多个按钮来提交表单到不同的操作。在Cake 2.4中,这是可行的,但在2.6中,提交到任何非默认表单操作都会导致黑洞'auth'错误。
就在我的表单结束之前,我有多个提交按钮,如下所示:

echo $this->Form->button('Default', array(
    'type' => 'submit',
));
echo $this->Form->button('Alternate 1', array(
    'type' => 'submit',
    'formaction' => '/posts/otheraction',
)); 
echo $this->Form->button('Alernate 2', array(
    'type' => 'submit',
    'formaction' => '/posts/anotheraction',
));

Reading the docs,我看到了‘auth’ Indicates a form validation error, or a controller/action mismatch error.。但是,这在过去是有效的-似乎事情变得更严格了。如何让Cake接受从其他操作提交的表单而不完全关闭安全性?

envsm3lx

envsm3lx1#

这似乎是不可能的(至少在没有跳过足够大的铁环的情况下是不可能的),因为动作是无条件地合并到令牌中的。

$hashParts = array(
    $this->_lastAction, // <<<<<<<<<<<
    serialize($fields),
    $unlocked,
    Configure::read('Security.salt')
);
$fields = Security::hash(implode('', $hashParts), 'sha1');

http://github.com/cakephp/cakephp/blob/2.7.1/lib/Cake/View/Helper/FormHelper.php#L589

此外,禁用此行为可能会削弱安全性,因为将数据发布到不用于的操作可能会导致意外行为。
您可以通过一个操作来处理这个问题,例如,为各个提交按钮指定一个名称和一个值,然后在控制器操作中计算该值并执行任何需要执行的操作
第一个

4bbkushb

4bbkushb2#

我使用jQuery来更改表单的操作
HTML按钮:

<button id='saveSubmitter'>Save</button>

浏览器:

$('body').on('click', '#saveSubmitter',
  function(e){
      e.preventDefault()
      $('form').attr('action', '<?php echo Router::url( array("action"=>"save")); ?>')
      $('form').submit()
  }
)

相关问题