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.
1. Escaping output in Blade
Section titled “1. Escaping output in Blade”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 &#038;.
A block rendered in Blade
bladeClick the faulty lines, then check. 2 bug(s) to find.
0 of 2 found
Explanations
2. REST and AJAX: who may call?
Section titled “2. REST and AJAX: who may call?”REST: a permission, always stated
Section titled “REST: a permission, always stated”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: logged-in by default
Section titled “AJAX: logged-in by default”#[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 nonce in the example
Section titled “The nonce in the example”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
phpClick the faulty lines, then check. 2 bug(s) to find.
0 of 2 found
Explanations
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_attendeesandnew 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_filesandupdate_corecan only be granted withallowSensitive: trueon#[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,contributorandsubscribercannot be redeclared with#[Role]: adjust them with#[ModifyRole]. #[Without]removes a capability; it never sets it tofalse. A role only grants or removes.assignRole()andremoveRole()do not check the rights of the code calling them, likeWP_User::set_role(): where a user can trigger them, checkpromote_usersfirst.
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
phpClick the faulty lines, then check. 2 bug(s) to find.
0 of 2 found
Explanations
4. Typed meta and Block Bindings: what becomes visible
Section titled “4. Typed meta and Block Bindings: what becomes visible”Typed meta in REST
Section titled “Typed meta in REST”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.
Block Bindings: the visibility rules
Section titled “Block Bindings: the visibility rules”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: trueand a key without a leading underscore; - user meta:
showInRestis not enough,pollora/author-metaonly reads a meta marked#[Meta(public: true)]; pollora/optiononly reads the options listed inconfig/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
phpClick the faulty lines, then check. 2 bug(s) to find.
0 of 2 found
Explanations
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
phpClick the faulty lines, then check. 2 bug(s) to find.
0 of 2 found
Explanations
6. Reviewing what an agent wrote
Section titled “6. Reviewing what an agent wrote”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
diffClick the faulty lines, then check. 4 bug(s) to find. Diff: only the added (+) lines are to be reviewed.
@@ -0,0 +1,12 @@@@ -3,6 +3,7 @@-#[Role('event_manager', label: 'Event manager', inherits: 'author')]#[Grants('moderate_comments')]final class EventManager {}@@ -4,6 +4,9 @@#[UserMeta]class MemberProfile{#[Meta(showInRest: true)]public bool $newsletterOptIn = false;}@@ -10,3 +10,4 @@pollora_register(ModuleType::Theme);
0 of 4 found
Explanations
Check what you remember
Section titled “Check what you remember”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 forget_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?
-
ALLaddswp_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.
-
ALLis 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 forEveryoneis for routes that are public on purpose. -
permissionCallback: new Can(EventCap::ExportAttendees) -
permissionCallback: Can::classCantakes 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
Your score
0%
0 / 0 correct
Going further
Section titled “Going further”- Gutenberg Blocks: escaping in
render.blade.php - REST API and AJAX: permissions and access levels
- Middleware: sessions, the
webgroup and CSRF - Roles & Capabilities: safety rules,
pollora:roles:* - Typed Meta and Block Bindings: data visibility
- Abilities: permissions, behaviour, schema
- AI coding agents for WordPress projects: what Nectar does and does not do
Answered: 0 / 0 (0%)