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
16 changes: 16 additions & 0 deletions src/DNS/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ public function encode(?int $maxSize = null): string
return $header->encode() . substr($packet, Header::LENGTH);
}

/**
* Validate all response records without encoding the message.
*/
public function validate(): void
{
foreach ([
$this->answers,
$this->authority,
$this->additional,
] as $records) {
foreach ($records as $record) {
$record->validateRdata();
}
}
}

/**
* @param list<Record> $records
*/
Expand Down
8 changes: 8 additions & 0 deletions src/DNS/Message/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ public function encode(string $packet = ''): string
return $data;
}

/**
* Validate RDATA for this record type without encoding the full record.
*/
public function validateRdata(): void
{
$this->encodeRdata('');
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.

/**
* Encode RDATA based on record type.
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/DNS/Message/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,37 @@ class: Record::CLASS_IN,
$this->assertSame(600, $decoded->ttl);
}

public function testValidateRdataRejectsHostnameForARecord(): void
{
$record = new Record(
name: 'example.com',
type: Record::TYPE_A,
class: Record::CLASS_IN,
ttl: 300,
rdata: 'ns2.appwrite.zone'
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid IPv4 address: ns2.appwrite.zone');

$record->validateRdata();
}

public function testValidateRdataAcceptsHostnameForNsRecord(): void
{
$record = new Record(
name: 'example.com',
type: Record::TYPE_NS,
class: Record::CLASS_IN,
ttl: 300,
rdata: 'ns2.appwrite.zone'
);

$record->validateRdata();

$this->addToAssertionCount(1);
}

public function testConstructorTrimsWhitespaceFromName(): void
{
$record = new Record(
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/DNS/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Unit\Utopia\DNS;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Utopia\DNS\Exception\Message\DecodingException;
use Utopia\DNS\Exception\Message\PartialDecodingException;
Expand Down Expand Up @@ -726,6 +727,46 @@ public function testExtremeAnswerTruncationPreservesAuthoritativeFlag(): void
);
}

#[DataProvider('invalidSectionProvider')]
public function testValidateChecksAnswerAuthorityAndAdditionalRecords(string $invalidSection): void
{
$question = new Question('example.com', Record::TYPE_A);
$query = Message::query($question, id: 0x5508);

$invalid = [
new Record('ns2.appwrite.zone', Record::TYPE_A, Record::CLASS_IN, 300, 'ns2.appwrite.zone'),
];
$valid = [
new Record('example.com', Record::TYPE_NS, Record::CLASS_IN, 300, 'ns2.appwrite.zone'),
];

$response = Message::response(
$query->header,
Message::RCODE_NOERROR,
questions: $query->questions,
answers: $invalidSection === 'answers' ? $invalid : $valid,
authority: $invalidSection === 'authority' ? $invalid : $valid,
additional: $invalidSection === 'additional' ? $invalid : $valid
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid IPv4 address: ns2.appwrite.zone');

$response->validate();
}

/**
* @return array<string, array<int, string>>
*/
public static function invalidSectionProvider(): array
{
return [
'answers' => ['answers'],
'authority' => ['authority'],
'additional' => ['additional'],
];
}

/**
* NODATA-style response: zero answers but populated authority. When the
* authority section can't fit, it's dropped without setting TC — there
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Expand Down
Loading