Skip to content

fix: prevent nil pointer dereference panics in TestClientCertRequired#968

Merged
Mzack9999 merged 3 commits intoprojectdiscovery:devfrom
mendarb:fix/nil-deref-test-client-cert
Mar 20, 2026
Merged

fix: prevent nil pointer dereference panics in TestClientCertRequired#968
Mzack9999 merged 3 commits intoprojectdiscovery:devfrom
mendarb:fix/nil-deref-test-client-cert

Conversation

@mendarb
Copy link
Copy Markdown

@mendarb mendarb commented Mar 20, 2026

Summary

Fixes #952 - go test ./... panics with nil pointer dereference in TestClientCertRequired across three packages.

Root cause: Error checks used t.Errorf (which logs but continues execution) instead of t.Fatalf (which stops the test). When ConnectWithOptions or other setup calls fail, the nil response is still dereferenced on the next line, causing a panic.

Changes across 3 files:

  • pkg/tlsx/tls/tls_test.go: Replace t.Errorf with t.Fatalf for url parsing, dialer init, client init, and ConnectWithOptions errors
  • pkg/tlsx/ztls/ztls_test.go: Same t.Errorf -> t.Fatalf changes, plus add missing nil guard on the final else if branch (was dereferencing both pointers without checking nil)
  • pkg/tlsx/openssl/openssl_test.go: Replace t.Error/t.Errorf with t.Fatalf for args building and openssl execution (also fixes potential nil result dereference in result.Command)

Test plan

  • go test ./pkg/tlsx/tls/... no longer panics on connection failure
  • go test ./pkg/tlsx/ztls/... no longer panics on connection failure
  • go test ./pkg/tlsx/openssl/... no longer panics on connection failure
  • Tests that succeed continue to behave identically (only error paths are affected)

Summary by CodeRabbit

  • Tests
    • Improved test reliability by strengthening error handling in TLS/SSL test suites—tests now terminate immediately on critical setup failures instead of continuing execution.
    • Added null-safety guards to prevent potential issues in assertion comparisons.

Replace t.Errorf with t.Fatalf in error paths where execution would
continue and dereference a nil response pointer, causing panics.
Also add missing nil guard in ztls comparison logic and fix potential
nil result dereference in openssl test.

Fixes projectdiscovery#952

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@neo-by-projectdiscovery-dev
Copy link
Copy Markdown

neo-by-projectdiscovery-dev bot commented Mar 20, 2026

Neo - PR Security Review

No security issues found

Highlights

  • Adds comprehensive test coverage including regression tests for handshake timeout handling
  • Updates dependencies and Go modules to newer versions
  • Refactors error handling and improves code organization across TLS client implementations
  • Continues test panic fixes from previous commit with additional test improvements

Comment @pdneo help for available commands. · Open in Neo

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 20, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e83cac77-118f-489e-8a12-42d9ccdb65b6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

Test error handling improvements across three test files replacing non-fatal error reporting with fatal errors to prevent execution continuation after critical failures. Adds nil-dereference guards to prevent panics when TLS handshakes fail and test code attempts to access nil connection state.

Changes

Cohort / File(s) Summary
TLS/ZTLS/OpenSSL Test Error Handling
pkg/tlsx/openssl/openssl_test.go, pkg/tlsx/tls/tls_test.go, pkg/tlsx/ztls/ztls_test.go
Replaced t.Errorf with t.Fatalf on setup and connection failure paths (URL parsing, dialer/client initialization, ConnectWithOptions errors) to abort tests immediately. Simplified OpenSSL error message. Added explicit nil guards before dereferencing in ztls_test.go assertion logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Hops through test suites with care,
Fatal errors everywhere!
No more nil panics in the night,
Guards and checks make tests run right. 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: preventing nil pointer dereference panics in TestClientCertRequired by converting error handlers from t.Error/t.Errorf to t.Fatalf and adding nil guards.
Linked Issues check ✅ Passed The PR fully addresses issue #952 by replacing t.Errorf/t.Error with t.Fatalf to short-circuit on failures and adding nil guards, directly preventing the reported nil pointer dereference panics across all three test files.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing nil pointer dereference issues in test error handling; no unrelated modifications to production code or other test logic were introduced.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@Mzack9999 Mzack9999 changed the base branch from main to dev March 20, 2026 23:14
@Mzack9999 Mzack9999 merged commit 161fb4e into projectdiscovery:dev Mar 20, 2026
10 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.

go test panics in TestClientCertRequired on macOS (nil deref)

2 participants