#!/usr/bin/env python3 """ EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER Package: agent_prompt_injection_via_git_commit_shield Title: Agent Prompt Injection via Git Commit & PR Shield MCP Domain: Git Provenance Armor, Indirect Prompt Injection Defense, Pull-Request Threat Analysis 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": [{"tool": tool_name, "defect": str(e)}]} violations = [] if tool_name == 'intercept_git_commit_prompt_injection' and 'execute_intercept_git_commit_prompt_injection_unverified()' in stripped_code: violations.append({'tool': 'intercept_git_commit_prompt_injection', 'defect': 'Unverified invocation of execute_intercept_git_commit_prompt_injection_unverified()', 'remediation': 'with enforce_intercept_git_commit_prompt_injection_policy():'}) if tool_name == 'intercept_git_commit_prompt_injection' and 'execute_intercept_git_commit_prompt_injection_unverified()' in stripped_code: violations.append({'tool': 'intercept_git_commit_prompt_injection', 'defect': 'Unverified invocation of execute_intercept_git_commit_prompt_injection_unverified()', 'remediation': 'with enforce_intercept_git_commit_prompt_injection_policy():'}) if tool_name == 'intercept_git_commit_prompt_injection' and 'execute_intercept_git_commit_prompt_injection_unverified()' in stripped_code: violations.append({'tool': 'intercept_git_commit_prompt_injection', 'defect': 'Unverified invocation of execute_intercept_git_commit_prompt_injection_unverified()', 'remediation': 'with enforce_intercept_git_commit_prompt_injection_policy():'}) if tool_name == 'intercept_git_commit_prompt_injection' and 'execute_intercept_git_commit_prompt_injection_unverified()' in stripped_code: violations.append({'tool': 'intercept_git_commit_prompt_injection', 'defect': 'Unverified invocation of execute_intercept_git_commit_prompt_injection_unverified()', 'remediation': 'with enforce_intercept_git_commit_prompt_injection_policy():'}) if tool_name == 'sanitize_pull_request_markdown_payload' and 'execute_sanitize_pull_request_markdown_payload_unverified()' in stripped_code: violations.append({'tool': 'sanitize_pull_request_markdown_payload', 'defect': 'Unverified invocation of execute_sanitize_pull_request_markdown_payload_unverified()', 'remediation': 'with enforce_sanitize_pull_request_markdown_payload_policy():'}) if tool_name == 'sanitize_pull_request_markdown_payload' and 'execute_sanitize_pull_request_markdown_payload_unverified()' in stripped_code: violations.append({'tool': 'sanitize_pull_request_markdown_payload', 'defect': 'Unverified invocation of execute_sanitize_pull_request_markdown_payload_unverified()', 'remediation': 'with enforce_sanitize_pull_request_markdown_payload_policy():'}) if tool_name == 'sanitize_pull_request_markdown_payload' and 'execute_sanitize_pull_request_markdown_payload_unverified()' in stripped_code: violations.append({'tool': 'sanitize_pull_request_markdown_payload', 'defect': 'Unverified invocation of execute_sanitize_pull_request_markdown_payload_unverified()', 'remediation': 'with enforce_sanitize_pull_request_markdown_payload_policy():'}) if tool_name == 'sanitize_pull_request_markdown_payload' and 'execute_sanitize_pull_request_markdown_payload_unverified()' in stripped_code: violations.append({'tool': 'sanitize_pull_request_markdown_payload', 'defect': 'Unverified invocation of execute_sanitize_pull_request_markdown_payload_unverified()', 'remediation': 'with enforce_sanitize_pull_request_markdown_payload_policy():'}) if tool_name == 'quarantine_untrusted_contributor_instructions' and 'execute_quarantine_untrusted_contributor_instructions_unverified()' in stripped_code: violations.append({'tool': 'quarantine_untrusted_contributor_instructions', 'defect': 'Unverified invocation of execute_quarantine_untrusted_contributor_instructions_unverified()', 'remediation': 'with enforce_quarantine_untrusted_contributor_instructions_policy():'}) if tool_name == 'quarantine_untrusted_contributor_instructions' and 'execute_quarantine_untrusted_contributor_instructions_unverified()' in stripped_code: violations.append({'tool': 'quarantine_untrusted_contributor_instructions', 'defect': 'Unverified invocation of execute_quarantine_untrusted_contributor_instructions_unverified()', 'remediation': 'with enforce_quarantine_untrusted_contributor_instructions_policy():'}) if tool_name == 'quarantine_untrusted_contributor_instructions' and 'execute_quarantine_untrusted_contributor_instructions_unverified()' in stripped_code: violations.append({'tool': 'quarantine_untrusted_contributor_instructions', 'defect': 'Unverified invocation of execute_quarantine_untrusted_contributor_instructions_unverified()', 'remediation': 'with enforce_quarantine_untrusted_contributor_instructions_policy():'}) if tool_name == 'quarantine_untrusted_contributor_instructions' and 'execute_quarantine_untrusted_contributor_instructions_unverified()' in stripped_code: violations.append({'tool': 'quarantine_untrusted_contributor_instructions', 'defect': 'Unverified invocation of execute_quarantine_untrusted_contributor_instructions_unverified()', 'remediation': 'with enforce_quarantine_untrusted_contributor_instructions_policy():'}) if tool_name == 'audit_git_hook_injection_vector' and 'execute_audit_git_hook_injection_vector_unverified()' in stripped_code: violations.append({'tool': 'audit_git_hook_injection_vector', 'defect': 'Unverified invocation of execute_audit_git_hook_injection_vector_unverified()', 'remediation': 'with enforce_audit_git_hook_injection_vector_policy():'}) if tool_name == 'audit_git_hook_injection_vector' and 'execute_audit_git_hook_injection_vector_unverified()' in stripped_code: violations.append({'tool': 'audit_git_hook_injection_vector', 'defect': 'Unverified invocation of execute_audit_git_hook_injection_vector_unverified()', 'remediation': 'with enforce_audit_git_hook_injection_vector_policy():'}) if tool_name == 'audit_git_hook_injection_vector' and 'execute_audit_git_hook_injection_vector_unverified()' in stripped_code: violations.append({'tool': 'audit_git_hook_injection_vector', 'defect': 'Unverified invocation of execute_audit_git_hook_injection_vector_unverified()', 'remediation': 'with enforce_audit_git_hook_injection_vector_policy():'}) if tool_name == 'audit_git_hook_injection_vector' and 'execute_audit_git_hook_injection_vector_unverified()' in stripped_code: violations.append({'tool': 'audit_git_hook_injection_vector', 'defect': 'Unverified invocation of execute_audit_git_hook_injection_vector_unverified()', 'remediation': 'with enforce_audit_git_hook_injection_vector_policy():'}) if tool_name == 'enforce_prompt_context_role_separation' and 'execute_enforce_prompt_context_role_separation_unverified()' in stripped_code: violations.append({'tool': 'enforce_prompt_context_role_separation', 'defect': 'Unverified invocation of execute_enforce_prompt_context_role_separation_unverified()', 'remediation': 'with enforce_enforce_prompt_context_role_separation_policy():'}) if tool_name == 'enforce_prompt_context_role_separation' and 'execute_enforce_prompt_context_role_separation_unverified()' in stripped_code: violations.append({'tool': 'enforce_prompt_context_role_separation', 'defect': 'Unverified invocation of execute_enforce_prompt_context_role_separation_unverified()', 'remediation': 'with enforce_enforce_prompt_context_role_separation_policy():'}) if tool_name == 'enforce_prompt_context_role_separation' and 'execute_enforce_prompt_context_role_separation_unverified()' in stripped_code: violations.append({'tool': 'enforce_prompt_context_role_separation', 'defect': 'Unverified invocation of execute_enforce_prompt_context_role_separation_unverified()', 'remediation': 'with enforce_enforce_prompt_context_role_separation_policy():'}) if tool_name == 'enforce_prompt_context_role_separation' and 'execute_enforce_prompt_context_role_separation_unverified()' in stripped_code: violations.append({'tool': 'enforce_prompt_context_role_separation', 'defect': 'Unverified invocation of execute_enforce_prompt_context_role_separation_unverified()', 'remediation': 'with enforce_enforce_prompt_context_role_separation_policy():'}) if tool_name == 'verify_pgp_commit_signature_trust' and 'execute_verify_pgp_commit_signature_trust_unverified()' in stripped_code: violations.append({'tool': 'verify_pgp_commit_signature_trust', 'defect': 'Unverified invocation of execute_verify_pgp_commit_signature_trust_unverified()', 'remediation': 'with enforce_verify_pgp_commit_signature_trust_policy():'}) if tool_name == 'verify_pgp_commit_signature_trust' and 'execute_verify_pgp_commit_signature_trust_unverified()' in stripped_code: violations.append({'tool': 'verify_pgp_commit_signature_trust', 'defect': 'Unverified invocation of execute_verify_pgp_commit_signature_trust_unverified()', 'remediation': 'with enforce_verify_pgp_commit_signature_trust_policy():'}) if tool_name == 'verify_pgp_commit_signature_trust' and 'execute_verify_pgp_commit_signature_trust_unverified()' in stripped_code: violations.append({'tool': 'verify_pgp_commit_signature_trust', 'defect': 'Unverified invocation of execute_verify_pgp_commit_signature_trust_unverified()', 'remediation': 'with enforce_verify_pgp_commit_signature_trust_policy():'}) if tool_name == 'verify_pgp_commit_signature_trust' and 'execute_verify_pgp_commit_signature_trust_unverified()' in stripped_code: violations.append({'tool': 'verify_pgp_commit_signature_trust', 'defect': 'Unverified invocation of execute_verify_pgp_commit_signature_trust_unverified()', 'remediation': 'with enforce_verify_pgp_commit_signature_trust_policy():'}) if tool_name == 'fuzz_indirect_injection_obfuscation' and 'execute_fuzz_indirect_injection_obfuscation_unverified()' in stripped_code: violations.append({'tool': 'fuzz_indirect_injection_obfuscation', 'defect': 'Unverified invocation of execute_fuzz_indirect_injection_obfuscation_unverified()', 'remediation': 'with enforce_fuzz_indirect_injection_obfuscation_policy():'}) if tool_name == 'fuzz_indirect_injection_obfuscation' and 'execute_fuzz_indirect_injection_obfuscation_unverified()' in stripped_code: violations.append({'tool': 'fuzz_indirect_injection_obfuscation', 'defect': 'Unverified invocation of execute_fuzz_indirect_injection_obfuscation_unverified()', 'remediation': 'with enforce_fuzz_indirect_injection_obfuscation_policy():'}) if tool_name == 'fuzz_indirect_injection_obfuscation' and 'execute_fuzz_indirect_injection_obfuscation_unverified()' in stripped_code: violations.append({'tool': 'fuzz_indirect_injection_obfuscation', 'defect': 'Unverified invocation of execute_fuzz_indirect_injection_obfuscation_unverified()', 'remediation': 'with enforce_fuzz_indirect_injection_obfuscation_policy():'}) if tool_name == 'fuzz_indirect_injection_obfuscation' and 'execute_fuzz_indirect_injection_obfuscation_unverified()' in stripped_code: violations.append({'tool': 'fuzz_indirect_injection_obfuscation', 'defect': 'Unverified invocation of execute_fuzz_indirect_injection_obfuscation_unverified()', 'remediation': 'with enforce_fuzz_indirect_injection_obfuscation_policy():'}) if tool_name == 'circuit_break_malicious_commit_execution' and 'execute_circuit_break_malicious_commit_execution_unverified()' in stripped_code: violations.append({'tool': 'circuit_break_malicious_commit_execution', 'defect': 'Unverified invocation of execute_circuit_break_malicious_commit_execution_unverified()', 'remediation': 'with enforce_circuit_break_malicious_commit_execution_policy():'}) if tool_name == 'circuit_break_malicious_commit_execution' and 'execute_circuit_break_malicious_commit_execution_unverified()' in stripped_code: violations.append({'tool': 'circuit_break_malicious_commit_execution', 'defect': 'Unverified invocation of execute_circuit_break_malicious_commit_execution_unverified()', 'remediation': 'with enforce_circuit_break_malicious_commit_execution_policy():'}) is_safe = (len(violations) == 0) and syntax_valid return { "status": "VALIDATED" if is_safe else "VIOLATION_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") or arguments.get("code_content") or "" analysis = analyze_ast_invariance(code, tool_name) is_safe = analysis.get("is_safe", False) violations = analysis.get("violations", []) return { "package": "agent_prompt_injection_via_git_commit_shield", "tool": tool_name, "result": analysis, "status": "VIOLATION_DETECTED" if (violations or not is_safe) else "PASS_VALIDATED", "defect_detected": bool(violations or not is_safe), "violations_detected": bool(violations), "is_vulnerable": bool(violations or not is_safe), "is_safe": is_safe } def main(): while True: line = sys.stdin.readline() if not line: break try: req = json.loads(line) method = req.get("method") if method == "initialize": res = { "protocolVersion": "2024-11-05", "capabilities": {"tools": {}}, "serverInfo": {"name": "agent_prompt_injection_via_git_commit_shield", "version": "2.5.0"} } elif method == "tools/list": res = {"tools": [{"name": "intercept_git_commit_prompt_injection", "description": "Scans git commit messages and PR bodies for prompt injection keywords attempting system overrides."}, {"name": "sanitize_pull_request_markdown_payload", "description": "Strips hidden zero-width spaces, malicious HTML comments, and adversarial Unicode from markdown issue descriptions."}, {"name": "quarantine_untrusted_contributor_instructions", "description": "Flags system-override instructions authored by external non-maintainer contributors."}, {"name": "audit_git_hook_injection_vector", "description": "Detects adversarial shell directives inside .git/hooks/ and .githooks templates."}, {"name": "enforce_prompt_context_role_separation", "description": "Enforces clear delimiters preventing LLMs from executing commit messages as commands."}, {"name": "verify_pgp_commit_signature_trust", "description": "Asserts that commit messages triggering agentic refactoring are cryptographically signed with trusted maintainer keys."}, {"name": "fuzz_indirect_injection_obfuscation", "description": "Tests for base64, ROT13, and leetspeak prompt injection payloads inside git patches."}, {"name": "circuit_break_malicious_commit_execution", "description": "Kills agent subprocess if untrusted commit instructions attempt to trigger code execution."}]} 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()