Unify domain validation regex to fix custom domain issues

This commit is contained in:
Julien Nahum 2024-01-10 11:32:22 +01:00
parent dd83528a3a
commit c01f566ba9
4 changed files with 8 additions and 4 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\Workspace\CustomDomainRequest;
use App\Models\Workspace;
use Illuminate\Http\Request;
@ -14,7 +15,7 @@ class CaddyController extends Controller
]);
// make sure domain is valid
$domain = $request->input('domain');
if (!preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}$/', $domain)) {
if (!preg_match(CustomDomainRequest::CUSTOM_DOMAINS_REGEX, $domain)) {
return $this->error([
'success' => false,
'message' => 'Invalid domain',

View File

@ -2,6 +2,7 @@
namespace App\Http\Middleware;
use App\Http\Requests\Workspace\CustomDomainRequest;
use App\Models\Forms\Form;
use App\Models\Workspace;
use Closure;
@ -22,7 +23,7 @@ class CustomDomainRestriction
}
$customDomain = $request->header(self::CUSTOM_DOMAIN_HEADER);
if (!preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}$/', $customDomain)) {
if (!preg_match(CustomDomainRequest::CUSTOM_DOMAINS_REGEX, $customDomain)) {
return response()->json([
'success' => false,
'message' => 'Invalid domain',

View File

@ -4,6 +4,7 @@
namespace App\Http\Requests;
use App\Http\Requests\Workspace\CustomDomainRequest;
use App\Models\Forms\Form;
use App\Rules\OneEmailPerLine;
use Illuminate\Validation\Rule;
@ -126,7 +127,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
// Custom SEO
'seo_meta' => 'nullable|array',
'custom_domain' => 'sometimes|nullable|regex:/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,20}$/'
'custom_domain' => 'sometimes|nullable|regex:'. CustomDomainRequest::CUSTOM_DOMAINS_REGEX,
];
}

View File

@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class CustomDomainRequest extends FormRequest
{
const CUSTOM_DOMAINS_REGEX = '/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,20}$/';
public Workspace $workspace;
public array $customDomains = [];
@ -32,7 +33,7 @@ class CustomDomainRequest extends FormRequest
$domains = collect($value)->filter(function ($domain) {
return !empty( trim($domain) );
})->each(function($domain) use (&$errors) {
if (!preg_match('/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,20}$/', $domain)) {
if (!preg_match(self::CUSTOM_DOMAINS_REGEX, $domain)) {
$errors[] = 'Invalid domain: ' . $domain;
}
});