Skip to content

Module 12 · Security

Duration: 60 min · Prerequisites: modules 1, 2 and 6 (content types and capabilities, REST routes, AJAX).

By the end of this module you can spot in Pollora code where data leaves unescaped, where an endpoint is open and nobody meant it to be, where too many rights are handed out, and you know how to review what an AI agent wrote with those questions in mind.

The “find the bugs” snippets in this module are exercises: they contain deliberate defects. They are never code to copy.

In a Blade view, {{ }} escapes the value; {!! !!} writes it as is. For a block rendered by render.blade.php, the documentation is precise: use {!! !!} only for get_block_wrapper_attributes() and $content, which WordPress already escaped. Everything else (block attributes, text typed in the editor) goes through {{ }}.

<section {!! get_block_wrapper_attributes(['class' => 'py-16']) !!}>
<h2>{{ $attributes['heading'] ?? '' }}</h2>
<a href="{{ esc_url_raw($attributes['buttonUrl'] ?? '#') }}">Go</a>
{!! $content !!}
</section>

For a URL, filter the protocol with esc_url_raw() and let {{ }} escape: esc_url() already HTML-encodes, so inside {{ }} an & would come out as &amp;#038;.

A block rendered in Blade

blade

Click the faulty lines, then check. 2 bug(s) to find.

0 of 2 found

A #[WpRestRoute] route sets its permission on the class or on the method, and the method’s permission replaces the class’s. The classes provided: Everyone (anyone, logged in or not), IsLoggedIn, IsAdmin, and new Can('capability') for a WordPress capability (an instance, because Can takes arguments; parameter: 'id' passes a request parameter for a meta capability such as edit_post).

Since pollora/wp-rest 2.0 (framework 13.39.3), a route that names no permission is refused: rest_forbidden, status 401 for a visitor, 403 for a logged-in user. A route that is public on purpose says so with Everyone::class. Before 2.0 such a route stayed public: an older project may still behave that way, and an agent may “fix” a refused route by pasting Everyone everywhere.

#[Ajax] and the Ajax facade serve logged-in users only by default (AjaxAccess::LOGGED, hook wp_ajax_{action}). AjaxAccess::ALL (or forAllUsers()) adds wp_ajax_nopriv_{action}, and AjaxAccess::GUEST serves guests only: exposing wp_ajax_nopriv_* lets any unauthenticated visitor call the endpoint. The documentation makes it a conscious decision.

The documentation’s example sends _wpnonce: myApp.nonce with the request, and the script receives that nonce through localize(), created on the PHP side with wp_create_nonce('wp_rest'). That is all the AJAX page says: it does not say who verifies this nonce on the server. So do not rely on it as a handler’s only protection: it is the access restriction (AjaxAccess, REST permission, capability) that protects.

A REST route and an AJAX handler

php

Click the faulty lines, then check. 2 bug(s) to find.

0 of 2 found

3. Roles and capabilities: do not hand out more than needed

Section titled “3. Roles and capabilities: do not hand out more than needed”
  • Check a capability, not a role. @can('export_attendees'), can:export_attendees and new Can(…) stay right when a second role is given the capability; @role('event_manager') becomes wrong.
  • Sensitive capabilities. manage_options, edit_users, create_users, delete_users, promote_users, unfiltered_html, unfiltered_upload, install_plugins, activate_plugins, edit_plugins, edit_themes, edit_files and update_core can only be granted with allowSensitive: true on #[Role] or #[ModifyRole]. The flag makes the choice visible in code review; without it, discovery refuses the declaration (the error is logged with the class named, and the declaration is not applied).
  • Core roles. administrator, editor, author, contributor and subscriber cannot be redeclared with #[Role]: adjust them with #[ModifyRole].
  • #[Without] removes a capability; it never sets it to false. A role only grants or removes.
  • assignRole() and removeRole() do not check the rights of the code calling them, like WP_User::set_role(): where a user can trigger them, check promote_users first.

pollora roles:list and pollora roles:show <role> show the effective capabilities and where each comes from; pollora doctor flags users who carry a role removed from the code and capabilities given to users one by one, outside the roles.

Two roles declared in code

php

Click the faulty lines, then check. 2 bug(s) to find.

0 of 2 found

4. Typed meta and Block Bindings: what becomes visible

Section titled “4. Typed meta and Block Bindings: what becomes visible”

A meta declared with #[Meta] is exposed in REST only if showInRest: true (false by default). capability sets the capability required to write the meta through REST and the editor (by default: the right to edit the post or term), and rules adds Laravel validation rules checked on writes from PHP and REST. A key starting with an underscore is protected: WordPress hides it from custom fields and REST, and exposing it requires an explicit capability (without one, the declaration is refused at discovery). Sanitization applies to every write, wherever it comes from.

Anyone who can edit a post can bind any source to it, so each source only shows what may be public:

  • the post or term must be visible (nothing from an unpublished post without read_post, or one waiting for its password);
  • post and term meta: only showInRest: true and a key without a leading underscore;
  • user meta: showInRest is not enough, pollora/author-meta only reads a meta marked #[Meta(public: true)];
  • pollora/option only reads the options listed in config/block-bindings.php, none by default;
  • no source reads the logged-in visitor: the value would leak through the page cache.

