-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_validator.py
More file actions
154 lines (128 loc) · 5.12 KB
/
Copy pathcode_validator.py
File metadata and controls
154 lines (128 loc) · 5.12 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Enhanced Code Validator for watching_u_watching Framework
Performs comprehensive validation of LLM-generated audit code
If a suspected ethical breach (e.g., PII detected, unauthorized data retention) is found during validation, immediately follow the Ethical Incident Response Protocol:
https://github.com/genaforvena/watching_u_watching/blob/main/docs/ethical_incident_response.md
"""
import ast
import re
import sys
from typing import Dict, Any
def has_ethical_hooks(code: str) -> bool:
"""Checks for ethical review hooks using AST parsing"""
try:
tree = ast.parse(code)
return any(
isinstance(node, ast.Call)
and hasattr(node.func, 'id')
and 'ethical_review_hook' in node.func.id
for node in ast.walk(tree)
)
except SyntaxError:
return False
def validate_generated_code(code: str) -> Dict[str, Any]:
"""Comprehensive validation with detailed diagnostics"""
results = {
"valid": False,
"errors": [],
"warnings": [],
"details": {}
}
# Security checks
security_checks = {
"no_real_pii": not any(
kw in code for kw in
["real_email", "actual_phone", "live_ssn", "real_address"]
),
"no_network_calls": not re.search(
r"requests\.(get|post)|urllib|http\.client", code
),
"no_shell_commands": not re.search(
r"subprocess|os\.system|os\.popen", code
),
"no_file_io": not re.search(
r"open\(|\.write\(|\.save\(|pickle", code
)
}
# Framework compliance
compliance_checks = {
"uses_rate_limiter": "@rate_limiter" in code,
"uses_fake_data": "fake_data_helper" in code,
"has_ethical_hooks": has_ethical_hooks(code),
"inheritance_check": re.search(
r"class\s+\w+\(CorrespondenceAudit\)", code
) is not None,
"version_compatible": "compatibility_version >= 1.2" in code
}
# Performance indicators
perf_checks = {
"has_rate_limiting": "@rate_limiter" in code,
"uses_batching": "batch_size" in code,
"has_timeout": "timeout=" in code
}
# Combine all checks
all_checks = {**security_checks, **compliance_checks, **perf_checks}
results["details"] = all_checks
# Error diagnostics
if not security_checks["no_real_pii"]:
results["errors"].append("PII keywords detected - use fake_data_helper")
if not compliance_checks["inheritance_check"]:
results["errors"].append("Class must inherit from CorrespondenceAudit")
if not compliance_checks["has_ethical_hooks"]:
results["errors"].append("Missing ethical_review_hook call")
if not perf_checks["has_rate_limiting"]:
results["warnings"].append("Missing rate limiting - may cause API bans")
# Set overall validity
critical_passes = all(list(security_checks.values()) +
list(compliance_checks.values()))
results["valid"] = critical_passes
return results
def generate_autofix(code: str) -> str:
"""Attempts to automatically fix common issues"""
# Add missing inheritance
if "class " in code and "CorrespondenceAudit" not in code:
code = re.sub(r"class (\w+)", r"class \1(CorrespondenceAudit)", code)
# Add ethical hook
if not has_ethical_hooks(code) and "def generate_probes" in code:
probe_def_index = code.index("def generate_probes")
code = code[:probe_def_index] + (
"def ethical_review_hook(variations):\n"
" # Validate variations meet ethical standards\n"
" pass\n\n"
) + code[probe_def_index:]
# Add call in generate_probes
code = code.replace(
"def generate_probes",
"def generate_probes(self, num_pairs):\n"
" ethical_review_hook(self.VARIATIONS)",
1
)
return code
if __name__ == "__main__":
"""Command-line interface for validation"""
import argparse
parser = argparse.ArgumentParser(description="Validate generated audit code")
parser.add_argument("file", help="Path to code file")
parser.add_argument("--fix", action="store_true", help="Attempt auto-fix")
args = parser.parse_args()
with open(args.file, "r") as f:
code = f.read()
result = validate_generated_code(code)
print(f"Validation Status: {'VALID' if result['valid'] else 'INVALID'}")
print("\nDetailed Results:")
for check, passed in result["details"].items():
print(f"- {check}: {'✓' if passed else '✗'}")
if result["errors"]:
print("\nERRORS:")
for error in result["errors"]:
print(f"- {error}")
if result["warnings"]:
print("\nWARNINGS:")
for warning in result["warnings"]:
print(f"- {warning}")
if not result["valid"] and args.fix:
print("\nAttempting auto-fix...")
fixed_code = generate_autofix(code)
with open(args.file, "w") as f:
f.write(fixed_code)
print("Auto-fix applied. Please review changes.")