# Ajax Security > Úsalo al registrar o manejar AJAX en WordPress vía admin-ajax.php: verifica el nonce, comprueba permisos, sanitiza cada campo y responde con wp_send_json_*, evitando CSRF e inyecciones. Fuente: https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/ajax-security Markdown: https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/ajax-security.md Repositorio: https://github.com/wpultimatesecurity/WordPress-Security-Skills Autor: wpultimatesecurity Licencia: MIT Actualizado: hace 2 meses Coste de contexto: 119 tok instalada, 3.1k tok al activarse, 4.2k tok con todos los archivos del bundle Bundle: 3 archivos, 16 KB Permisos que pide: ninguno declarado ## Instalación Un skill son archivos markdown: los mismos archivos valen para cualquier agente y lo único que cambia es el directorio de destino, es decir la bandera `--agent`. Añade `-g` para instalarlo en todos los proyectos de la máquina. ```bash # Claude Code npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill ajax-security --agent claude-code # Cursor npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill ajax-security --agent cursor # Codex npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill ajax-security --agent codex # Gemini CLI npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill ajax-security --agent gemini # Windsurf npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill ajax-security --agent windsurf # Cline npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill ajax-security --agent cline ``` ## Qué hace - Verifica el nonce con check_ajax_referer antes de actuar en el handler de AJAX - Comprueba current_user_can inmediatamente después del nonce para controlar permisos - Aplica wp_unslash y sanitiza cada campo de $_POST/$_GET según su tipo - Fuerza la salida por wp_send_json_success/wp_send_json_error o wp_die, nunca con echo directo - Añade throttling antes de acciones costosas o anónimas, con atomicidad real en límites estrictos ## Cuándo usarla - Al registrar o manejar una acción AJAX vía admin-ajax.php con wp_ajax_{action} o wp_ajax_nopriv_{action} - Al escribir JavaScript que hace POST a admin_url('admin-ajax.php') con jQuery, fetch o wp.apiFetch - Al devolver JSON, fragmentos HTML o cualquier dato calculado en el servidor desde un handler AJAX ## Qué la activa - "Registra una acción wp_ajax_my_plugin_vote segura con nonce y capability check" - "Revisa este handler de admin-ajax.php por fallos de CSRF o sanitización" - "Añade un endpoint AJAX público de búsqueda con límite de peticiones" ## Antes de instalar - Requiere entorno WordPress con PHP compatible (los ejemplos usan sintaxis PHP 7.4). ## Archivos - SKILL.md — 12 KB - references/checklist.md — 766 B - references/secure-ajax-handler.php — 3 KB ## SKILL.md Reproducido tal cual desde wpultimatesecurity/WordPress-Security-Skills bajo MIT. Esta sección es el documento original y está en inglés. # AJAX security ## When to use this skill Use this skill whenever code registers or handles an AJAX action through WordPress's `admin-ajax.php` endpoint: - `add_action( 'wp_ajax_{action}', ... )` for logged-in users. - `add_action( 'wp_ajax_nopriv_{action}', ... )` for logged-out visitors. - JavaScript posting to `admin_url( 'admin-ajax.php' )` with jQuery, `fetch`, or `wp.apiFetch`. - Returning JSON, HTML fragments, or any server-computed data from an AJAX handler. If the request changes state or exposes privileged data, it needs a nonce **and** a capability check. `wp_ajax_nopriv_*` is intentionally unauthenticated — never use it for admin actions. Related: see the `nonces-csrf-protection` skill for the full nonce lifecycle and the `rest-api-security` skill for REST endpoints. ## Core principles (and why they matter) 1. **`wp_ajax_*` vs `wp_ajax_nopriv_*` are different trust boundaries.** `nopriv` fires for anonymous visitors; use it only for truly public actions. Privileged actions must use `wp_ajax_*`. 2. **Nonce + capability for privileged actions.** `check_ajax_referer()` checks a CSRF token, not authenticated origin or authorization; `current_user_can()` checks permission. 3. **Sanitize every input field.** `$_POST` values in AJAX are just as attacker-controllable as any other request. `wp_unslash()` then sanitize to type before use. 4. **Exit through `wp_send_json_*` or `wp_die()`.** Never echo raw output and fall through. JSON responses keep the contract explicit and prevent accidental HTML/PHP leakage. 5. **Scope the nonce to the action.** A nonce for `my_plugin_delete_item_42` cannot be replayed to delete item 99. 6. **Fail closed.** Any failed check returns a JSON error or dies; the handler never continues to act. 7. **Throttle anonymous and expensive actions before the work.** Public nonces can be obtained and replayed by attackers; they are not rate limits. Transient counters are best-effort load shedding only: read/increment/write is non-atomic and cached entries can disappear early. Strict limits need an atomic shared backend or an edge/server limiter with defined windows, trusted client identity, and failure policy. ## Step-by-step implementation 1. Register the handler with the correct hook (`wp_ajax_*` for authenticated, `wp_ajax_nopriv_*` for public). 2. Enqueue the script and pass the AJAX URL + nonce via `wp_localize_script()` or `wp_add_inline_script()`. 3. In the handler, verify the nonce first with `check_ajax_referer( $action, $query_arg, false )`. Passing `false` lets you return a clean JSON error instead of dying with `-1`. 4. Check `current_user_can()` immediately after the nonce. 5. `wp_unslash()` and sanitize every `$_POST` / `$_GET` field, using the right sanitizer for the type (`absint`, `sanitize_text_field`, `sanitize_key`, `sanitize_email`, etc.). 6. Enforce the abuse policy **before** queries, mail, or other expensive actions; return `wp_send_json_error( ..., 429 )` when rejected. Use an atomic limiter where bypass would create a security or cost risk; a transient is only a best-effort supplement. 7. Perform the authorized action. 8. Return `wp_send_json_success()` or `wp_send_json_error()` and stop. ### Supporting references | Reference | Load when | | --- | --- | | [AJAX security checklist](references/checklist.md) | Before final verification of AJAX security controls. | | [Secure AJAX handler](references/secure-ajax-handler.php) | Implementing the privileged admin-ajax.php profile-update flow with nonce, capability, input validation, and JSON responses. | ## Common AI mistakes / anti-patterns ### Mistake 1 — No nonce verification ```php // ❌ Insecure: any site can POST to this action and trigger the handler. add_action( 'wp_ajax_my_plugin_vote', function () { $post_id = absint( $_POST['post_id'] ); update_post_meta( $post_id, '_votes', get_post_meta( $post_id, '_votes', true ) + 1 ); wp_send_json_success(); } ); ``` ```php // ✅ Secure: verify nonce, then capability, then act. add_action( 'wp_ajax_my_plugin_vote', 'my_plugin_ajax_vote' ); function my_plugin_ajax_vote() { if ( ! check_ajax_referer( 'my_plugin_vote', 'nonce', false ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed.', 'my-plugin' ) ), 403 ); } if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( array( 'message' => __( 'Forbidden.', 'my-plugin' ) ), 403 ); } $post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0; if ( $post_id <= 0 ) { wp_send_json_error( array( 'message' => __( 'Invalid post.', 'my-plugin' ) ), 400 ); } update_post_meta( $post_id, '_votes', get_post_meta( $post_id, '_votes', true ) + 1 ); wp_send_json_success(); } ``` ### Mistake 2 — Using `wp_ajax_nopriv_*` for a privileged action ```php // ❌ Insecure: anonymous users can now save settings. add_action( 'wp_ajax_nopriv_my_plugin_save_settings', 'my_plugin_save_settings' ); ``` ```php // ✅ Secure: privileged actions use wp_ajax_* only. add_action( 'wp_ajax_my_plugin_save_settings', 'my_plugin_save_settings' ); function my_plugin_save_settings() { check_ajax_referer( 'my_plugin_save_settings', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Forbidden.', 'my-plugin' ) ), 403 ); } // ...sanitize + save... wp_send_json_success(); } ``` ### Mistake 3 — Echoing raw `$_POST` or building HTML without escaping ```php // ❌ Insecure: reflected XSS. echo '
' . $_POST['message'] . '
'; ``` ```php // ✅ Secure: escape at the point of output, even inside a JSON payload. $message = sanitize_text_field( wp_unslash( $_POST['message'] ?? '' ) ); wp_send_json_success( array( 'html' => '
' . esc_html( $message ) . '
' ) ); ``` ### Mistake 4 — Forgetting `wp_unslash()` ```php // ❌ Risky: WordPress slashes superglobals; data may contain escaped quotes. $label = sanitize_text_field( $_POST['label'] ); ``` ```php // ✅ Secure: unslash first, then sanitize. $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; ``` ### Mistake 5 — Trusting `is_user_logged_in()` instead of a capability ```php // ❌ Insecure: every logged-in user, including subscribers, can run this. if ( is_user_logged_in() ) { delete_option( 'my_plugin_config' ); } ``` ```php // ✅ Secure: check a real capability. if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Forbidden.', 'my-plugin' ) ), 403 ); } ``` ### Mistake 6 — Unthrottled public action ```php // ❌ Insecure: anonymous endpoint, no rate limit — unbounded cost per visitor. add_action( 'wp_ajax_nopriv_my_plugin_search', 'my_plugin_ajax_search' ); function my_plugin_ajax_search() { $q = isset( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : ''; wp_send_json_success( my_plugin_run_expensive_query( $q ) ); } ``` ```php // Best-effort only: public, read-only search with bounded output. add_action( 'wp_ajax_nopriv_my_plugin_search', 'my_plugin_ajax_search' ); function my_plugin_ajax_search() { if ( ! isset( $_POST['q'] ) || ! is_string( $_POST['q'] ) || strlen( $_POST['q'] ) > 200 ) { wp_send_json_error( array( 'message' => 'Invalid query.' ), 400 ); } $q = sanitize_text_field( wp_unslash( $_POST['q'] ) ); if ( '' === $q ) { wp_send_json_error( array( 'message' => 'Query required.' ), 400 ); } // Fixed minute buckets; counters can race or be evicted. Not a strict quota. $ip = $_SERVER['REMOTE_ADDR'] ?? ''; $window = intdiv( time(), MINUTE_IN_SECONDS ); $key = 'my_plugin_rl_' . wp_hash( 'search|' . $ip . '|' . $window ); $count = (int) get_transient( $key ); if ( $count >= 20 ) { wp_send_json_error( array( 'message' => __( 'Too many requests.', 'my-plugin' ) ), 429 ); } set_transient( $key, $count + 1, MINUTE_IN_SECONDS ); // The counter is checked before the query, never after sending the response. $ids = get_posts( array( 's' => $q, 'post_type' => 'post', 'post_status' => 'publish', 'numberposts' => 10, 'fields' => 'ids', ) ); wp_send_json_success( $ids ); } ``` This read-only public search does not use a nonce as an abuse control. State-changing or privileged handlers still need their CSRF and capability checks. Fixed windows allow bursts across boundaries; parallel requests can lose counter increments, and cache eviction/storage failure can reset this best-effort counter. Do not use it as the sole control for mail sending, paid APIs, voting integrity, or brute-force defense. `REMOTE_ADDR` identifies the direct peer: behind a proxy it may identify the proxy, not the visitor, and shared NATs group users together. Only trust forwarded addresses after a configured trusted proxy strips client-supplied headers and supplies a validated address; never read arbitrary `X-Forwarded-For` directly. Strict distributed limits need an atomic shared operation (including expiry, e.g. a Redis script), or an edge/server limiter on all relevant routes. See `authentication-session-security` for login-specific considerations. ## Correct code examples A complete, copy-paste-ready AJAX handler (PHP + JavaScript) is in [`references/secure-ajax-handler.php`](references/secure-ajax-handler.php). ## Checklist - [ ] The action is registered on `wp_ajax_*` for privileged flows or `wp_ajax_nopriv_*` only when the action is truly public. - [ ] The handler verifies the nonce with `check_ajax_referer()` before acting. - [ ] A `current_user_can()` check runs immediately after nonce verification. - [ ] Every `$_POST` / `$_GET` field is `wp_unslash()`-ed and sanitized to type. - [ ] The handler exits via `wp_send_json_success()`, `wp_send_json_error()`, or `wp_die()`. - [ ] Output inside JSON responses is escaped for its context (`esc_html`, `esc_attr`, `esc_url`). - [ ] The nonce action string is specific and scoped (e.g., includes an object id). - [ ] Secrets are never returned to the browser or logged. - [ ] Abuse controls run before expensive actions; strict limits use atomic shared or edge enforcement, not transients. - [ ] Client identity accounts for trusted proxies/shared IPs; arbitrary forwarded headers are not trusted. ## Official references - [Transients API — early expiry and caching semantics](https://developer.wordpress.org/apis/transients/) - [AJAX in Plugins — Plugin Handbook](https://developer.wordpress.org/plugins/javascript/ajax/) - [`check_ajax_referer()`](https://developer.wordpress.org/reference/functions/check_ajax_referer/) - [`wp_create_nonce()`](https://developer.wordpress.org/reference/functions/wp_create_nonce/) - [`wp_send_json_success()`](https://developer.wordpress.org/reference/functions/wp_send_json_success/) - [`wp_send_json_error()`](https://developer.wordpress.org/reference/functions/wp_send_json_error/) - [`wp_localize_script()`](https://developer.wordpress.org/reference/functions/wp_localize_script/) - [`current_user_can()`](https://developer.wordpress.org/reference/functions/current_user_can/) - [`wp_unslash()`](https://developer.wordpress.org/reference/functions/wp_unslash/) - [OWASP — Cross-Site Request Forgery (CSRF)](https://owasp.org/www-community/attacks/csrf) ## Dónde encaja - Categoría: [Seguridad](https://skillsagentes.com/categorias/seguridad.md) — Auditorías, revisión de dependencias, manejo de secretos y modelado de amenazas. - Creador: [wpultimatesecurity](https://skillsagentes.com/creators/wpultimatesecurity.md) — 27 skills en el directorio - [Todas las skills](https://skillsagentes.com/skills.md) - [Ranking de instalaciones](https://skillsagentes.com/ranking.md) ## Otras skills del mismo repositorio - [Security Auditing Code Review](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/security-auditing-code-review.md): Úsalo al auditar o revisar código de un plugin o tema WordPress existente, triar un informe de vulnerabilidad o endurecer código heredado, con una metodología sistemática. - [Authentication Session Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/authentication-session-security.md): Úsalo cuando el código inicia o cierra sesión, gestiona cookies o tokens de sesión, limita intentos de login o construye un formulario de login propio en WordPress. - [Dependency Supply Chain Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/dependency-supply-chain-security.md): Úsalo cuando un plugin o tema empaquete una librería de terceros, cargue un asset desde un CDN, ejecute código remoto, use Composer o prepare el zip distribuible. - [Secure Plugin Development](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/secure-plugin-development.md): Úsalo al iniciar un plugin o tema de WordPress, al añadir hooks o funciones que manejan peticiones, opciones o salida; establece la línea base segura por defecto. - [Gutenberg Block Editor Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/gutenberg-block-editor-security.md): Úsalo al construir bloques dinámicos o funciones del editor de bloques: render_callback, ServerSideRender, datos REST o register_rest_field, para evitar XSS almacenado y control de acceso roto. ## Skills relacionadas - [Ai Llm Integration Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/ai-llm-integration-security.md): Úsalo al construir o revisar funciones de plugin o tema de WordPress que llaman a un LLM: chatbots, generación de contenido, RAG, agentes con herramientas, Abilities API o exposición MCP. - [Authentication Session Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/authentication-session-security.md): Úsalo cuando el código inicia o cierra sesión, gestiona cookies o tokens de sesión, limita intentos de login o construye un formulario de login propio en WordPress. - [Capability Permission Checks](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/capability-permission-checks.md): Protege operaciones privilegiadas en WordPress con current_user_can() usando la capability correcta (no el rol), checks por objeto y nonce, evitando escalada de privilegios y control de acceso roto. - [Cron Background Job Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/cron-background-job-security.md): Úsalo al programar eventos cron con wp_schedule_event / wp_schedule_single_event o al escribir el callback. Revalida la autorización contra el contexto guardado, no con current_user_can, y mantiene los secretos fuera de las URLs de cron. - [Dependency Supply Chain Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/dependency-supply-chain-security.md): Úsalo cuando un plugin o tema empaquete una librería de terceros, cargue un asset desde un CDN, ejecute código remoto, use Composer o prepare el zip distribuible. --- Skills Agentes · [Índice de páginas en markdown](https://skillsagentes.com/sitemap.md) · [Inicio](https://skillsagentes.com/index.md)