Claude Skill

br-owned-resource-guards

Add Better Route 1.1 ownership authorization to raw routes and Resource DSL endpoints. Use when authenticated users may access only their own records, orders, profiles, memberships, tokens, or subscriptions.

LLM Mart · 0 points · 1 views 0 listing impressions 0 install-command copies
Download Lonsdale201-wp-agent-skills-better-route_br-owned-resource-guards-15222a9.zip · 2 KB

Install

skills CLI npx skills add https://github.com/Lonsdale201/wp-agent-skills/tree/main/better-route/br-owned-resource-guards
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install lonsdale201-wp-agent-skills@llmmart
Git git clone https://github.com/Lonsdale201/wp-agent-skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole lonsdale201/wp-agent-skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Better Route ownership guards

Authentication establishes identity; ownership authorization establishes whether that identity may access this object.

The Resource helper guidance below targets the URL-bound ownership fix in 1.1.3/2.0. On 1.1.2 or older, do not rely on OwnedResourcePolicy::currentUserOwns() for URL-bound object authorization: its merged request ID can differ from the handler's URL ID. Use an explicit URL-bound policy until the security fix is installed.

Raw route

use BetterRoute\Middleware\Auth\OwnershipGuardMiddleware;

$guard = new OwnershipGuardMiddleware(
    ownerResolver: static function ($context): ?int {
        return my_resource_owner_id((int) ($context->request->get_url_params()['id'] ?? 0));
    },
    bypassCapability: 'manage_options',
    deniedStatus: 404
);

$router->get('/records/(?P<id>\d+)', $handler)
    ->middleware([$auth, $guard])
    ->protectedByMiddleware('bearerAuth');

Run authentication before the guard. It resolves identity from the normalized auth.userId, then auth.subject, then the native WordPress current user. The owner resolver must load ownership server-side from the route resource; never trust a submitted owner ID.

Resource DSL

use BetterRoute\Resource\OwnedResourcePolicy;

Resource::make('records')
    ->policy(OwnedResourcePolicy::currentUserOwns(
        ownerResolver: static fn (int $id): ?int => my_resource_owner_id($id),
        ownedActions: ['get', 'update', 'delete'],
        bypassCapability: 'manage_options',
        allowListForAuthenticatedUsers: true
    ));

allowListForAuthenticatedUsers: true grants list permission to logged-in WordPress users; it does not filter the result. Apply an owner predicate in the repository/query, or disable the generated list permission, before exposing user-owned collections.

Rules

  • Prefer denial as 404 when revealing object existence would leak data. Use 403 only for an intentionally discoverable object.
  • Use narrowly scoped, reviewed bypass capabilities.
  • Check ownership against the current stored record during writes, not a stale client copy.
  • Cover get, update, and delete independently; list filtering is a separate control.
  • Combine write authorization with optimistic locking and atomic idempotency when concurrency or duplicate side effects matter.

Test another user's ID, absent object, anonymous access, subject-only identity, native WordPress identity, admin bypass, and list-result isolation.

Source references: src/Middleware/Auth/OwnershipGuardMiddleware.php, src/Resource/OwnedResourcePolicy.php.

Explicit URL policy for older installations

Until the library fix is installed, use an explicit callback for each owned action:

use BetterRoute\Resource\ResourcePolicy;

$ownsUrl = static function ($request): bool {
    $raw = $request->get_url_params()['id'] ?? null;
    $valid = (is_int($raw) && $raw > 0)
        || (is_string($raw) && ctype_digit($raw) && (int) $raw > 0);
    $userId = get_current_user_id();
    return $valid && $userId > 0 && (int) my_resource_owner_id((int) $raw) === $userId;
};

// On the existing Resource definition:
->policy(ResourcePolicy::callbacks([
    'get' => $ownsUrl,
    'update' => $ownsUrl,
    'delete' => $ownsUrl,
]))

Resource permission callbacks require the authenticated native WP user before dispatch. Middleware running after WordPress permissions does not establish that earlier identity. List authorization and filtering remain separate requirements.

References

