Files
pterodactyl-panel/app/Http/Middleware/Api/AuthenticateIPAccess.php
T
Sandip Ghimire bb4e89826e fix(security): harden path validation, session revocation, and request handling (#7255)
# Security, Validation & Session Improvements

## Changes

* **Hardened IP/CIDR validation**
* Properly handles invalid IP addresses and CIDR ranges during API key
creation.
* Invalid IP/CIDR data now fails safely instead of causing a 500 error.

* **Improved reCAPTCHA error handling**
  * Handles network and verification failures gracefully.
  * Fixed undefined result notices and removed duplicate checks.

* **Added file permission activity logging**
* File permission (`chmod`) changes are now recorded in the activity
log.

* **Removed duplicate 2FA middleware**
* Cleaned up duplicate `RequireTwoFactorAuthentication` registration
from API routes.

* **Improved session revocation on password changes**
  * Password changes now invalidate other active sessions.
* Rotates the `remember_token` and removes stale database sessions where
applicable.
* Applies consistently across password resets and user password changes.

* **Reduced unnecessary public storage access**
* Disabled automatic public storage routes for the local filesystem disk
to reduce the attack surface.

* **Hardened file manager request validation**
  * Added strict validation for file permission modes.
  * Fixed optional `root` validation rules.
  * Added length limits for file paths, roots, and file/folder names.

* **Hardened mount source and target path validation**
* Prevents path traversal and subpath-based bypasses in mount source and
target validation.
  * Handles trailing slashes and normalized paths correctly.
* Prevents mounts from targeting protected subdirectories, such as
another server's volume directory.
2026-10-07 12:39:12 -04:00

80 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Http\Middleware\Api;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use IPTools\Exception\IpException;
use IPTools\Exception\NetworkException;
use IPTools\IP;
use IPTools\Range;
use Laravel\Sanctum\TransientToken;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Http\Concerns\ResolvesRequestContext;
use Pterodactyl\Models\ApiKey;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class AuthenticateIPAccess
{
use ResolvesRequestContext;
/**
* Determine if a request IP has permission to access the API.
*
* @throws Exception
* @throws AccessDeniedHttpException
*/
/**
* @param Closure(Request):Response $next
* @return Response
*/
public function handle(Request $request, Closure $next): mixed
{
$user = $this->authenticatedUser($request);
// SAFETY: Sanctum may supply a TransientToken for stateful requests; authenticated API requests use the configured ApiKey model.
/** @var TransientToken|ApiKey|null $token */
$token = $user->currentAccessToken();
// If this is a stateful request just push the request through to the next
// middleware in the stack, there is nothing we need to explicitly check. If
// this is a valid API Key, but there is no allowed IP restriction, also pass
// the request through.
if ($token === null || $token instanceof TransientToken || empty($token->allowed_ips)) {
return $next($request);
}
$requestIp = $request->ip();
throw_if($requestIp === null, AccessDeniedHttpException::class, 'The request did not contain a valid IP address.');
try {
$find = new IP($requestIp);
} catch (IpException) {
throw new AccessDeniedHttpException('The request did not contain a valid IP address.');
}
foreach ($token->allowed_ips as $ip) {
try {
if (Range::parse($ip)->contains($find)) {
return $next($request);
}
} catch (IpException | NetworkException | InvalidArgumentException $exception) {
// Ignore the error entirely
continue;
}
}
Activity::event('auth:ip-blocked')
->actor($user)
->subject($user, $token)
->property('identifier', $token->identifier)
->log();
throw new AccessDeniedHttpException('This IP address ('.$request->ip().') does not have permission to access the API using these credentials.');
}
}