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

Code before:

        return ret

    # Handle events sent to this module
    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}")

        # 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

        data = self.query(eventData)
        if data is not None:
            for n in data:
                try:
                    if not self.opts['api_key']:
                        site = n["Title"]
                    else:
                        site = n["Name"]
                except Exception as e:
                    self.sf.debug(f"Unable to parse result from HaveIBeenPwned?: {e}")
                    continue

                evt = eventName + "_COMPROMISED"
                # Notify other modules of what you've found
                e = SpiderFootEvent(evt, eventData + " [" + site + "]",
                                    self.__name__, event)
                self.notifyListeners(e)

        pasteData = self.queryPaste(eventData)
        if pasteData is None:
            return None

        sites = {
            "Pastebin": "https://pastebin.com/",
            "Pastie": "http://pastie.org/",
            "Slexy": "https://slexy.org/",
            "Ghostbin": "https://ghostbin.com/",
            "QuickLeak": "http://www.quickleak.net/",
            "JustPaste": "https://justpaste.it/",
        }

Review comment (suggestion):
Different fields depending on whether an API key is used or not? Isn't an API key always required for HIBP? This logic is probably legacy, for back when HIBP had two modes of operation (API key or no API key), but that isn't supported anymore AFAIK.

Code after:

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

        if self.errorState:
            return None

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

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

        # 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

        data = self.query(eventData)
        if data is not None:
            for n in data:
                try:
                    site = n["Name"]
                except Exception as e:
                    self.sf.debug(f"Unable to parse result from HaveIBeenPwned?: {e}")
                    continue

                # Notify other modules of what you've found
                e = SpiderFootEvent("EMAILADDR_COMPROMISED", eventData + " [" + site + "]",
                                    self.__name__, event)
                self.notifyListeners(e)

        pasteData = self.queryPaste(eventData)
        if pasteData is None:
            return None

        sites = {
            "Pastebin": "https://pastebin.com/",
            "Pastie": "http://pastie.org/p/",
            "Slexy": "https://slexy.org/view/",
            "Ghostbin": "https://ghostbin.com/paste/",
            "JustPaste": "https://justpaste.it/",
        }
        links = set()
        for n in pasteData: