Skip to content

Commit 540cc82

Browse files
committed
Don't resolve database hosts; closes pterodactyl#2237
1 parent 61e9771 commit 540cc82

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

app/Http/Middleware/Api/Client/Server/SubuserBelongsToServer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Pterodactyl\Http\Middleware\Api\Client\Server;
44

55
use Closure;
6-
use Exception;
76
use Illuminate\Http\Request;
87

98
class SubuserBelongsToServer

app/Http/Requests/Admin/DatabaseHostFormRequest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ protected function getValidatorInstance()
2929
$this->merge(['node_id' => null]);
3030
}
3131

32-
$this->merge([
33-
'host' => gethostbyname($this->input('host')),
34-
]);
35-
3632
return parent::getValidatorInstance();
3733
}
3834
}

app/Models/DatabaseHost.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pterodactyl\Models;
44

5+
use Pterodactyl\Rules\ResolvesToIPAddress;
6+
57
class DatabaseHost extends Model
68
{
79
/**
@@ -51,13 +53,25 @@ class DatabaseHost extends Model
5153
*/
5254
public static $validationRules = [
5355
'name' => 'required|string|max:255',
54-
'host' => 'required|unique:database_hosts,host',
56+
'host' => 'required|string',
5557
'port' => 'required|numeric|between:1,65535',
5658
'username' => 'required|string|max:32',
5759
'password' => 'nullable|string',
5860
'node_id' => 'sometimes|nullable|integer|exists:nodes,id',
5961
];
6062

63+
/**
64+
* @return array
65+
*/
66+
public static function getRules()
67+
{
68+
$rules = parent::getRules();
69+
70+
$rules['host'] = array_merge($rules['host'], [ new ResolvesToIPAddress() ]);
71+
72+
return $rules;
73+
}
74+
6175
/**
6276
* Gets the node associated with a database host.
6377
*

app/Rules/ResolvesToIPAddress.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Pterodactyl\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class ResolvesToIPAddress implements Rule
8+
{
9+
/**
10+
* Validate that a given string can correctly resolve to a valid IPv4 address.
11+
*
12+
* @param string $attribute
13+
* @param mixed $value
14+
* @return bool
15+
*/
16+
public function passes($attribute, $value): bool
17+
{
18+
// inet_pton returns false if the value passed through is not a valid IP address, so we'll just
19+
// use that a nice ugly PHP hack to determine if we should pass this off to the gethostbyname
20+
// call below.
21+
$isIP = inet_pton($attribute) !== false;
22+
23+
// If the value received is not an IP address try to look it up using the gethostbyname() call.
24+
// If that returns the same value that we passed in then it means it did not resolve to anything
25+
// and we should fail this validation call.
26+
return $isIP || gethostbyname($value) !== $value;
27+
}
28+
29+
/**
30+
* Return a validation message for use when this rule fails.
31+
*
32+
* @return string
33+
*/
34+
public function message(): string
35+
{
36+
return 'The :attribute must be a valid IPv4 address or hostname that resolves to a valid IPv4 address.';
37+
}
38+
39+
/**
40+
* Convert the rule to a validation string. This is necessary to avoid
41+
* issues with Eloquence which tries to use this rule as a string.
42+
*
43+
* @return string
44+
*/
45+
public function __toString()
46+
{
47+
return 'p_resolves_to_ip_address';
48+
}
49+
}

0 commit comments

Comments
 (0)