-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(security): restrict admin actions to IP ranges
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
- Loading branch information
Showing
9 changed files
with
137 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lib/private/AppFramework/Middleware/Security/Exceptions/AdminIpNotAllowedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
namespace OC\AppFramework\Middleware\Security\Exceptions; | ||
|
||
use OCP\AppFramework\Http; | ||
|
||
/** | ||
* Class AdminIpNotAllowed is thrown when a resource has been requested by a | ||
* an admin user connecting from an unauthorized IP address | ||
* See configuration `allowed_admin_ranges` | ||
* | ||
* @package OC\AppFramework\Middleware\Security\Exceptions | ||
*/ | ||
class AdminIpNotAllowedException extends SecurityException { | ||
public function __construct(string $message) { | ||
parent::__construct($message, Http::STATUS_FORBIDDEN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Security; | ||
|
||
use IPLib\Factory; | ||
use OCP\IConfig; | ||
use OCP\IRequest; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class RemoteIpAddress { | ||
public function __construct( | ||
private IConfig $config, | ||
private IRequest $request, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function allowsAdminActions(): bool { | ||
if (false === $allowedAdminRanges = $this->config->getSystemValue('allowed_admin_ranges', false)) { | ||
return true; | ||
} | ||
|
||
if (!is_array($allowedAdminRanges)) { | ||
$this->logger->warning('Invalid configuration for "allowed_admin_ranges"'); | ||
return true; | ||
} | ||
|
||
if (count($allowedAdminRanges) === 0) { | ||
return true; | ||
} | ||
|
||
$ipAddress = Factory::parseAddressString($this->request->getRemoteAddress()); | ||
foreach ($allowedAdminRanges as $rangeString) { | ||
$range = Factory::parseRangeString($rangeString); | ||
if ($range->contains($ipAddress)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters