Skip to content

Commit 309c493

Browse files
author
strongHunter
committed
Added error logging to typosquatting
1 parent fd69918 commit 309c493

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

guarddog/analyzer/metadata/go/typosquatting.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import json
2+
import logging
23
import os
34
from typing import Optional
45

56
from guarddog.analyzer.metadata.typosquatting import TyposquatDetector
67
from guarddog.utils.config import TOP_PACKAGES_CACHE_LOCATION
78

9+
log = logging.getLogger("guarddog")
10+
811

912
class GoTyposquatDetector(TyposquatDetector):
1013
"""Detector for typosquatting attacks for go modules. Checks for distance one Levenshtein,
@@ -38,8 +41,8 @@ def _get_top_packages_local(self, path: str) -> list[dict]:
3841
with open(path, "r") as f:
3942
result = json.load(f)
4043
return result
41-
except FileNotFoundError as e:
42-
pass # TODO: log error
44+
except FileNotFoundError:
45+
log.error(f"File not found: {path}")
4346

4447
def detect(
4548
self,

guarddog/analyzer/metadata/npm/typosquatting.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
import os
34
from datetime import datetime, timedelta
45
from typing import Optional
@@ -7,6 +8,8 @@
78
from guarddog.utils.config import TOP_PACKAGES_CACHE_LOCATION
89
import requests
910

11+
log = logging.getLogger("guarddog")
12+
1013

1114
class NPMTyposquatDetector(TyposquatDetector):
1215
"""Detector for typosquatting attacks. Detects if a package name is a typosquat of one of the top 5000 packages.
@@ -56,8 +59,8 @@ def _get_top_packages_local(self, path: str) -> list[dict]:
5659
with open(path, "r") as f:
5760
result = json.load(f)
5861
return result
59-
except FileNotFoundError as e:
60-
pass # TODO: log error
62+
except FileNotFoundError:
63+
log.error(f"File not found: {path}")
6164

6265
def _get_top_packages_network(self, url: tuple[str]) -> list[dict]:
6366
try:
@@ -68,10 +71,10 @@ def _get_top_packages_network(self, url: tuple[str]) -> list[dict]:
6871
result = list([i["name"] for i in response_data[0:8000]])
6972

7073
return result
71-
except json.JSONDecodeError as e:
72-
pass # TODO: log error
74+
except json.JSONDecodeError:
75+
log.error(f"Couldn`t convert to json: \"{response.text}\"")
7376
except requests.exceptions.RequestException as e:
74-
pass # TODO: log error
77+
log.error(f"Network error: {e}")
7578

7679
def detect(
7780
self,

guarddog/analyzer/metadata/pypi/typosquatting.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def _get_top_packages_local(self, path: str) -> list[dict]:
7878
with open(path, "r") as f:
7979
result = json.load(f)
8080
return result
81-
except FileNotFoundError as e:
82-
pass # TODO: log error
81+
except FileNotFoundError:
82+
log.error(f"File not found: {path}")
8383

8484
def _get_top_packages_network(self, url: tuple[str]) -> list[dict]:
8585
try:
@@ -90,10 +90,10 @@ def _get_top_packages_network(self, url: tuple[str]) -> list[dict]:
9090
result = response_data
9191

9292
return result
93-
except json.JSONDecodeError as e:
94-
pass # TODO: log error
93+
except json.JSONDecodeError:
94+
log.error(f"Couldn`t convert to json: \"{response.text}\"")
9595
except requests.exceptions.RequestException as e:
96-
pass # TODO: log error
96+
log.error(f"Network error: {e}")
9797

9898
def detect(self, package_info, path: Optional[str] = None, name: Optional[str] = None,
9999
version: Optional[str] = None) -> tuple[bool, Optional[str]]:

0 commit comments

Comments
 (0)