首页 > 解决方案 > TYPO3 如何避免表单 objectID 操作?

问题描述

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

操纵

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

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

此致

标签: formscontrollertypo3extbasehmac

解决方案


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

TYPO3hmac用于内部用途,并具有hmac在 GeneralUtility 类下调用的静态函数。

概念:

我们根据对象的 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);
}

这里我们hmac根据对象 uid 和一个你可以单独指定的词来生成。然后我们将其传递给 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存在。用户可能已删除隐藏字段以避免比较。hmac如果 TYPO3在传递的参数 ( )中没有找到任何参数,$args['hmac']那么它会将用户重定向到指定的页面,并且不会更新对象。

如果 TYPO3 找到 a ,则使用给定的 uid( ) 和您生成的单词hmac生成另一个。如果不匹配,则意味着用户已经操纵了 uid。然后 TYPO3 将用户重定向到指定的页面并且对象不会被更新。hmac$args['object']['__identity']hmac

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

此致


推荐阅读