Catching WordPress privilege escalation without a signature: check the capability again at the write
Disclosure first: I make Ensomedia Security, a free WordPress plugin. Version 1.1.34 ships the feature described here. This post is about the technique, the hooks it needs and the cases it cannot cover, so you can build a small version yourself or judge ours.
The problem
Many serious flaws in WordPress plugins end the same way. A request that should have no rights reaches code that writes a role or an option, and nothing on that path asks who is calling.
The result is a new administrator, registration opened with administrator as the default role, or a replaced administration email.
A request firewall matches the shape of attacks somebody has already described. A flaw published this morning has no rule yet.
The idea
WordPress checks capabilities in its screens and REST controllers. The low-level functions do not check anything:
update_option( 'default_role', 'administrator' ); // works for any caller
$user->set_role( 'administrator' ); // so does this
So ask the question a second time, at the write itself: who is the signed-in user of this request, and would the dashboard let that user make this change? If the answer is no, record the write or refuse it.
No signature of the flaw is needed, because the check looks at the outcome.
The outcomes worth watching
The list is short:
- an account, or a role that accounts already hold, gets rights to manage users, plugins, themes or settings, or to post unfiltered HTML (the dashboard asks for
promote_users) users_can_registerordefault_rolechanges (manage_options)siteurl,homeoradmin_emailchanges (manage_options)active_pluginschanges (activate_plugins), or the theme is switched (switch_themes)- on multisite, the
site_adminsnetwork option changes
Where to hook
| Write | Hook that sees it | Can it refuse? |
|---|---|---|
update_option() |
pre_update_option filter |
Yes, return the old value |
add_option() |
sanitize_option_{$option} filter, added_option action |
No filter refuses. The sanitize filter runs before the insert and can replace the value |
delete_option() |
delete_option, deleted_option actions |
No. Put the value back afterwards |
WP_User::set_role(), WP_User::add_role() |
set_user_role, add_user_role actions |
No, they fire after the write. Set the role back |
| Role written as raw user meta | update_user_metadata, add_user_metadata filters |
Yes, return a non-null value |
| New account | user_register action |
No. Change the role afterwards |
The user meta row matters. A user's roles live in the {prefix}capabilities meta key, and a plugin can write that key with update_user_meta() without ever calling set_role().
A minimal version for options:
add_filter( 'pre_update_option', function ( $value, $option, $old_value ) {
$watched = array( 'users_can_register', 'default_role', 'admin_email', 'siteurl', 'home' );
if ( ! in_array( strtolower( $option ), $watched, true ) || $value === $old_value ) {
return $value;
}
if ( ( defined( 'WP_CLI' ) && WP_CLI ) || wp_installing() || current_user_can( 'manage_options' ) ) {
return $value;
}
error_log( 'Refused a change of ' . $option . ' made with no right to make it' );
return $old_value; // update_option() sees no change and writes nothing.
}, PHP_INT_MAX, 3 );
What goes wrong in a naive version
These are the cases our own review found in the first versions of the feature:
- Option names in another letter case. On MySQL with the default collation,
update_option( 'Default_Role', ... )writes the same row asdefault_role. Compare names in lower case. - Roles removed and created again.
remove_role()followed byadd_role()with more capabilities changes what every holder of that role can do, and no user hook fires. Watch the{prefix}user_rolesoption. - The guard switched off first. If the mode is an option, a request can delete that option or drop your plugin from
active_pluginsbefore it does the real change. Both writes need the same check. - No signed-in user is normal in some places. WP-CLI, installation, updates and scheduled tasks run without a user, and so does a sign-in service that creates administrators. Refuse everything without a user and you break them. You need a short list of trusted contexts and a way for the site owner to add to it.
wp-activate.php. It loads WordPress withWP_INSTALLINGset, so ordinary plugins do not load there on a single site. We write a small must-use file for that one script.
Because of the fourth point, start by recording only. Read a few days of the log on a real site before refusing anything.
What this cannot see
- A flaw that makes WordPress treat the request as an administrator's. The check asks who is signed in, so such a request passes.
- Code that writes to the database with its own SQL instead of the WordPress functions.
- Code that runs before your plugin loads.
- Other code in the same request that undoes the refusal.
It is one more layer for the day updates and the firewall both fail. It replaces neither.
How we tested ours
A table of 232 cases on a copy of a real WordPress with WooCommerce: attempts to grant rights through each route above, and ordinary work such as creating accounts in the dashboard, WooCommerce installing its roles, a customer registering at checkout, and plugin and theme updates.
The first run found several routes where a change went through with no trace. We fixed them and ran the whole table twice more. In the third run every escalation attempt was refused or put back and no ordinary action was refused.
This is our own test and there is no independent one. The table was not run on multisite or on MySQL. Those paths have unit tests only.
Where the code is
The plugin is GPL and the feature is one file, includes/class-account-guard.php. It starts in Record only, also after an update, and changes nothing on the site in that mode.
Plugin page: https://shieldwave.io/wordpress-plugin?utm_source=devto&utm_medium=community&utm_campaign=launch-2026-11
Longer write-up for site owners: https://shieldwave.io/blog/wordpress-account-and-settings-guard/?utm_source=devto&utm_medium=community&utm_campaign=launch-2026-11
Comments
No comments yet. Start the discussion.