Logo: Capability Permission Checks

Capability Permission Checks

Organisatioun
wpultimatesecurity
capability-permission-checks

Benotzt dëst, wann Dir Adminsäiten, Menüpunkten, AJAX-/REST-Handler oder Aktiounslinken derbäisetzt oder Code schreift, deen eng privilegéiert Aktioun a WordPress ausféiert. Erlaabt Aktiounen nëmme mat current_user_can() an der passender Fäegkeet (net mat Rollen), och bei Kontrollen op eenzel Objeten ewéi edit_post, a kombinéiert d'Kontroll mat engem Nonce. Verhënnert d'Eskalatioun vu Rechter a fehlerhaft Zougrëffskontroll. Setzt dëst proaktiv bei all privilegéierte Codewee an.

Add to TypingMind

Iwwersiicht

Editeurwpultimatesecurity
RepositoryWordPress-Security-Skills
Skill-Nummcapability-permission-checks
Stären
54
Forks
3
Matgeliwwert Dateien
2
LizenzMIT
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 2 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    wpultimatesecurity huet de Skill op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Capability Permission Checks an TypingMind, fir e mat all LLM ze benotzen, oder setzt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vu GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier, déi hei drënner steet, an a bestätegt d'Installatioun.
    https://github.com/wpultimatesecurity/WordPress-Security-Skills/tree/dev/skills/capability-permission-checks
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill derbäisetzen → Iwwer eng GitHub-URL. Pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an d'Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/wpultimatesecurity/WordPress-Security-Skills.git /tmp/WordPress-Security-Skills
mkdir -p .claude/skills
cp -r /tmp/WordPress-Security-Skills/skills/capability-permission-checks .claude/skills/capability-permission-checks
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Capability Permission Checks an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Capability Permission Checks automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Capability Permission Checks net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Schlësselen.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Capability & permission checks (authorization)

When to use this skill

Use this skill wherever code does something not everyone should be able to do:

  • Admin menu/submenu pages and settings screens.
  • AJAX (wp_ajax_*) and REST permission_callbacks.
  • admin-post.php handlers and action links (delete/approve/activate).
  • Editing options, users, posts, taxonomies, files, or plugin state.
  • Anything reading or writing another user's data.

Authorization answers "is this user allowed?" — distinct from the nonce, which answers "did this request come from us?" (see nonces-csrf-protection). Use both.

Core principles (and why they matter)

  1. Check capabilities, not roles. Roles are bundles of capabilities that admins can reassign. current_user_can( 'administrator' ) is a bug — test the capability (manage_options), so custom roles and multisite behave correctly.
  2. Use the most specific capability. manage_options for settings, edit_posts to create posts, edit_post/delete_post (with an ID) for a specific post. Broad caps over-grant.
  3. Authorization and CSRF are independent. A capability check without a nonce is CSRF-able; a nonce without a capability check lets any logged-in user act. Pair them.
  4. Re-check at every entry point. Hiding a menu item or button is UX, not security. The handler that does the work must check again — the URL/endpoint is directly reachable.
  5. Per-object checks need the object ID. current_user_can( 'edit_post', $post_id ) respects ownership and post-type rules; current_user_can( 'edit_posts' ) alone does not.
  6. Fail closed. Deny by default; only proceed when the check explicitly passes.

Step-by-step implementation

  1. Identify the operation and the capability it requires (use a meta cap with an ID for per-object actions).
  2. At the start of the handler, after the nonce check: if ( ! current_user_can( $cap[, $object_id] ) ) { deny(); }.
  3. deny() = wp_die( ..., 403 ), wp_send_json_error( ..., 403 ), or a REST WP_Error with rest_forbidden / status 403.
  4. For menus/REST, supply the capability where the API expects it (add_menu_page capability arg, REST permission_callback).
  5. Never gate solely by hiding UI.

Supporting references

ReferenceLoad when
Capability check examplesImplementing capability-gated admin pages, AJAX handlers, per-object actions, and REST callbacks.
Capability / authorization checklistBefore final verification of the capability / authorization controls.

Common AI mistakes / anti-patterns

Mistake 1 — Checking the role instead of the capability

php
// ❌ Insecure: roles are mutable; this misses custom/multisite setups.
if ( in_array( 'administrator', wp_get_current_user()->roles, true ) ) {
    do_admin_thing();
}
php
// ✅ Secure: test the capability.
if ( current_user_can( 'manage_options' ) ) {
    do_admin_thing();
}

Mistake 2 — Relying on a hidden menu/button as the boundary

php
// ❌ Insecure: handler trusts that only admins saw the link.
add_action( 'admin_post_purge_cache', function () {
    purge_cache(); // directly reachable via admin-post.php by anyone logged in
} );
php
// ✅ Secure: the handler re-checks nonce + capability.
add_action( 'admin_post_purge_cache', function () {
    check_admin_referer( 'purge_cache' );
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'Forbidden', 'my-plugin' ), 403 );
    }
    purge_cache();
} );

Mistake 3 — Generic capability where a per-object check is needed

