简体   繁体   English

TYPO3 如何避免表单 objectID 操作?

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

I would like to avoid the manipulation of the hidden field (__identify) in a form.我想避免在表单中操作隐藏字段 (__identify)。 For example the edit form.例如edit表单。 If someone goes to the inspector and change the value to another uid then the update action will actually update the manipulated value instead of the original.如果有人去检查器并将值更改为另一个uid那么update操作实际上将更新操作值而不是原始值。

操纵

Now if someone changes this to 8 then the update action will update the object with the uid 8.现在,如果有人将其更改为8那么更新操作将使用 uid 8 更新对象。

Is there a way to avoid such action?有没有办法避免这种行为?

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

Best regards此致

Thanks to @Daniel Siepmann (typo3.slack.com) for pointing me to the right direction.感谢@Daniel Siepmann (typo3.slack.com) 为我指明了正确的方向。 So the answer is simple and easy to implement.所以答案很简单,也很容易实现。

TYPO3 uses hmac for internal purposes and has a static function called hmac under the GeneralUtility class. TYPO3 将hmac用于内部目的,并在 GeneralUtility 类下有一个名为hmac的静态函数。

Concept:概念:

We create a hidden field in the form with a hmac string based on the uid of the object and a word of your choice.我们使用基于对象的 uid 和您选择的单词的 hmac 字符串在表单中创建一个隐藏字段。 (To make the decryption more difficult for the attacker). (为了使攻击者更难解密)。 Then on the controller we regenerate the hmac with the uid that has been passed via the form arguments to the controller and the word we previously defined.然后在控制器上,我们使用通过表单参数传递给控制器​​的 uid 和我们之前定义的单词重新生成 hmac。 If they match, then the object can be updated.如果它们匹配,则可以更新对象。 If not, then we redirect the user to another page (Error or list view, it is up to you).如果没有,那么我们将用户重定向到另一个页面(错误或列表视图,由您决定)。

How to use it:如何使用它:

your_extension/Classes/Controller/YourController.php 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);
}

Here we generate the hmac based on the object uid and a word that you can alone specify.这里我们根据对象uid和一个你可以单独指定的词来生成hmac Then we pass it to the FrontEnd in order to add it on the hidden field and later to compare it.然后我们将它传递给 FrontEnd,以便将其添加到隐藏字段上,然后再进行比较。

VERY IMPORTANT: I would strongly recommend to use a word as well.非常重要:我强烈建议您也使用一个词。 It must be the same everywhere you use it.无论您在哪里使用它,它都必须相同。 For me now the word is yourWord .对我来说,现在这个词是yourWord

your_extension/Resources/Private/Templates/Edit.html 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>

Here we define the hidden field with the hmac value.这里我们用 hmac 值定义隐藏字段。 We are going to compare it in the controller.我们将在控制器中进行比较。

your_extension/Classes/Controller/YourController.php 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']);
   }
}

Here we first evaluate if the hmac exists.这里我们首先评估hmac存在。 The user might have deleted the hidden field to avoid the comparisson.用户可能已删除隐藏字段以避免比较。 If TYPO3 does not find any hmac in the passed arguments ( $args['hmac'] ) then it will redirect the user to the specified page and the object won't be updated.如果 TYPO3 在传递的参数( $args['hmac'] )中没有找到任何hmac ,那么它会将用户重定向到指定的页面,并且不会更新对象。

If TYPO3 finds a hmac , then generates another hmac with the given uid ( $args['object']['__identity'] ) and the word you generated the previous hmac .如果TYPO3找到hmac ,然后生成另一个hmac与给定的UID( $args['object']['__identity']你产生的前一个字hmac If it does not match, that means that the user has manipulated the uid.如果不匹配,则表示用户操作了 uid。 Then TYPO3 redirects the user to the specified page and the object won't be updated.然后 TYPO3 将用户重定向到指定的页面并且对象不会被更新。

All this could be written more elegantly but for the sake of this answer, i tried to make it short.所有这些都可以写得更优雅,但为了这个答案,我试图让它简短。

Best regards此致

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

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