fix(security): Resolve remaining code scanning alerts
This commit is contained in:
@@ -47,17 +47,19 @@ def check_env_vars() -> tuple[bool, list[str]]:
|
||||
return len(missing) == 0, missing
|
||||
|
||||
|
||||
def _format_api_failure(response: httpx.Response) -> str:
|
||||
"""Return a sanitized API failure message without echoing sensitive payloads."""
|
||||
def _format_api_failure(response: httpx.Response) -> dict[str, str]:
|
||||
"""Return sanitized API failure details without echoing sensitive payloads."""
|
||||
try:
|
||||
error = response.json().get("error", {})
|
||||
except ValueError:
|
||||
error = {}
|
||||
error_code = error.get("code", "?")
|
||||
return f"API request failed (status {response.status_code}, code {error_code})."
|
||||
return {
|
||||
"status_code": str(response.status_code),
|
||||
"error_code": str(error.get("code", "?")),
|
||||
}
|
||||
|
||||
|
||||
def test_api_connection() -> tuple[bool, str]:
|
||||
def test_api_connection() -> tuple[bool, dict[str, str]]:
|
||||
"""Test connection to WhatsApp Cloud API."""
|
||||
token = os.environ.get("WHATSAPP_TOKEN", "")
|
||||
phone_id = os.environ.get("PHONE_NUMBER_ID", "")
|
||||
@@ -72,24 +74,26 @@ def test_api_connection() -> tuple[bool, str]:
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
return True, (
|
||||
f"Phone: {data.get('display_phone_number', 'N/A')}\n"
|
||||
f" Name: {data.get('verified_name', 'N/A')}\n"
|
||||
f" Status: {data.get('code_verification_status', 'N/A')}\n"
|
||||
f" Quality: {data.get('quality_rating', 'N/A')}"
|
||||
)
|
||||
else:
|
||||
return False, _format_api_failure(response)
|
||||
return True, {
|
||||
"phone": str(data.get("display_phone_number", "N/A")),
|
||||
"name": str(data.get("verified_name", "N/A")),
|
||||
"status": str(data.get("code_verification_status", "N/A")),
|
||||
"quality": str(data.get("quality_rating", "N/A")),
|
||||
}
|
||||
|
||||
return False, _format_api_failure(response)
|
||||
|
||||
except httpx.ConnectError:
|
||||
return False, "Connection failed. Check your internet connection."
|
||||
return False, {"reason": "Connection failed. Check your internet connection."}
|
||||
except httpx.TimeoutException:
|
||||
return False, "Request timed out after 10 seconds."
|
||||
return False, {"reason": "Request timed out after 10 seconds."}
|
||||
except Exception as exc:
|
||||
return False, f"Unexpected {exc.__class__.__name__} while contacting the Graph API."
|
||||
return False, {
|
||||
"reason": f"Unexpected {exc.__class__.__name__} while contacting the Graph API."
|
||||
}
|
||||
|
||||
|
||||
def test_waba_access() -> tuple[bool, str]:
|
||||
def test_waba_access() -> tuple[bool, dict[str, str]]:
|
||||
"""Test access to WhatsApp Business Account."""
|
||||
token = os.environ.get("WHATSAPP_TOKEN", "")
|
||||
waba_id = os.environ.get("WABA_ID", "")
|
||||
@@ -104,12 +108,14 @@ def test_waba_access() -> tuple[bool, str]:
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
count = len(data.get("data", []))
|
||||
return True, f"WABA accessible. {count} phone number(s) found."
|
||||
else:
|
||||
return False, _format_api_failure(response)
|
||||
return True, {"count": str(count)}
|
||||
|
||||
return False, _format_api_failure(response)
|
||||
|
||||
except Exception as exc:
|
||||
return False, f"Unexpected {exc.__class__.__name__} while checking WABA access."
|
||||
return False, {
|
||||
"reason": f"Unexpected {exc.__class__.__name__} while checking WABA access."
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
@@ -151,23 +157,37 @@ def main():
|
||||
|
||||
# Check 2: API connection
|
||||
print("[2/3] Testing API connection (Phone Number)...")
|
||||
api_ok, api_msg = test_api_connection()
|
||||
api_ok, api_details = test_api_connection()
|
||||
if api_ok:
|
||||
print(f" OK - Connected successfully")
|
||||
print(f" {api_msg}")
|
||||
print(" OK - Connected successfully")
|
||||
print(f" Phone: {api_details['phone']}")
|
||||
print(f" Name: {api_details['name']}")
|
||||
print(f" Status: {api_details['status']}")
|
||||
print(f" Quality: {api_details['quality']}")
|
||||
else:
|
||||
print(f" FAIL - {api_msg}")
|
||||
if "reason" in api_details:
|
||||
print(f" FAIL - {api_details['reason']}")
|
||||
else:
|
||||
print(" FAIL - API request failed.")
|
||||
print(f" HTTP Status: {api_details['status_code']}")
|
||||
print(f" Error Code: {api_details['error_code']}")
|
||||
all_ok = False
|
||||
|
||||
print()
|
||||
|
||||
# Check 3: WABA access
|
||||
print("[3/3] Testing WABA access...")
|
||||
waba_ok, waba_msg = test_waba_access()
|
||||
waba_ok, waba_details = test_waba_access()
|
||||
if waba_ok:
|
||||
print(f" OK - {waba_msg}")
|
||||
print(" OK - WABA accessible")
|
||||
print(f" Phone Numbers Found: {waba_details['count']}")
|
||||
else:
|
||||
print(f" FAIL - {waba_msg}")
|
||||
if "reason" in waba_details:
|
||||
print(f" FAIL - {waba_details['reason']}")
|
||||
else:
|
||||
print(" FAIL - API request failed.")
|
||||
print(f" HTTP Status: {waba_details['status_code']}")
|
||||
print(f" Error Code: {waba_details['error_code']}")
|
||||
all_ok = False
|
||||
|
||||
print()
|
||||
|
||||
@@ -47,17 +47,19 @@ def check_env_vars() -> tuple[bool, list[str]]:
|
||||
return len(missing) == 0, missing
|
||||
|
||||
|
||||
def _format_api_failure(response: httpx.Response) -> str:
|
||||
"""Return a sanitized API failure message without echoing sensitive payloads."""
|
||||
def _format_api_failure(response: httpx.Response) -> dict[str, str]:
|
||||
"""Return sanitized API failure details without echoing sensitive payloads."""
|
||||
try:
|
||||
error = response.json().get("error", {})
|
||||
except ValueError:
|
||||
error = {}
|
||||
error_code = error.get("code", "?")
|
||||
return f"API request failed (status {response.status_code}, code {error_code})."
|
||||
return {
|
||||
"status_code": str(response.status_code),
|
||||
"error_code": str(error.get("code", "?")),
|
||||
}
|
||||
|
||||
|
||||
def test_api_connection() -> tuple[bool, str]:
|
||||
def test_api_connection() -> tuple[bool, dict[str, str]]:
|
||||
"""Test connection to WhatsApp Cloud API."""
|
||||
token = os.environ.get("WHATSAPP_TOKEN", "")
|
||||
phone_id = os.environ.get("PHONE_NUMBER_ID", "")
|
||||
@@ -72,24 +74,26 @@ def test_api_connection() -> tuple[bool, str]:
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
return True, (
|
||||
f"Phone: {data.get('display_phone_number', 'N/A')}\n"
|
||||
f" Name: {data.get('verified_name', 'N/A')}\n"
|
||||
f" Status: {data.get('code_verification_status', 'N/A')}\n"
|
||||
f" Quality: {data.get('quality_rating', 'N/A')}"
|
||||
)
|
||||
else:
|
||||
return False, _format_api_failure(response)
|
||||
return True, {
|
||||
"phone": str(data.get("display_phone_number", "N/A")),
|
||||
"name": str(data.get("verified_name", "N/A")),
|
||||
"status": str(data.get("code_verification_status", "N/A")),
|
||||
"quality": str(data.get("quality_rating", "N/A")),
|
||||
}
|
||||
|
||||
return False, _format_api_failure(response)
|
||||
|
||||
except httpx.ConnectError:
|
||||
return False, "Connection failed. Check your internet connection."
|
||||
return False, {"reason": "Connection failed. Check your internet connection."}
|
||||
except httpx.TimeoutException:
|
||||
return False, "Request timed out after 10 seconds."
|
||||
return False, {"reason": "Request timed out after 10 seconds."}
|
||||
except Exception as exc:
|
||||
return False, f"Unexpected {exc.__class__.__name__} while contacting the Graph API."
|
||||
return False, {
|
||||
"reason": f"Unexpected {exc.__class__.__name__} while contacting the Graph API."
|
||||
}
|
||||
|
||||
|
||||
def test_waba_access() -> tuple[bool, str]:
|
||||
def test_waba_access() -> tuple[bool, dict[str, str]]:
|
||||
"""Test access to WhatsApp Business Account."""
|
||||
token = os.environ.get("WHATSAPP_TOKEN", "")
|
||||
waba_id = os.environ.get("WABA_ID", "")
|
||||
@@ -104,12 +108,14 @@ def test_waba_access() -> tuple[bool, str]:
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
count = len(data.get("data", []))
|
||||
return True, f"WABA accessible. {count} phone number(s) found."
|
||||
else:
|
||||
return False, _format_api_failure(response)
|
||||
return True, {"count": str(count)}
|
||||
|
||||
return False, _format_api_failure(response)
|
||||
|
||||
except Exception as exc:
|
||||
return False, f"Unexpected {exc.__class__.__name__} while checking WABA access."
|
||||
return False, {
|
||||
"reason": f"Unexpected {exc.__class__.__name__} while checking WABA access."
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
@@ -151,23 +157,37 @@ def main():
|
||||
|
||||
# Check 2: API connection
|
||||
print("[2/3] Testing API connection (Phone Number)...")
|
||||
api_ok, api_msg = test_api_connection()
|
||||
api_ok, api_details = test_api_connection()
|
||||
if api_ok:
|
||||
print(f" OK - Connected successfully")
|
||||
print(f" {api_msg}")
|
||||
print(" OK - Connected successfully")
|
||||
print(f" Phone: {api_details['phone']}")
|
||||
print(f" Name: {api_details['name']}")
|
||||
print(f" Status: {api_details['status']}")
|
||||
print(f" Quality: {api_details['quality']}")
|
||||
else:
|
||||
print(f" FAIL - {api_msg}")
|
||||
if "reason" in api_details:
|
||||
print(f" FAIL - {api_details['reason']}")
|
||||
else:
|
||||
print(" FAIL - API request failed.")
|
||||
print(f" HTTP Status: {api_details['status_code']}")
|
||||
print(f" Error Code: {api_details['error_code']}")
|
||||
all_ok = False
|
||||
|
||||
print()
|
||||
|
||||
# Check 3: WABA access
|
||||
print("[3/3] Testing WABA access...")
|
||||
waba_ok, waba_msg = test_waba_access()
|
||||
waba_ok, waba_details = test_waba_access()
|
||||
if waba_ok:
|
||||
print(f" OK - {waba_msg}")
|
||||
print(" OK - WABA accessible")
|
||||
print(f" Phone Numbers Found: {waba_details['count']}")
|
||||
else:
|
||||
print(f" FAIL - {waba_msg}")
|
||||
if "reason" in waba_details:
|
||||
print(f" FAIL - {waba_details['reason']}")
|
||||
else:
|
||||
print(" FAIL - API request failed.")
|
||||
print(f" HTTP Status: {waba_details['status_code']}")
|
||||
print(f" Error Code: {waba_details['error_code']}")
|
||||
all_ok = False
|
||||
|
||||
print()
|
||||
|
||||
Reference in New Issue
Block a user