generated from github/codespaces-react
-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Integrate Google Safe Browsing API #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| requests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import re | ||
| import urllib.request | ||
| import requests | ||
| import os | ||
| from urllib.parse import urlparse | ||
| from .heuristics import ( | ||
| URGENCY_KEYWORDS, | ||
|
|
@@ -17,6 +19,41 @@ | |
| SUSPICIOUS_URL_PATTERNS | ||
| ) | ||
|
|
||
| def check_google_safe_browsing(url, api_key): | ||
| """ | ||
| Checks a URL against the Google Safe Browsing API. | ||
| Returns a tuple: (is_suspicious, reason) | ||
| """ | ||
| if not api_key: | ||
| return False, "Google Safe Browsing API key not configured." | ||
|
|
||
| api_url = f"https://safebrowsing.googleapis.com/v4/threatMatches:find?key={api_key}" | ||
| payload = { | ||
| "client": { | ||
| "clientId": "social-media-analyzer", | ||
| "clientVersion": "1.0.0" | ||
| }, | ||
| "threatInfo": { | ||
| "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING", "UNWANTED_SOFTWARE", "POTENTIALLY_HARMFUL_APPLICATION"], | ||
| "platformTypes": ["ANY_PLATFORM"], | ||
| "threatEntryTypes": ["URL"], | ||
| "threatEntries": [{"url": url}] | ||
| } | ||
| } | ||
| try: | ||
| response = requests.post(api_url, json=payload, timeout=10) | ||
| if response.status_code == 200: | ||
| data = response.json() | ||
| if "matches" in data: | ||
| threat_type = data["matches"][0]["threatType"] | ||
| return True, f"Flagged by Google Safe Browsing as {threat_type}." | ||
| else: | ||
| return False, "Clean according to Google Safe Browsing." | ||
| else: | ||
| return False, f"Google Safe Browsing API error: {response.status_code}" | ||
| except requests.RequestException as e: | ||
| return False, f"Could not connect to Google Safe Browsing: {e}" | ||
|
|
||
| def get_legitimate_domains(platform=None): | ||
| """ | ||
| Returns a list of legitimate domains for a given platform, | ||
|
|
@@ -35,16 +72,24 @@ def get_domain_from_url(url): | |
| domain = url.split("/")[0].split("?")[0] | ||
| return domain.lower() | ||
|
|
||
| def is_url_suspicious(url, platform=None): | ||
| def is_url_suspicious(url, platform=None, api_key=None): | ||
| """ | ||
| Checks if a URL is suspicious based on various patterns and lists. | ||
| Checks if a URL is suspicious based on various patterns and lists, | ||
| including Google Safe Browsing. | ||
| Returns a tuple: (bool_is_suspicious, reason_string) | ||
| """ | ||
| # 1. Google Safe Browsing Check | ||
| if api_key: | ||
| is_susp, reason = check_google_safe_browsing(url, api_key) | ||
| if is_susp: | ||
| return True, reason | ||
|
|
||
| # 2. Local Heuristics | ||
| normalized_url = url.lower() | ||
| domain = get_domain_from_url(url) | ||
| legitimate_domains = get_legitimate_domains(platform) | ||
|
|
||
| # 1. Check if the domain is in the legitimate list for the platform | ||
| # Check if the domain is in the legitimate list for the platform | ||
| if domain in legitimate_domains: | ||
| # Still check for impersonation patterns that might include the legit domain | ||
| for pattern in SUSPICIOUS_URL_PATTERNS: | ||
|
|
@@ -53,24 +98,24 @@ def is_url_suspicious(url, platform=None): | |
| return True, f"URL impersonates a legitimate domain: {pattern}" | ||
| return False, "URL domain is on the legitimate list." | ||
|
|
||
| # 2. Check against known suspicious patterns | ||
| # Check against known suspicious patterns | ||
| for pattern in SUSPICIOUS_URL_PATTERNS: | ||
| if re.search(pattern, normalized_url, re.IGNORECASE): | ||
| return True, f"URL matches suspicious pattern: {pattern}" | ||
|
|
||
| # 3. Check for suspicious TLDs | ||
| # Check for suspicious TLDs | ||
| suspicious_tld_regex = re.compile(r"\.(" + "|".join(tld.lstrip('.') for tld in SUSPICIOUS_TLDS) + r")$", re.IGNORECASE) | ||
| if suspicious_tld_regex.search(domain): | ||
| return True, f"URL uses a potentially suspicious TLD." | ||
|
|
||
| # 4. Check if a known legitimate service name is part of the domain, but it's not official | ||
| # Check if a known legitimate service name is part of the domain, but it's not official | ||
| for service in LEGITIMATE_DOMAINS.keys(): | ||
| if service != "general" and service in domain: | ||
| return True, f"URL contains the name of a legitimate service ('{service}') but is not an official domain." | ||
|
|
||
| return False, "URL does not match common suspicious patterns." | ||
|
|
||
| def analyze_text_for_scams(text_content, platform=None): | ||
| def analyze_text_for_scams(text_content, platform=None, api_key=None): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (code-quality): Low code quality found in analyze_text_for_scams - 25% ( ExplanationThe quality score for this function is below the quality threshold of 25%.This score is a combination of the method length, cognitive complexity and working memory. How can you solve this? It might be worth refactoring this function to make it shorter and more readable.
|
||
| """ | ||
| Analyzes a block of text content for various scam indicators. | ||
| """ | ||
|
|
@@ -103,10 +148,14 @@ def analyze_text_for_scams(text_content, platform=None): | |
| # 2. Regex-based checks | ||
| found_urls = URL_PATTERN.findall(text_content) | ||
| for url_str in found_urls: | ||
| is_susp, reason = is_url_suspicious(url_str, platform) | ||
| is_susp, reason = is_url_suspicious(url_str, platform, api_key) | ||
| url_analysis = {"url": url_str, "is_suspicious": is_susp, "reason": reason} | ||
| if is_susp: | ||
| score += HEURISTIC_WEIGHTS.get("SUSPICIOUS_URL_PATTERN", 3.0) | ||
| # Increase score significantly if flagged by Google | ||
| if "Google Safe Browsing" in reason: | ||
| score += HEURISTIC_WEIGHTS.get("GOOGLE_SAFE_BROWSING_HIT", 10.0) | ||
| else: | ||
| score += HEURISTIC_WEIGHTS.get("SUSPICIOUS_URL_PATTERN", 3.0) | ||
| indicators_found.append(f"Suspicious URL found: {url_str} (Reason: {reason})") | ||
| urls_analyzed_details.append(url_analysis) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
use-named-expression)low-code-quality)Explanation
The quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.
How can you solve this?
It might be worth refactoring this function to make it shorter and more readable.
their own functions. This is the most important thing you can do - ideally a
function should be less than 10 lines.
sits together within the function rather than being scattered.