Skip to content

Commit

Permalink
Merge pull request #529 from aseure/limit-proxy-usage-to-administrator
Browse files Browse the repository at this point in the history
Prevent usage of `cloudflare_proxy` action on /admin-ajax endpoint for non-Administrator users
  • Loading branch information
aseure committed Jan 4, 2024
2 parents 4ad24c8 + f3e8f74 commit a84c48b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Integration/IntegrationAPIInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public function getDomainList($userId = null);
* @return mixed
*/
public function getUserId();

/**
* @return boolean
*/
public function isCurrentUserAdministrator();
}
1 change: 1 addition & 0 deletions src/Test/WordPress/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function testPluginActionLinksGetAdminUrl()

public function testInitProxyCallsProxyRun()
{
$this->mockWordPressAPI->method('isCurrentUserAdministrator')->willReturn(true);
$this->mockProxy->expects($this->once())->method('run');
$this->hooks->initProxy();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Test/WordPress/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function testRunHandlesGet()
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['proxyURL'] = 'proxyUrl';
$_GET['proxyURLType'] = 'proxyUrlType';
$this->mockWordPressAPI->method('isCurrentUserAdministrator')->willReturn(true);
$this->mockRequestRouter->expects($this->once())->method('route');
$mockWPDie = $this->getFunctionMock('CF\WordPress', 'wp_die');
$this->mockProxy->run();
Expand All @@ -72,6 +73,7 @@ public function testRunHandlesPost()
$mockFileGetContents->expects($this->any())->willReturn($jsonBody);
$mockWPVerifyNonce = $this->getFunctionMock('CF\WordPress', 'wp_verify_nonce');
$mockWPVerifyNonce->expects($this->once())->willReturn(true);
$this->mockWordPressAPI->method('isCurrentUserAdministrator')->willReturn(true);
$this->mockRequestRouter->expects($this->once())->method('route');
$mockWPDie = $this->getFunctionMock('CF\WordPress', 'wp_die');
$this->mockProxy->run();
Expand Down
4 changes: 4 additions & 0 deletions src/WordPress/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function setRequestRouter(RequestRouter $requestRouter)

public function run()
{
if (!$this->wordpressAPI->isCurrentUserAdministrator()) {
return;
}

header('Content-Type: application/json');

$request = $this->createRequest();
Expand Down
8 changes: 8 additions & 0 deletions src/WordPress/WordPressAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,12 @@ public function checkIfValidCloudflareSubdomain($response, $domainName)

return false;
}

/**
* @return boolean
*/
public function isCurrentUserAdministrator()
{
return $this->wordPressWrapper->currentUserCan('administrator');
}
}
5 changes: 5 additions & 0 deletions src/WordPress/WordPressWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function getSiteURL()

return strtolower($site_url);
}

public function currentUserCan($capabilities)
{
return current_user_can($capabilities);
}
}

0 comments on commit a84c48b

Please sign in to comment.