File size: 2,595 Bytes
07e1348
 
d5030c9
65494f9
07e1348
 
 
 
 
d5030c9
 
07e1348
 
 
 
d5030c9
07e1348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5030c9
 
 
 
 
 
07e1348
 
 
 
 
 
 
d5030c9
 
 
 
 
 
 
 
 
 
07e1348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5030c9
 
65494f9
d5030c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
import json
import os

SAFE_BROWSING_API_KEY = os.getenv("GOOGLE_SAFE_BROWSING_API_KEY")
SAFE_BROWSING_URL = "https://safebrowsing.googleapis.com/v4/threatMatches:find"

def check_urls_with_google_safebrowsing(urls):
    """
    Debug-enabled version:
    - Prints payload and raw response to help troubleshoot Safe Browsing issues.
    Returns a dict {url: bool is_malicious}.
    """
    result = {}
    if not SAFE_BROWSING_API_KEY:
        print("No GOOGLE_SAFE_BROWSING_API_KEY found. Returning all URLs as safe.")
        for u in urls:
            result[u] = False
        return result

    # Build threatEntries for each URL
    threat_entries = [{"url": u} for u in urls]

    payload = {
        "client": {
            "clientId": "my-smishing-detector",
            "clientVersion": "1.0"
        },
        "threatInfo": {
            "threatTypes": [
                "MALWARE",
                "SOCIAL_ENGINEERING",
                "UNWANTED_SOFTWARE",
                "POTENTIALLY_HARMFUL_APPLICATION"
            ],
            "platformTypes": ["ANY_PLATFORM"],
            "threatEntryTypes": ["URL"],
            "threatEntries": threat_entries
        }
    }

    print("---- Safe Browsing Debug ----")
    print("REQUEST Payload (JSON):")
    print(json.dumps(payload, indent=2))
    print("REQUEST Endpoint:", SAFE_BROWSING_URL, "Key:", SAFE_BROWSING_API_KEY)
    print("URLs being checked:", urls)

    try:
        resp = requests.post(
            SAFE_BROWSING_URL,
            params={"key": SAFE_BROWSING_API_KEY},
            json=payload,
            timeout=10
        )

        print("RESPONSE Status Code:", resp.status_code)
        try:
            data = resp.json()
            print("RESPONSE JSON:")
            print(json.dumps(data, indent=2))
        except Exception as parse_error:
            print("Error parsing response as JSON:", parse_error)
            data = {}

        # If "matches" is present, some URL is flagged
        malicious_urls = set()
        if "matches" in data:
            for match in data["matches"]:
                threat_url = match.get("threat", {}).get("url")
                if threat_url:
                    malicious_urls.add(threat_url)

        for u in urls:
            result[u] = (u in malicious_urls)

    except Exception as e:
        print(f"Error contacting Safe Browsing API: {e}")
        # default: everything safe if error
        for u in urls:
            result[u] = False

    print("RESULTS (url -> malicious):", result)
    print("---- End Debug ----\n")

    return result