juvo/wp-secure-actions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

juvo/wp-secure-actions

Composer 安装命令:

composer require juvo/wp-secure-actions

包简介

Framework to securely handle verifiable actions in wordpress

README 文档

README

PHP Test/Analyse

WordPress Secure Actions

This library allows you to create verifiable actions with an expiration date and an execution limit. Possible use cases:

  • One-time-links
  • Trackable links (number of clicks)
  • Slack-like magic login that authenticates a user via e-mail
  • Secured download links to avoid direct file access
  • Double opt-in process

Secure Actions uses its own database table to avoid further pollution and cleans up after itself.

Installation

Simply instantiate the Manager class as early as possible by adding the following snippet to your functions.php or an early executed plugin file:

 // Init Secure Actions  
 \juvo\WordPressSecureActions\Manager::getInstance();

Secure Actions will take care it is only loaded once. It will automatically register the cleanup cron job and default URL handling process.

Usage

Parameter Type Description
$name string Required Unique name to identify the action e.g. in filters
$callback string array Required Callback action to execution whenever the action is triggered.
$args array Optional The parameters to be passed to the callback, as an indexed array. Defaults to array().
$expiration int Optional Action expiration interval in seconds. Defaults to -1.
$limit int Optional Action execution limit. Defaults to -1.
$persistent bool Optional Determines if an action should be deleted when expired or limit reached. Defaults to false.
key string Optional Sets the given password instead if choosing a random one

Example

Create and execute Action

This example creates an action that sends an email to users who updated their profile. In this example, the action is executed immediately, but you can execute it any time later within its expiration interval.

// 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); 

Use action in URL

In the following example, we are going to inform a WordPress user if his profile was updated. The user will receive an email containing a login link that automatically redirects him to the profile page. Since the callback function has no return value, the is code is not able to detect a successful execution and cannot automatically increment the counter. Therefore, we have to do it manually.

// 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;
} 

Advanced usages

Cleanup

In some cases, you want to change the cleanup functions behaviour. The following examples demonstrate how to use the secure_action_cleanup filter.

// 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;
} 

Action URL redirect

If you use the secure action URL feature, the user will be redirected to the front page after the execution. It is possible to change that behaviour by hooking into the following filter.

Parameter Type Description
$url string Required URL to redirect to. Defaults to get_site_url()
$action \juvo\WordPressSecureActions\Action|WP_Error Optional Action lookup result from the db. If your action exceeded some limits and is not using the "persistent" flag, this parameter will most likely be an WP_Error instance because the deletion workflow is triggered during the action execution.
$result mixed Optional The action´s execution result. Will most likely be an instance of WP_Error if the action exceeded some limits.
apply_filters( 'juvo_secure_actions_catch_action_redirect', $url, $action, $executionResult);

Security

1. Key Generation and Storage:

  • Unique Keys: The library generates unique keys for each action using the WordPress core function wp_generate_password(). This ensures randomness and unpredictability for the keys, especially with a length of 28 characters.
  • Hashing: Before storing the key in the database, it is hashed using the WordPress PasswordHash class, which implements a portable PHP password hashing framework. This means the original key is never stored in plain text, further protecting against potential database breaches.
  • Verification: The hashed key is used for verification during the execution of actions, ensuring only the person with the original key can execute the given action.

2. Action Execution Verifications:

  • Key Validity: During the execution of an action, the provided key is verified against the hashed key in the database. If there's a mismatch, the action will not proceed.
  • Expiration: Each action can have an expiration time. If an attempt is made to execute the action after its expiry, the attempt will be declined, and the action will be deleted.
  • Execution Limits: A limit can be set on the number of times an action can be executed. Once this limit is reached, further execution attempts are declined.
  • Callback Integrity: The library ensures the callback associated with the action is callable before proceeding with the execution. This prevents potential misconfigurations or tampered callback data from causing unexpected behaviors.

3. Error Handling:

  • The library returns WP_Error objects in cases of execution failures, ensuring the end user gets a descriptive message about what went wrong without exposing sensitive system details.

4. Recommendations:

  • Key Storage: Make sure, to never store the plain keys as they give direct access to the underlying process.
  • Limit Persistent Actions: The library provides an option for actions to persist even after reaching their limits or expiration. While this can be useful in certain scenarios, it's recommended to use this feature judiciously.

Composer

composer require juvo/wp-secure-actions

juvo/wp-secure-actions 适用场景与选型建议

juvo/wp-secure-actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.17k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「actions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 juvo/wp-secure-actions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 juvo/wp-secure-actions 我们能提供哪些服务?
定制开发 / 二次开发

基于 juvo/wp-secure-actions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2021-04-21