Spaces:
Runtime error
Runtime error
Upload safety.py
Browse files
safety.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
class SafetyManager:
|
3 |
+
def __init__(self):
|
4 |
+
self.banned_phrases = ["delete all", "shutdown", "format", "upload private"]
|
5 |
+
self.allowed_actions = set()
|
6 |
+
self.audit_log = []
|
7 |
+
|
8 |
+
def is_safe_command(self, command: str) -> bool:
|
9 |
+
for phrase in self.banned_phrases:
|
10 |
+
if phrase in command.lower():
|
11 |
+
self.log_event(f"Blocked unsafe command: '{command}'")
|
12 |
+
return False
|
13 |
+
return True
|
14 |
+
|
15 |
+
def authorize_action(self, action_name: str):
|
16 |
+
self.allowed_actions.add(action_name)
|
17 |
+
|
18 |
+
def revoke_action(self, action_name: str):
|
19 |
+
self.allowed_actions.discard(action_name)
|
20 |
+
|
21 |
+
def is_action_allowed(self, action_name: str) -> bool:
|
22 |
+
allowed = action_name in self.allowed_actions
|
23 |
+
if not allowed:
|
24 |
+
self.log_event(f"Unauthorized action attempt: '{action_name}'")
|
25 |
+
return allowed
|
26 |
+
|
27 |
+
def log_event(self, entry: str):
|
28 |
+
self.audit_log.append(entry)
|
29 |
+
|
30 |
+
def get_audit_log(self):
|
31 |
+
return self.audit_log
|