forms - TYPO3 如何避免表单 objectID 操作?
问题描述
我想避免在表单中操作隐藏字段(__identify)。例如edit
表格。如果有人去检查员并将值更改为另一个值,uid
那么该update
操作实际上将更新操纵值而不是原始值。
现在,如果有人将其更改为,8
那么更新操作将使用 uid 8 更新对象。
有没有办法避免这种行为?
- 打字稿3:v9
- 模式:作曲家模式
此致
解决方案
感谢@Daniel Siepmann (typo3.slack.com) 为我指明了正确的方向。所以答案很简单,也很容易实现。
TYPO3
hmac
用于内部用途,并具有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
所有这些都可以写得更优雅,但为了这个答案,我试图让它简短。
此致
推荐阅读
- awk - 我的 AWK 脚本是否正确?
- node.js - 显示成功信息
- javascript - Java 脚本范围
- python - 使用 SQL Alchemy 导入数据并替换给定条件
- javascript - iOS 上的 Safari>=11 默认情况下会阻止 cookie 和网络存储 - 如何保存(第一方)数据?
- sql - SQL JOIN 两个表并显示表 A 的所有行
- php - 如何将查询mysql转换为laravel查询
- c# - 在 C# 中使用 .matches .Concat 进行正则表达式和正确捕获
- reactjs - 迭代数组中的对象
- python - 离散数据关于时间的双重积分