-
Notifications
You must be signed in to change notification settings - Fork 40
fix: harden VirusTotal error handling, logging, and rate-limit accounting (t147.3) #468
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -38,25 +38,25 @@ readonly NC='\033[0m' | |||||
|
|
||||||
| log_info() { | ||||||
| local msg="$1" | ||||||
| echo -e "${BLUE}[virustotal]${NC} ${msg}" | ||||||
| echo -e "${BLUE}[virustotal]${NC} ${msg}" >&2 | ||||||
| return 0 | ||||||
| } | ||||||
|
|
||||||
| log_success() { | ||||||
| local msg="$1" | ||||||
| echo -e "${GREEN}[OK]${NC} ${msg}" | ||||||
| echo -e "${GREEN}[OK]${NC} ${msg}" >&2 | ||||||
| return 0 | ||||||
| } | ||||||
|
|
||||||
| log_warning() { | ||||||
| local msg="$1" | ||||||
| echo -e "${YELLOW}[WARN]${NC} ${msg}" | ||||||
| echo -e "${YELLOW}[WARN]${NC} ${msg}" >&2 | ||||||
| return 0 | ||||||
| } | ||||||
|
|
||||||
| log_error() { | ||||||
| local msg="$1" | ||||||
| echo -e "${RED}[ERROR]${NC} ${msg}" | ||||||
| echo -e "${RED}[ERROR]${NC} ${msg}" >&2 | ||||||
| return 0 | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -173,12 +173,16 @@ vt_request() { | |||||
| } | ||||||
|
|
||||||
| # Check for API errors | ||||||
| # Exit codes: 0=success, 1=general error, 2=not found (allows callers to distinguish) | ||||||
| local error_code="" | ||||||
| error_code=$(echo "$response" | jq -r '.error.code // empty' 2>/dev/null || echo "") | ||||||
| if [[ -n "$error_code" ]]; then | ||||||
| local error_msg="" | ||||||
| error_msg=$(echo "$response" | jq -r '.error.message // "Unknown error"' 2>/dev/null || echo "Unknown error") | ||||||
| log_error "VT API error: ${error_code} - ${error_msg}" | ||||||
| if [[ "$error_code" == "NotFoundError" ]]; then | ||||||
| return 2 | ||||||
| fi | ||||||
| return 1 | ||||||
| fi | ||||||
|
|
||||||
|
|
@@ -253,8 +257,11 @@ cmd_scan_file() { | |||||
| fi | ||||||
|
|
||||||
| local response="" | ||||||
| response=$(vt_request "GET" "/files/${sha256}") || { | ||||||
| # File not in VT database -- this is normal for text/markdown files | ||||||
| local vt_exit=0 | ||||||
| response=$(vt_request "GET" "/files/${sha256}") || vt_exit=$? | ||||||
|
|
||||||
| if [[ $vt_exit -eq 2 ]]; then | ||||||
| # NotFoundError: file not in VT database -- normal for text/markdown files | ||||||
| if [[ "$quiet" == "true" ]]; then | ||||||
| echo "UNKNOWN" | ||||||
| else | ||||||
|
|
@@ -263,7 +270,15 @@ cmd_scan_file() { | |||||
| fi | ||||||
| echo "UNKNOWN|File not in VT database" | ||||||
| return 0 | ||||||
| } | ||||||
| elif [[ $vt_exit -ne 0 ]]; then | ||||||
| # Real API error (quota exceeded, network failure, etc.) | ||||||
| if [[ "$quiet" == "true" ]]; then | ||||||
| echo "UNKNOWN" | ||||||
| else | ||||||
| log_error "API request failed for file: ${file}" | ||||||
| fi | ||||||
| return 1 | ||||||
| fi | ||||||
|
|
||||||
| if [[ "$output_json" == "true" ]]; then | ||||||
| echo "$response" | ||||||
|
|
@@ -502,15 +517,21 @@ cmd_scan_skill() { | |||||
| sha256=$(file_sha256 "$file") || continue | ||||||
|
|
||||||
| local response="" | ||||||
| response=$(vt_request "GET" "/files/${sha256}" 2>/dev/null) || { | ||||||
| # Not in VT database -- normal for text files | ||||||
| local vt_exit=0 | ||||||
| response=$(vt_request "GET" "/files/${sha256}" 2>/dev/null) || vt_exit=$? | ||||||
| request_count=$((request_count + 1)) | ||||||
|
|
||||||
| if [[ $vt_exit -ne 0 ]]; then | ||||||
| if [[ "$quiet" != "true" ]]; then | ||||||
| echo -e " ${BLUE}SKIP${NC} ${basename_file} (not in VT database)" | ||||||
| if [[ $vt_exit -eq 2 ]]; then | ||||||
| echo -e " ${BLUE}SKIP${NC} ${basename_file} (not in VT database)" | ||||||
| else | ||||||
| echo -e " ${YELLOW}SKIP${NC} ${basename_file} (API error)" | ||||||
| fi | ||||||
| fi | ||||||
| unknown_count=$((unknown_count + 1)) | ||||||
| continue | ||||||
| } | ||||||
| request_count=$((request_count + 1)) | ||||||
| fi | ||||||
|
|
||||||
| local verdict="" | ||||||
| verdict=$(parse_verdict "$response") | ||||||
|
|
@@ -551,28 +572,20 @@ cmd_scan_skill() { | |||||
| done | ||||||
|
|
||||||
| # Phase 2: Extract and scan URLs from skill content | ||||||
| local -A domains_seen=() | ||||||
| for file in "${files[@]}"; do | ||||||
| # Extract URLs (http/https) from file content | ||||||
| while IFS= read -r url; do | ||||||
| # Skip common safe domains | ||||||
| case "$url" in | ||||||
| *github.com*|*githubusercontent.com*|*npmjs.com*|*pypi.org*|*docs.virustotal.com*) | ||||||
| continue | ||||||
| ;; | ||||||
| esac | ||||||
| urls_found+=("$url") | ||||||
|
|
||||||
| # Extract domain | ||||||
| local domain="" | ||||||
| domain=$(echo "$url" | sed -E 's|https?://([^/]+).*|\1|') | ||||||
| local already_found=false | ||||||
| for d in "${domains_found[@]+"${domains_found[@]}"}"; do | ||||||
| if [[ "$d" == "$domain" ]]; then | ||||||
| already_found=true | ||||||
| break | ||||||
| fi | ||||||
| done | ||||||
| if [[ "$already_found" == "false" ]]; then | ||||||
| if [[ -z "${domains_seen[$domain]+x}" ]]; then | ||||||
| domains_seen[$domain]=1 | ||||||
| domains_found+=("$domain") | ||||||
| fi | ||||||
| done < <(grep -oE 'https?://[^ "'"'"'<>]+' "$file" 2>/dev/null | sort -u || true) | ||||||
|
|
@@ -594,13 +607,16 @@ cmd_scan_skill() { | |||||
| rate_limit_wait | ||||||
|
|
||||||
| local response="" | ||||||
| response=$(vt_request "GET" "/domains/${domain}" 2>/dev/null) || { | ||||||
| local vt_exit=0 | ||||||
| response=$(vt_request "GET" "/domains/${domain}" 2>/dev/null) || vt_exit=$? | ||||||
|
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. Similar to the file scan, the
Suggested change
|
||||||
| request_count=$((request_count + 1)) | ||||||
|
|
||||||
| if [[ $vt_exit -ne 0 ]]; then | ||||||
| if [[ "$quiet" != "true" ]]; then | ||||||
| echo -e " ${BLUE}SKIP${NC} ${domain} (lookup failed)" | ||||||
| fi | ||||||
| continue | ||||||
| } | ||||||
| request_count=$((request_count + 1)) | ||||||
| fi | ||||||
|
|
||||||
| local verdict="" | ||||||
| verdict=$(parse_verdict "$response") | ||||||
|
|
@@ -748,6 +764,14 @@ main() { | |||||
| local command="${1:-help}" | ||||||
| shift || true | ||||||
|
|
||||||
| # Verify required dependencies (jq is needed for all scan commands) | ||||||
| if [[ "$command" != "help" && "$command" != "--help" && "$command" != "-h" ]]; then | ||||||
| if ! command -v jq &>/dev/null; then | ||||||
| log_error "jq is required but not installed (brew install jq)" | ||||||
| return 1 | ||||||
| fi | ||||||
| fi | ||||||
|
|
||||||
| # Parse global flags | ||||||
| local output_json=false | ||||||
| local quiet=false | ||||||
|
|
||||||
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.
The
2>/dev/nullhere suppresses the detailed API error messages thatvt_requestlogs to stderr. Since a major goal of this PR is to improve logging and error handling, it would be better to let these errors be displayed. This would provide more context when an API error occurs, rather than just the generic "API error" message printed later. The caller can still choose to suppress stderr if needed.