Från mina egna experiment:
"from dataclasses import dataclass
import re
@dataclass
class Heuristic:
name: str
weight: float
check: callable
class SafetyPolicy:
def __init__(self):
self.heuristics = []
def add(self, heuristic):
self.heuristics.append(heuristic)
def evaluate(self, text):
score = 0.0
matches = []
for h in self.heuristics:
if h.check(text):
score += h.weight
matches.append(h.name)
return {
"risk_score": score,
"matched": matches,
"allow": score < 1.5
}
policy = SafetyPolicy()
policy.add(
Heuristic(
"Credential harvesting",
1.0,
lambda t: bool(re.search(r"\b(password|login|credential)\b", t, re.I))
)
)
policy.add(
Heuristic(
"Explicit malicious intent",
1.2,
lambda t: any(
word in t.lower()
for word in [
"steal",
"bypass",
"evade",
"disable security"
]
)
)
)
policy.add(
Heuristic(
"Mass automation",
0.6,
lambda t: any(
word in t.lower()
for word in [
"thousands",
"bulk",
"automatically"
]
)
)
)
examples = [
"Write a login page.",
"Automatically collect passwords from thousands of users.",
"Explain password hashing.",
]
for e in examples:
print(e)
print(policy.evaluate(e))
print()" där utfallet blir "Write a login page.
{'risk_score': 0.0, 'matched': [], 'allow': True}
Automatically collect passwords from thousands of users.
{
'risk_score': 2.8,
'matched': [
'Credential harvesting',
'Explicit malicious intent',
'Mass automation'
],
'allow': False
}
Explain password hashing.
{'risk_score': 1.0, 'matched': ['Credential harvesting'], 'allow': True}"