繁体   English   中英

TYPO3 如何避免表单 objectID 操作?

[英]TYPO3 How do i avoid form objectID manipulation?

我想避免在表单中操作隐藏字段 (__identify)。 例如edit表单。 如果有人去检查器并将值更改为另一个uid那么update操作实际上将更新操作值而不是原始值。

操纵

现在,如果有人将其更改为8那么更新操作将使用 uid 8 更新对象。

有没有办法避免这种行为?

  • TYPO3:v9
  • 模式:作曲家模式

此致

感谢@Daniel Siepmann (typo3.slack.com) 为我指明了正确的方向。 所以答案很简单,也很容易实现。

TYPO3 将hmac用于内部目的,并在 GeneralUtility 类下有一个名为hmac的静态函数。

概念:

我们使用基于对象的 uid 和您选择的单词的 hmac 字符串在表单中创建一个隐藏字段。 (为了使攻击者更难解密)。 然后在控制器上,我们使用通过表单参数传递给控制器​​的 uid 和我们之前定义的单词重新生成 hmac。 如果它们匹配,则可以更新对象。 如果没有,那么我们将用户重定向到另一个页面(错误或列表视图,由您决定)。

如何使用它:

your_extension/Classes/Controller/YourController.php

public function editAction(Object $object)
{
   $hmac = GeneralUtility::hmac($object->getUid(), 'yourWord');
   $this->view->assign('hmac', $hmac);
   $this->view->assign('object', $object);
}

这里我们根据对象uid和一个你可以单独指定的词来生成hmac 然后我们将它传递给 FrontEnd,以便将其添加到隐藏字段上,然后再进行比较。

非常重要:我强烈建议您也使用一个词。 无论您在哪里使用它,它都必须相同。 对我来说,现在这个词是yourWord

your_extension/Resources/Private/Templates/Edit.html

<f:form action="update" name="object" object="{object}" extensionName="ExtensionName" pageUid="{settings.flexform.pages.update.pid}" enctype="multipart/form-data">
    <f:form.hidden name="hmac" value="{hmac}" />
    {...}
</f:form>

这里我们用 hmac 值定义隐藏字段。 我们将在控制器中进行比较。

your_extension/Classes/Controller/YourController.php

public function initializeUpdateAction() {
   $args = $this->request->getArguments();

   /*Check if the user has not deleted the hmac hidden field*/
   if ($args['hmac']) {

      /*Regenerate the hmac to compare it with the one from the $args variable*/
       $hmac = GeneralUtility::hmac($args['object']['__identity'], 'yourWord');

       if ($hmac !== $args['hmac']) {
             $this->redirect('list', 'ControllerName', 'ExtensionName', null, $this->settings['global']['error']['pid']);
        }
   }
   else {
      $this->redirect('list', 'ControllerName', 'ExtensionName', null, $this->settings['global']['error']['pid']);
   }
}

这里我们首先评估hmac存在。 用户可能已删除隐藏字段以避免比较。 如果 TYPO3 在传递的参数( $args['hmac'] )中没有找到任何hmac ,那么它会将用户重定向到指定的页面,并且不会更新对象。

如果TYPO3找到hmac ,然后生成另一个hmac与给定的UID( $args['object']['__identity']你产生的前一个字hmac 如果不匹配,则表示用户操作了 uid。 然后 TYPO3 将用户重定向到指定的页面并且对象不会被更新。

所有这些都可以写得更优雅,但为了这个答案,我试图让它简短。

此致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM