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

Code before:

        res = self.sf.fetchUrl(
            f"https://{self.opts['api_hostname']}/v1/assess/ip/{ip}",
            timeout=self.opts['_fetchtimeout'],
            useragent=self.opts['_useragent'],
            headers=headers
        )

        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'] == '401':
            self.error("You are not authorized by ThreatJammer.com. Check your API key.")
            self.errorState = True
            return None

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

        if res['code'] == '422':
            self.error("ThreatJammer.com could not process the IP address. Check the format.")
            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

Review comment (bug):
Note that `errorState` should only be used in cases where the module should no longer be invoked for other events. It's used in cases where the upstream or module should no longer be called due to some kind of critical failure.

Code after:

        res = self.sf.fetchUrl(
            f"https://{self.opts['api_hostname']}/v1/assess/ip/{ip}",
            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}")