Skip to content

Commit 407b6df

Browse files
fix(validation): add IP validation for custom DNS servers input
1 parent 3b2e6e1 commit 407b6df

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

app/Livewire/Settings/Advanced.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Livewire\Settings;
44

55
use App\Models\InstanceSettings;
6+
use App\Rules\ValidDnsServers;
67
use App\Rules\ValidIpOrCidr;
78
use Livewire\Attributes\Validate;
89
use Livewire\Component;
@@ -20,7 +21,6 @@ class Advanced extends Component
2021
#[Validate('boolean')]
2122
public bool $is_dns_validation_enabled;
2223

23-
#[Validate('nullable|string')]
2424
public ?string $custom_dns_servers = null;
2525

2626
#[Validate('boolean')]
@@ -43,7 +43,7 @@ public function rules()
4343
'is_registration_enabled' => 'boolean',
4444
'do_not_track' => 'boolean',
4545
'is_dns_validation_enabled' => 'boolean',
46-
'custom_dns_servers' => 'nullable|string',
46+
'custom_dns_servers' => ['nullable', 'string', new ValidDnsServers],
4747
'is_api_enabled' => 'boolean',
4848
'allowed_ips' => ['nullable', 'string', new ValidIpOrCidr],
4949
'is_sponsorship_popup_enabled' => 'boolean',

app/Rules/ValidDnsServers.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Validation\ValidationRule;
7+
8+
class ValidDnsServers implements ValidationRule
9+
{
10+
public function validate(string $attribute, mixed $value, Closure $fail): void
11+
{
12+
if (empty($value)) {
13+
return;
14+
}
15+
16+
$entries = explode(',', $value);
17+
$invalidEntries = [];
18+
19+
foreach ($entries as $entry) {
20+
$entry = trim($entry);
21+
22+
if (empty($entry)) {
23+
continue;
24+
}
25+
26+
if (! filter_var($entry, FILTER_VALIDATE_IP)) {
27+
$invalidEntries[] = $entry;
28+
}
29+
}
30+
31+
if (! empty($invalidEntries)) {
32+
$fail('The following entries are not valid DNS server IP addresses: '.implode(', ', $invalidEntries));
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)