From 572b46580a076d94eda1c2340c8954b4448b9ab5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 3 Jun 2024 22:12:37 +0000 Subject: [PATCH] Better name validation code. --- src/DbConfig.php | 13 ++++++++++--- tests/DbConfigTest.php | 8 ++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/DbConfig.php b/src/DbConfig.php index 5f22759..cbc218f 100644 --- a/src/DbConfig.php +++ b/src/DbConfig.php @@ -34,8 +34,16 @@ class DbConfig implements IConfig { } public static function validateName(string $name): bool { - // this should better validate the format, this allows for a lot of shittery - return preg_match('#^([a-z][a-zA-Z0-9._]+)$#', $name) === 1; + $parts = explode('.', $name); + foreach($parts as $part) { + if($part === '' || trim($part) !== $part) + return false; + + if(preg_match('#^([a-z][a-zA-Z0-9_]+)$#', $part) !== 1) + return false; + } + + return true; } /** @@ -221,6 +229,5 @@ class DbConfig implements IConfig { $stmt->addParameter(2, serialize($value)); $stmt->execute(); } - } } diff --git a/tests/DbConfigTest.php b/tests/DbConfigTest.php index 540a340..8406126 100644 --- a/tests/DbConfigTest.php +++ b/tests/DbConfigTest.php @@ -144,4 +144,12 @@ final class DbConfigTest extends TestCase { $this->assertEquals([1234, 56.789, 'Mewow!', true, 'jeff'], $this->config->getArray('test.array')); $this->assertEquals(false, $this->config->getBoolean('test.array')); } + + public function testNameValidation(): void { + $this->assertTrue(\Syokuhou\DbConfig::validateName('th1s.iS.vAL1d')); + $this->assertFalse(\Syokuhou\DbConfig::validateName('')); + $this->assertFalse(\Syokuhou\DbConfig::validateName('this..is.not.valid')); + $this->assertFalse(\Syokuhou\DbConfig::validateName('this..is.not.valid')); + $this->assertFalse(\Syokuhou\DbConfig::validateName('First.may.Not.be.uppercase')); + } }