[英]TYPO3 How do i avoid form objectID manipulation?
感谢@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.