Don't resolve database hosts; closes #2237

This commit is contained in:
Dane Everitt 2020-08-19 20:38:51 -07:00
parent 61e9771333
commit 540cc82e3d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 64 additions and 6 deletions

View file

@ -3,7 +3,6 @@
namespace Pterodactyl\Http\Middleware\Api\Client\Server; namespace Pterodactyl\Http\Middleware\Api\Client\Server;
use Closure; use Closure;
use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SubuserBelongsToServer class SubuserBelongsToServer

View file

@ -29,10 +29,6 @@ class DatabaseHostFormRequest extends AdminFormRequest
$this->merge(['node_id' => null]); $this->merge(['node_id' => null]);
} }
$this->merge([
'host' => gethostbyname($this->input('host')),
]);
return parent::getValidatorInstance(); return parent::getValidatorInstance();
} }
} }

View file

@ -2,6 +2,8 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Pterodactyl\Rules\ResolvesToIPAddress;
class DatabaseHost extends Model class DatabaseHost extends Model
{ {
/** /**
@ -51,13 +53,25 @@ class DatabaseHost extends Model
*/ */
public static $validationRules = [ public static $validationRules = [
'name' => 'required|string|max:255', 'name' => 'required|string|max:255',
'host' => 'required|unique:database_hosts,host', 'host' => 'required|string',
'port' => 'required|numeric|between:1,65535', 'port' => 'required|numeric|between:1,65535',
'username' => 'required|string|max:32', 'username' => 'required|string|max:32',
'password' => 'nullable|string', 'password' => 'nullable|string',
'node_id' => 'sometimes|nullable|integer|exists:nodes,id', 'node_id' => 'sometimes|nullable|integer|exists:nodes,id',
]; ];
/**
* @return array
*/
public static function getRules()
{
$rules = parent::getRules();
$rules['host'] = array_merge($rules['host'], [ new ResolvesToIPAddress() ]);
return $rules;
}
/** /**
* Gets the node associated with a database host. * Gets the node associated with a database host.
* *

View file

@ -0,0 +1,49 @@
<?php
namespace Pterodactyl\Rules;
use Illuminate\Contracts\Validation\Rule;
class ResolvesToIPAddress implements Rule
{
/**
* Validate that a given string can correctly resolve to a valid IPv4 address.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
// inet_pton returns false if the value passed through is not a valid IP address, so we'll just
// use that a nice ugly PHP hack to determine if we should pass this off to the gethostbyname
// call below.
$isIP = inet_pton($attribute) !== false;
// If the value received is not an IP address try to look it up using the gethostbyname() call.
// If that returns the same value that we passed in then it means it did not resolve to anything
// and we should fail this validation call.
return $isIP || gethostbyname($value) !== $value;
}
/**
* Return a validation message for use when this rule fails.
*
* @return string
*/
public function message(): string
{
return 'The :attribute must be a valid IPv4 address or hostname that resolves to a valid IPv4 address.';
}
/**
* Convert the rule to a validation string. This is necessary to avoid
* issues with Eloquence which tries to use this rule as a string.
*
* @return string
*/
public function __toString()
{
return 'p_resolves_to_ip_address';
}
}