socketdev 3.4.2__py3-none-any.whl → 3.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ from typing import Any, Dict, List, Optional, Union
5
5
  from dataclasses import dataclass, asdict, field
6
6
  import urllib.parse
7
7
  from ..core.dedupe import Dedupe
8
+ from ..core.enums import unknown_enum_value
8
9
  from ..utils import IntegrationType, Utils
9
10
 
10
11
  log = logging.getLogger("socketdev")
@@ -12,9 +13,12 @@ log = logging.getLogger("socketdev")
12
13
 
13
14
  class SocketPURL_Type(str, Enum):
14
15
  UNKNOWN = "unknown"
16
+ ALPM = "alpm"
15
17
  APK = "apk"
16
18
  BITBUCKET = "bitbucket"
17
19
  CARGO = "cargo"
20
+ CHROME = "chrome"
21
+ CLAWHUB = "clawhub"
18
22
  COCOAPODS = "cocoapods"
19
23
  COMPOSER = "composer"
20
24
  CONAN = "conan"
@@ -22,6 +26,8 @@ class SocketPURL_Type(str, Enum):
22
26
  CRAN = "cran"
23
27
  DEB = "deb"
24
28
  DOCKER = "docker"
29
+ EDGE_EXTENSION = "edge-extension"
30
+ FIREFOX_EXTENSION = "firefox-extension"
25
31
  GEM = "gem"
26
32
  GENERIC = "generic"
27
33
  GITHUB = "github"
@@ -37,21 +43,17 @@ class SocketPURL_Type(str, Enum):
37
43
  OCI = "oci"
38
44
  PUB = "pub"
39
45
  PYPI = "pypi"
46
+ QPKG = "qpkg"
40
47
  RPM = "rpm"
48
+ SOCKET = "socket"
49
+ SWID = "swid"
41
50
  SWIFT = "swift"
51
+ VSCODE = "vscode"
52
+ VSCODE_EXTENSION = "vscode-extension"
42
53
 
43
54
  @classmethod
44
55
  def _missing_(cls, value):
45
- # The API can emit purl types this SDK does not know about yet. Fall
46
- # back to UNKNOWN instead of raising so one artifact cannot fail an
47
- # entire response parse (same forward-compat approach as
48
- # SocketCategory, https://github.com/SocketDev/socket-sdk-python/issues/78).
49
- log.warning(
50
- "Unknown SocketPURL_Type %r; falling back to UNKNOWN. "
51
- "Upgrade socketdev to pick up newer purl types.",
52
- value,
53
- )
54
- return cls.UNKNOWN
56
+ return unknown_enum_value(cls.__name__, value, cls.UNKNOWN)
55
57
 
56
58
 
57
59
  class SocketIssueSeverity(str, Enum):
@@ -59,6 +61,15 @@ class SocketIssueSeverity(str, Enum):
59
61
  MIDDLE = "middle"
60
62
  HIGH = "high"
61
63
  CRITICAL = "critical"
64
+ # Sentinel for severities the API adds later. Deliberately not an existing
65
+ # level: falling back to LOW would hide a serious finding and CRITICAL
66
+ # would manufacture one, so consumers get an explicit "not understood"
67
+ # value to branch on instead of a guess.
68
+ UNKNOWN = "unknown"
69
+
70
+ @classmethod
71
+ def _missing_(cls, value):
72
+ return unknown_enum_value(cls.__name__, value, cls.UNKNOWN)
62
73
 
63
74
 
64
75
  class SocketCategory(str, Enum):
@@ -70,6 +81,10 @@ class SocketCategory(str, Enum):
70
81
  MISCELLANEOUS = "miscellaneous"
71
82
  OTHER = "other" # Added to match backend API responses
72
83
 
84
+ @classmethod
85
+ def _missing_(cls, value):
86
+ return unknown_enum_value(cls.__name__, value, cls.MISCELLANEOUS)
87
+
73
88
 
74
89
  class DiffType(str, Enum):
75
90
  ADDED = "added"
@@ -77,9 +92,21 @@ class DiffType(str, Enum):
77
92
  UNCHANGED = "unchanged"
78
93
  REPLACED = "replaced"