Files (wp-agent-skills)
  • agents
    • openai.yaml 227 B
      Loading…
  • SKILL.md 4.1 KB
    ---
    name: br-owned-resource-guards
    description: Add Better Route 1.1 ownership authorization to raw routes and Resource DSL endpoints. Use when authenticated users may access only their own records, orders, profiles, memberships, tokens, or subscriptions.
    metadata:
      wp-skills-author: "Soczó Kristóf"
      wp-skills-contact: "mailto:lonsdale201@hotmail.com"
      wp-skills-plugin: "better-route"
      wp-skills-plugin-version-tested: "2.0.0"
      wp-skills-php-min: "8.3"
      wp-skills-last-updated: "2026-10-20"
    ---
    
    # Better Route ownership guards
    
    Authentication establishes identity; ownership authorization establishes whether that identity may access this object.
    
    The Resource helper guidance below targets the URL-bound ownership fix in 1.1.3/2.0.
    On 1.1.2 or older, do not rely on `OwnedResourcePolicy::currentUserOwns()` for
    URL-bound object authorization: its merged request ID can differ from the handler's
    URL ID. Use an explicit URL-bound policy until the security fix is installed.
    
    ## Raw route
    
    ```php
    use BetterRoute\Middleware\Auth\OwnershipGuardMiddleware;
    
    $guard = new OwnershipGuardMiddleware(
        ownerResolver: static function ($context): ?int {
            return my_resource_owner_id((int) ($context->request->get_url_params()['id'] ?? 0));
        },
        bypassCapability: 'manage_options',
        deniedStatus: 404
    );
    
    $router->get('/records/(?P<id>\d+)', $handler)
        ->middleware([$auth, $guard])
        ->protectedByMiddleware('bearerAuth');
    ```
    
    Run authentication before the guard. It resolves identity from the normalized `auth.userId`, then `auth.subject`, then the native WordPress current user. The owner resolver must load ownership server-side from the route resource; never trust a submitted owner ID.
    
    ## Resource DSL
    
    ```php
    use BetterRoute\Resource\OwnedResourcePolicy;
    
    Resource::make('records')
        ->policy(OwnedResourcePolicy::currentUserOwns(
            ownerResolver: static fn (int $id): ?int => my_resource_owner_id($id),
            ownedActions: ['get', 'update', 'delete'],
            bypassCapability: 'manage_options',
            allowListForAuthenticatedUsers: true
        ));
    ```
    
    `allowListForAuthenticatedUsers: true` grants list permission to logged-in WordPress users; it does not filter the result. Apply an owner predicate in the repository/query, or disable the generated list permission, before exposing user-owned collections.
    
    ## Rules
    
    - Prefer denial as `404` when revealing object existence would leak data. Use `403` only for an intentionally discoverable object.
    - Use narrowly scoped, reviewed bypass capabilities.
    - Check ownership against the current stored record during writes, not a stale client copy.
    - Cover `get`, `update`, and `delete` independently; list filtering is a separate control.
    - Combine write authorization with optimistic locking and atomic idempotency when concurrency or duplicate side effects matter.
    
    Test another user's ID, absent object, anonymous access, subject-only identity, native WordPress identity, admin bypass, and list-result isolation.
    
    Source references: `src/Middleware/Auth/OwnershipGuardMiddleware.php`, `src/Resource/OwnedResourcePolicy.php`.
    
    ## Explicit URL policy for older installations
    
    Until the library fix is installed, use an explicit callback for each owned action:
    
    ```php
    use BetterRoute\Resource\ResourcePolicy;
    
    $ownsUrl = static function ($request): bool {
        $raw = $request->get_url_params()['id'] ?? null;
        $valid = (is_int($raw) && $raw > 0)
            || (is_string($raw) && ctype_digit($raw) && (int) $raw > 0);
        $userId = get_current_user_id();
        return $valid && $userId > 0 && (int) my_resource_owner_id((int) $raw) === $userId;
    };
    
    // On the existing Resource definition:
    ->policy(ResourcePolicy::callbacks([
        'get' => $ownsUrl,
        'update' => $ownsUrl,
        'delete' => $ownsUrl,
    ]))
    ```
    
    Resource permission callbacks require the authenticated native WP user before
    dispatch. Middleware running after WordPress permissions does not establish that
    earlier identity. List authorization and filtering remain separate requirements.
    
    ## References
    
    - Official documentation: <https://lonsdale201.github.io/better-docs/docs/better-route/agents>
    

Comments (0)

Sign in to join the conversation.

No comments yet.

Reviews (0)

No reviews yet.