Skip to content

Commit ae80dda

Browse files
Prevent a netlas nil pointer dereference
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.
1 parent 8feb51f commit ae80dda

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

pkg/subscraping/sources/netlas/netlas.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Se
6363

6464
// Pick an API key
6565
randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name())
66-
resp, err := session.HTTPRequest(ctx, http.MethodGet, countUrl, "", map[string]string{
66+
resp1, err := session.HTTPRequest(ctx, http.MethodGet, countUrl, "", map[string]string{
6767
"accept": "application/json",
6868
"X-API-Key": randomApiKey,
6969
}, nil, subscraping.BasicAuth{})
@@ -72,19 +72,19 @@ func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Se
7272
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
7373
s.errors++
7474
return
75-
} else if resp.StatusCode != 200 {
76-
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("request rate limited with status code %d", resp.StatusCode)}
75+
} else if resp1.StatusCode != 200 {
76+
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("request rate limited with status code %d", resp1.StatusCode)}
7777
s.errors++
7878
return
7979
}
8080
defer func() {
81-
if err := resp.Body.Close(); err != nil {
81+
if err := resp1.Body.Close(); err != nil {
8282
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
8383
s.errors++
8484
}
8585
}()
8686

87-
body, err := io.ReadAll(resp.Body)
87+
body, err := io.ReadAll(resp1.Body)
8888
if err != nil {
8989
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("error reading ressponse body")}
9090
s.errors++
@@ -120,7 +120,7 @@ func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Se
120120
// Pick an API key
121121
randomApiKey = subscraping.PickRandom(s.apiKeys, s.Name())
122122

123-
resp, err = session.HTTPRequest(ctx, http.MethodPost, apiUrl, "", map[string]string{
123+
resp2, err := session.HTTPRequest(ctx, http.MethodPost, apiUrl, "", map[string]string{
124124
"accept": "application/json",
125125
"X-API-Key": randomApiKey,
126126
"Content-Type": "application/json"}, strings.NewReader(string(jsonRequestBody)), subscraping.BasicAuth{})
@@ -130,20 +130,20 @@ func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Se
130130
return
131131
}
132132
defer func() {
133-
if err := resp.Body.Close(); err != nil {
133+
if err := resp2.Body.Close(); err != nil {
134134
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
135135
s.errors++
136136
}
137137
}()
138-
body, err = io.ReadAll(resp.Body)
138+
body, err = io.ReadAll(resp2.Body)
139139
if err != nil {
140140
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("error reading ressponse body")}
141141
s.errors++
142142
return
143143
}
144144

145-
if resp.StatusCode == 429 {
146-
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("request rate limited with status code %d", resp.StatusCode)}
145+
if resp2.StatusCode == 429 {
146+
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("request rate limited with status code %d", resp2.StatusCode)}
147147
s.errors++
148148
return
149149
}

0 commit comments

Comments
 (0)