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

Code before:

        headers = {
            'Accept': "application/json",
            'X-API-KEY': self.opts['api_key']
        }

        res = self.sf.fetchUrl(
            queryString,
            headers=headers,
            timeout=15,
            useragent=self.opts['_useragent']
        )

        return json.loads(res['content'])

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

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

        if self.errorState:
            return None

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

        # Don't look up stuff twice
        if eventData in self.results:
            self.sf.debug(f"Skipping {eventData}, already checked.")
            return None
        else:
            self.results[eventData] = True

        if eventName == "IP_ADDRESS" or eventName == "IPV6_ADDRESS":
            data = self.query(eventData, eventName)
            if data is None:
                return None

            resultSet = data.get('data')
            if resultSet:
                if resultSet.get('score') >= self.opts['fraud_threshold']:
                    maliciousDesc = f"SEON [{eventData}]\n - FRAUD SCORE: {resultSet.get('score')}"
                    evt = SpiderFootEvent("MALICIOUS_IPADDR", maliciousDesc, self.__name__, event)
                    self.notifyListeners(evt)

                    if resultSet.get('tor'):

Review comment (suggestion):
Should have the regular 404, 429, etc. checks here before returning and setting `errorState` if appropriate.

Code after:

        headers = {
            'Accept': "application/json",
            'X-API-KEY': self.opts['api_key']
        }

        res = self.sf.fetchUrl(
            queryString,
            headers=headers,
            timeout=15,
            useragent=self.opts['_useragent']
        )

        if res['code'] == '429':
            self.sf.error("You are being rate-limited by seon.io")
            return None

        if res['code'] != "200":
            self.sf.error("Error retrieving search results from seon.io")
            return None

        if res['code'] == '404':
            self.sf.error("API Endpoint not found")
            return None

        return json.loads(res['content'])

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

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

        if self.errorState:
            return None

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

        # Don't look up stuff twice
        if eventData in self.results:
            self.sf.debug(f"Skipping {eventData}, already checked.")
            return None
        else:
            self.results[eventData] = True

        dataFound = False