Skip to content

Commit f40d29b

Browse files
committed
chore: convert from IP to range of IP
Signed-off-by: Vitor Mattos <[email protected]>
1 parent a3e9959 commit f40d29b

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ Available features:
1616
occ app:enable admin_group_manager
1717
occ app:enable groupquota
1818
```
19-
- Allowed IP
19+
- Allowed IP range
2020

21-
By security, this API only receive requests from a specific IP.
22-
- Run a tail with grep to watch by the word "Unauthorized access".
21+
By security, this API only receive requests from a specific IP range. This could be enabled or not. To enable you will need to run the follow command:
22+
```bash
23+
occ config:system:set admin_group_manager_allowed_range 0 --value <theWordPressIp>
24+
```
25+
26+
To test if your setting is working fine, use a IP range that don't match with WordPressIP and tun a tail with grep to watch by the word "Unauthorized access".
2327
```bash
2428
tail -f data/nextcloud.log|grep "Unauthorized access"
2529
```
26-
- Do a request to API endpoint and go back to terminal to check the logs and get the IP.
27-
- With the IP, run the follow command:
28-
```bash
29-
occ config:system:set admin_group_manager_allowed_ip --value <theIdentifiedIp>
30-
```
3130

3231
## Performance improving
3332
Systemd service

lib/Middleware/InjectionMiddleware.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace OCA\AdminGroupManager\Middleware;
1010

11+
use OC\Security\Ip\Address;
12+
use OC\Security\Ip\Range;
1113
use OCA\AdminGroupManager\Controller\AEnvironmentAwareOCSController;
1214
use OCA\AdminGroupManager\Controller\Attribute\RestrictIp;
1315
use OCP\AppFramework\Controller;
@@ -48,11 +50,19 @@ public function beforeController(Controller $controller, string $methodName) {
4850
}
4951

5052
private function restrictIp(): void {
51-
$ip = $this->request->getRemoteAddress();
52-
$allowed = $this->config->getSystemValue('admin_group_manager_allowed_ip');
53-
if ($allowed !== $ip) {
54-
$this->logger->error('Unauthorized access to API', ['IP' => $ip]);
55-
throw new OCSException('', Http::STATUS_UNAUTHORIZED);
53+
$ip = new Address(
54+
$this->request->getRemoteAddress()
55+
);
56+
$ranges = $this->config->getSystemValue('admin_group_manager_allowed_range');
57+
if (!is_array($ranges) || empty($ranges)) {
58+
return;
5659
}
60+
foreach ($ranges as $range) {
61+
if ((new Range($range))->contains($ip)) {
62+
return;
63+
}
64+
}
65+
$this->logger->error('Unauthorized access to API', ['IP' => $ip]);
66+
throw new OCSException('', Http::STATUS_UNAUTHORIZED);
5767
}
5868
}

0 commit comments

Comments
 (0)