fix(validation): add IP validation for custom DNS servers input
This commit is contained in:
parent
3b2e6e11f1
commit
407b6df744
2 changed files with 37 additions and 2 deletions
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Rules\ValidDnsServers;
|
||||
use App\Rules\ValidIpOrCidr;
|
||||
use Livewire\Attributes\Validate;
|
||||
use Livewire\Component;
|
||||
|
|
@ -20,7 +21,6 @@ class Advanced extends Component
|
|||
#[Validate('boolean')]
|
||||
public bool $is_dns_validation_enabled;
|
||||
|
||||
#[Validate('nullable|string')]
|
||||
public ?string $custom_dns_servers = null;
|
||||
|
||||
#[Validate('boolean')]
|
||||
|
|
@ -43,7 +43,7 @@ public function rules()
|
|||
'is_registration_enabled' => 'boolean',
|
||||
'do_not_track' => 'boolean',
|
||||
'is_dns_validation_enabled' => 'boolean',
|
||||
'custom_dns_servers' => 'nullable|string',
|
||||
'custom_dns_servers' => ['nullable', 'string', new ValidDnsServers],
|
||||
'is_api_enabled' => 'boolean',
|
||||
'allowed_ips' => ['nullable', 'string', new ValidIpOrCidr],
|
||||
'is_sponsorship_popup_enabled' => 'boolean',
|
||||
|
|
|
|||
35
app/Rules/ValidDnsServers.php
Normal file
35
app/Rules/ValidDnsServers.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class ValidDnsServers implements ValidationRule
|
||||
{
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entries = explode(',', $value);
|
||||
$invalidEntries = [];
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$entry = trim($entry);
|
||||
|
||||
if (empty($entry)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! filter_var($entry, FILTER_VALIDATE_IP)) {
|
||||
$invalidEntries[] = $entry;
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($invalidEntries)) {
|
||||
$fail('The following entries are not valid DNS server IP addresses: '.implode(', ', $invalidEntries));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue