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

Code before:

        time.sleep(1)

        if res['code'] == '429':
            self.error("You are being rate-limited by ThreatJammer.com")
            self.errorState = True
            return None

        if res['code'] != "200":
            self.error("Error retrieving search results from ThreatJammer.com")
            self.errorState = True
            return None

        if res['content'] is None:
            self.error("Received no content from ThreatJammer.com")
            self.errorState = True
            return None

        try:
            return json.loads(res['content'])
        except Exception as e:
            self.debug(f"Error processing JSON response: {e}")
            return None

        return None

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

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

        if self.opts["api_key"] == "":
            self.error(
                f"You enabled {self.__class__.__name__} but did not set an API key!"
            )
            self.errorState = True
            return

        if eventData in self.results:
            self.debug(f"Skipping {eventData}, already checked.")
            return

        self.results[eventData] = True

        if eventName.startswith("AFFILIATE") and not self.opts['checkaffiliates']:
            return

        if eventName in ['IP_ADDRESS', 'IPV6_ADDRESS']:
            blacklist_type = "BLACKLISTED_IPADDR"

Review comment (suggestion):
Does the server reply with an error when the provided API key is invalid (401 / 403) or quota exceeded ? If so, this function should handle the appropriate code explicitly and set `errorState` with an appropriate error, prior to presuming all non-200 codes are fatal.


Code after:
            timeout=self.opts['_fetchtimeout'],
            useragent=self.opts['_useragent'],
            headers=headers
        )

        time.sleep(1)

        if res['code'] == '400':
            self.error("ThreatJammer.com rejected the IP address. Use only public IP addresses.")
            return None

        if res['code'] == '422':
            self.error("ThreatJammer.com could not process the IP address. Check the format.")
            return None

        if res['code'] == '429':
            self.error("You are being rate-limited by ThreatJammer.com")
            self.errorState = True
            return None

        if res['code'] == '401':
            self.error("You are not authorized by ThreatJammer.com. Check your API key.")
            self.errorState = True
            return None

        if res['code'] != "200":
            self.error("ThreatJammer.com could not process the IP address. Unknown error.")
            self.errorState = True
            return None

        if res['content'] is None:
            self.error("Received no content from ThreatJammer.com")
            self.errorState = True
            return None

        try:
            return json.loads(res['content'])
        except Exception as e:
            self.debug(f"Error processing JSON response: {e}")

        return None

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

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

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