-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Prevent a netlas nil pointer dereference #1630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent a netlas nil pointer dereference #1630
Conversation
This uses two _different_ variables for the two _different_ HTTP requests, since two `defer` functions referenced the same `resp` variable. This setup could cause a nil pointer dereference in the following scenario: 1. The first request succeeds (a `defer` on `resp` is added). 2. The second request fails (which sets `resp` to nil, and then the first `defer` attempts to reference `resp.Body`). This change prevents that by not reusing the same variable for the second request.
WalkthroughRefactors HTTP handling in netlas.go by splitting a single response variable into two: resp1 for the initial GET (count) and resp2 for the POST (download). Updates status checks, body reads, and closures to reference the correct response object. No public interfaces or external behaviors changed. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Poem
✨ Finishing Touches
🧪 Generate 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
pkg/subscraping/sources/netlas/netlas.go (1)
80-85: Consider using session.DiscardHTTPResponse for consistency.Based on the retrieved learnings, the preferred pattern in subfinder sources is to use
defer session.DiscardHTTPResponse(resp)after successful requests. While the current manual body closure approach works correctly, using the session's discard method would be more consistent with the codebase patterns.Consider updating the defer statements:
defer func() { - if err := resp1.Body.Close(); err != nil { - results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} - s.errors++ - } + session.DiscardHTTPResponse(resp1) }()defer func() { - if err := resp2.Body.Close(); err != nil { - results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} - s.errors++ - } + session.DiscardHTTPResponse(resp2) }()Also applies to: 132-137
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
pkg/subscraping/sources/netlas/netlas.go(4 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: x-stp
PR: projectdiscovery/subfinder#0
File: :0-0
Timestamp: 2025-06-24T16:03:30.467Z
Learning: When fixing HTTP response handling bugs in subfinder sources, the correct pattern is to use `defer session.DiscardHTTPResponse(resp)` after successful requests to ensure the response body remains open for reading, and call `session.DiscardHTTPResponse(resp)` immediately in error cases.
📚 Learning: 2025-07-17T12:07:51.521Z
Learnt from: 0x4500
PR: projectdiscovery/subfinder#1612
File: pkg/subscraping/agent.go:0-0
Timestamp: 2025-07-17T12:07:51.521Z
Learning: In subfinder subscraping sources, when a source needs to handle specific HTTP status codes differently (like treating 204 as success), the check should be implemented within the individual source's code rather than modifying the global httpRequestWrapper in agent.go. This keeps the special handling localized and avoids affecting other sources.
Applied to files:
pkg/subscraping/sources/netlas/netlas.go
📚 Learning: 2025-06-24T16:03:30.467Z
Learnt from: x-stp
PR: projectdiscovery/subfinder#0
File: :0-0
Timestamp: 2025-06-24T16:03:30.467Z
Learning: When fixing HTTP response handling bugs in subfinder sources, the correct pattern is to use `defer session.DiscardHTTPResponse(resp)` after successful requests to ensure the response body remains open for reading, and call `session.DiscardHTTPResponse(resp)` immediately in error cases.
Applied to files:
pkg/subscraping/sources/netlas/netlas.go
🧬 Code Graph Analysis (1)
pkg/subscraping/sources/netlas/netlas.go (11)
pkg/subscraping/sources/bevigil/bevigil.go (1)
Source(19-25)pkg/subscraping/sources/bufferover/bufferover.go (1)
Source(25-31)pkg/subscraping/sources/c99/c99.go (1)
Source(16-22)pkg/subscraping/sources/censys/censys.go (1)
Source(57-63)pkg/subscraping/sources/builtwith/builtwith.go (1)
Source(33-39)pkg/subscraping/sources/chaos/chaos.go (1)
Source(14-20)pkg/subscraping/sources/chinaz/chinaz.go (1)
Source(15-21)pkg/subscraping/sources/certspotter/certspotter.go (1)
Source(20-26)pkg/subscraping/sources/digitalyama/digitalyama.go (1)
Source(14-20)pkg/subscraping/sources/dnsdumpster/dnsdumpster.go (1)
Source(23-29)pkg/subscraping/sources/dnsdb/dnsdb.go (1)
Source(41-47)
🔇 Additional comments (8)
pkg/subscraping/sources/netlas/netlas.go (8)
66-69: LGTM! Correct fix for nil pointer dereference.The separation of response variables (
resp1for the GET request,resp2for the POST request) effectively prevents the nil pointer dereference scenario described in the PR objectives.
75-76: LGTM! Status code check correctly references the first response.The status code check now properly references
resp1.StatusCodeinstead of the previously sharedrespvariable.
81-81: LGTM! Body closure correctly references the first response.The defer closure now properly references
resp1.Bodyfor the GET request response.
87-87: LGTM! Body reading correctly references the first response.The body reading operation now properly references
resp1.Bodyfor the GET request response.
123-126: LGTM! Second HTTP request uses separate response variable.The POST request now uses
resp2variable, preventing interference with the first request's response handling.
133-133: LGTM! Body closure correctly references the second response.The defer closure now properly references
resp2.Bodyfor the POST request response.
138-138: LGTM! Body reading correctly references the second response.The body reading operation now properly references
resp2.Bodyfor the POST request response.
145-146: LGTM! Status code checks correctly reference the second response.The rate limiting check now properly references
resp2.StatusCodefor the POST request response.
|
+1 currently, I'm frequently experiencing this dereference issue while running subfinder ; panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0xfb92b9]
goroutine 3320 [running]:
github.com/projectdiscovery/subfinder/v2/pkg/subscraping/sources/netlas.(*Source).Run.func1.2()
/home/runner/work/subfinder/subfinder/v2/pkg/subscraping/sources/netlas/netlas.go:81 +0x19
github.com/projectdiscovery/subfinder/v2/pkg/subscraping/sources/netlas.(*Source).Run.func1()
/home/runner/work/subfinder/subfinder/v2/pkg/subscraping/sources/netlas/netlas.go:130 +0xe3b
created by github.com/projectdiscovery/subfinder/v2/pkg/subscraping/sources/netlas.(*Source).Run in goroutine 3241
/home/runner/work/subfinder/subfinder/v2/pkg/subscraping/sources/netlas/netlas.go:51 +0xe5 |
|
Note that the build tests are broken overall and can be fixed by #1631. |
|
#1631 has been merged; everything except the |
|
Thanks for the PR! |
This uses two different variables for the two different HTTP requests, since two
deferfunctions referenced the samerespvariable.This setup could cause a nil pointer dereference in the following scenario:
deferonrespis added).respto nil, and then the firstdeferattempts to referenceresp.Body).This change prevents that by not reusing the same variable for the second request.
closes #1634
Summary by CodeRabbit