Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix: accept underscores in domain hostnames for API URL validation

Osamaali31374b1077010f6
diff --git a/app/Http/Controllers/Api/ApplicationsController.php b/app/Http/Controllers/Api/ApplicationsController.php
index 5e5405a7ae..b2f09e0084 100644
--- a/app/Http/Controllers/Api/ApplicationsController.php
+++ b/app/Http/Controllers/Api/ApplicationsController.php
@@ -1084,7 +1084,7 @@ class ApplicationsController extends Controller
$errors = [];
$urls = $urls->map(function ($url) use (&$errors) {
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = "Invalid URL: {$url}";
return $url;
@@ -1325,7 +1325,7 @@ class ApplicationsController extends Controller
$errors = [];
$urls = $urls->map(function ($url) use (&$errors) {
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = "Invalid URL: {$url}";
return $url;
@@ -1538,7 +1538,7 @@ class ApplicationsController extends Controller
$errors = [];
$urls = $urls->map(function ($url) use (&$errors) {
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = "Invalid URL: {$url}";
return $url;
@@ -2490,7 +2490,7 @@ class ApplicationsController extends Controller
return null;
}
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = 'Invalid URL: '.$url;
return $url;
@@ -2553,7 +2553,7 @@ class ApplicationsController extends Controller
$errors = [];
$urls = $urls->map(function ($url) use (&$errors) {
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = "Invalid URL: {$url}";
return $url;
@@ -3866,7 +3866,7 @@ class ApplicationsController extends Controller
return null;
}
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = 'Invalid URL: '.$url;
return str($url)->lower();
diff --git a/app/Http/Controllers/Api/ServicesController.php b/app/Http/Controllers/Api/ServicesController.php
index 11a23d46c5..b0c6aa8a54 100644
--- a/app/Http/Controllers/Api/ServicesController.php
+++ b/app/Http/Controllers/Api/ServicesController.php
@@ -57,7 +57,7 @@ class ServicesController extends Controller
});
$urls = $urls->map(function ($url) use (&$errors) {
- if (! filter_var($url, FILTER_VALIDATE_URL)) {
+ if (! isValidDomainUrl($url)) {
$errors[] = "Invalid URL: {$url}";
return $url;
diff --git a/bootstrap/helpers/domains.php b/bootstrap/helpers/domains.php
index ff77a78e2e..1aa1825105 100644
--- a/bootstrap/helpers/domains.php
+++ b/bootstrap/helpers/domains.php
@@ -4,6 +4,16 @@ use App\Models\Application;
use App\Models\ServiceApplication;
use Illuminate\Support\Collection;
+function isValidDomainUrl(string $url): bool
+{
+ // PHP's FILTER_VALIDATE_URL rejects underscores in the host, but they are
+ // accepted by browsers, Let's Encrypt, and common Docker service naming
+ // (e.g. https://myapp_service.example.com). Validate against a copy with
+ // underscores replaced by hyphens (a valid host character) so such domains
+ // are not wrongly rejected; URLs without underscores are unaffected.
+ return filter_var(str_replace('_', '-', $url), FILTER_VALIDATE_URL) !== false;
+}
+
function checkDomainUsage(ServiceApplication|Application|null $resource = null, ?string $domain = null)
{
$conflicts = [];
diff --git a/tests/Unit/IsValidDomainUrlTest.php b/tests/Unit/IsValidDomainUrlTest.php
new file mode 100644
index 0000000000..940eaecea8
--- /dev/null
+++ b/tests/Unit/IsValidDomainUrlTest.php
@@ -0,0 +1,22 @@
+<?php
+
+it('accepts hostnames containing underscores', function () {
+ // Regression: PHP's FILTER_VALIDATE_URL rejects underscores in the host,
+ // which blocked valid service domains (e.g. Docker service naming) from
+ // being saved and getting Let's Encrypt certificates. See issue #10597.
+ expect(isValidDomainUrl('https://myapp_service.example.com'))->toBeTrue();
+ expect(isValidDomainUrl('http://my_app.example.com'))->toBeTrue();
+ expect(isValidDomainUrl('https://a_b_c.example.com/path'))->toBeTrue();
+});
+
+it('accepts ordinary domains and URLs', function () {
+ expect(isValidDomainUrl('https://example.com'))->toBeTrue();
+ expect(isValidDomainUrl('http://sub.example.com:8080/path?q=1'))->toBeTrue();
+ expect(isValidDomainUrl('https://example.com/a_b'))->toBeTrue();
+});
+
+it('rejects strings that are not valid URLs', function () {
+ expect(isValidDomainUrl('not a url'))->toBeFalse();
+ expect(isValidDomainUrl('example.com'))->toBeFalse();
+ expect(isValidDomainUrl(''))->toBeFalse();
+});