Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /github_app /webhook_handler.py
| """ | |
| GitHub Webhook Handler für Zen-AI-Pentest. | |
| Verarbeitet GitHub Webhook Events für PR Scans und Push Events. | |
| """ | |
| import asyncio | |
| import hashlib | |
| import hmac | |
| import logging | |
| from dataclasses import dataclass | |
| from typing import Any, Optional | |
| from checks import ( | |
| CheckRun, | |
| CheckRunConclusion, | |
| CheckRunManager, | |
| CheckRunOutput, | |
| CheckRunStatus, | |
| ) | |
| logger = logging.getLogger(__name__) | |
| class WebhookEvent: | |
| """Repräsentiert ein GitHub Webhook Event.""" | |
| event_type: str | |
| delivery_id: str | |
| payload: dict[str, Any] | |
| signature_valid: bool = True | |
| def repository(self) -> dict[str, Any]: | |
| """Hole Repository Informationen.""" | |
| return self.payload.get("repository", {}) | |
| def owner(self) -> str: | |
| """Hole Repository Owner.""" | |
| repo = self.repository | |
| return repo.get("owner", {}).get("login", "") | |
| def repo_name(self) -> str: | |
| """Hole Repository Name.""" | |
| return self.repository.get("name", "") | |
| def installation_id(self) -> Optional[int]: | |
| """Hole Installation ID.""" | |
| return self.payload.get("installation", {}).get("id") | |
| class WebhookVerifier: | |
| """Verifiziert GitHub Webhook Signaturen.""" | |
| def __init__(self, secret: Optional[str]): | |
| self.secret = secret.encode() if secret else None | |
| def verify(self, payload: bytes, signature: str) -> bool: | |
| """Verifiziere die Webhook Signatur.""" | |
| if not self.secret: | |
| logger.warning("Webhook Secret nicht konfiguriert - Signatur wird nicht geprüft") | |
| return True | |
| if not signature.startswith("sha256="): | |
| return False | |
| expected = hmac.new(self.secret, payload, hashlib.sha256).hexdigest() | |
| provided = signature[7:] # Entferne "sha256=" | |
| return hmac.compare_digest(provided, expected) | |
| class ZenConfigLoader: | |
| """Lädt Zen-AI-Pentest Konfiguration aus Repository.""" | |
| DEFAULT_CONFIG = { | |
| "version": 1, | |
| "scans": [ | |
| { | |
| "name": "Default Web Security Scan", | |
| "target": "${{ env.APP_URL }}", | |
| "type": "web", | |
| "policy": "safe", | |
| "fail_on": "high", | |
| } | |
| ], | |
| "notifications": {"pr_comments": True, "checks": True}, | |
| } | |
| def __init__(self, token: str, owner: str, repo: str): | |
| self.token = token | |
| self.owner = owner | |
| self.repo = repo | |
| async def load_config(self, ref: str = "HEAD") -> dict[str, Any]: | |
| """Lade Konfiguration aus .github/zen-pentest.yml.""" | |
| import httpx | |
| url = f"https://api.github.com/repos/{self.owner}/{self.repo}/contents/.github/zen-pentest.yml" | |
| headers = {"Authorization": f"Bearer {self.token}", "Accept": "application/vnd.github.v3.raw"} | |
| params = {"ref": ref} | |
| try: | |
| async with httpx.AsyncClient() as client: | |
| response = await client.get(url, headers=headers, params=params) | |
| if response.status_code == 404: | |
| logger.info("Keine zen-pentest.yml gefunden, nutze Default-Konfiguration") | |
| return self.DEFAULT_CONFIG.copy() | |
| response.raise_for_status() | |
| import yaml | |
| config = yaml.safe_load(response.text) | |
| logger.info(f"Konfiguration geladen: {len(config.get('scans', []))} Scans definiert") | |
| return config | |
| except Exception as e: | |
| logger.warning(f"Fehler beim Laden der Konfiguration: {e}") | |
| return self.DEFAULT_CONFIG.copy() | |
| class GitHubAppClient: | |
| """GitHub App API Client mit JWT Authentifizierung.""" | |
| def __init__(self, app_id: str, private_key: str): | |
| self.app_id = app_id | |
| self.private_key = private_key | |
| self._token_cache: dict[int, tuple[str, float]] = {} # installation_id -> (token, expiry) | |
| def _generate_jwt(self) -> str: | |
| """Generiere JWT für GitHub App Authentifizierung.""" | |
| import time | |
| try: | |
| from github import GithubIntegration | |
| # Wenn PyGithub verfügbar, nutze es | |
| integration = GithubIntegration(self.app_id, self.private_key) | |
| return integration.create_jwt() | |
| except ImportError: | |
| # Fallback: Manuelle JWT Erstellung | |
| import jwt | |
| now = int(time.time()) | |
| payload = {"iat": now, "exp": now + 600, "iss": self.app_id} # 10 Minuten | |
| return jwt.encode(payload, self.private_key, algorithm="RS256") | |
| async def get_installation_token(self, installation_id: int) -> str: | |
| """Hole oder generiere ein Installation Access Token.""" | |
| import time | |
| import httpx | |
| # Prüfe Cache | |
| if installation_id in self._token_cache: | |
| token, expiry = self._token_cache[installation_id] | |
| if time.time() < expiry - 60: # 1 Minute Puffer | |
| return token | |
| jwt_token = self._generate_jwt() | |
| url = f"https://api.github.com/app/installations/{installation_id}/access_tokens" | |
| headers = {"Authorization": f"Bearer {jwt_token}", "Accept": "application/vnd.github.v3+json"} | |
| async with httpx.AsyncClient() as client: | |
| response = await client.post(url, headers=headers) | |
| response.raise_for_status() | |
| data = response.json() | |
| token = data["token"] | |
| expires_at = data.get("expires_at") | |
| # Parse Expiry | |
| if expires_at: | |
| from datetime import datetime | |
| expiry = datetime.fromisoformat(expires_at.replace("Z", "+00:00")).timestamp() | |
| else: | |
| expiry = time.time() + 3600 # 1 Stunde Default | |
| self._token_cache[installation_id] = (token, expiry) | |
| return token | |
| class PRCommentManager: | |
| """Verwaltet PR Comments für Security Findings.""" | |
| def __init__(self, token: str, owner: str, repo: str): | |
| self.token = token | |
| self.owner = owner | |
| self.repo = repo | |
| self.headers = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github.v3+json"} | |
| async def create_or_update_comment(self, pr_number: int, scan_results: list[dict], scan_config: dict) -> None: | |
| """Erstelle oder aktualisiere einen PR Comment mit Scan Ergebnissen.""" | |
| import httpx | |
| # Suche nach bestehendem Comment | |
| existing_comment_id = await self._find_existing_comment(pr_number) | |
| # Generiere Comment Body | |
| body = self._generate_comment_body(scan_results, scan_config) | |
| if existing_comment_id: | |
| # Update bestehenden Comment | |
| url = f"https://api.github.com/repos/{self.owner}/{self.repo}/issues/comments/{existing_comment_id}" | |
| async with httpx.AsyncClient() as client: | |
| await client.patch(url, headers=self.headers, json={"body": body}) | |
| else: | |
| # Erstelle neuen Comment | |
| url = f"https://api.github.com/repos/{self.owner}/{self.repo}/issues/{pr_number}/comments" | |
| async with httpx.AsyncClient() as client: | |
| await client.post(url, headers=self.headers, json={"body": body}) | |
| async def _find_existing_comment(self, pr_number: int) -> Optional[int]: | |
| """Suche nach bestehendem Zen-AI-Pentest Comment.""" | |
| import httpx | |
| url = f"https://api.github.com/repos/{self.owner}/{self.repo}/issues/{pr_number}/comments" | |
| async with httpx.AsyncClient() as client: | |
| response = await client.get(url, headers=self.headers) | |
| response.raise_for_status() | |
| comments = response.json() | |
| for comment in comments: | |
| if "<!-- zen-ai-pentest -->" in comment.get("body", ""): | |
| return comment["id"] | |
| return None | |
| def _generate_comment_body(self, scan_results: list[dict], scan_config: dict) -> str: | |
| """Generiere Markdown Body für PR Comment.""" | |
| # Zähle Findings | |
| severity_counts = {"critical": 0, "high": 0, "medium": 0, "low": 0, "info": 0} | |
| all_findings = [] | |
| for result in scan_results: | |
| findings = result.get("findings", []) | |
| all_findings.extend(findings) | |
| for finding in findings: | |
| severity = finding.get("severity", "info").lower() | |
| if severity in severity_counts: | |
| severity_counts[severity] += 1 | |
| total = sum(severity_counts.values()) | |
| # Bestimme Status Badge | |
| if severity_counts["critical"] > 0: | |
| status_badge = "🔴 **Action Required**" | |
| elif severity_counts["high"] > 0: | |
| status_badge = "🟠 **Review Recommended**" | |
| elif total > 0: | |
| status_badge = "🟡 **Informational**" | |
| else: | |
| status_badge = "🟢 **All Clear**" | |
| lines = [ | |
| "<!-- zen-ai-pentest -->", | |
| "## 🔒 Zen AI Pentest Security Scan", | |
| "", | |
| f"{status_badge}", | |
| "", | |
| "### Summary", | |
| "", | |
| "| Severity | Count |", | |
| "|----------|-------|", | |
| f"| 🔴 Critical | {severity_counts['critical']} |", | |
| f"| 🟠 High | {severity_counts['high']} |", | |
| f"| 🟡 Medium | {severity_counts['medium']} |", | |
| f"| 🔵 Low | {severity_counts['low']} |", | |
| f"| ⚪ Info | {severity_counts['info']} |", | |
| "", | |
| ] | |
| if all_findings: | |
| lines.extend( | |
| [ | |
| "### Top Findings", | |
| "", | |
| ] | |
| ) | |
| for finding in all_findings[:10]: | |
| severity = finding.get("severity", "info").upper() | |
| name = finding.get("name", "Unknown") | |
| description = finding.get("description", "")[:200] | |
| lines.append(f"**[{severity}]** {name}") | |
| lines.append(f"> {description}...") | |
| lines.append("") | |
| lines.extend( | |
| [ | |
| "---", | |
| "*Powered by [Zen-AI-Pentest](https://github.com/SHAdd0WTAka/zen-ai-pentest)*", | |
| ] | |
| ) | |
| return "\n".join(lines) | |
| class WebhookHandler: | |
| """Haupt-Handler für GitHub Webhook Events.""" | |
| def __init__( | |
| self, | |
| app_id: str, | |
| private_key: str, | |
| webhook_secret: Optional[str] = None, | |
| zen_api_url: str = "http://localhost:8000", | |
| zen_api_key: Optional[str] = None, | |
| ): | |
| self.app_client = GitHubAppClient(app_id, private_key) | |
| self.verifier = WebhookVerifier(webhook_secret) | |
| self.zen_api_url = zen_api_url | |
| self.zen_api_key = zen_api_key | |
| async def handle(self, event: WebhookEvent) -> dict[str, Any]: | |
| """Route Event zum passenden Handler.""" | |
| if not event.signature_valid: | |
| logger.warning(f"Ungültige Signatur für Event {event.delivery_id}") | |
| return {"status": "error", "message": "Invalid signature"} | |
| handler_map = { | |
| "pull_request": self._handle_pull_request, | |
| "push": self._handle_push, | |
| "check_run": self._handle_check_run, | |
| "installation": self._handle_installation, | |
| } | |
| handler = handler_map.get(event.event_type) | |
| if handler: | |
| return await handler(event) | |
| logger.debug(f"Unbehandelter Event-Typ: {event.event_type}") | |
| return {"status": "ignored", "event_type": event.event_type} | |
| async def _handle_pull_request(self, event: WebhookEvent) -> dict[str, Any]: | |
| """Handle pull_request Events.""" | |
| action = event.payload.get("action") | |
| # Nur bei bestimmten Actions scannen | |
| if action not in ["opened", "synchronize", "reopened"]: | |
| return {"status": "ignored", "reason": f"Action {action} nicht relevant"} | |
| pr = event.payload.get("pull_request", {}) | |
| pr_number = pr.get("number") | |
| head_sha = pr.get("head", {}).get("sha") | |
| pr.get("base", {}).get("ref") | |
| logger.info(f"PR #{pr_number} {action} - Starte Scan für {head_sha[:7]}") | |
| # Hole Installation Token | |
| installation_id = event.installation_id | |
| if not installation_id: | |
| return {"status": "error", "message": "Keine Installation ID"} | |
| token = await self.app_client.get_installation_token(installation_id) | |
| # Lade Konfiguration | |
| config_loader = ZenConfigLoader(token, event.owner, event.repo_name) | |
| config = await config_loader.load_config(ref=head_sha) | |
| # Erstelle Check Run | |
| check_manager = CheckRunManager(token, event.owner, event.repo_name) | |
| check_run = CheckRun( | |
| name="Zen AI Pentest", head_sha=head_sha, status=CheckRunStatus.QUEUED, external_id=f"zen-pentest-pr-{pr_number}" | |
| ) | |
| check_run = await check_manager.create_check_run(check_run) | |
| # Starte Scan (asynchron) | |
| # In Produktion: Queue für Background Jobs nutzen (Celery, RQ, etc.) | |
| import asyncio | |
| asyncio.create_task( | |
| self._execute_scan( | |
| check_manager=check_manager, | |
| comment_manager=PRCommentManager(token, event.owner, event.repo_name), | |
| check_run=check_run, | |
| pr_number=pr_number, | |
| config=config, | |
| head_sha=head_sha, | |
| ) | |
| ) | |
| return {"status": "started", "check_run_id": check_run.id, "pr_number": pr_number} | |
| async def _handle_push(self, event: WebhookEvent) -> dict[str, Any]: | |
| """Handle push Events.""" | |
| ref = event.payload.get("ref", "") | |
| # Nur bei bestimmten Branches scannen | |
| if ref not in ["refs/heads/main", "refs/heads/develop", "refs/heads/master"]: | |
| return {"status": "ignored", "reason": f"Branch {ref} nicht konfiguriert"} | |
| head_sha = event.payload.get("after") | |
| logger.info(f"Push zu {ref} - Starte Scan für {head_sha[:7]}") | |
| installation_id = event.installation_id | |
| if not installation_id: | |
| return {"status": "error", "message": "Keine Installation ID"} | |
| token = await self.app_client.get_installation_token(installation_id) | |
| # Ähnlich wie PR, aber ohne PR Comment | |
| config_loader = ZenConfigLoader(token, event.owner, event.repo_name) | |
| config = await config_loader.load_config(ref=head_sha) | |
| check_manager = CheckRunManager(token, event.owner, event.repo_name) | |
| check_run = CheckRun( | |
| name="Zen AI Pentest", | |
| head_sha=head_sha, | |
| status=CheckRunStatus.QUEUED, | |
| external_id=f"zen-pentest-push-{head_sha[:8]}", | |
| ) | |
| check_run = await check_manager.create_check_run(check_run) | |
| # Starte Scan | |
| import asyncio | |
| asyncio.create_task( | |
| self._execute_scan( | |
| check_manager=check_manager, | |
| comment_manager=None, # Keine PR Comments bei Push | |
| check_run=check_run, | |
| pr_number=None, | |
| config=config, | |
| head_sha=head_sha, | |
| ) | |
| ) | |
| return {"status": "started", "check_run_id": check_run.id, "ref": ref} | |
| async def _execute_scan( | |
| self, | |
| check_manager: CheckRunManager, | |
| comment_manager: Optional[PRCommentManager], | |
| check_run: CheckRun, | |
| pr_number: Optional[int], | |
| config: dict, | |
| head_sha: str, | |
| ) -> None: | |
| """Führe den Security Scan aus.""" | |
| try: | |
| # Update zu "in_progress" | |
| check_run.status = CheckRunStatus.IN_PROGRESS | |
| check_run.started_at = self._iso_timestamp() | |
| await check_manager.update_check_run(check_run) | |
| # Führe Scans durch | |
| scan_results = [] | |
| for scan_config in config.get("scans", []): | |
| result = await self._run_single_scan(scan_config) | |
| scan_results.append(result) | |
| # Erstelle Output | |
| output = check_manager.create_scan_summary(scan_results, config) | |
| # Bestimme Conclusion | |
| fail_on = config.get("global", {}).get("fail_on", "high") | |
| conclusion = check_manager.determine_conclusion(scan_results, fail_on) | |
| # Update Check Run | |
| check_run.status = CheckRunStatus.COMPLETED | |
| check_run.conclusion = conclusion | |
| check_run.output = output | |
| check_run.completed_at = self._iso_timestamp() | |
| await check_manager.update_check_run(check_run) | |
| # Erstelle PR Comment wenn aktiviert und PR vorhanden | |
| if comment_manager and pr_number: | |
| notifications = config.get("notifications", {}) | |
| if notifications.get("pr_comments", True): | |
| await comment_manager.create_or_update_comment(pr_number, scan_results, config) | |
| logger.info(f"Scan für {head_sha[:7]} abgeschlossen: {conclusion.value}") | |
| except Exception as e: | |
| logger.exception("Fehler beim Scan") | |
| # Update Check Run mit Fehler | |
| check_run.status = CheckRunStatus.COMPLETED | |
| check_run.conclusion = CheckRunConclusion.FAILURE | |
| check_run.output = CheckRunOutput(title="Scan Failed", summary=f"❌ Scan failed with error: {str(e)}", text=str(e)) | |
| check_run.completed_at = self._iso_timestamp() | |
| await check_manager.update_check_run(check_run) | |
| async def _run_single_scan(self, scan_config: dict) -> dict: | |
| """Führe einen einzelnen Scan über die Zen-AI-Pentest API aus.""" | |
| import httpx | |
| # Ersetze Environment Variablen im Target | |
| target = scan_config.get("target", "") | |
| target = self._interpolate_env_vars(target) | |
| # Baue Scan Request | |
| scan_request = { | |
| "name": scan_config.get("name", "GitHub App Scan"), | |
| "target": target, | |
| "scan_type": scan_config.get("type", "web"), | |
| "policy": scan_config.get("policy", "safe"), | |
| "tools": scan_config.get("tools", []), | |
| "timeout": scan_config.get("timeout", 300), | |
| } | |
| headers = {} | |
| if self.zen_api_key: | |
| headers["Authorization"] = f"Bearer {self.zen_api_key}" | |
| try: | |
| async with httpx.AsyncClient() as client: | |
| # Starte Scan | |
| response = await client.post(f"{self.zen_api_url}/scans", json=scan_request, headers=headers, timeout=60.0) | |
| response.raise_for_status() | |
| scan_data = response.json() | |
| scan_id = scan_data.get("id") | |
| # Warte auf Ergebnis (Polling) | |
| max_wait = scan_config.get("timeout", 300) | |
| poll_interval = 5 | |
| waited = 0 | |
| while waited < max_wait: | |
| await asyncio.sleep(poll_interval) | |
| waited += poll_interval | |
| status_response = await client.get(f"{self.zen_api_url}/scans/{scan_id}", headers=headers) | |
| status_response.raise_for_status() | |
| status_data = status_response.json() | |
| if status_data.get("status") in ["completed", "failed", "cancelled"]: | |
| # Hole Findings | |
| findings_response = await client.get(f"{self.zen_api_url}/scans/{scan_id}/findings", headers=headers) | |
| findings_response.raise_for_status() | |
| findings = findings_response.json() | |
| return { | |
| "scan_id": scan_id, | |
| "config": scan_config, | |
| "status": status_data.get("status"), | |
| "findings": findings.get("items", []), | |
| "started_at": status_data.get("started_at"), | |
| "completed_at": status_data.get("completed_at"), | |
| } | |
| # Timeout | |
| return { | |
| "scan_id": scan_id, | |
| "config": scan_config, | |
| "status": "timeout", | |
| "findings": [], | |
| "error": f"Scan timed out after {max_wait}s", | |
| } | |
| except Exception as e: | |
| return {"config": scan_config, "status": "error", "findings": [], "error": str(e)} | |
| def _interpolate_env_vars(self, value: str) -> str: | |
| """Ersetze ${{ env.VAR }} Patterns mit Umgebungsvariablen.""" | |
| import os | |
| import re | |
| pattern = r"\$\{\{\s*env\.(\w+)\s*\}\}" | |
| def replace(match): | |
| var_name = match.group(1) | |
| return os.getenv(var_name, f"${{{{env.{var_name}}}}}") | |
| return re.sub(pattern, replace, value) | |
| def _iso_timestamp(self) -> str: | |
| """Generiere ISO 8601 Timestamp.""" | |
| from datetime import datetime, timezone | |
| return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") | |
| async def _handle_check_run(self, event: WebhookEvent) -> dict[str, Any]: | |
| """Handle check_run Events (z.B. Re-run Requests).""" | |
| # Implementierung für Re-run Requests | |
| return {"status": "ignored"} | |
| async def _handle_installation(self, event: WebhookEvent) -> dict[str, Any]: | |
| """Handle installation Events.""" | |
| action = event.payload.get("action") | |
| if action == "created": | |
| logger.info(f"Neue Installation: {event.installation_id}") | |
| elif action == "deleted": | |
| logger.info(f"Installation entfernt: {event.installation_id}") | |
| return {"status": "ok", "action": action} | |
Xet Storage Details
- Size:
- 21.7 kB
- Xet hash:
- 3b70ca74d2233b167639ee32be3012c5825dd92ca354e9d824394b29e23f49f8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.