--- name: ai-llm-integration-security description: > Use when building or reviewing WordPress plugin or theme features that call an LLM or AI provider: chatbots, content generation, summarization, AI search/RAG over site content, agents that call tools, Abilities API abilities, or MCP exposure. Treats model output and all context as untrusted, binds every tool action to the human user's capabilities, confirms destructive actions server-side, keeps provider keys server-side, limits retrieval to what the user can read, and caps operator-paid spend. compatibility: "Examples generally use PHP 7.4 syntax. The Abilities API requires WordPress 6.9 or later (wp_register_ability() on wp_abilities_api_init, categories on wp_abilities_api_categories_init). Provider SDKs and the MCP adapter change quickly; check their current documentation." license: MIT metadata: tags: "wordpress, security, ai, llm, prompt-injection, abilities-api, mcp" --- # AI and LLM integration security ## When to use this skill Use this skill when WordPress code sends data to, or acts on output from, a language model: - A chatbot, assistant, or "generate/summarize/translate" button in wp-admin or on the front end. - AI search or retrieval-augmented generation (RAG) over posts, products, orders, or tickets. - An agent that calls tools: plugin functions, REST routes, Abilities API abilities, or an MCP server that exposes site actions to external AI clients. - Storing AI provider API keys, or exposing AI features to anonymous or low-role users. Do **not** use it for ordinary outbound HTTP (use `http-api-ssrf-prevention`), generic secrets storage (use `secrets-credentials-management`), or REST routes with no model in the loop (use `rest-api-security`). ## Core principles (and why they matter) 1. **Model output is untrusted input.** It can contain HTML, script, SQL fragments, URLs, shell text, or function names chosen by whoever influenced the prompt. Escape it for its output context and validate it before any use, exactly like `$_POST`. 2. **Everything in the context window can carry instructions.** Post content, comments, reviews, order notes, uploaded documents, fetched web pages, and earlier tool results are attacker-writable in most sites (prompt injection). A system prompt telling the model to ignore them is not a security control. 3. **Authorization belongs to the human, not the model.** Every tool or ability checks `current_user_can()` for the specific object when it executes, with the same capability the equivalent UI action requires. The model cannot grant capabilities, and a tool must never run as a more privileged user than the one chatting. 4. **Destructive or outward actions need server-enforced confirmation.** Deleting, publishing, emailing, refunding, changing roles, or installing code requires the user to approve the exact action and arguments; the server verifies that approval, not a flag the model sets. 5. **Only put into context what this user may read.** Retrieval, tool results, and system-prompt data must respect `read_post`/`read_private_posts` and object ownership; anything in the context can be echoed back by the model. 6. **Provider keys and spend stay server-side.** Keys never reach the browser. Anonymous and low-role access is rate-limited and quota-capped, because each request costs the site owner money. ## Step-by-step implementation 1. **Map the flow.** List each entry point (REST/AJAX route, admin page, cron, MCP), who can reach it, every source that enters the prompt, every tool the model can call, and every place the output lands (HTML, post content, email, options, database queries). 2. **Gate the entry point.** Nonce plus capability for admin features; for public chat, a nonce, a per-user/IP rate limit, message size limits, and a daily spend cap. 3. **Assemble context by permission.** Retrieve only posts and records the current user can read; strip secrets and other users' PII; mark untrusted content as data (delimiters help the model but are not a control). 4. **Register tools narrowly.** One purpose per tool, strict JSON input schemas, a `permission_callback` that checks the real capability for the specific object, and argument validation in the callback itself. Keep dangerous capabilities (code, files, users, plugins, settings) out of model-callable tools. 5. **Confirm before effects.** For destructive or outward tools, return a proposal with a server-stored, single-use confirmation token bound to the user, tool, and arguments; execute only when the user approves that token through a separate nonce-protected request. 6. **Treat output as input.** Escape with `esc_html()` or `wp_kses_post()` for display, sanitize before storage, and never pass it to `eval`, `call_user_func` with a model-chosen name, SQL, `include`, `wp_remote_get()` on model-chosen URLs, or redirects. 7. **Protect keys and log safely.** Store keys in a `wp-config.php` constant or an encrypted option, call the provider only from PHP, set request timeouts, and log metadata rather than full prompts containing PII. 8. **Test denied paths.** Exercise a subscriber asking for admin tools, an injected instruction inside a post being summarized, a model reply containing `