Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions plugins/data-machine/inc/Abilities/PermissionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PermissionHelper {
'use_tools' => 'datamachine_use_tools',
'view_logs' => 'datamachine_view_logs',
'create_own_agent' => 'datamachine_create_own_agent',
'web_fetch' => 'publish_posts',
'web_fetch' => 'edit_posts',
);

/**
Expand Down Expand Up @@ -179,8 +179,9 @@ public static function can_manage(): bool {
* - daily_memory: reading and appending the acting agent's daily memory.
* Scope comes from the execution principal; overwrite and delete stay
* behind can_manage() inside DailyMemoryAbilities.
* - web_fetch: reading a public web page as text maps to publish_posts,
* the same author level the web_fetch tool already requires.
* - web_fetch: reading a public http(s) page as text (SSRF-protected,
* nothing persisted) maps to core's edit_posts, so anyone who can draft
* content (contributors, submit-for-review roles) can research it.
*
* @since 0.37.0
* @var string[]
Expand Down Expand Up @@ -736,7 +737,7 @@ public static function can_use_ability( string $ability_slug, string $category =
* @return bool
*/
private static function agent_can( string $action ): bool {
$capability = self::CAPABILITY_MAP[ $action ] ?? null;
$capability = self::resolve_action_capability( $action );
if ( empty( $capability ) || self::$agent_owner_id <= 0 ) {
return false;
}
Expand Down Expand Up @@ -1050,15 +1051,58 @@ public static function owns_agent_resource( ?int $resource_agent_id, int $resour
* @return bool
*/
private static function user_can( int $user_id, string $action ): bool {
$mapped_capability = self::CAPABILITY_MAP[ $action ] ?? null;
$mapped_capability = self::resolve_action_capability( $action );

if ( empty( $mapped_capability ) ) {
if ( null === $mapped_capability ) {
return false;
}

return self::user_has_capability( $user_id, $mapped_capability );
}

/**
* Resolve the WordPress capability required for a Data Machine action.
*
* Single read point for CAPABILITY_MAP, used by both user_can() and
* agent_can() so user sessions, pre-authenticated contexts and agent
* tokens all enforce the same (possibly filtered) capability.
*
* @param string $action Action key.
* @return string|null Capability, or null when the action is unknown or unmapped.
*/
private static function resolve_action_capability( string $action ): ?string {
$mapped = self::CAPABILITY_MAP[ $action ] ?? null;

/**
* Filter the WordPress capability required for a Data Machine action.
*
* Lets a platform retune which capability gates an action (for
* example require publish_posts for web_fetch again, or grant
* view_logs to a custom capability) without patching Data Machine.
* Applies to user sessions, pre-authenticated contexts and agent
* tokens alike; agent tokens remain bounded by their capability
* ceiling. Return an empty value to deny the action outright.
*
* Management holders (manage_flows / manage_settings / manage_agents)
* still pass actions listed in IMPLIED_BY_MANAGEMENT before this
* filter is consulted, and manage_options still satisfies any mapped
* capability for user principals.
*
* @since next
*
* @param string|null $mapped Capability from the built-in map, or null if the action is unmapped.
* @param string $action Data Machine action key (e.g. 'web_fetch', 'manage_flows').
*/
$capability = apply_filters( 'datamachine_action_capability', $mapped, $action );

if ( ! is_string( $capability ) ) {
return null;
}

$capability = trim( $capability );
return '' === $capability ? null : $capability;
}

/**
* Check a concrete WordPress capability for a user under Data Machine policy.
*
Expand Down
149 changes: 149 additions & 0 deletions plugins/data-machine/tests/Unit/Abilities/WebFetchPermissionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Coverage for #3993: datamachine/web-fetch is a read-only, SSRF-protected
* public page fetch, so it is gated by core `edit_posts` (anyone who can
* draft content) rather than `publish_posts`. Action capabilities are also
* filterable through `datamachine_action_capability`.
*
* @package DataMachine\Tests\Unit\Abilities
* @see https://github.com/Extra-Chill/data-machine/issues/3993
*/

namespace DataMachine\Tests\Unit\Abilities;

use DataMachine\Abilities\PermissionHelper;
use DataMachine\Abilities\WebFetchAbilities;
use WP_UnitTestCase;

class WebFetchPermissionTest extends WP_UnitTestCase {

/**
* Filter callbacks registered by a test, removed in tear_down().
*
* @var callable[]
*/
private array $filters = array();

public function set_up(): void {
parent::set_up();

// The suite runs under WP-CLI-like conditions in some environments;
// make sure capability checks are actually evaluated.
add_filter( 'datamachine_cli_bypass_permissions', '__return_false' );

new WebFetchAbilities();
}

public function tear_down(): void {
foreach ( $this->filters as $callback ) {
remove_filter( 'datamachine_action_capability', $callback );
}
$this->filters = array();

remove_filter( 'datamachine_cli_bypass_permissions', '__return_false' );
PermissionHelper::clear_agent_context();
wp_set_current_user( 0 );

parent::tear_down();
}

private function login_as( string $role ): int {
$user_id = self::factory()->user->create( array( 'role' => $role ) );
wp_set_current_user( $user_id );
return $user_id;
}

private function add_capability_filter( callable $callback ): void {
$this->filters[] = $callback;
add_filter( 'datamachine_action_capability', $callback, 10, 2 );
}

public function test_contributor_without_publish_posts_can_web_fetch(): void {
$user_id = $this->login_as( 'contributor' );

$this->assertTrue( user_can( $user_id, 'edit_posts' ) );
$this->assertFalse( user_can( $user_id, 'publish_posts' ) );

$this->assertTrue( PermissionHelper::can( 'web_fetch' ) );

$ability = wp_get_ability( 'datamachine/web-fetch' );
$this->assertNotNull( $ability );
$this->assertTrue( $ability->check_permissions( array( 'url' => 'https://example.com/' ) ) );
}

public function test_subscriber_cannot_web_fetch(): void {
$this->login_as( 'subscriber' );

$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );

$ability = wp_get_ability( 'datamachine/web-fetch' );
$this->assertNotNull( $ability );
$this->assertNotTrue( $ability->check_permissions( array( 'url' => 'https://example.com/' ) ) );
}

public function test_logged_out_visitor_cannot_web_fetch(): void {
wp_set_current_user( 0 );

$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );
}

public function test_agent_token_ceiling_bounds_web_fetch(): void {
$owner_id = self::factory()->user->create( array( 'role' => 'contributor' ) );

PermissionHelper::set_agent_context( 321, $owner_id, array( 'edit_posts' ), 654 );
$this->assertTrue( PermissionHelper::can( 'web_fetch' ) );

PermissionHelper::clear_agent_context();
PermissionHelper::set_agent_context( 321, $owner_id, array( 'read' ), 654 );
$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );
}

public function test_filter_can_remap_an_action_capability(): void {
$this->login_as( 'contributor' );
$this->assertTrue( PermissionHelper::can( 'web_fetch' ) );

$seen = array();
$this->add_capability_filter(
static function ( $capability, $action ) use ( &$seen ) {
$seen[ $action ] = $capability;
return 'web_fetch' === $action ? 'publish_posts' : $capability;
}
);

$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );
$this->assertSame( 'edit_posts', $seen['web_fetch'] ?? null, 'Filter receives the built-in mapping.' );
}

public function test_filter_can_grant_an_action_to_a_lower_role(): void {
$this->login_as( 'subscriber' );
$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );

$this->add_capability_filter(
static fn( $capability, $action ) => 'web_fetch' === $action ? 'read' : $capability
);

$this->assertTrue( PermissionHelper::can( 'web_fetch' ) );
}

public function test_filter_applies_to_agent_token_context(): void {
$owner_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
PermissionHelper::set_agent_context( 321, $owner_id, null, 654 );
$this->assertTrue( PermissionHelper::can( 'web_fetch' ) );

$this->add_capability_filter(
static fn( $capability, $action ) => 'web_fetch' === $action ? 'publish_posts' : $capability
);

$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );
}

public function test_filter_returning_empty_denies_the_action(): void {
$this->login_as( 'contributor' );

$this->add_capability_filter(
static fn( $capability, $action ) => 'web_fetch' === $action ? '' : $capability
);

$this->assertFalse( PermissionHelper::can( 'web_fetch' ) );
}
}
Loading