A binding that cannot show anything raises no error, the block keeps its saved content: pollora doctor and Tools › Site Health read them (pollora binding:list shows what can be bound, pollora meta:list the meta).

An option and a binding source

php

Click the faulty lines, then check. 2 bug(s) to find.

0 of 2 found

5. Abilities: deny by default, per-object permission

Section titled “5. Abilities: deny by default, per-object permission”

An ability with no can() (or no authorize()) refuses everything: this is deliberate, a forgotten check makes the ability useless, not public. The permission callback receives the same input as the body, which allows a per-object check (edit_post on the identifier concerned, rather than a blanket edit_posts). Return a WP_Error rather than false to explain the refusal.

Each ability also declares its behaviour (Reads by default, Creates, Updates, Deletes), published as annotations that clients use to decide how careful to be: a read may run unattended, a delete should be confirmed. These annotations are advisory: WordPress does not enforce them, and the permission is what protects the site. Set a maximum on anything that sizes a query, and prefer enum() over a free string.

An ability that updates a post

php

Click the faulty lines, then check. 2 bug(s) to find.

0 of 2 found

The documentation’s facts: Nectar only loads when APP_ENV is local or development, its MCP tools are all read-only, and you still review what the agent writes. The guide adds that the guidelines state a key rule: never call WordPress registration functions directly, use attributes and discovery. An agent that writes register_post_type() or a raw add_action() bypasses what Pollora checks for you.

What follows is course advice, not a documented rule: never paste an API key, a token or a password into a conversation with an agent, or into a file it will read.

Reviewing an agent's diff

diff

Click the faulty lines, then check. 4 bug(s) to find. Diff: only the added (+) lines are to be reviewed.

  1. --- a/app/Cms/Api/EventAttendeesAPI.php
  2. +++ b/app/Cms/Api/EventAttendeesAPI.php
  3. @@ -0,0 +1,12 @@
  4. --- a/app/Cms/Roles/EventManager.php
  5. +++ b/app/Cms/Roles/EventManager.php
  6. @@ -3,6 +3,7 @@
  7. -#[Role('event_manager', label: 'Event manager', inherits: 'author')]
  8. #[Grants('moderate_comments')]
  9. final class EventManager {}
  10. --- a/app/Cms/Meta/MemberProfile.php
  11. +++ b/app/Cms/Meta/MemberProfile.php
  12. @@ -4,6 +4,9 @@
  13. #[UserMeta]
  14. class MemberProfile
  15. {
  16. #[Meta(showInRest: true)]
  17. public bool $newsletterOptIn = false;
  18. }
  19. --- a/functions.php
  20. +++ b/functions.php
  21. @@ -10,3 +10,4 @@
  22. pollora_register(ModuleType::Theme);

0 of 4 found

Writing a title typed in the editor

In render.blade.php, how do you write the $attributes['heading'] title typed by an editor?

  • {!! $attributes['heading'] !!}, like $content

    {!! !!} is reserved for get_block_wrapper_attributes() and $content, which WordPress already escaped.

  • {{ $attributes['heading'] ?? '' }}
  • {!! esc_url_raw($attributes['heading']) !!}

    esc_url_raw() is for URLs, and the output stays raw.

{{ }} escapes; raw output is the exception.

An AJAX handler opened by mistake

An agent declares #[Ajax('my_orders', access: AjaxAccess::ALL)] for a handler that returns a customer’s orders. What is wrong?

  • ALL adds wp_ajax_nopriv_my_orders: a logged-out visitor can call it
  • Nothing: without a nonce, WordPress always refuses the call

    The AJAX page does not say who verifies the nonce on the server.

  • ALL is the default of #[Ajax]

    The default is AjaxAccess::LOGGED.

The default (logged-in only) is enough here; opening a handler to the public is a decision to make consciously.

A REST route without a permission

A #[WpRestRoute] route returns the attendee list and names no permission. The agent “fixes” it with Everyone::class. What is the right correction?

  • Keep Everyone::class: that is what the documentation asks for

    Everyone is for routes that are public on purpose.

  • permissionCallback: new Can(EventCap::ExportAttendees)
  • permissionCallback: Can::class

    Can takes arguments: pass an instance.

Without a permission the route was refused (rest_forbidden): that was the right default behaviour.

Granting manage_options

What happens to #[Role('helper')] with #[Grants('manage_options')] and no allowSensitive: true?

  • The role gets the capability, with a warning
  • Discovery refuses the declaration: the error is logged with the class named, and it is not applied
  • The role gets the capability but not for existing users

    This behaviour does not exist in the documentation.

Adding allowSensitive: true only makes the choice visible in code review: the review has to refuse it when it has no reason to be.

An ability with no check

What does an ability declared with neither can() nor authorize() do?

  • It is public, like a REST route without a permission before 2.0
  • It refuses everything: a forgotten check makes it unusable rather than public
  • It is reserved for administrators

    There is no such rule: without a permission, nothing is allowed.

The behaviour annotations, on the other hand, are advisory: the permission is what protects the site.

Quiz Progress

0 / 0 questions answered (0%)

0 correct