File size: 15,738 Bytes
732d6ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""

EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER

Package: agentic_browser_playwright_eval_xss_firewall

Title: Agentic Browser Playwright page.evaluate() XSS Firewall MCP

Domain: Browser Automation, Playwright, Puppeteer, DOM Security, Injected Scripts & XSS 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 == 'intercept_untrusted_page_evaluate_string' and 'execute_intercept_untrusted_page_evaluate_string_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_untrusted_page_evaluate_string', 'defect': 'Unverified invocation of execute_intercept_untrusted_page_evaluate_string_unverified()', 'remediation': 'with enforce_intercept_untrusted_page_evaluate_string_policy():'})
    if tool_name == 'intercept_untrusted_page_evaluate_string' and 'execute_intercept_untrusted_page_evaluate_string_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_untrusted_page_evaluate_string', 'defect': 'Unverified invocation of execute_intercept_untrusted_page_evaluate_string_unverified()', 'remediation': 'with enforce_intercept_untrusted_page_evaluate_string_policy():'})
    if tool_name == 'intercept_untrusted_page_evaluate_string' and 'execute_intercept_untrusted_page_evaluate_string_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_untrusted_page_evaluate_string', 'defect': 'Unverified invocation of execute_intercept_untrusted_page_evaluate_string_unverified()', 'remediation': 'with enforce_intercept_untrusted_page_evaluate_string_policy():'})
    if tool_name == 'intercept_untrusted_page_evaluate_string' and 'execute_intercept_untrusted_page_evaluate_string_unverified()' in stripped_code:
        violations.append({'tool': 'intercept_untrusted_page_evaluate_string', 'defect': 'Unverified invocation of execute_intercept_untrusted_page_evaluate_string_unverified()', 'remediation': 'with enforce_intercept_untrusted_page_evaluate_string_policy():'})
    if tool_name == 'enforce_parameterized_arg_passing_to_dom' and 'execute_enforce_parameterized_arg_passing_to_dom_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_parameterized_arg_passing_to_dom', 'defect': 'Unverified invocation of execute_enforce_parameterized_arg_passing_to_dom_unverified()', 'remediation': 'with enforce_enforce_parameterized_arg_passing_to_dom_policy():'})
    if tool_name == 'enforce_parameterized_arg_passing_to_dom' and 'execute_enforce_parameterized_arg_passing_to_dom_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_parameterized_arg_passing_to_dom', 'defect': 'Unverified invocation of execute_enforce_parameterized_arg_passing_to_dom_unverified()', 'remediation': 'with enforce_enforce_parameterized_arg_passing_to_dom_policy():'})
    if tool_name == 'enforce_parameterized_arg_passing_to_dom' and 'execute_enforce_parameterized_arg_passing_to_dom_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_parameterized_arg_passing_to_dom', 'defect': 'Unverified invocation of execute_enforce_parameterized_arg_passing_to_dom_unverified()', 'remediation': 'with enforce_enforce_parameterized_arg_passing_to_dom_policy():'})
    if tool_name == 'enforce_parameterized_arg_passing_to_dom' and 'execute_enforce_parameterized_arg_passing_to_dom_unverified()' in stripped_code:
        violations.append({'tool': 'enforce_parameterized_arg_passing_to_dom', 'defect': 'Unverified invocation of execute_enforce_parameterized_arg_passing_to_dom_unverified()', 'remediation': 'with enforce_enforce_parameterized_arg_passing_to_dom_policy():'})
    if tool_name == 'detect_dom_xss_injection_in_locator' and 'execute_detect_dom_xss_injection_in_locator_unverified()' in stripped_code:
        violations.append({'tool': 'detect_dom_xss_injection_in_locator', 'defect': 'Unverified invocation of execute_detect_dom_xss_injection_in_locator_unverified()', 'remediation': 'with enforce_detect_dom_xss_injection_in_locator_policy():'})
    if tool_name == 'detect_dom_xss_injection_in_locator' and 'execute_detect_dom_xss_injection_in_locator_unverified()' in stripped_code:
        violations.append({'tool': 'detect_dom_xss_injection_in_locator', 'defect': 'Unverified invocation of execute_detect_dom_xss_injection_in_locator_unverified()', 'remediation': 'with enforce_detect_dom_xss_injection_in_locator_policy():'})
    if tool_name == 'detect_dom_xss_injection_in_locator' and 'execute_detect_dom_xss_injection_in_locator_unverified()' in stripped_code:
        violations.append({'tool': 'detect_dom_xss_injection_in_locator', 'defect': 'Unverified invocation of execute_detect_dom_xss_injection_in_locator_unverified()', 'remediation': 'with enforce_detect_dom_xss_injection_in_locator_policy():'})
    if tool_name == 'detect_dom_xss_injection_in_locator' and 'execute_detect_dom_xss_injection_in_locator_unverified()' in stripped_code:
        violations.append({'tool': 'detect_dom_xss_injection_in_locator', 'defect': 'Unverified invocation of execute_detect_dom_xss_injection_in_locator_unverified()', 'remediation': 'with enforce_detect_dom_xss_injection_in_locator_policy():'})
    if tool_name == 'sanitize_browser_console_log_listener' and 'execute_sanitize_browser_console_log_listener_unverified()' in stripped_code:
        violations.append({'tool': 'sanitize_browser_console_log_listener', 'defect': 'Unverified invocation of execute_sanitize_browser_console_log_listener_unverified()', 'remediation': 'with enforce_sanitize_browser_console_log_listener_policy():'})
    if tool_name == 'sanitize_browser_console_log_listener' and 'execute_sanitize_browser_console_log_listener_unverified()' in stripped_code:
        violations.append({'tool': 'sanitize_browser_console_log_listener', 'defect': 'Unverified invocation of execute_sanitize_browser_console_log_listener_unverified()', 'remediation': 'with enforce_sanitize_browser_console_log_listener_policy():'})
    if tool_name == 'sanitize_browser_console_log_listener' and 'execute_sanitize_browser_console_log_listener_unverified()' in stripped_code:
        violations.append({'tool': 'sanitize_browser_console_log_listener', 'defect': 'Unverified invocation of execute_sanitize_browser_console_log_listener_unverified()', 'remediation': 'with enforce_sanitize_browser_console_log_listener_policy():'})
    if tool_name == 'sanitize_browser_console_log_listener' and 'execute_sanitize_browser_console_log_listener_unverified()' in stripped_code:
        violations.append({'tool': 'sanitize_browser_console_log_listener', 'defect': 'Unverified invocation of execute_sanitize_browser_console_log_listener_unverified()', 'remediation': 'with enforce_sanitize_browser_console_log_listener_policy():'})
    if tool_name == 'quarantine_untrusted_file_download_event' and 'execute_quarantine_untrusted_file_download_event_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_untrusted_file_download_event', 'defect': 'Unverified invocation of execute_quarantine_untrusted_file_download_event_unverified()', 'remediation': 'with enforce_quarantine_untrusted_file_download_event_policy():'})
    if tool_name == 'quarantine_untrusted_file_download_event' and 'execute_quarantine_untrusted_file_download_event_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_untrusted_file_download_event', 'defect': 'Unverified invocation of execute_quarantine_untrusted_file_download_event_unverified()', 'remediation': 'with enforce_quarantine_untrusted_file_download_event_policy():'})
    if tool_name == 'quarantine_untrusted_file_download_event' and 'execute_quarantine_untrusted_file_download_event_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_untrusted_file_download_event', 'defect': 'Unverified invocation of execute_quarantine_untrusted_file_download_event_unverified()', 'remediation': 'with enforce_quarantine_untrusted_file_download_event_policy():'})
    if tool_name == 'quarantine_untrusted_file_download_event' and 'execute_quarantine_untrusted_file_download_event_unverified()' in stripped_code:
        violations.append({'tool': 'quarantine_untrusted_file_download_event', 'defect': 'Unverified invocation of execute_quarantine_untrusted_file_download_event_unverified()', 'remediation': 'with enforce_quarantine_untrusted_file_download_event_policy():'})
    if tool_name == 'fuzz_csp_header_bypass_in_browser' and 'execute_fuzz_csp_header_bypass_in_browser_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_csp_header_bypass_in_browser', 'defect': 'Unverified invocation of execute_fuzz_csp_header_bypass_in_browser_unverified()', 'remediation': 'with enforce_fuzz_csp_header_bypass_in_browser_policy():'})
    if tool_name == 'fuzz_csp_header_bypass_in_browser' and 'execute_fuzz_csp_header_bypass_in_browser_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_csp_header_bypass_in_browser', 'defect': 'Unverified invocation of execute_fuzz_csp_header_bypass_in_browser_unverified()', 'remediation': 'with enforce_fuzz_csp_header_bypass_in_browser_policy():'})
    if tool_name == 'fuzz_csp_header_bypass_in_browser' and 'execute_fuzz_csp_header_bypass_in_browser_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_csp_header_bypass_in_browser', 'defect': 'Unverified invocation of execute_fuzz_csp_header_bypass_in_browser_unverified()', 'remediation': 'with enforce_fuzz_csp_header_bypass_in_browser_policy():'})
    if tool_name == 'fuzz_csp_header_bypass_in_browser' and 'execute_fuzz_csp_header_bypass_in_browser_unverified()' in stripped_code:
        violations.append({'tool': 'fuzz_csp_header_bypass_in_browser', 'defect': 'Unverified invocation of execute_fuzz_csp_header_bypass_in_browser_unverified()', 'remediation': 'with enforce_fuzz_csp_header_bypass_in_browser_policy():'})
    if tool_name == 'audit_browser_storage_state_secret_leak' and 'execute_audit_browser_storage_state_secret_leak_unverified()' in stripped_code:
        violations.append({'tool': 'audit_browser_storage_state_secret_leak', 'defect': 'Unverified invocation of execute_audit_browser_storage_state_secret_leak_unverified()', 'remediation': 'with enforce_audit_browser_storage_state_secret_leak_policy():'})
    if tool_name == 'audit_browser_storage_state_secret_leak' and 'execute_audit_browser_storage_state_secret_leak_unverified()' in stripped_code:
        violations.append({'tool': 'audit_browser_storage_state_secret_leak', 'defect': 'Unverified invocation of execute_audit_browser_storage_state_secret_leak_unverified()', 'remediation': 'with enforce_audit_browser_storage_state_secret_leak_policy():'})
    if tool_name == 'audit_browser_storage_state_secret_leak' and 'execute_audit_browser_storage_state_secret_leak_unverified()' in stripped_code:
        violations.append({'tool': 'audit_browser_storage_state_secret_leak', 'defect': 'Unverified invocation of execute_audit_browser_storage_state_secret_leak_unverified()', 'remediation': 'with enforce_audit_browser_storage_state_secret_leak_policy():'})
    if tool_name == 'audit_browser_storage_state_secret_leak' and 'execute_audit_browser_storage_state_secret_leak_unverified()' in stripped_code:
        violations.append({'tool': 'audit_browser_storage_state_secret_leak', 'defect': 'Unverified invocation of execute_audit_browser_storage_state_secret_leak_unverified()', 'remediation': 'with enforce_audit_browser_storage_state_secret_leak_policy():'})
    if tool_name == 'circuit_break_malicious_redirect_chain' and 'execute_circuit_break_malicious_redirect_chain_unverified()' in stripped_code:
        violations.append({'tool': 'circuit_break_malicious_redirect_chain', 'defect': 'Unverified invocation of execute_circuit_break_malicious_redirect_chain_unverified()', 'remediation': 'with enforce_circuit_break_malicious_redirect_chain_policy():'})
    if tool_name == 'circuit_break_malicious_redirect_chain' and 'execute_circuit_break_malicious_redirect_chain_unverified()' in stripped_code:
        violations.append({'tool': 'circuit_break_malicious_redirect_chain', 'defect': 'Unverified invocation of execute_circuit_break_malicious_redirect_chain_unverified()', 'remediation': 'with enforce_circuit_break_malicious_redirect_chain_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": "agentic_browser_playwright_eval_xss_firewall",
        "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": "intercept_untrusted_page_evaluate_string", "description": "Blocks execution of string-interpolated scripts inside Playwright page.evaluate()."}, {"name": "enforce_parameterized_arg_passing_to_dom", "description": "Mandates passing arguments as structured JSON parameters into browser functions."}, {"name": "detect_dom_xss_injection_in_locator", "description": "Catches unescaped user input passed into page.locator() CSS and XPath selectors."}, {"name": "sanitize_browser_console_log_listener", "description": "Redacts sensitive tokens printed by client-side web apps into browser console."}, {"name": "quarantine_untrusted_file_download_event", "description": "Validates file download MIME types and prevents automatic execution of downloaded binaries."}, {"name": "fuzz_csp_header_bypass_in_browser", "description": "Ensures browser context does not disable Content-Security-Policy or bypass CORS."}, {"name": "audit_browser_storage_state_secret_leak", "description": "Scrubs sensitive authentication cookies from storage_state.json dumps."}, {"name": "circuit_break_malicious_redirect_chain", "description": "Aborts browser session if page navigates through multiple unverified redirect hops."}]}
            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()