# Cron Background Job Security > Ú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. Fuente: https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/cron-background-job-security Markdown: https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/cron-background-job-security.md Repositorio: https://github.com/wpultimatesecurity/WordPress-Security-Skills Autor: wpultimatesecurity Licencia: MIT Actualizado: hace 2 meses Coste de contexto: 106 tok instalada, 2.3k tok al activarse, 3.5k tok con todos los archivos del bundle Bundle: 3 archivos, 14 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 cron-background-job-security --agent claude-code # Cursor npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill cron-background-job-security --agent cursor # Codex npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill cron-background-job-security --agent codex # Gemini CLI npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill cron-background-job-security --agent gemini # Windsurf npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill cron-background-job-security --agent windsurf # Cline npx -y skills add wpultimatesecurity/WordPress-Security-Skills --skill cron-background-job-security --agent cline ``` ## Qué hace - Hace que el agente trate los callbacks de cron de WordPress como ejecutados sin usuario autenticado - Sustituye `current_user_can()` por verificación contra contexto almacenado en el momento de programar la tarea - Evita que se guarden secretos en los argumentos o URLs de cron - Añade validación de los argumentos almacenados antes de usarlos en el callback - Añade guardas con `wp_next_scheduled()` y limpieza con `wp_clear_scheduled_hook()` en la desactivación ## Cuándo usarla - Al llamar a `wp_schedule_event()` o `wp_schedule_single_event()` - Al escribir el callback de un hook de cron (`add_action('mi_hook', ...)`) - Al ejecutar tareas a través de `wp-cron.php`, incluido el cron real externo - Al almacenar contexto (ids de usuario, argumentos) para que una tarea actúe más tarde ## Qué la activa - "Crea una tarea cron en WordPress que exporte datos de un usuario" - "Revisa este callback de wp_schedule_event, ¿está bien protegido?" - "Añade un cron diario a mi plugin con limpieza al desactivar" - "¿Es seguro pasar un token como argumento en wp_schedule_single_event?" ## Archivos - SKILL.md — 9 KB - references/checklist.md — 697 B - references/secure-cron-job.php — 4 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. # Cron & background job security ## When to use this skill Use this skill whenever code schedules or runs WordPress cron events: - Calling `wp_schedule_event()` or `wp_schedule_single_event()`. - Writing a callback for a cron hook (`add_action( 'my_plugin_daily_sync', ... )`). - Running jobs through `wp-cron.php`, including externally triggered real cron. - Storing context (user ids, arguments) for a job to act on later. Cron callbacks run with **no current user**. `current_user_can()` returns false for everything, and `wp-cron.php` is publicly reachable. Authorization must be captured at schedule time and re-verified inside the callback against stored context, not the absent current user. Related: see the `secrets-credentials-management` skill for keeping keys out of cron URLs and the `capability-permission-checks` skill for normal request authorization. ## Core principles (and why they matter) 1. **No `current_user_can()` inside cron callbacks.** There is no logged-in user during cron execution. Capture the acting user or capability context when scheduling and re-verify it from stored data. 2. **`wp-cron.php` is public.** Anyone can trigger it. The callback itself must enforce authorization, not rely on the trigger being hidden. 3. **Do not put secrets in scheduled URLs or args.** Cron arguments are stored in the `cron` option; treat them as persistent but not secret. 4. **Sanitize and validate stored args.** The job reads args that were stored earlier; validate them again before acting. 5. **Use `wp_next_scheduled()` to prevent duplicates.** Without it, reactivations or repeated `init` hooks can schedule the same event many times. 6. **Clean up on deactivation.** Use `wp_clear_scheduled_hook()` so events don't run for a deactivated plugin. ## Step-by-step implementation 1. Schedule the event with `wp_next_scheduled()` guard: ```php if ( ! wp_next_scheduled( 'my_plugin_daily_sync' ) ) { wp_schedule_event( time(), 'daily', 'my_plugin_daily_sync' ); } ``` 2. If the job needs per-object authorization, store a user id or capability context alongside the args at schedule time (e.g., the user who queued a one-time export). 3. In the callback, re-verify the stored context: - Look up the user and check `user_can( $user_id, $capability )`. - Validate object ids with `get_post()` / `get_userdata()` and check ownership/permission. 4. Sanitize every stored arg before use. 5. Avoid passing secrets or raw user input as cron args. 6. On plugin deactivation, clear the hook. ### Supporting references | Reference | Load when | | --- | --- | | [Cron & background job security checklist](references/checklist.md) | Before final verification of the cron & background job security controls. | | [Secure cron job](references/secure-cron-job.php) | Implementing a recurring cron job with stored authorization context and deactivation cleanup. | ## Common AI mistakes / anti-patterns ### Mistake 1 — Using `current_user_can()` inside a cron callback ```php // ❌ Insecure: current_user_can is meaningless during cron; this always fails. add_action( 'my_plugin_daily_sync', function () { if ( ! current_user_can( 'manage_options' ) ) { return; } my_plugin_sync(); } ); ``` ```php // ✅ Secure: the scheduled event itself is the authorization boundary; validate data only. add_action( 'my_plugin_daily_sync', 'my_plugin_run_daily_sync' ); function my_plugin_run_daily_sync() { $settings = get_option( 'my_plugin_sync_settings', array() ); $endpoint = isset( $settings['endpoint'] ) ? esc_url_raw( $settings['endpoint'] ) : ''; if ( ! $endpoint ) { return; } // No current_user_can needed — the fact the event was scheduled is the policy. my_plugin_sync( $endpoint ); } ``` ### Mistake 2 — Passing a secret in the cron URL ```php // ❌ Insecure: secret lands in the cron option and may appear in logs. wp_schedule_single_event( time(), 'my_plugin_fetch', array( 'token' => $api_secret ) ); ``` ```php // ✅ Secure: store the secret in a wp-config constant or encrypted option; pass only ids. wp_schedule_single_event( time(), 'my_plugin_fetch', array( 'feed_id' => $feed_id ) ); // Callback reads the secret from a secure location. ``` ### Mistake 3 — Trusting `$_GET` passed to a cron trigger ```php // ❌ Insecure: wp-cron.php is public and $_GET is attacker-controllable. add_action( 'my_plugin_run_export', function () { $user_id = (int) $_GET['user_id']; my_plugin_export_for_user( $user_id ); } ); ``` ```php // ✅ Secure: capture and validate context at schedule time, then re-verify inside the job. function my_plugin_queue_export( $user_id ) { if ( ! current_user_can( 'manage_options' ) ) { return false; } $user_id = absint( $user_id ); wp_schedule_single_event( time(), 'my_plugin_run_export', array( 'user_id' => $user_id ) ); } add_action( 'my_plugin_run_export', 'my_plugin_run_export_callback' ); function my_plugin_run_export_callback( $args ) { $user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : 0; if ( $user_id <= 0 || ! user_can( $user_id, 'read' ) ) { return; } my_plugin_export_for_user( $user_id ); } ``` ### Mistake 4 — Scheduling duplicate events ```php // ❌ Risky: creates a new event on every init if not guarded. add_action( 'init', function () { wp_schedule_event( time(), 'hourly', 'my_plugin_hourly' ); } ); ``` ```php // ✅ Secure: guard with wp_next_scheduled(). add_action( 'init', function () { if ( ! wp_next_scheduled( 'my_plugin_hourly' ) ) { wp_schedule_event( time(), 'hourly', 'my_plugin_hourly' ); } } ); ``` ### Mistake 5 — Forgetting to unschedule on deactivation ```php // ❌ Risky: scheduled hooks outlive the plugin — nothing clears them. // (No register_deactivation_hook() anywhere in the plugin.) register_activation_hook( __FILE__, function () { if ( ! wp_next_scheduled( 'my_plugin_hourly' ) ) { wp_schedule_event( time(), 'hourly', 'my_plugin_hourly' ); } } ); ``` ```php // ✅ Secure: clear scheduled hooks on deactivation. register_deactivation_hook( __FILE__, 'my_plugin_deactivate' ); function my_plugin_deactivate() { wp_clear_scheduled_hook( 'my_plugin_hourly' ); wp_clear_scheduled_hook( 'my_plugin_daily_sync' ); } ``` ## Correct code examples An integration example — schedule, callback with stored-context verification, and deactivation cleanup — is in [`references/secure-cron-job.php`](references/secure-cron-job.php). It calls `my_plugin_generate_user_export( $user_id )`, a project callback your plugin must provide (the file documents the contract); adapt names and hooks to your plugin. ## Checklist - [ ] Cron callbacks do not rely on `current_user_can()`. - [ ] Per-object jobs capture the acting user/context at schedule time and re-verify it in the callback. - [ ] Secrets are not passed as cron arguments or embedded in cron URLs. - [ ] Stored cron args are sanitized and validated before use. - [ ] `wp_next_scheduled()` prevents duplicate scheduled events. - [ ] Custom recurrence intervals are added via the `cron_schedules` filter before scheduling. - [ ] Scheduled hooks are cleared on plugin deactivation. - [ ] Failures are logged without exposing secrets or internal paths. ## Official references - [`wp_schedule_event()`](https://developer.wordpress.org/reference/functions/wp_schedule_event/) - [`wp_schedule_single_event()`](https://developer.wordpress.org/reference/functions/wp_schedule_single_event/) - [`wp_next_scheduled()`](https://developer.wordpress.org/reference/functions/wp_next_scheduled/) - [`wp_clear_scheduled_hook()`](https://developer.wordpress.org/reference/functions/wp_clear_scheduled_hook/) - [`wp_unschedule_event()`](https://developer.wordpress.org/reference/functions/wp_unschedule_event/) - [`user_can()`](https://developer.wordpress.org/reference/functions/user_can/) - [`DISABLE_WP_CRON` constant](https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#disable-cron) - [Cron — Plugin Handbook](https://developer.wordpress.org/plugins/cron/) - [OWASP — Unvalidated Redirects and Forwards](https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html) ## 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. - [Ajax Security](https://skillsagentes.com/skills/wpultimatesecurity/wordpress-security-skills/ajax-security.md): Ú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. - [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. - [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)