Skip to content

Fix import exception#40

Merged
eldadfux merged 1 commit intomainfrom
fix-import-exception
Feb 19, 2026
Merged

Fix import exception#40
eldadfux merged 1 commit intomainfrom
fix-import-exception

Conversation

@eldadfux
Copy link
Member

@eldadfux eldadfux commented Feb 19, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Improved DNS message truncation handling to properly mark responses as non-authoritative when the authority section is dropped to meet size constraints in edge case scenarios.
  • Tests

    • Added test coverage for DNS message truncation with authority section removal.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

Walkthrough

The pull request modifies DNS message truncation handling to address NODATA/NXDOMAIN edge cases. In src/DNS/Message.php, the RFC-truncation flow now marks NODATA/NXDOMAIN scenarios as non-authoritative when dropping the authority section, enforcing this behavior via a new isNodataOrNxdomain flag during Step 2 response construction and a separate isNodataOrNxdomainTruncated check when finalizing the truncated response. A unit test testEncodeNodataWithTruncationDroppingAuthority() is added to tests/unit/DNS/MessageTest.php to verify encoding behavior when truncation removes the authority section, validating that both answers and authority sections are dropped and the response header becomes non-authoritative. The test method appears to be added twice in the same file.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title 'Fix import exception' does not match the actual changes, which involve RFC-truncation handling for NODATA/NXDOMAIN scenarios and truncation logic. Update the title to reflect the actual changes, such as 'Fix NODATA/NXDOMAIN handling in RFC-truncation flow' or 'Ensure non-authoritative marking when truncation drops authority section'.
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-import-exception

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/DNS/Message.php (1)

306-320: Correct coverage for the answer-truncation edge case.

Using $fittingAnswers === [] rather than $this->answers === [] is intentional: it also guards the case where the original response had answers but none fit individually within maxSize, which would otherwise produce a NOERROR + answers:[] + authority:[] response that still triggers the NODATA constructor exception when authoritative is true.

Optional: The NODATA/NXDOMAIN condition is spelled out twice (lines 262-263 and 307-308). Extracting a small private helper (e.g., isNodataOrNxdomain(array $answers): bool) would eliminate the duplication and make the intent explicit at both call sites.

♻️ Proposed helper extraction
+    private function isNodataOrNxdomain(array $answers): bool
+    {
+        return ($this->header->responseCode === self::RCODE_NOERROR && $answers === [])
+            || $this->header->responseCode === self::RCODE_NXDOMAIN;
+    }

Then in encodeWithTruncation:

-        $isNodataOrNxdomain = ($this->header->responseCode === self::RCODE_NOERROR && $this->answers === [])
-            || $this->header->responseCode === self::RCODE_NXDOMAIN;
+        $isNodataOrNxdomain = $this->isNodataOrNxdomain($this->answers);
-        $isNodataOrNxdomainTruncated = ($this->header->responseCode === self::RCODE_NOERROR && $fittingAnswers === [])
-            || $this->header->responseCode === self::RCODE_NXDOMAIN;
+        $isNodataOrNxdomainTruncated = $this->isNodataOrNxdomain($fittingAnswers);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/DNS/Message.php` around lines 306 - 320, The code correctly uses
$fittingAnswers rather than $this->answers to decide NODATA/NXDOMAIN
authoritative behavior in encodeWithTruncation, but the same condition is
duplicated; extract a small private helper (e.g., isNodataOrNxdomain(array
$answers): bool) and replace both uses with calls to that helper, then compute
$isNodataOrNxdomainTruncated = $this->isNodataOrNxdomain($fittingAnswers) and
pass authoritative: $isNodataOrNxdomainTruncated ? false :
$this->header->authoritative when building the truncated response to eliminate
duplication and make intent explicit.
tests/unit/DNS/MessageTest.php (1)

463-493: Test logic is correct; consider adding NXDOMAIN parity coverage.

The setup correctly targets Step 2 truncation: header (12B) + question for 'empty.example.com' TXT (~23B) ≈ 35B, well under the 80B limit, while the full packet with the SOA record exceeds it. The assertions confirm the expected non-authoritative, authority-dropped outcome.

The symmetric scenario — an NXDOMAIN response with SOA in authority where truncation drops the authority section — is not currently covered. That path follows the same $isNodataOrNxdomain branch in Step 2 and is worth a test case.

🧪 Suggested NXDOMAIN parity test
public function testEncodeNxdomainWithTruncationDroppingAuthority(): void
{
    $question = new Question('missing.example.com', Record::TYPE_A);
    $query = Message::query($question, id: 0x5678);

    $soa = new Record(
        'example.com',
        Record::TYPE_SOA,
        Record::CLASS_IN,
        300,
        'ns.example.com. hostmaster.example.com. 2024010101 3600 600 86400 300'
    );

    $response = Message::response(
        $query->header,
        Message::RCODE_NXDOMAIN,
        questions: $query->questions,
        answers: [],
        authority: [$soa],
        additional: [],
        authoritative: true
    );

    $truncated = $response->encode(80);
    $decoded = Message::decode($truncated);

    $this->assertSame(Message::RCODE_NXDOMAIN, $decoded->header->responseCode);
    $this->assertCount(0, $decoded->answers);
    $this->assertCount(0, $decoded->authority);
    $this->assertFalse($decoded->header->authoritative, 'Dropped authority => non-authoritative');
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/unit/DNS/MessageTest.php` around lines 463 - 493, Add a symmetric test
case covering NXDOMAIN truncation similar to the existing
testEncodeNodataWithTruncationDroppingAuthority: create a Question (e.g.
'missing.example.com' A), build a query via Message::query, create the SOA
Record, construct a response with Message::response using
Message::RCODE_NXDOMAIN, authoritative=true and authority containing the SOA,
call Message::encode with the same small size (80) to force truncation, decode
via Message::decode and assert header->responseCode is Message::RCODE_NXDOMAIN,
answers and authority are empty, and header->authoritative is false; name the
test method testEncodeNxdomainWithTruncationDroppingAuthority to mirror the
existing test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/DNS/Message.php`:
- Around line 306-320: The code correctly uses $fittingAnswers rather than
$this->answers to decide NODATA/NXDOMAIN authoritative behavior in
encodeWithTruncation, but the same condition is duplicated; extract a small
private helper (e.g., isNodataOrNxdomain(array $answers): bool) and replace both
uses with calls to that helper, then compute $isNodataOrNxdomainTruncated =
$this->isNodataOrNxdomain($fittingAnswers) and pass authoritative:
$isNodataOrNxdomainTruncated ? false : $this->header->authoritative when
building the truncated response to eliminate duplication and make intent
explicit.

In `@tests/unit/DNS/MessageTest.php`:
- Around line 463-493: Add a symmetric test case covering NXDOMAIN truncation
similar to the existing testEncodeNodataWithTruncationDroppingAuthority: create
a Question (e.g. 'missing.example.com' A), build a query via Message::query,
create the SOA Record, construct a response with Message::response using
Message::RCODE_NXDOMAIN, authoritative=true and authority containing the SOA,
call Message::encode with the same small size (80) to force truncation, decode
via Message::decode and assert header->responseCode is Message::RCODE_NXDOMAIN,
answers and authority are empty, and header->authoritative is false; name the
test method testEncodeNxdomainWithTruncationDroppingAuthority to mirror the
existing test.

@eldadfux eldadfux force-pushed the fix-import-exception branch from 143e12d to 31f8969 Compare February 19, 2026 16:05
@eldadfux eldadfux merged commit 574327f into main Feb 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants