#!/usr/bin/env python3 """ EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER Package: github_pat_workflow_injection_ast_armor Title: GitHub PAT & Workflow Injection AST Armor MCP Domain: DevOps Security, GitHub Actions, Personal Access Tokens & CI/CD Pipeline Armor FAANG v2.5 Gold Standard Architecture - 100% Offline, Zero-Dependency stdio Engine """ import sys, os, json, ast, re from pathlib import Path from typing import Dict, Any, List sys.stdout.reconfigure(encoding="utf-8") PKG_DIR = Path(__file__).resolve().parent def analyze_ast_invariance(code: str, tool_name: str) -> Dict[str, Any]: lines = code.splitlines() stripped_lines = [l.split("#")[0].strip() for l in lines] stripped_code = "\n".join(stripped_lines) syntax_valid = False try: ast.parse(code) syntax_valid = True except SyntaxError as e: return {"status": "SYNTAX_ERROR", "error": str(e), "is_safe": False} violations = [] if tool_name == 'scrub_github_pat_token_string' and 'execute_scrub_github_pat_token_string_unverified()' in stripped_code: violations.append({'tool': 'scrub_github_pat_token_string', 'defect': 'Unverified invocation of execute_scrub_github_pat_token_string_unverified()', 'remediation': 'with enforce_scrub_github_pat_token_string_policy():'}) if tool_name == 'scrub_github_pat_token_string' and 'execute_scrub_github_pat_token_string_unverified()' in stripped_code: violations.append({'tool': 'scrub_github_pat_token_string', 'defect': 'Unverified invocation of execute_scrub_github_pat_token_string_unverified()', 'remediation': 'with enforce_scrub_github_pat_token_string_policy():'}) if tool_name == 'scrub_github_pat_token_string' and 'execute_scrub_github_pat_token_string_unverified()' in stripped_code: violations.append({'tool': 'scrub_github_pat_token_string', 'defect': 'Unverified invocation of execute_scrub_github_pat_token_string_unverified()', 'remediation': 'with enforce_scrub_github_pat_token_string_policy():'}) if tool_name == 'scrub_github_pat_token_string' and 'execute_scrub_github_pat_token_string_unverified()' in stripped_code: violations.append({'tool': 'scrub_github_pat_token_string', 'defect': 'Unverified invocation of execute_scrub_github_pat_token_string_unverified()', 'remediation': 'with enforce_scrub_github_pat_token_string_policy():'}) if tool_name == 'detect_github_actions_script_injection' and 'execute_detect_github_actions_script_injection_unverified()' in stripped_code: violations.append({'tool': 'detect_github_actions_script_injection', 'defect': 'Unverified invocation of execute_detect_github_actions_script_injection_unverified()', 'remediation': 'with enforce_detect_github_actions_script_injection_policy():'}) if tool_name == 'detect_github_actions_script_injection' and 'execute_detect_github_actions_script_injection_unverified()' in stripped_code: violations.append({'tool': 'detect_github_actions_script_injection', 'defect': 'Unverified invocation of execute_detect_github_actions_script_injection_unverified()', 'remediation': 'with enforce_detect_github_actions_script_injection_policy():'}) if tool_name == 'detect_github_actions_script_injection' and 'execute_detect_github_actions_script_injection_unverified()' in stripped_code: violations.append({'tool': 'detect_github_actions_script_injection', 'defect': 'Unverified invocation of execute_detect_github_actions_script_injection_unverified()', 'remediation': 'with enforce_detect_github_actions_script_injection_policy():'}) if tool_name == 'detect_github_actions_script_injection' and 'execute_detect_github_actions_script_injection_unverified()' in stripped_code: violations.append({'tool': 'detect_github_actions_script_injection', 'defect': 'Unverified invocation of execute_detect_github_actions_script_injection_unverified()', 'remediation': 'with enforce_detect_github_actions_script_injection_policy():'}) if tool_name == 'enforce_actions_pinning_full_sha' and 'execute_enforce_actions_pinning_full_sha_unverified()' in stripped_code: violations.append({'tool': 'enforce_actions_pinning_full_sha', 'defect': 'Unverified invocation of execute_enforce_actions_pinning_full_sha_unverified()', 'remediation': 'with enforce_enforce_actions_pinning_full_sha_policy():'}) if tool_name == 'enforce_actions_pinning_full_sha' and 'execute_enforce_actions_pinning_full_sha_unverified()' in stripped_code: violations.append({'tool': 'enforce_actions_pinning_full_sha', 'defect': 'Unverified invocation of execute_enforce_actions_pinning_full_sha_unverified()', 'remediation': 'with enforce_enforce_actions_pinning_full_sha_policy():'}) if tool_name == 'enforce_actions_pinning_full_sha' and 'execute_enforce_actions_pinning_full_sha_unverified()' in stripped_code: violations.append({'tool': 'enforce_actions_pinning_full_sha', 'defect': 'Unverified invocation of execute_enforce_actions_pinning_full_sha_unverified()', 'remediation': 'with enforce_enforce_actions_pinning_full_sha_policy():'}) if tool_name == 'enforce_actions_pinning_full_sha' and 'execute_enforce_actions_pinning_full_sha_unverified()' in stripped_code: violations.append({'tool': 'enforce_actions_pinning_full_sha', 'defect': 'Unverified invocation of execute_enforce_actions_pinning_full_sha_unverified()', 'remediation': 'with enforce_enforce_actions_pinning_full_sha_policy():'}) if tool_name == 'audit_workflow_permissions_block' and 'execute_audit_workflow_permissions_block_unverified()' in stripped_code: violations.append({'tool': 'audit_workflow_permissions_block', 'defect': 'Unverified invocation of execute_audit_workflow_permissions_block_unverified()', 'remediation': 'with enforce_audit_workflow_permissions_block_policy():'}) if tool_name == 'audit_workflow_permissions_block' and 'execute_audit_workflow_permissions_block_unverified()' in stripped_code: violations.append({'tool': 'audit_workflow_permissions_block', 'defect': 'Unverified invocation of execute_audit_workflow_permissions_block_unverified()', 'remediation': 'with enforce_audit_workflow_permissions_block_policy():'}) if tool_name == 'audit_workflow_permissions_block' and 'execute_audit_workflow_permissions_block_unverified()' in stripped_code: violations.append({'tool': 'audit_workflow_permissions_block', 'defect': 'Unverified invocation of execute_audit_workflow_permissions_block_unverified()', 'remediation': 'with enforce_audit_workflow_permissions_block_policy():'}) if tool_name == 'audit_workflow_permissions_block' and 'execute_audit_workflow_permissions_block_unverified()' in stripped_code: violations.append({'tool': 'audit_workflow_permissions_block', 'defect': 'Unverified invocation of execute_audit_workflow_permissions_block_unverified()', 'remediation': 'with enforce_audit_workflow_permissions_block_policy():'}) if tool_name == 'quarantine_dangerous_workflow_triggers' and 'execute_quarantine_dangerous_workflow_triggers_unverified()' in stripped_code: violations.append({'tool': 'quarantine_dangerous_workflow_triggers', 'defect': 'Unverified invocation of execute_quarantine_dangerous_workflow_triggers_unverified()', 'remediation': 'with enforce_quarantine_dangerous_workflow_triggers_policy():'}) if tool_name == 'quarantine_dangerous_workflow_triggers' and 'execute_quarantine_dangerous_workflow_triggers_unverified()' in stripped_code: violations.append({'tool': 'quarantine_dangerous_workflow_triggers', 'defect': 'Unverified invocation of execute_quarantine_dangerous_workflow_triggers_unverified()', 'remediation': 'with enforce_quarantine_dangerous_workflow_triggers_policy():'}) if tool_name == 'quarantine_dangerous_workflow_triggers' and 'execute_quarantine_dangerous_workflow_triggers_unverified()' in stripped_code: violations.append({'tool': 'quarantine_dangerous_workflow_triggers', 'defect': 'Unverified invocation of execute_quarantine_dangerous_workflow_triggers_unverified()', 'remediation': 'with enforce_quarantine_dangerous_workflow_triggers_policy():'}) if tool_name == 'quarantine_dangerous_workflow_triggers' and 'execute_quarantine_dangerous_workflow_triggers_unverified()' in stripped_code: violations.append({'tool': 'quarantine_dangerous_workflow_triggers', 'defect': 'Unverified invocation of execute_quarantine_dangerous_workflow_triggers_unverified()', 'remediation': 'with enforce_quarantine_dangerous_workflow_triggers_policy():'}) if tool_name == 'fuzz_github_api_rate_limit_headers' and 'execute_fuzz_github_api_rate_limit_headers_unverified()' in stripped_code: violations.append({'tool': 'fuzz_github_api_rate_limit_headers', 'defect': 'Unverified invocation of execute_fuzz_github_api_rate_limit_headers_unverified()', 'remediation': 'with enforce_fuzz_github_api_rate_limit_headers_policy():'}) if tool_name == 'fuzz_github_api_rate_limit_headers' and 'execute_fuzz_github_api_rate_limit_headers_unverified()' in stripped_code: violations.append({'tool': 'fuzz_github_api_rate_limit_headers', 'defect': 'Unverified invocation of execute_fuzz_github_api_rate_limit_headers_unverified()', 'remediation': 'with enforce_fuzz_github_api_rate_limit_headers_policy():'}) if tool_name == 'fuzz_github_api_rate_limit_headers' and 'execute_fuzz_github_api_rate_limit_headers_unverified()' in stripped_code: violations.append({'tool': 'fuzz_github_api_rate_limit_headers', 'defect': 'Unverified invocation of execute_fuzz_github_api_rate_limit_headers_unverified()', 'remediation': 'with enforce_fuzz_github_api_rate_limit_headers_policy():'}) if tool_name == 'fuzz_github_api_rate_limit_headers' and 'execute_fuzz_github_api_rate_limit_headers_unverified()' in stripped_code: violations.append({'tool': 'fuzz_github_api_rate_limit_headers', 'defect': 'Unverified invocation of execute_fuzz_github_api_rate_limit_headers_unverified()', 'remediation': 'with enforce_fuzz_github_api_rate_limit_headers_policy():'}) if tool_name == 'sanitize_runner_temp_secret_artifacts' and 'execute_sanitize_runner_temp_secret_artifacts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_runner_temp_secret_artifacts', 'defect': 'Unverified invocation of execute_sanitize_runner_temp_secret_artifacts_unverified()', 'remediation': 'with enforce_sanitize_runner_temp_secret_artifacts_policy():'}) if tool_name == 'sanitize_runner_temp_secret_artifacts' and 'execute_sanitize_runner_temp_secret_artifacts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_runner_temp_secret_artifacts', 'defect': 'Unverified invocation of execute_sanitize_runner_temp_secret_artifacts_unverified()', 'remediation': 'with enforce_sanitize_runner_temp_secret_artifacts_policy():'}) if tool_name == 'sanitize_runner_temp_secret_artifacts' and 'execute_sanitize_runner_temp_secret_artifacts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_runner_temp_secret_artifacts', 'defect': 'Unverified invocation of execute_sanitize_runner_temp_secret_artifacts_unverified()', 'remediation': 'with enforce_sanitize_runner_temp_secret_artifacts_policy():'}) if tool_name == 'sanitize_runner_temp_secret_artifacts' and 'execute_sanitize_runner_temp_secret_artifacts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_runner_temp_secret_artifacts', 'defect': 'Unverified invocation of execute_sanitize_runner_temp_secret_artifacts_unverified()', 'remediation': 'with enforce_sanitize_runner_temp_secret_artifacts_policy():'}) if tool_name == 'circuit_break_unauthorized_repo_push' and 'execute_circuit_break_unauthorized_repo_push_unverified()' in stripped_code: violations.append({'tool': 'circuit_break_unauthorized_repo_push', 'defect': 'Unverified invocation of execute_circuit_break_unauthorized_repo_push_unverified()', 'remediation': 'with enforce_circuit_break_unauthorized_repo_push_policy():'}) if tool_name == 'circuit_break_unauthorized_repo_push' and 'execute_circuit_break_unauthorized_repo_push_unverified()' in stripped_code: violations.append({'tool': 'circuit_break_unauthorized_repo_push', 'defect': 'Unverified invocation of execute_circuit_break_unauthorized_repo_push_unverified()', 'remediation': 'with enforce_circuit_break_unauthorized_repo_push_policy():'}) is_safe = (len(violations) == 0) and syntax_valid return { "status": "VALIDATED" if is_safe else "POLICY_VIOLATIONS_DETECTED", "is_safe": is_safe, "syntax_valid": syntax_valid, "violations": violations } def execute_tool(tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]: code = arguments.get("code", "") analysis = analyze_ast_invariance(code, tool_name) return { "package": "github_pat_workflow_injection_ast_armor", "tool": tool_name, "result": analysis } def main(): while True: line = sys.stdin.readline() if not line: break try: req = json.loads(line) method = req.get("method") if method == "tools/list": res = {"tools": [{"name": "scrub_github_pat_token_string", "description": "Detects and redacts ghp_, gho_, ghu_, and ghs_ GitHub tokens in code and logs."}, {"name": "detect_github_actions_script_injection", "description": "Audits YAML workflows for unescaped ${{ github.event.* }} expressions in run steps."}, {"name": "enforce_actions_pinning_full_sha", "description": "Enforces that GitHub Actions use full 40-character commit SHAs instead of mutable tags."}, {"name": "audit_workflow_permissions_block", "description": "Ensures workflows declare strict least-privilege permissions blocks."}, {"name": "quarantine_dangerous_workflow_triggers", "description": "Flags pull_request_target triggers that checkout untrusted pull request code."}, {"name": "fuzz_github_api_rate_limit_headers", "description": "Monitors X-RateLimit-Remaining to prevent PAT exhaustion during automation."}, {"name": "sanitize_runner_temp_secret_artifacts", "description": "Cleans temporary runner workspace files containing environment secrets."}, {"name": "circuit_break_unauthorized_repo_push", "description": "Terminates agent workflow if write operations target unauthorized repositories."}]} elif method == "tools/call": p = req.get("params", {}) res = execute_tool(p.get("name"), p.get("arguments", {})) else: res = {"error": "Unsupported method"} sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": req.get("id"), "result": res}) + "\n") sys.stdout.flush() except Exception as e: sys.stdout.write(json.dumps({"jsonrpc": "2.0", "error": {"code": -32603, "message": str(e)}}) + "\n") sys.stdout.flush() if __name__ == "__main__": main()