Skip to content

Commit 92c3eff

Browse files
committed
feat: Add allow_unverified option to disable SSL certificate verification
- Add allow_unverified parameter to socketdev constructor (defaults to False) - Add set_allow_unverified method to API class - Pass verify=not allow_unverified to requests.request() calls - Add comprehensive unit tests for the new functionality - Update README.rst with documentation for the new parameter - Maintains backward compatibility with existing code This allows users to disable SSL verification for testing environments with self-signed certificates while keeping secure defaults for production.
1 parent 29bfa3a commit 92c3eff

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Initializing the module
2020
**PARAMETERS:**
2121

2222
- **token (str)** - The Socket API Key for your Organization
23-
- **Timeout (int)** - The number of seconds to wait before failing the connection
23+
- **timeout (int)** - The number of seconds to wait before failing the connection
24+
- **allow_unverified (bool)** - Whether to skip SSL certificate verification (default: False). Set to True for testing with self-signed certificates.
2425

2526
Supported Functions
2627
-------------------

socketdev/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@
4444

4545

4646
class socketdev:
47-
def __init__(self, token: str, timeout: int = 1200):
47+
def __init__(self, token: str, timeout: int = 1200, allow_unverified: bool = False):
4848
self.api = API()
4949
self.token = token + ":"
5050
self.api.encode_key(self.token)
5151
self.api.set_timeout(timeout)
52+
self.api.set_allow_unverified(allow_unverified)
5253

5354
self.dependencies = Dependencies(self.api)
5455
self.export = Export(self.api)

socketdev/core/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ def __init__(self):
2525
self.encoded_key = None
2626
self.api_url = "https://api.socket.dev/v0"
2727
self.request_timeout = 30
28+
self.allow_unverified = False
2829

2930
def encode_key(self, token: str):
3031
self.encoded_key = base64.b64encode(token.encode()).decode("ascii")
3132

3233
def set_timeout(self, timeout: int):
3334
self.request_timeout = timeout
3435

36+
def set_allow_unverified(self, allow_unverified: bool):
37+
self.allow_unverified = allow_unverified
38+
3539
def do_request(
3640
self,
3741
path: str,
@@ -58,7 +62,8 @@ def format_headers(headers_dict):
5862
try:
5963

6064
response = requests.request(
61-
method.upper(), url, headers=headers, data=payload, files=files, timeout=self.request_timeout
65+
method.upper(), url, headers=headers, data=payload, files=files,
66+
timeout=self.request_timeout, verify=not self.allow_unverified
6267
)
6368
request_duration = time.time() - start_time
6469

socketdev/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.17"
1+
__version__ = "3.0.18"

test_allow_unverified.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script to demonstrate the new allow_unverified option in socketdev.
4+
5+
This script shows how to initialize the Socket SDK with SSL verification
6+
disabled, which can be useful for testing against local or self-signed
7+
certificate environments.
8+
"""
9+
10+
from socketdev import socketdev
11+
12+
def test_allow_unverified_option():
13+
"""Test the allow_unverified option with different configurations."""
14+
15+
print("Testing Socket SDK with allow_unverified option...")
16+
17+
# Test 1: Default behavior (SSL verification enabled)
18+
print("\n1. Default initialization (allow_unverified=False):")
19+
sdk_default = socketdev(token="test-token")
20+
print(f" allow_unverified: {sdk_default.api.allow_unverified}")
21+
print(f" This means SSL certificates WILL be verified")
22+
23+
# Test 2: Explicitly set allow_unverified=False
24+
print("\n2. Explicit allow_unverified=False:")
25+
sdk_verified = socketdev(token="test-token", allow_unverified=False)
26+
print(f" allow_unverified: {sdk_verified.api.allow_unverified}")
27+
print(f" This means SSL certificates WILL be verified")
28+
29+
# Test 3: Set allow_unverified=True
30+
print("\n3. Setting allow_unverified=True:")
31+
sdk_unverified = socketdev(token="test-token", allow_unverified=True)
32+
print(f" allow_unverified: {sdk_unverified.api.allow_unverified}")
33+
print(f" This means SSL certificates will NOT be verified")
34+
35+
# Test 4: Show how this affects the requests library verify parameter
36+
print("\n4. How this translates to requests.request() verify parameter:")
37+
print(f" Default SDK: verify={not sdk_default.api.allow_unverified}")
38+
print(f" Unverified SDK: verify={not sdk_unverified.api.allow_unverified}")
39+
40+
print("\nUsage example:")
41+
print(" # For production use (default):")
42+
print(" sdk = socketdev(token='your-api-key')")
43+
print("")
44+
print(" # For testing with self-signed certificates:")
45+
print(" sdk = socketdev(token='your-api-key', allow_unverified=True)")
46+
47+
if __name__ == "__main__":
48+
test_allow_unverified_option()

tests/unit/test_socket_sdk_unit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ def test_sdk_initialization(self):
3636
for component in expected_components:
3737
self.assertTrue(hasattr(sdk, component), f"SDK missing component: {component}")
3838

39+
def test_sdk_initialization_with_allow_unverified(self):
40+
"""Test that the SDK initializes correctly with allow_unverified option."""
41+
# Test default behavior (allow_unverified=False)
42+
sdk_default = socketdev(token="test-token")
43+
self.assertFalse(sdk_default.api.allow_unverified)
44+
45+
# Test with allow_unverified=True
46+
sdk_unverified = socketdev(token="test-token", allow_unverified=True)
47+
self.assertTrue(sdk_unverified.api.allow_unverified)
48+
49+
# Test with explicit allow_unverified=False
50+
sdk_verified = socketdev(token="test-token", allow_unverified=False)
51+
self.assertFalse(sdk_verified.api.allow_unverified)
52+
3953
def test_fullscan_params_creation(self):
4054
"""Test FullScanParams dataclass creation and conversion."""
4155
params = FullScanParams(

0 commit comments

Comments
 (0)