Skip to content

Comments

fix: prevent nil pointer panic in WAF detector#6368

Merged
Ice3man543 merged 1 commit intodevfrom
fix/waf-detector-nil-pointer
Aug 5, 2025
Merged

fix: prevent nil pointer panic in WAF detector#6368
Ice3man543 merged 1 commit intodevfrom
fix/waf-detector-nil-pointer

Conversation

@knakul853
Copy link
Contributor

@knakul853 knakul853 commented Aug 4, 2025

  • Add nil checks for detector and regexCache in DetectWAF()
  • Add nil check for individual regex entries before MatchString()
  • Add comprehensive unit tests for nil pointer scenarios
  • Prevents runtime panic when WAF detector encounters nil pointers during regex matching

Proposed changes

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Bug Fixes

    • Improved stability of WAF detection to prevent errors when handling uninitialized or partially initialized detectors.
  • Tests

    • Added tests to ensure WAF detection handles nil pointers safely and does not cause application crashes.

- Add nil checks for detector and regexCache in DetectWAF()
- Add nil check for individual regex entries before MatchString()
- Add comprehensive unit tests for nil pointer scenarios
- Prevents runtime panic when WAF detector encounters nil pointers during regex matching
@knakul853 knakul853 requested a review from Ice3man543 August 4, 2025 15:45
@auto-assign auto-assign bot requested a review from dogancanbakir August 4, 2025 15:45
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 4, 2025

Walkthrough

Nil pointer safety was added to the DetectWAF method in the WafDetector struct by introducing checks for nil receivers, nil maps, and nil regex pointers. Corresponding unit tests were implemented to verify that these scenarios do not cause panics and return expected results.

Changes

Cohort / File(s) Change Summary
WafDetector nil safety
pkg/output/stats/waf/waf.go
Added nil checks for receiver, regexCache map, and regex pointers in DetectWAF to prevent nil pointer dereference.
Nil pointer safety tests
pkg/output/stats/waf/waf_test.go
Introduced tests for nil pointer safety in DetectWAF, covering nil receiver, nil map, nil regex, and empty map scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant WafDetector
    Note over Tester: Call DetectWAF with possible nils
    Tester->>WafDetector: DetectWAF(content)
    alt WafDetector or regexCache is nil
        WafDetector-->>Tester: return ("", false)
    else regexCache has nil regex
        WafDetector-->>Tester: skip nil, continue
    else regex matches
        WafDetector-->>Tester: return (name, true)
    else no match
        WafDetector-->>Tester: return ("", false)
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • dwisiswant0

Poem

A bunny once hopped through some code,
Where nils could cause panic and woe.
With careful new checks,
And tests for defects,
Now WAF detection’s safe—on we go!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cff86b5 and b685d63.

📒 Files selected for processing (2)
  • pkg/output/stats/waf/waf.go (1 hunks)
  • pkg/output/stats/waf/waf_test.go (2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: dogancanbakir
PR: projectdiscovery/nuclei#6059
File: pkg/authprovider/file.go:144-145
Timestamp: 2025-02-19T13:21:35.933Z
Learning: In the Nuclei project, when checking auth strategies returned from LookupAddr, use len() check instead of nil check as the function returns an empty slice for no matches.
📚 Learning: the user dwisiswant0 declined adding documentation for internal api changes in pkg/fuzz/component/pa...
Learnt from: dwisiswant0
PR: projectdiscovery/nuclei#6290
File: pkg/fuzz/component/path_test.go:43-44
Timestamp: 2025-06-30T16:33:26.746Z
Learning: The user dwisiswant0 declined adding documentation for internal API changes in pkg/fuzz/component/path when the change was from nested URL field access (rebuilt.URL.Path, rebuilt.URL.String()) to direct field access (rebuilt.Path, rebuilt.String()), indicating they don't consider additional documentation necessary for such internal API modifications.

Applied to files:

  • pkg/output/stats/waf/waf.go
🔇 Additional comments (4)
pkg/output/stats/waf/waf.go (2)

56-58: Excellent defensive programming approach.

The nil checks for both the receiver and regexCache effectively prevent runtime panics while maintaining clean early-return semantics. This addresses the core issue described in the PR objectives.


61-61: Good addition of regex nil check.

The nil check before MatchString() prevents panics when regexCache contains nil regex pointers, completing the defensive programming approach.

pkg/output/stats/waf/waf_test.go (2)

3-6: Appropriate import addition.

The regexp import is necessary for the new test cases that verify nil pointer safety with regex objects.


65-121: Comprehensive and well-structured test coverage.

The test function excellently covers all nil pointer scenarios addressed by the code changes:

  • Nil detector receiver
  • Nil regexCache map
  • Nil regex entries in cache
  • Empty regexCache

The use of defer/recover to catch panics and the assertion logic are both appropriate and follow Go testing best practices.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/waf-detector-nil-pointer

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@knakul853
Copy link
Contributor Author

@coderabbitai
create an issue for this, it was causing panic during waf detection
here is trace

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation]

