Skip to content

Commit 5b02ccb

Browse files
authored
Merge pull request #79 from zmap/phillip/78-module-err-behavior
Prevent modules from returning a nil result
2 parents a1d72e0 + 7ef9657 commit 5b02ccb

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎annotate.go‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/csv"
2020
"fmt"
2121
"os/signal"
22+
"reflect"
2223
"slices"
2324
"syscall"
2425
"time"
@@ -246,6 +247,19 @@ func AnnotateWrite(path string, out <-chan string, wg *sync.WaitGroup) {
246247
log.Debug("write thread finished")
247248
}
248249

250+
func isPtrNil(i any) bool {
251+
if i == nil {
252+
return true
253+
}
254+
val := reflect.ValueOf(i)
255+
switch val.Kind() {
256+
case reflect.Pointer, reflect.Interface, reflect.Slice, reflect.Map:
257+
return val.IsNil()
258+
default:
259+
return false
260+
}
261+
}
262+
249263
func AnnotateWorker(conf *GlobalConf, a Annotator, inChan <-chan inProcessIP,
250264
outChan chan<- inProcessIP, fieldName string, wg *sync.WaitGroup, i int) {
251265
name := a.GetFieldName()
@@ -257,9 +271,18 @@ func AnnotateWorker(conf *GlobalConf, a Annotator, inChan <-chan inProcessIP,
257271
for inProcess := range inChan {
258272
if fieldName != "" && slices.Contains([]string{"json", "csv"}, conf.InputFileType) {
259273
p := inProcess.Out[fieldName].(map[string]interface{})
260-
p[name] = a.Annotate(inProcess.Ip)
274+
res := a.Annotate(inProcess.Ip)
275+
if isPtrNil(res) {
276+
res = struct{}{} // Don't return null, breaks downstream JSON parsing
277+
}
278+
p[name] = res
279+
261280
} else {
262-
inProcess.Out[name] = a.Annotate(inProcess.Ip)
281+
res := a.Annotate(inProcess.Ip)
282+
if isPtrNil(res) {
283+
res = struct{}{} // Don't return null, breaks downstream JSON parsing
284+
}
285+
inProcess.Out[name] = res
263286
}
264287
outChan <- inProcess
265288
}

‎censys.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ var censysAPIHostLookupURL = "https://api.platform.censys.io/v3/global/asset/hos
8383
// Annotate performs a Censys host lookup for the given IP address and returns the results.
8484
// If an error occurs or a lookup fails, it returns nil
8585
func (a *CensysAnnotator) Annotate(ip net.IP) interface{} {
86-
8786
req, err := http.NewRequest("GET", censysAPIHostLookupURL+ip.String(), nil)
8887
if err != nil {
8988
// If we can't even form a request, we'll fail to enrich anything. Erroring out.

0 commit comments

Comments
 (0)