juvo / wp-secure-actions
WordPress 中安全处理可验证操作的框架
3.3.4
2023-09-27 11:35 UTC
Requires
- php: >=7.2.0
- berlindb/core: ^2.0
- woocommerce/action-scheduler: ^3.6
Requires (Dev)
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.10.6
- szepeviktor/phpstan-wordpress: ^v1.1.7
README
WordPress 安全操作
此库允许您创建带有过期日期和执行限制的可验证操作。可能的用例
- 一次性链接
- 可追踪链接(点击次数)
- 类似 Slack 的魔法登录,通过电子邮件验证用户
- 安全的下载链接,以避免直接文件访问
- 双重确认流程
Secure Actions 使用其自己的数据库表来避免进一步污染,并在操作完成后进行清理。
安装
尽早通过将以下片段添加到您的 functions.php 或早期执行的插件文件中来实例化 Manager
类
// Init Secure Actions \juvo\WordPressSecureActions\Manager::getInstance();
Secure Actions 将确保它只加载一次。它将自动注册清理定时任务和默认 URL 处理过程。
使用方法
示例
创建和执行操作
此示例创建了一个操作,用于向更新了个人资料的用户发送电子邮件。在此示例中,操作立即执行,但您可以在其过期间隔内的任何时间执行它。
// Create action at any time after init hook was executed add_action( 'profile_update', 'createAction', 10, 2 ); function createAction( $user_id, $old_user_data ) { $user = get_userdata( $user_id ); $secActionsManager= \juvo\WordPressSecureActions\Manager::getInstance(); // Create action $key = $secActionsManager->addAction( "send_mail_$user_id", // name "wp_mail", // callback [ $user->user_email, // arg1 "Secure action executed", // arg2 "The secure action was executed successfully." // arg3 ] ); } // Execute the stored action any time later (\juvo\WordPressSecureActions\Manager::getInstance())->executeAction($key);
在 URL 中使用操作
在以下示例中,我们将通知 WordPress 用户他们的个人资料是否已更新。用户将收到包含登录链接的电子邮件,该链接会自动将其重定向到个人资料页面。由于回调函数没有返回值,因此代码无法检测成功执行并自动递增计数器。因此,我们必须手动执行。
// Create action at any time after init hook was executed add_action( 'profile_update', 'createAction', 10, 2 ); function createAction( $user_id, $old_user_data ) { $user = get_userdata( $user_id ); $secActionsManager= \juvo\WordPressSecureActions\Manager::getInstance(); // Create Action $key = $secActionsManager->addAction( "send_mail_$user_id", // name "ourCallbackFunction", // callback [ $user, // arg1 ] ); // Generate url with helper function to automatically execute action $actionUrl = $secActionsManager->buildActionUrl($key); // Send mail containing the url wp_mail( $user->user_email, "Profile Updated", "Your profile was updated click here to check it out $actionUrl" ); } // Callback that executes when link is clicked function ourCallbackFunction(WP_User $user, Action $action) { wp_set_auth_cookie($user->ID, false); do_action('wp_login', $user->user_login, $user); // Manually increment count because this callback function has no return value \juvo\WordPressSecureActions\Manager::getInstance()->incrementCount($action); wp_safe_redirect(get_edit_profile_url($user->ID)); // Redirect to profile page exit; }
高级使用
清理
在某些情况下,您可能想要更改清理函数的行为。以下示例演示了如何使用 secure_action_cleanup
过滤器。
// Disable cleanup add_filter( 'secure_action_cleanup', function() { return false; }, 10, 3 ); // Exclude based on name add_filter( 'secure_action_cleanup', 'whitelistActions', 10, 3 ); function whitelistActions(bool $delete, Action $action, string $name) { if ($name === "my_action") { return false; } return $delete; }
操作 URL 重定向
如果您使用安全操作 URL 功能,用户在执行后将重定向到主页。您可以通过挂钩以下过滤器来更改此行为。
apply_filters( 'juvo_secure_actions_catch_action_redirect', $url, $action, $executionResult);
安全性
1. 密钥生成和存储
- 唯一密钥:该库使用 WordPress 核心函数
wp_generate_password()
为每个操作生成唯一密钥。这确保了密钥的随机性和不可预测性,尤其是对于 28 个字符的长度。 - 哈希:在将密钥存储到数据库之前,它使用 WordPress
PasswordHash
类进行哈希,该类实现了一个可移植的 PHP 密码哈希框架。这意味着原始密钥永远不会以纯文本形式存储,从而进一步防止潜在的数据库泄露。 - 验证:在执行操作期间使用的哈希密钥用于验证,确保只有具有原始密钥的人可以执行给定的操作。
2. 操作执行验证
- 密钥有效性:在执行操作期间,提供的密钥与数据库中的哈希密钥进行验证。如果存在不匹配,则操作将不会进行。
- 过期:每个操作都可以有到期时间。如果尝试在操作过期后执行操作,则尝试将被拒绝,并将删除操作。
- 执行限制:可以设置操作的执行次数限制。一旦达到此限制,进一步的执行尝试将被拒绝。
- 回调完整性:库确保在执行之前操作关联的回调是可调用的。这可以防止潜在的配置错误或篡改的回调数据导致意外行为。
3. 错误处理
- 该库在执行失败的情况下返回
WP_Error
对象,确保最终用户能够接收到关于发生错误的描述性消息,同时不会暴露敏感的系统细节。
4. 建议
- 密钥存储:请确保不要存储明文密钥,因为它们会直接访问底层进程。
- 限制持久操作:该库提供了一个选项,允许操作在达到限制或过期后仍然持续。虽然这在某些情况下可能很有用,但建议谨慎使用此功能。
Composer
composer require juvo/wp-secure-actions