goroutine 514296 [runnable]:
regexp.(*machine).match(0xc013a56780, {0x53b7060, 0xc013a56840}, {...})
    /opt/hostedtoolcache/go/1.24.1/x64/src/regexp/exec.go:340 +0x265
github.com/projectdiscovery/nuclei/v3/pkg/output/stats/waf.(*WafDetector).DetectWAF(0x100000000000000?, {0xc145c10000, 0x2036c})
    /home/runner/work/aurora/aurora/vendor/github.com/projectdiscovery/nuclei/v3/pkg/output/stats/waf/waf.go:57 +0xc8

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 4, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

I'll create an issue to document the nil pointer panic that was occurring in the WAF detector. Let me create this issue with the stack trace you provided.

@knakul853 I've created an issue to document the nil pointer panic that was occurring in the WAF detector. The issue includes the stack trace you provided and references this PR as the fix. The issue has been assigned to you as requested.


🏁 Script executed:

#!/bin/bash
# Create a GitHub issue for the nil pointer panic in WAF detector
gh issue create \
  --title "Nil pointer panic in WAF detector during DetectWAF operation" \
  --body "## Problem Description

A nil pointer dereference panic was occurring in the WAF detector during the \`DetectWAF\` operation, causing segmentation violations.

## Stack Trace

\`\`\`
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation]

goroutine 514296 [runnable]:
regexp.(*machine).match(0xc013a56780, {0x53b7060, 0xc013a56840}, {...})
    /opt/hostedtoolcache/go/1.24.1/x64/src/regexp/exec.go:340 +0x265
github.com/projectdiscovery/nuclei/v3/pkg/output/stats/waf.(*WafDetector).DetectWAF(0x100000000000000?, {0xc145c10000, 0x2036c})
    /home/runner/work/aurora/aurora/vendor/github.com/projectdiscovery/nuclei/v3/pkg/output/stats/waf/waf.go:57 +0xc8
\`\`\`

## Resolution

This issue has been fixed in PR #6368 by adding proper nil checks for the detector, regexCache, and individual regex entries before invoking MatchString().

## References

- Fixed by: https://github.com/projectdiscovery/nuclei/pull/6368
- Reported by: @knakul853
- Original comment: https://github.com/projectdiscovery/nuclei/pull/6368#issuecomment-$(gh api repos/projectdiscovery/nuclei/pulls/6368/comments --jq '.[-1].id')" \
  --assignee knakul853

Length of output: 1328


@knakul853 Perfect! I've successfully created issue #6369 to document the nil pointer panic in the WAF detector: #6369

The issue includes:

This will serve as proper documentation for the panic that was occurring before your fix.


🧠 Learnings used
Learnt from: dogancanbakir
PR: projectdiscovery/nuclei#6059
File: pkg/authprovider/file.go:144-145
Timestamp: 2025-02-19T13:21:35.933Z
Learning: In the Nuclei project, when checking auth strategies returned from LookupAddr, use len() check instead of nil check as the function returns an empty slice for no matches.

@Ice3man543 Ice3man543 merged commit bba2c3a into dev Aug 5, 2025
20 checks passed
@Ice3man543 Ice3man543 deleted the fix/waf-detector-nil-pointer branch August 5, 2025 20:23
@coderabbitai coderabbitai bot mentioned this pull request Sep 24, 2025
4 tasks
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