Personal AI, On Personal Devices
Python
feat: add credential stripping, tool output wrapping, and severity policy
Tarun Suresh4cc16972ecbc
diff --git a/src/openjarvis/security/credential_stripper.py b/src/openjarvis/security/credential_stripper.pynew file mode 100644index 00000000..661b4c96--- /dev/null+++ b/src/openjarvis/security/credential_stripper.py@@ -0,0 +1,31 @@+from __future__ import annotations++import re+from typing import List, Tuple++_CREDENTIAL_PATTERNS: List[Tuple[str, re.Pattern[str]]] = [+ ("api_key", re.compile(r"sk-[a-zA-Z0-9_-]{20,}")),+ ("aws_key", re.compile(r"AKIA[0-9A-Z]{16}")),+ ("github_token", re.compile(r"ghp_[a-zA-Z0-9]{36}")),+ ("github_token", re.compile(r"gho_[a-zA-Z0-9]{36}")),+ ("slack_token", re.compile(r"xoxb-[0-9A-Za-z\-]+")),+ ("bearer_token", re.compile(r"Bearer\s+[a-zA-Z0-9_\-.]{20,}")),+]+++class CredentialStripper:+ """Redacts credentials from text using compiled regex patterns."""++ def __init__(self) -> None:+ self._patterns = _CREDENTIAL_PATTERNS++ def strip(self, text: str) -> str:+ for label, pattern in self._patterns:+ text = pattern.sub(f"[REDACTED:{label}]", text)+ return text+++def wrap_tool_output(tool_name: str, content: str, success: bool = True) -> str:+ status = "success" if success else "error"+ header = f'<tool_result name="{tool_name}" status="{status}">'+ return f"{header}\n{content}\n</tool_result>"diff --git a/src/openjarvis/security/severity_policy.py b/src/openjarvis/security/severity_policy.pynew file mode 100644index 00000000..4e8c93f9--- /dev/null+++ b/src/openjarvis/security/severity_policy.py@@ -0,0 +1,22 @@+from __future__ import annotations++from openjarvis.security.types import ThreatLevel++_DEFAULT_ACTIONS = {+ ThreatLevel.CRITICAL: "block",+ ThreatLevel.HIGH: "warn",+ ThreatLevel.MEDIUM: "sanitize",+ ThreatLevel.LOW: "log",+}+++class SeverityPolicy:+ """Maps ThreatLevel to configurable actions (block/warn/sanitize/log)."""++ def __init__(self, overrides: dict[ThreatLevel, str] | None = None) -> None:+ self._actions = dict(_DEFAULT_ACTIONS)+ if overrides:+ self._actions.update(overrides)++ def action_for(self, level: ThreatLevel) -> str:+ return self._actions.get(level, "log")diff --git a/tests/security/test_credential_stripper.py b/tests/security/test_credential_stripper.pynew file mode 100644index 00000000..899ea1b4--- /dev/null+++ b/tests/security/test_credential_stripper.py@@ -0,0 +1,44 @@+from __future__ import annotations+++def test_strips_openai_key():+ from openjarvis.security.credential_stripper import CredentialStripper+ stripper = CredentialStripper()+ text = "Error: auth failed with key sk-proj-abc123def456ghi789jkl012mno345pqr678stu901vwx234"+ result = stripper.strip(text)+ assert "sk-proj-" not in result+ assert "[REDACTED:" in result+++def test_strips_aws_key():+ from openjarvis.security.credential_stripper import CredentialStripper+ stripper = CredentialStripper()+ text = "Using credentials AKIAIOSFODNN7EXAMPLE for access"+ result = stripper.strip(text)+ assert "AKIA" not in result+ assert "[REDACTED:" in result+++def test_strips_github_token():+ from openjarvis.security.credential_stripper import CredentialStripper+ stripper = CredentialStripper()+ text = "Token: ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij"+ result = stripper.strip(text)+ assert "ghp_" not in result+++def test_preserves_normal_text():+ from openjarvis.security.credential_stripper import CredentialStripper+ stripper = CredentialStripper()+ text = "The function returned 42 results."+ result = stripper.strip(text)+ assert result == text+++def test_tool_output_wrapping():+ from openjarvis.security.credential_stripper import wrap_tool_output+ content = "Search results: found 3 items"+ wrapped = wrap_tool_output("web_search", content, success=True)+ assert '<tool_result name="web_search" status="success">' in wrapped+ assert "Search results" in wrapped+ assert "</tool_result>" in wrappeddiff --git a/tests/security/test_severity_policy.py b/tests/security/test_severity_policy.pynew file mode 100644index 00000000..907ca826--- /dev/null+++ b/tests/security/test_severity_policy.py@@ -0,0 +1,29 @@+from __future__ import annotations+++def test_severity_policy_block():+ from openjarvis.security.severity_policy import SeverityPolicy+ from openjarvis.security.types import ThreatLevel+ policy = SeverityPolicy()+ assert policy.action_for(ThreatLevel.CRITICAL) == "block"+++def test_severity_policy_warn():+ from openjarvis.security.severity_policy import SeverityPolicy+ from openjarvis.security.types import ThreatLevel+ policy = SeverityPolicy()+ assert policy.action_for(ThreatLevel.HIGH) == "warn"+++def test_severity_policy_sanitize():+ from openjarvis.security.severity_policy import SeverityPolicy+ from openjarvis.security.types import ThreatLevel+ policy = SeverityPolicy()+ assert policy.action_for(ThreatLevel.MEDIUM) == "sanitize"+++def test_severity_policy_log():+ from openjarvis.security.severity_policy import SeverityPolicy+ from openjarvis.security.types import ThreatLevel+ policy = SeverityPolicy()+ assert policy.action_for(ThreatLevel.LOW) == "log"