效率与协作
Mistake 2 — Using
input-sanitization-validation
Use when reading any external input in WordPress — $_GET, $_POST, $_REQUEST, $_COOKIE, REST params, shortcode/block attributes, option/meta values, or remote API responses. Unslashes then sanitizes to the correct type (sanitize_text_field, sanitize_email, absint, sanitize_key, wp_kses_post, esc_url_raw) and validates values against expected sets. Apply proactively before storing or using any untrusted value.
wpultimatesecurity · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add wpultimatesecurity/WordPress-Security-Skills --skill input-sanitization-validation技能说明
name: input-sanitization-validation description: > Use when reading any external input in WordPress — $_GET, $_POST, $_REQUEST, $_COOKIE, REST params, shortcode/block attributes, option/meta values, or remote API responses. Unslashes then sanitizes to the correct type (sanitize_text_field, sanitize_email, absint, sanitize_key, wp_kses_post, esc_url_raw) and validates values against expected sets. Apply proactively before storing or using any untrusted value. compatibility: "Examples generally use PHP 7.4 syntax; check each API against target WordPress/PHP versions. Use maintained WordPress and supported PHP in production. Shell examples require their named tools." license: MIT metadata: tags: "wordpress, security, php, sanitization, validation, input"
Input sanitization & validation
When to use this skill
Use this skill whenever a value enters your code from outside:
- Superglobals:
$_GET,$_POST,$_REQUEST,$_COOKIE,$_SERVER. - REST API parameters, AJAX payloads, form fields.
- Shortcode attributes, block attributes, widget settings.
- Values read back from
get_option, post meta, or user meta that originated from users. - Bodies returned by remote APIs (
wp_remote_get, etc.).
Sanitize on input, escape on output — two different jobs. This skill covers input;
see output-escaping for output.
Core principles (and why they matter)
- All external input is untrusted. Even data from your own admin screens can be forged (CSRF) or tampered with. Sanitize regardless of source.
- Unslash before sanitizing. WordPress adds slashes to superglobals. Calling
wp_unslash()first prevents corrupted data (it\'sinstead ofit's) and ensures sanitizers see the real value. - Sanitize to the expected type. A function that expects an integer should call
absint(); an email,sanitize_email(). Type-appropriate sanitizing shrinks the attack surface far more than a generic catch-all. - Validation ≠ sanitization. Sanitizing cleans a value; validating rejects values
that don't belong. Use both: sanitize the shape, then validate against allowed sets
(
in_arraywith strict comparison, range checks,is_email). - Prefer allowlists over blocklists. Define what is permitted, reject the rest.
- Sanitize rich content with
wp_kses/wp_kses_post, never withstrip_tags—strip_tagsleaves dangerous attributes and is not XSS-safe.
Step-by-step implementation
- Confirm the value is set with
isset()/ null coalescing and a safe default. - Unslash:
wp_unslash( $_POST['x'] ). - Sanitize to type: pick the function matching the data (see cheatsheet).
- Validate: check ranges, allowed values, formats; reject invalid input.
- Use or store the clean value. Re-escape when later output.
$status = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : '';
$allowed = array( 'draft', 'publish', 'pending' );
$status = in_array( $status, $allowed, true ) ? $status : 'draft'; // validate + fallback
Supporting references
| Reference | Load when |
|---|---|
| Input sanitization & validation checklist | Before final verification of the input sanitization & validation controls. |
| Sanitization & validation cheatsheet | Choosing the applicable WordPress API or control for input sanitization and validation. |
Common AI mistakes / anti-patterns
Mistake 1 — Sanitizing without unslashing
// ❌ Insecure/buggy: slashes corrupt the value and bypass intent.
$name = sanitize_text_field( $_POST['name'] );
// ✅ Correct: unslash first.
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
Mistake 2 — Using strip_tags() as if it were XSS-safe
// ❌ Insecure: strip_tags keeps event handlers/attributes and is not escaping.
$bio = strip_tags( $_POST['bio'] );
update_user_meta( $user_id, 'bio', $bio );
// ✅ Secure: allow a known-safe subset with wp_kses_post (or a custom allowlist).
$bio = isset( $_POST['bio'] ) ? wp_kses_post( wp_unslash( $_POST['bio'] ) ) : '';
update_user_meta( $user_id, 'bio', $bio );
Mistake 3 — Wrong sanitizer for the type
// ❌ Insecure: text sanitizer on something that must be an integer / URL.
$post_id = sanitize_text_field( $_GET['post_id'] ); // "12 OR 1=1" survives as a string
$link = sanitize_text_field( $_POST['link'] ); // allows javascript: scheme
// ✅ Secure: type-correct sanitizers.
$post_id = absint( $_GET['post_id'] ?? 0 );
$link = esc_url_raw( wp_unslash( $_POST['link'] ?? '' ) ); // for storage; esc_url for output
Mistake 4 — Sanitizing but never validating
// ❌ Weak: clean string, but any value is accepted.
$role = sanitize_key( wp_unslash( $_POST['role'] ?? '' ) );
$user->set_role( $role ); // attacker sends "administrator"
// ✅ Secure: validate against an allowlist.
$role = sanitize_key( wp_unslash( $_POST['role'] ?? '' ) );
$allowed = array( 'subscriber', 'contributor', 'author' );
if ( ! in_array( $role, $allowed, true ) ) {
wp_die( esc_html__( 'Invalid role.', 'my-plugin' ), 400 );
}
$user->set_role( $role );
Mistake 5 — Sanitizing arrays as if they were scalars
// ❌ Buggy/insecure: passing an array to a scalar sanitizer.
$ids = sanitize_text_field( $_POST['ids'] ); // returns "" for arrays
// ✅ Correct: map the sanitizer across the array.
$ids = isset( $_POST['ids'] ) && is_array( $_POST['ids'] )
? array_map( 'absint', wp_unslash( $_POST['ids'] ) )
: array();
Mistake 6 — Open redirect via unvalidated URL
// ❌ Insecure: attacker can redirect to a phishing site.
$redirect = $_GET['redirect_to'];
wp_redirect( $redirect );
exit;
// ✅ Secure: validate redirect targets with wp_validate_redirect and use wp_safe_redirect.
$r
阅读完整来源 ↗