php
// ❌ Insecure: any author can edit ANY post, not just their own.
if ( current_user_can( 'edit_posts' ) ) {
    wp_update_post( array( 'ID' => $post_id, 'post_title' => $title ) );
}
php
// ✅ Secure: meta capability with the object ID enforces ownership.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
    wp_die( esc_html__( 'You cannot edit this post.', 'my-plugin' ), 403 );
}
wp_update_post( array( 'ID' => $post_id, 'post_title' => $title ) );

Mistake 4 — REST route with no real permission callback

php
// ❌ Insecure: open to the world.
register_rest_route( 'my/v1', '/settings', array(
    'methods'             => 'POST',
    'callback'            => 'my_save',
    'permission_callback' => '__return_true',
) );
php
// ✅ Secure: enforce the capability in permission_callback.
register_rest_route( 'my/v1', '/settings', array(
    'methods'             => 'POST',
    'callback'            => 'my_save',
    'permission_callback' => function () {
        return current_user_can( 'manage_options' );
    },
) );

Mistake 5 — Checking capability but never branching

php
// ❌ Insecure: result ignored; action runs regardless.
current_user_can( 'delete_users' );
delete_user_account( $id );
php
// ✅ Secure: branch and fail closed.
if ( ! current_user_can( 'delete_users' ) ) {
    wp_die( esc_html__( 'Forbidden', 'my-plugin' ), 403 );
}
delete_user_account( $id );

Mistake 6 — Assigning roles or capabilities from request data

php
// ❌ Insecure: privilege escalation by posting role=administrator.
$user->set_role( $_POST['role'] );
php
// ✅ Secure: validate the role against a hardcoded allowlist.
$allowed_roles = array( 'subscriber', 'contributor', 'author' );
$role          = isset( $_POST['role'] ) ? sanitize_key( wp_unslash( $_POST['role'] ) ) : '';
if ( ! in_array( $role, $allowed_roles, true ) ) {
    wp_die( esc_html__( 'Invalid role.', 'my-plugin' ), 400 );
}
$user->set_role( $role );

Mistake 7 — Directly adding capabilities via user meta

php
// ❌ Insecure: grants arbitrary capabilities.
$user->add_cap( $_POST['capability'] );
php
// ✅ Secure: never add capabilities from input; use role-based allowlists.
$allowed_caps = array( 'edit_posts', 'publish_posts' );
$cap          = isset( $_POST['capability'] ) ? sanitize_key( wp_unslash( $_POST['capability'] ) ) : '';
if ( ! in_array( $cap, $allowed_caps, true ) ) {
    wp_die( esc_html__( 'Invalid capability.', 'my-plugin' ), 400 );
}
$user->add_cap( $cap );

Related: see the multisite-security skill for network vs. site scope and the nonces-csrf-protection skill for pairing these checks with a nonce.

Correct code examples

A complete example covering an admin page, an AJAX handler, a per-object action, and a REST permission callback is in references/capability-check-example.php.

php
// Common capabilities and when to use them.
current_user_can( 'manage_options' );          // site settings / options
current_user_can( 'edit_posts' );              // create/edit posts (general)
current_user_can( 'edit_post', $post_id );     // edit THIS post (ownership-aware)
current_user_can( 'publish_posts' );           // publish
current_user_can( 'upload_files' );            // media uploads
current_user_can( 'edit_users' );              // manage other users
current_user_can( 'install_plugins' );         // install/activate plugins

Checklist

  • Every privileged handler calls current_user_can() and branches on the result.
  • Capabilities are tested, never roles.
  • The most specific capability is used; per-object actions pass the object ID.
  • The check is paired with a nonce verification on state-changing requests.
  • Admin pages re-check the capability on render (not just at menu registration).
  • REST routes define a real permission_callback (never __return_true for writes).
  • AJAX handlers check capability after check_ajax_referer.
  • Denied requests fail closed with a 403 / WP_Error.

Official references

Matgeliwwert Dateien

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Capability Permission Checks?

Benotzt dëst, wann Dir Adminsäiten, Menüpunkten, AJAX-/REST-Handler oder Aktiounslinken derbäisetzt oder Code schreift, deen eng privilegéiert Aktioun a WordPress ausféiert. Erlaabt Aktiounen nëmme mat current_user_can() an der passender Fäegkeet (net mat Rollen), och bei Kontrollen op eenzel Objeten ewéi edit_post, a kombinéiert d'Kontroll mat engem Nonce. Verhënnert d'Eskalatioun vu Rechter a fehlerhaft Zougrëffskontroll. Setzt dëst proaktiv bei all privilegéierte Codewee an.

Firwat Capability Permission Checks op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Capability Permission Checks besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt, amplaz en Abonnement pro Benotzerplaz ze bezuelen. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Capability Permission Checks an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/wpultimatesecurity/WordPress-Security-Skills/tree/dev/skills/capability-permission-checks an. TypingMind liest d'Datei SKILL.md, bëndelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Capability Permission Checks benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Capability Permission Checks benotzen?

Esou vill wéi Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Capability Permission Checks domat benotzen – zum Beispill mat GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Capability Permission Checks gratis?

Jo. wpultimatesecurity huet de Skill ënner der Lizenz MIT op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbare Pak vun Instruktiounen, deen engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt e maache soll a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a kee Lafzäitëmfeld a funktionéiere mat all Modell.

Alleguer weisen

Elo Ären eegene KI-Aarbechtsberäich ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