Repository: smicallef/spiderfoot
File: modules/sfp_dnsdb.py (Python)

Code before:
        if res["code"] == "429":
            self.sf.error("You are being rate-limited by DNSDB", False)
            self.errorState = True
            return None

        if res["content"] is None:
            self.sf.info(f"No DNSDB record found for {query}")
            return None

        splittedContent = res["content"].strip().split("\n")
        if len(splittedContent) == 2:
            self.sf.info(f"No DNSDB record found for {query}")
            return None

        try:
            records = []
            for content in splittedContent:
                records.append(json.loads(content))
        except Exception as e:
            self.sf.error(f"Error processing JSON response from DNSDB: {e}", False)
            return None

        return records[1:-1]

    def emit(self, etype, data, pevent, notify=True):
        evt = SpiderFootEvent(etype, data, self.__name__, pevent)
        if notify:
            self.notifyListeners(evt)
        return evt

    def isTooOld(self, lastSeen):
        ageLimitTs = int(time.time()) - (86400 * self.opts["age_limit_days"])
        if self.opts["age_limit_days"] > 0 and lastSeen < ageLimitTs:
            self.sf.debug("Record found but too old, skipping.")
            return True
        return False

    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

        if self.errorState:
            return None

        self.sf.debug(f"Received event, {eventName}, from {srcModuleName}")

        if self.opts["api_key"] == "":
            self.sf.error("You enabled sfp_dnsdb but did not set an API key!", False)
            self.errorState = True
            return None

Review comment (bug):
Any likelihood of an out of bounds error here if the length of `splittedContent` isn't as expected?

Code after:
        if res["code"] == "429":
            self.sf.error("You are being rate-limited by DNSDB", False)
            self.errorState = True
            return None

        if res["content"] is None:
            self.sf.info(f"No DNSDB record found for {query}")
            return None

        splittedContent = res["content"].strip().split("\n")
        if len(splittedContent) == 2:
            self.sf.info(f"No DNSDB record found for {query}")
            return None
        elif len(splittedContent) < 2:
            self.sf.info(f"Unexpected DNSDB response {query}")
            return None

        try:
            records = []
            for content in splittedContent:
                records.append(json.loads(content))
        except json.JSONDecodeError as e:
            self.sf.error(f"Error processing JSON response from DNSDB: {e}", False)
            return None

        return records[1:-1]

    def emit(self, etype, data, pevent, notify=True):
        evt = SpiderFootEvent(etype, data, self.__name__, pevent)
        if notify:
            self.notifyListeners(evt)
        return evt

    def isTooOld(self, lastSeen):
        ageLimitTs = int(time.time()) - (86400 * self.opts["age_limit_days"])
        if self.opts["age_limit_days"] > 0 and lastSeen < ageLimitTs:
            self.sf.debug("Record found but too old, skipping.")
            return True
        return False

    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

        if self.errorState:
            return None

        self.sf.debug(f"Received event, {eventName}, from {srcModuleName}")

        if self.opts["api_key"] == "":