Skip to content

Commit c701aa0

Browse files
committed
Add support for CIDR ranges on API
1 parent 317698a commit c701aa0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

app/Http/Middleware/APISecretToken.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Pterodactyl\Http\Middleware;
44

55
use Crypt;
6+
use IPTools\IP;
7+
use IPTools\Range;
68

79
use Pterodactyl\Models\APIKey;
810
use Pterodactyl\Models\APIPermission;
@@ -49,8 +51,15 @@ public function authenticate(Request $request, Route $route)
4951
// Check for Resource Permissions
5052
if (!empty($request->route()->getName())) {
5153
if(!is_null($key->allowed_ips)) {
52-
if (!in_array($request->ip(), json_decode($key->allowed_ips))) {
53-
throw new AccessDeniedHttpException('This IP address does not have permission to use this API key.');
54+
$inRange = false;
55+
foreach(json_decode($key->allowed_ips) as $ip) {
56+
if (Range::parse($ip)->contains(new IP($request->ip()))) {
57+
$inRange = true;
58+
break;
59+
}
60+
}
61+
if (!$inRange) {
62+
throw new AccessDeniedHttpException('This IP address <' . $request->ip() . '> does not have permission to use this API key.');
5463
}
5564
}
5665

0 commit comments

Comments
 (0)