-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(security): restrict admin actions to IP ranges #46473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Settings\SetupChecks; | ||
|
|
||
| use OC\Security\Ip\Range; | ||
| use OC\Security\Ip\RemoteAddress; | ||
| use OCP\IConfig; | ||
| use OCP\IL10N; | ||
| use OCP\SetupCheck\ISetupCheck; | ||
| use OCP\SetupCheck\SetupResult; | ||
|
|
||
| class AllowedAdminRanges implements ISetupCheck { | ||
| public function __construct( | ||
| private IConfig $config, | ||
| private IL10N $l10n, | ||
| ) { | ||
| } | ||
|
|
||
| public function getCategory(): string { | ||
| return 'system'; | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->l10n->t('Allowed admin IP ranges'); | ||
| } | ||
|
|
||
| public function run(): SetupResult { | ||
| $allowedAdminRanges = $this->config->getSystemValue(RemoteAddress::SETTING_NAME, false); | ||
| if ( | ||
| $allowedAdminRanges === false | ||
| || (is_array($allowedAdminRanges) && empty($allowedAdminRanges)) | ||
| ) { | ||
| return SetupResult::success($this->l10n->t('Admin IP filtering isn’t applied.')); | ||
| } | ||
|
|
||
| if (!is_array($allowedAdminRanges)) { | ||
| return SetupResult::error( | ||
| $this->l10n->t( | ||
| 'Configuration key "%1$s" expects an array (%2$s found). Admin IP range validation will not be applied.', | ||
| [RemoteAddress::SETTING_NAME, gettype($allowedAdminRanges)], | ||
Check failureCode scanning / Psalm UndefinedClass
Class, interface or enum named OC\Security\Ip\RemoteAddress does not exist
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $invalidRanges = array_filter($allowedAdminRanges, static fn (mixed $range): bool => !is_string($range) || !Range::isValid($range)); | ||
| if (!empty($invalidRanges)) { | ||
| return SetupResult::warning( | ||
| $this->l10n->t( | ||
| 'Configuration key "%1$s" contains invalid IP range(s): "%2$s"', | ||
| [RemoteAddress::SETTING_NAME, implode('", "', $invalidRanges)], | ||
Check failureCode scanning / Psalm UndefinedClass
Class, interface or enum named OC\Security\Ip\RemoteAddress does not exist
|
||
| ), | ||
| ); | ||
| } | ||
|
|
||
| return SetupResult::success($this->l10n->t('Admin IP filtering is correctly configured.')); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
UndefinedClass