Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ public static function encodeName(string $name): string
return $res;
}

public static function decodeName(string $string, int &$offset = 0): string
public static function decodeName(string $string, int &$offset = 0, int $depth = 0): string
{
if ($depth > 127) {
throw new \UnexpectedValueException('DNS name has too many compression pointers');
}

$len = ord($string[$offset]);
++$offset;

Expand All @@ -163,7 +167,7 @@ public static function decodeName(string $string, int &$offset = 0): string
$offset += $len;
$len = ord($string[$offset]);
if ($len & 0b11000000) {
$name .= self::decodeName($string, $offset);
$name .= self::decodeName($string, $offset, $depth + 1);
break;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Badcow\DNS\ResourceRecord;
use Badcow\DNS\UnsetValueException;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

class MessageTest extends TestCase
{
Expand Down Expand Up @@ -266,6 +267,12 @@ public function testWire8(): void
$this->assertEquals($expectation, $msg->toWire());
}

public function testWire10(): void
{
$this->expectException(UnexpectedValueException::class);
Message::fromWire($this->getWireTestData(10));
}

/**
* @throws UnsupportedTypeException
* @throws \Exception
Expand Down
13 changes: 13 additions & 0 deletions tests/Resources/wire/wire_test.data10
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# HEADER SECTION
30 99 # ID
01 00 # FLAGS
00 01 # QDCOUNT=1
00 00 # ANCOUNT=0
00 00 # NSCOUNT=0
00 00 # ARCOUNT=0

# QUESTION SECTION
01 61 # QNAME label: len=1, "a"
c0 0c # compression pointer -> offset 0x0C (12) = start of QNAME
00 01 # QTYPE=A
00 01 # QCLASS=IN
Loading