From 66efd428c17b5ca0fe044e689039851fb806d9a5 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Mon, 11 Nov 2024 14:11:33 +0000 Subject: [PATCH] Allow underscores in hostnames (#16683) --- LibreNMS/Util/Validate.php | 2 +- tests/CommonFunctionsTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LibreNMS/Util/Validate.php b/LibreNMS/Util/Validate.php index 1d2f193c08d4..1f5135d4aee3 100644 --- a/LibreNMS/Util/Validate.php +++ b/LibreNMS/Util/Validate.php @@ -47,7 +47,7 @@ public static function hostname($hostname) // maximum length is 253 characters, maximum segment size is 63 return - preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*\.?$/i", $hostname) //valid chars check + preg_match("/^([a-z\d](-*[a-z\d_])*)(\.([a-z\d](-*[a-z\d_])*))*\.?$/i", $hostname) //valid chars check && preg_match('/^.{1,253}$/', $hostname) //overall length check && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*\.?$/", $hostname); } diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php index 72b41250b789..07d31cfde40b 100644 --- a/tests/CommonFunctionsTest.php +++ b/tests/CommonFunctionsTest.php @@ -127,7 +127,7 @@ public function testIsValidHostname(): void $this->assertTrue(Validate::hostname('www.averylargedomainthatdoesnotreallyexist.com'), 'www.averylargedomainthatdoesnotreallyexist.com'); $this->assertTrue(Validate::hostname('cont-ains.h-yph-en-s.com'), 'cont-ains.h-yph-en-s.com'); $this->assertTrue(Validate::hostname('cisco-3750x'), 'cisco-3750x'); - $this->assertFalse(Validate::hostname('cisco_3750x'), 'cisco_3750x'); + $this->assertTrue(Validate::hostname('cisco_3750x'), 'cisco_3750x'); $this->assertFalse(Validate::hostname('goo gle.com'), 'goo gle.com'); $this->assertFalse(Validate::hostname('google..com'), 'google..com'); $this->assertFalse(Validate::hostname('google.com '), 'google.com ');