Skip to content

Lesson 2 of 9 · 28m

Security for Plugin Authors: Inputs, Capabilities, Nonces, and Output

The practical trust model: validate intent, authorize the actor, sanitize input by shape, and escape output by context — every time.

Most plugin vulnerabilities are not exotic memory corruption. They are ordinary web failures wearing WordPress clothing: an AJAX action with no nonce, a capability check that says is_user_logged_in() instead of checking what the user can do, SQL built with concatenation, or a file path assembled from $_GET. The attacker does not need to defeat WordPress. They only need to reach your callback.

Think in four gates. Intent: did this user deliberately initiate this action from our UI? Authorization: may this account perform it on this object? Input shape: is every scalar and array the type and range we expect? Output context: when data leaves PHP, is it encoded for HTML, attribute, URL, JavaScript, or SQL correctly?

Nonces prove intent, not permission

check_ajax_referer('acme_rotate_key', 'nonce');

if (!current_user_can('manage_woocommerce')) {
    wp_send_json_error(['message' => 'Forbidden'], 403);
}

check_ajax_referer() says the browser knows a nonce minted for an authenticated session. It does not say that account is allowed to rotate keys. CSRF protection without authorization is still an authorization failure. Authorization without a nonce is still CSRFable. You need both for state-changing browser actions.

Also verify object-level permission, not just a broad role-ish capability:

$post_id = absint($_POST['post_id'] ?? 0);
if (!$post_id || !current_user_can('edit_post', $post_id)) {
    wp_send_json_error(['message' => 'Forbidden'], 403);
}

edit_post with an ID is the difference between an editor editing their own post and an editor editing the CEO's landing page.

Sanitize input by meaning, escape output by context

Sanitization is not a magic security wash. sanitize_text_field() is correct for a plain short string, wrong for HTML, useless for a file path, and not a substitute for validation. Validate shape first:

$mode = isset($_POST['mode']) ? sanitize_key(wp_unslash($_POST['mode'])) : '';
if (!in_array($mode, ['test', 'live'], true)) {
    wp_send_json_error(['message' => 'Invalid mode'], 422);
}

Then escape when rendering. In WordPress, unslash → validate/sanitize on input, escape late on output:

echo '<a href="' . esc_url($url) . '" class="' . esc_attr($class) . '">' . esc_html($label) . '</a>';

esc_html() for text nodes, esc_attr() for attributes, esc_url() for URLs, wp_json_encode() inside script data, and wp_kses() for constrained HTML. esc_sql() is not how professionals write SQL; use $wpdb->prepare().

Dangerous sinks plugin authors keep shipping

  • eval(), create_function(), dynamic calls from request data.
  • unserialize() on request or imported data; use JSON and explicit schemas.
  • File operations rooted in user-controlled paths. Canonicalize and enforce an allowed directory.
  • Arbitrary redirects from $_GET['redirect_to']; use wp_validate_redirect() and an allowlist.
  • Secrets in JavaScript-localized data. If the browser can read it, so can every visitor.

The security posture is operational too

Keep a private vulnerability disclosure path, use semantic versions, and ship fixes quickly without breaking unrelated behavior. When you discover an auth bypass, do not hide the commit inside a feature release and call it refactoring. Site operators need to know what risk they were exposed to and whether logs or data should be reviewed.

Field notes

  • current_user_can('manage_options') is rarely the right check for a feature; create and map narrower capabilities.
  • Use wp_unslash() before sanitizing values from $_POST/$_GET; WordPress adds slashes for legacy reasons.
  • In REST routes, use permission_callback and schema validation — do not recreate half of the API inside the callback.
  • Log security events without logging passwords, tokens, cookies, or personal data.

Was this lesson helpful?

Sign in to rate