From abf9906e0ba71405f2b7bba77f5d4e52c0529d00 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 11 Mar 2026 14:03:06 +0000 Subject: [PATCH] Trim whitespace from domain names in Record and Question constructors Co-Authored-By: Claude Opus 4.6 --- src/DNS/Message/Question.php | 2 +- src/DNS/Message/Record.php | 2 +- tests/unit/DNS/Message/QuestionTest.php | 14 +++++++++ tests/unit/DNS/Message/RecordTest.php | 40 +++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/DNS/Message/Question.php b/src/DNS/Message/Question.php index 6cdfff1..a74c428 100644 --- a/src/DNS/Message/Question.php +++ b/src/DNS/Message/Question.php @@ -13,7 +13,7 @@ public function __construct( public int $type, public int $class = Record::CLASS_IN ) { - $this->name = strtolower($name); + $this->name = trim(strtolower($name)); } /** diff --git a/src/DNS/Message/Record.php b/src/DNS/Message/Record.php index ca6721c..4188ed2 100644 --- a/src/DNS/Message/Record.php +++ b/src/DNS/Message/Record.php @@ -120,7 +120,7 @@ public function __construct( public ?int $weight = null, public ?int $port = null ) { - $this->name = strtolower($name); + $this->name = trim(strtolower($name)); } /** diff --git a/tests/unit/DNS/Message/QuestionTest.php b/tests/unit/DNS/Message/QuestionTest.php index 9f100bb..4c1dea9 100644 --- a/tests/unit/DNS/Message/QuestionTest.php +++ b/tests/unit/DNS/Message/QuestionTest.php @@ -61,4 +61,18 @@ public function testDecodeHandlesCompressionPointer(): void $this->assertSame(Record::CLASS_IN, $parsedSecond->class); $this->assertSame(strlen($message), $offset); } + + public function testConstructorTrimsWhitespaceFromName(): void + { + $question = new Question(' www.example.com ', Record::TYPE_A, Record::CLASS_IN); + + $this->assertSame('www.example.com', $question->name); + } + + public function testConstructorTrimsTabsAndNewlinesFromName(): void + { + $question = new Question("\t\nwww.example.com\r\n", Record::TYPE_A, Record::CLASS_IN); + + $this->assertSame('www.example.com', $question->name); + } } diff --git a/tests/unit/DNS/Message/RecordTest.php b/tests/unit/DNS/Message/RecordTest.php index 029a581..44cc053 100644 --- a/tests/unit/DNS/Message/RecordTest.php +++ b/tests/unit/DNS/Message/RecordTest.php @@ -482,4 +482,44 @@ class: Record::CLASS_IN, $this->assertSame(Record::TYPE_TXT, $decoded->type); $this->assertSame(600, $decoded->ttl); } + + public function testConstructorTrimsWhitespaceFromName(): void + { + $record = new Record( + name: ' example.com ', + type: Record::TYPE_A, + class: Record::CLASS_IN, + ttl: 300, + rdata: '93.184.216.34' + ); + + $this->assertSame('example.com', $record->name); + } + + public function testConstructorTrimsTabsAndNewlinesFromName(): void + { + $record = new Record( + name: "\t\nexample.com\r\n", + type: Record::TYPE_A, + class: Record::CLASS_IN, + ttl: 300, + rdata: '93.184.216.34' + ); + + $this->assertSame('example.com', $record->name); + } + + public function testWithNameTrimsWhitespace(): void + { + $record = new Record( + name: 'example.com', + type: Record::TYPE_A, + class: Record::CLASS_IN, + ttl: 300, + rdata: '93.184.216.34' + ); + + $renamed = $record->withName(' other.com '); + $this->assertSame('other.com', $renamed->name); + } }