跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
安全

wp-plugin-security

Security guidelines for WordPress plugin development: sanitization, validation, escaping (in PHP and in admin JavaScript), nonces, capabilities over objects, multisite privilege boundaries, SQL injection prevention, XSS protection, and CSRF mitigation. Use it when writing or reviewing any plugin code that handles user input, prints dynamic output, registers AJAX or REST endpoints, checks permissions, writes files shared by a network, or suppresses PHPCS security sniffs. Based on official WordPress Developer Resources and on a post-incident review of CVE-2026-81754.

fernandotellado · 已有 AI 审核记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add fernandotellado/ai-skills --skill wp-plugin-security

技能说明


name: wp-plugin-security description: "Security guidelines for WordPress plugin development: sanitization, validation, escaping (in PHP and in admin JavaScript), nonces, capabilities over objects, multisite privilege boundaries, SQL injection prevention, XSS protection, and CSRF mitigation. Use it when writing or reviewing any plugin code that handles user input, prints dynamic output, registers AJAX or REST endpoints, checks permissions, writes files shared by a network, or suppresses PHPCS security sniffs. Based on official WordPress Developer Resources and on a post-incident review of CVE-2026-81754." compatibility: "WordPress 6.0+ / PHP 7.4+. Applies to plugins, themes, and custom code." license: GPL-2.0-or-later metadata: author: fernando-tellado version: "1.2"

WordPress plugin security

When to use

Use this skill when:

  • Developing new WordPress plugins or themes
  • Reviewing existing code for security vulnerabilities
  • Handling user input (forms, AJAX, REST API)
  • Outputting dynamic content to the browser
  • Interacting with the database
  • Creating admin pages or settings
  • Implementing AJAX or REST endpoints
  • Processing file uploads

Scope: audit the surface, not the diff

Reviewing only what changed is how a flaw survives for years while every release passes its security gate. Two rules make the difference:

  • If the change touches an escaper, a validator, a capability check or an is_*_request() helper, the unit of review is the whole function and every one of its callers, not the lines of the diff. Ask "does this function do the right thing for all the contexts it is used in?", not "is this new value handled correctly?". In the incident behind these notes, a review looked at exactly the broken escaper, named it in the release notes and approved it, because it followed the path of the new value (which went to text position) instead of auditing the function and its other eleven uses in attribute position.
  • A review that only follows new data cannot find old flaws. Rotate: each review takes one whole subsystem and reads it end to end, even if nothing in it changed. And state in writing what the review did not cover, so the next one starts there instead of repeating the same blind spot.

Core security principles

The security mantra

Sanitize early
Escape late
Always validate
Never trust user input

Key concepts

  1. Sanitization: Clean/filter input data as soon as it is received
  2. Validation: Verify data matches expected format/values (prefer over sanitization)
  3. Escaping: Secure output data before rendering to prevent XSS
  4. Nonces: Protect against CSRF attacks on forms and URLs
  5. Capabilities: Verify user has permission to perform actions

Sanitization

Sanitize input data immediately upon receipt. Use the most specific function available.

Sanitization functions

FunctionUse case
sanitize_text_field()Single-line text input
sanitize_textarea_field()Multi-line text input
sanitize_email()Email addresses
sanitize_file_name()File names
sanitize_hex_color()Color values with hash
sanitize_hex_color_no_hash()Color values without hash
sanitize_html_class()HTML class names
sanitize_key()Keys (lowercase alphanumeric, dashes, underscores)
sanitize_meta()Meta values
sanitize_mime_type()MIME types
sanitize_option()Option values
sanitize_sql_orderby()SQL ORDER BY clauses
sanitize_title()Titles/slugs
sanitize_title_with_dashes()URL-friendly titles
sanitize_user()Usernames
sanitize_url()URLs for storage
wp_kses()HTML with allowed tags
wp_kses_post()HTML allowed in posts

Sanitization example

// Sanitize a text field from POST
$title = sanitize_text_field( $_POST['title'] ?? '' );

// Sanitize email
$email = sanitize_email( $_POST['email'] ?? '' );

// Sanitize URL for database storage
$url = sanitize_url( $_POST['website'] ?? '' );

// Sanitize textarea
$description = sanitize_textarea_field( $_POST['description'] ?? '' );

Important notes on sanitization

  • Never use escape functions for sanitization - they serve different purposes
  • And never use sanitization as escaping, which is the direction that actually causes breaches. No sanitize_* function prepares a value for a specific output context. sanitize_text_field() strips tags, so the value looks clean, but it does not touch quotes: a "sanitized" string can still close an HTML attribute and open a new one. Sanitizing is for storing, escaping is for printing, and the correct escape depends on where the value lands. Any review reasoning that stops at "this is already sanitized" has not finished
  • When using filter_var(), always specify a sanitizing filter (not FILTER_DEFAULT)
  • Process only the specific keys you need, not the entire $_POST/$_GET array
// CORRECT: Specify sanitizing filter
$post_id = filter_input( INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT );

// WRONG: No filter or FILTER_DEFAULT does not sanitize
$post_id = filter_input( INPUT_GET, 'post_id' ); // Insecure!

Validation

Validation verifies data matches expected patterns. Prefer validation over sanitization when possible.

Validation philosophies

Safelist (recommended)

Accept only known, trusted values:

$allowed_values = array( 'draft', 'pending', 'publish' );

// Use strict comparison (third parameter = true)
if ( in_array( $status, $allowed_values, true ) ) {
    // Valid
} else {
    wp_die( 'Invalid status' );
}

Format detection

Test data format and reject if invalid:

// Check alphanumeric only
if ( ! ctype_alnum( $data ) ) {
    wp_die( 'Invalid format' );
}

// Check against regex
if ( ! preg_match( '/^\d{5}(-\d{4})?$/', $zip_code ) ) {
    wp_die( 'Invalid ZIP code format' );
}

Type

阅读完整来源 ↗