79
94
  UPDATED = "updated"
95
+ # Not UNCHANGED: treating an unrecognized change as "nothing happened"
96
+ # would drop a real diff entry out of a comparison silently.
97
+ UNKNOWN = "unknown"
98
+
99
+ @classmethod
100
+ def _missing_(cls, value):
101
+ return unknown_enum_value(cls.__name__, value, cls.UNKNOWN)
80
102
 
81
103
 
82
104
  class ScanType(str, Enum):
105
+ # Deliberately strict, unlike the response-parsed enums in this module.
106
+ # ScanType only ever travels outbound: FullScanParams.to_dict() is
107
+ # urlencoded onto the create-scan query string, so an unrecognized value is
108
+ # the caller's typo, not API drift. Coercing it to a fallback would send
109
+ # scan_type=unknown to the API instead of failing at construction.
83
110
  SOCKET = "socket"
84
111
  SOCKET_TIER1 = "socket_tier1"
85
112
  SOCKET_BASICS = "socket_basics"
@@ -481,15 +508,9 @@ class SocketAlert:
481
508
 
482
509
  @classmethod
483
510
  def from_dict(cls, data: dict) -> "SocketAlert":
484
- try:
485
- category = SocketCategory(data["category"])
486
- except ValueError:
487
- log.warning(
488
- "Unknown SocketCategory %r; falling back to MISCELLANEOUS. "
489
- "Upgrade socketdev to pick up newer categories.",
490
- data["category"],
491
- )
492
- category = SocketCategory.MISCELLANEOUS
511
+ # SocketCategory._missing_ handles unrecognized values; see
512
+ # socketdev/core/enums.py.
513
+ category = SocketCategory(data["category"])
493
514
  return cls(
494
515
  key=data["key"],
495
516
  type=data["type"],
socketdev/npm/__init__.py CHANGED
@@ -1,28 +1,28 @@
1
- import logging
2
-
3
- log = logging.getLogger("socketdev")
4
-
5
- # TODO: Add response type classes for NPM endpoints
6
-
7
-
8
- class NPM:
9
- def __init__(self, api):
10
- self.api = api
11
-
12
- def issues(self, package: str, version: str) -> list:
13
- path = f"npm/{package}/{version}/issues"
14
- response = self.api.do_request(path=path)
15
- if response.status_code == 200:
16
- return response.json()
17
- log.error(f"Error getting npm issues: {response.status_code}")
18
- log.error(response.text)
19
- return []
20
-
21
- def score(self, package: str, version: str) -> list:
22
- path = f"npm/{package}/{version}/score"
23
- response = self.api.do_request(path=path)
24
- if response.status_code == 200:
25
- return response.json()
26
- log.error(f"Error getting npm score: {response.status_code}")
27
- log.error(response.text)
28
- return []
1
+ import logging
2
+
3
+ log = logging.getLogger("socketdev")
4
+
5
+ # TODO: Add response type classes for NPM endpoints
6
+
7
+
8
+ class NPM:
9
+ def __init__(self, api):
10
+ self.api = api
11
+
12
+ def issues(self, package: str, version: str) -> list:
13
+ path = f"npm/{package}/{version}/issues"
14
+ response = self.api.do_request(path=path)
15
+ if response.status_code == 200:
16
+ return response.json()
17
+ log.error(f"Error getting npm issues: {response.status_code}")
18
+ log.error(response.text)
19
+ return []
20
+
21
+ def score(self, package: str, version: str) -> list:
22
+ path = f"npm/{package}/{version}/score"
23
+ response = self.api.do_request(path=path)
24
+ if response.status_code == 200:
25
+ return response.json()
26
+ log.error(f"Error getting npm score: {response.status_code}")
27
+ log.error(response.text)
28
+ return []
@@ -1,19 +1,19 @@
1
- import logging
2
-
3
- log = logging.getLogger("socketdev")
4
-
5
- # TODO: Add response type classes for OpenAPI endpoints
6
-
7
-
8
- class OpenAPI:
9
- def __init__(self, api):
10
- self.api = api
11
-
12
- def get(self) -> dict:
13
- path = "openapi"
14
- response = self.api.do_request(path=path)
15
- if response.status_code == 200:
16
- return response.json()
17
- log.error(f"Error getting OpenAPI spec: {response.status_code}")
18
- log.error(response.text)
19
- return {}
1
+ import logging
2
+
3
+ log = logging.getLogger("socketdev")
4
+
5
+ # TODO: Add response type classes for OpenAPI endpoints
6
+
7
+
8
+ class OpenAPI:
9
+ def __init__(self, api):
10
+ self.api = api
11
+
12
+ def get(self) -> dict:
13
+ path = "openapi"
14
+ response = self.api.do_request(path=path)
15
+ if response.status_code == 200:
16
+ return response.json()
17
+ log.error(f"Error getting OpenAPI spec: {response.status_code}")
18
+ log.error(response.text)
19
+ return {}
socketdev/org/__init__.py CHANGED
@@ -1,34 +1,34 @@
1
- from typing import TypedDict, Dict
2
- import logging
3
-
4
- log = logging.getLogger("socketdev")
5
-
6
-
7
- class Organization(TypedDict):
8
- id: str
9
- name: str
10
- image: str
11
- plan: str
12
- slug: str
13
-
14
-
15
- class OrganizationsResponse(TypedDict):
16
- organizations: Dict[str, Organization]
17
- # Add other fields from the response if needed
18
-
19
-
20
- class Orgs:
21
- def __init__(self, api):
22
- self.api = api
23
-
24
- def get(self, use_types: bool = False) -> OrganizationsResponse:
25
- path = "organizations"
26
- response = self.api.do_request(path=path)
27
- if response.status_code == 200:
28
- result = response.json()
29
- if use_types:
30
- return OrganizationsResponse(result)
31
- return result
32
- log.error(f"Error getting organizations: {response.status_code}")
33
- log.error(response.text)
34
- return {"organizations": {}}
1
+ from typing import TypedDict, Dict
2
+ import logging
3
+
4
+ log = logging.getLogger("socketdev")
5
+
6
+
7
+ class Organization(TypedDict):
8
+ id: str
9
+ name: str
10
+ image: str
11
+ plan: str
12
+ slug: str
13
+
14
+
15
+ class OrganizationsResponse(TypedDict):
16
+ organizations: Dict[str, Organization]
17
+ # Add other fields from the response if needed
18
+
19
+
20
+ class Orgs:
21
+ def __init__(self, api):
22
+ self.api = api
23
+
24
+ def get(self, use_types: bool = False) -> OrganizationsResponse:
25
+ path = "organizations"
26
+ response = self.api.do_request(path=path)
27
+ if response.status_code == 200:
28
+ result = response.json()
29
+ if use_types:
30
+ return OrganizationsResponse(result)
31
+ return result
32
+ log.error(f"Error getting organizations: {response.status_code}")
33
+ log.error(response.text)
34
+ return {"organizations": {}}
@@ -1,19 +1,19 @@
1
- import logging
2
-
3
- log = logging.getLogger("socketdev")
4
-
5
- # TODO: Add response type classes for Quota endpoints
6
-
7
-
8
- class Quota:
9
- def __init__(self, api):
10
- self.api = api
11
-
12
- def get(self) -> dict:
13
- path = "quota"
14
- response = self.api.do_request(path=path)
15
- if response.status_code == 200:
16
- return response.json()
17
- log.error(f"Error getting quota: {response.status_code}")
18
- log.error(response.text)
19
- return {}
1
+ import logging
2
+
3
+ log = logging.getLogger("socketdev")
4
+
5
+ # TODO: Add response type classes for Quota endpoints
6
+
7
+
8
+ class Quota:
9
+ def __init__(self, api):
10
+ self.api = api
11
+
12
+ def get(self) -> dict:
13
+ path = "quota"
14
+ response = self.api.do_request(path=path)
15
+ if response.status_code == 200:
16
+ return response.json()
17
+ log.error(f"Error getting quota: {response.status_code}")
18
+ log.error(response.text)
19
+ return {}
@@ -1,87 +1,87 @@
1
- import logging
2
- from datetime import datetime, timedelta, timezone
3
-
4
- log = logging.getLogger("socketdev")
5
-
6
- # TODO: Add response type classes for Report endpoints
7
-
8
-
9
- class Report:
10
- def __init__(self, api):
11
- self.api = api
12
-
13
- def list(self, from_time: int = None) -> dict:
14
- """
15
- This function will return all reports from time specified.
16
- :param from_time: Unix epoch time in seconds. Will default self, to 30 days
17
- """
18
- if from_time is None:
19
- from_time = int((datetime.now(timezone.utc) - timedelta(days=30)).timestamp())
20
- else:
21
- from_time = int((datetime.now(timezone.utc) - timedelta(seconds=from_time)).timestamp())
22
-
23
- path = "report/list"
24
- if from_time is not None:
25
- path += f"?from={from_time}"
26
- response = self.api.do_request(path=path)
27
- if response.status_code == 200:
28
- return response.json()
29
- log.error(f"Error listing reports: {response.status_code}")
30
- log.error(response.text)
31
- return {}
32
-
33
- def delete(self, report_id: str) -> bool:
34
- path = f"report/delete/{report_id}"
35
- response = self.api.do_request(path=path, method="DELETE")
36
- if response.status_code == 200:
37
- return True
38
- log.error(f"Error deleting report: {response.status_code}")
39
- log.error(response.text)
40
- return False
41
-
42
- def view(self, report_id) -> dict:
43
- path = f"report/view/{report_id}"
44
- response = self.api.do_request(path=path)
45
- if response.status_code == 200:
46
- return response.json()
47
- log.error(f"Error viewing report: {response.status_code}")
48
- log.error(response.text)
49
- return {}
50
-
51
- def supported(self) -> dict:
52
- path = "report/supported"
53
- response = self.api.do_request(path=path)
54
- if response.status_code == 200:
55
- return response.json()
56
- log.error(f"Error getting supported reports: {response.status_code}")
57
- log.error(response.text)
58
- return {}
59
-
60
- def create(self, files: list) -> dict:
61
- # Handle both file path strings and file tuples
62
- open_files = []
63
- for file_entry in files:
64
- if isinstance(file_entry, tuple) and len(file_entry) == 2:
65
- name, file_data = file_entry
66
- if isinstance(file_data, tuple) and len(file_data) == 2:
67
- # Format: [("field_name", ("filename", file_obj))]
68
- filename, file_obj = file_data
69
- file_info = (name, (filename, file_obj, "text/plain"))
70
- open_files.append(file_info)
71
- else:
72
- # Format: [("field_name", "file_path")]
73
- file_info = (name, (name, open(file_data, "rb"), "text/plain"))
74
- open_files.append(file_info)
75
- else:
76
- # Handle other formats if needed
77
- log.error(f"Unexpected file format: {file_entry}")
78
- return {}
79
-
80
- path = "report/upload"
81
- payload = {}
82
- response = self.api.do_request(path=path, method="PUT", files=open_files, payload=payload)
83
- if response.status_code in (200, 201):
84
- return response.json()
85
- log.error(f"Error creating report: {response.status_code}")
86
- log.error(response.text)
87
- return {}
1
+ import logging
2
+ from datetime import datetime, timedelta, timezone
3
+
4
+ log = logging.getLogger("socketdev")
5
+
6
+ # TODO: Add response type classes for Report endpoints
7
+
8
+
9
+ class Report:
10
+ def __init__(self, api):
11
+ self.api = api
12
+
13
+ def list(self, from_time: int = None) -> dict:
14
+ """
15
+ This function will return all reports from time specified.
16
+ :param from_time: Unix epoch time in seconds. Will default self, to 30 days
17
+ """
18
+ if from_time is None:
19
+ from_time = int((datetime.now(timezone.utc) - timedelta(days=30)).timestamp())
20
+ else:
21
+ from_time = int((datetime.now(timezone.utc) - timedelta(seconds=from_time)).timestamp())
22
+
23
+ path = "report/list"
24
+ if from_time is not None:
25
+ path += f"?from={from_time}"
26
+ response = self.api.do_request(path=path)
27
+ if response.status_code == 200:
28
+ return response.json()
29
+ log.error(f"Error listing reports: {response.status_code}")
30
+ log.error(response.text)
31
+ return {}
32
+
33
+ def delete(self, report_id: str) -> bool:
34
+ path = f"report/delete/{report_id}"
35
+ response = self.api.do_request(path=path, method="DELETE")
36
+ if response.status_code == 200:
37
+ return True
38
+ log.error(f"Error deleting report: {response.status_code}")
39
+ log.error(response.text)
40
+ return False
41
+
42
+ def view(self, report_id) -> dict:
43
+ path = f"report/view/{report_id}"
44
+ response = self.api.do_request(path=path)
45
+ if response.status_code == 200:
46
+ return response.json()
47
+ log.error(f"Error viewing report: {response.status_code}")
48
+ log.error(response.text)
49
+ return {}
50
+
51
+ def supported(self) -> dict:
52
+ path = "report/supported"
53
+ response = self.api.do_request(path=path)
54
+ if response.status_code == 200:
55
+ return response.json()
56
+ log.error(f"Error getting supported reports: {response.status_code}")
57
+ log.error(response.text)
58
+ return {}
59
+
60
+ def create(self, files: list) -> dict:
61
+ # Handle both file path strings and file tuples
62
+ open_files = []
63
+ for file_entry in files:
64
+ if isinstance(file_entry, tuple) and len(file_entry) == 2:
65
+ name, file_data = file_entry
66
+ if isinstance(file_data, tuple) and len(file_data) == 2:
67
+ # Format: [("field_name", ("filename", file_obj))]
68
+ filename, file_obj = file_data
69
+ file_info = (name, (filename, file_obj, "text/plain"))
70
+ open_files.append(file_info)
71
+ else:
72
+ # Format: [("field_name", "file_path")]
73
+ file_info = (name, (name, open(file_data, "rb"), "text/plain"))
74
+ open_files.append(file_info)
75
+ else:
76
+ # Handle other formats if needed
77
+ log.error(f"Unexpected file format: {file_entry}")
78
+ return {}
79
+
80
+ path = "report/upload"
81
+ payload = {}
82
+ response = self.api.do_request(path=path, method="PUT", files=open_files, payload=payload)
83
+ if response.status_code in (200, 201):
84
+ return response.json()
85
+ log.error(f"Error creating report: {response.status_code}")
86
+ log.error(response.text)
87
+ return {}
@@ -1,31 +1,31 @@
1
- from typing import TypedDict, Union
2
- import logging
3
-
4
- log = logging.getLogger("socketdev")
5
-
6
-
7
- class Repo(TypedDict):
8
- name: str
9
- description: str
10
- homepage: str
11
- visibility: str
12
- archived: bool
13
- default_branch: str
14
-
15
-
16
- class Repositories:
17
- def __init__(self, api):
18
- self.api = api
19
-
20
- def list(self, use_types: bool = False) -> Union[dict, list[Repo]]:
21
- path = "repos"
22
- response = self.api.do_request(path=path)
23
- if response.status_code == 200:
24
- result = response.json()
25
- if use_types:
26
- return [Repo(repo) for repo in result]
27
- return result
28
-
29
- log.error(f"Error listing repositories: {response.status_code}")
30
- log.error(response.text)
31
- return []
1
+ from typing import TypedDict, Union
2
+ import logging
3
+
4
+ log = logging.getLogger("socketdev")
5
+
6
+
7
+ class Repo(TypedDict):
8
+ name: str
9
+ description: str
10
+ homepage: str
11
+ visibility: str
12
+ archived: bool
13
+ default_branch: str
14
+
15
+
16
+ class Repositories:
17
+ def __init__(self, api):
18
+ self.api = api
19
+
20
+ def list(self, use_types: bool = False) -> Union[dict, list[Repo]]:
21
+ path = "repos"
22
+ response = self.api.do_request(path=path)
23
+ if response.status_code == 200:
24
+ result = response.json()
25
+ if use_types:
26
+ return [Repo(repo) for repo in result]
27
+ return result
28
+
29
+ log.error(f"Error listing repositories: {response.status_code}")
30
+ log.error(response.text)
31
+ return []