Skip to content

Personal AI, On Personal Devices

Python

feat: BoundaryGuard module — scan/redact/block at device exit points

Jon Saad-Falcon7c1f98389155
diff --git a/src/openjarvis/security/boundary.py b/src/openjarvis/security/boundary.py
new file mode 100644
index 00000000..8b5572cf
--- /dev/null
+++ b/src/openjarvis/security/boundary.py
@@ -0,0 +1,130 @@
+"""BoundaryGuard — scans content at device exit points.
+
+Wraps SecretScanner and PIIScanner to redact, warn, or block
+secrets and PII before data leaves the device via cloud engines
+or external tool calls.
+"""
+
+from __future__ import annotations
+
+import logging
+from dataclasses import replace
+from typing import TYPE_CHECKING, List, Optional
+
+from openjarvis.core.types import ToolCall
+
+if TYPE_CHECKING:
+ from openjarvis.core.events import EventBus
+ from openjarvis.security._stubs import BaseScanner
+
+logger = logging.getLogger(__name__)
+
+
+class SecurityBlockError(Exception):
+ """Raised when mode='block' and secrets/PII are detected."""
+
+
+class BoundaryGuard:
+ """Scans outbound content for secrets and PII at device boundaries.
+
+ Parameters
+ ----------
+ mode:
+ Action on findings: ``"redact"`` replaces matches,
+ ``"warn"`` logs but passes through, ``"block"`` raises.
+ enabled:
+ Master switch. When ``False``, all content passes through.
+ bus:
+ Optional event bus for publishing SECURITY_ALERT events.
+ scanners:
+ Custom scanners. Defaults to SecretScanner + PIIScanner.
+ """
+
+ def __init__(
+ self,
+ mode: str = "redact",
+ *,
+ enabled: bool = True,
+ bus: Optional["EventBus"] = None,
+ scanners: Optional[List["BaseScanner"]] = None,
+ ) -> None:
+ self._mode = mode
+ self._enabled = enabled
+ self._bus = bus
+ if scanners is not None:
+ self._scanners = scanners
+ else:
+ self._scanners = self._default_scanners()
+
+ @staticmethod
+ def _default_scanners() -> List["BaseScanner"]:
+ from openjarvis.security.scanner import PIIScanner, SecretScanner
+
+ return [SecretScanner(), PIIScanner()]
+
+ def scan_outbound(self, content: str, destination: str) -> str:
+ """Scan text before it leaves the device.
+
+ Returns redacted text in ``"redact"`` mode, original text in
+ ``"warn"`` mode, or raises ``SecurityBlockError`` in ``"block"``
+ mode when findings are detected.
+ """
+ if not self._enabled or not content:
+ return content
+
+ has_findings = False
+ redacted = content
+ for scanner in self._scanners:
+ result = scanner.scan(content)
+ if result.findings:
+ has_findings = True
+ if self._mode == "redact":
+ redacted = scanner.redact(redacted)
+
+ if has_findings:
+ self._emit_alert(destination, content)
+ if self._mode == "block":
+ raise SecurityBlockError(
+ f"Secrets/PII detected in outbound content to {destination}"
+ )
+ if self._mode == "warn":
+ logger.warning(
+ "Secrets/PII detected in outbound content to %s", destination
+ )
+ return content
+ return redacted
+
+ return content
+
+ def check_outbound(self, tool_call: ToolCall) -> ToolCall:
+ """Scan tool call arguments before execution.
+
+ Returns a new ToolCall with redacted arguments if needed.
+ """
+ if not self._enabled or not tool_call.arguments:
+ return tool_call
+
+ redacted_args = self.scan_outbound(
+ tool_call.arguments, destination=f"tool:{tool_call.name}"
+ )
+ if redacted_args != tool_call.arguments:
+ return replace(tool_call, arguments=redacted_args)
+ return tool_call
+
+ def _emit_alert(self, destination: str, content: str) -> None:
+ if self._bus is None:
+ return
+ try:
+ from openjarvis.core.events import EventType
+
+ self._bus.publish(
+ EventType.SECURITY_ALERT,
+ {
+ "source": "boundary_guard",
+ "destination": destination,
+ "mode": self._mode,
+ "content_preview": content[:80],
+ },
+ )
+ except Exception:
+ logger.debug("Failed to emit security alert event", exc_info=True)
diff --git a/tests/security/test_boundary_guard.py b/tests/security/test_boundary_guard.py
new file mode 100644
index 00000000..62499023
--- /dev/null
+++ b/tests/security/test_boundary_guard.py
@@ -0,0 +1,104 @@
+"""Tests for BoundaryGuard — scanning at device exit points."""
+
+from __future__ import annotations
+
+import pytest
+
+from openjarvis.core.types import ToolCall
+
+
+class TestBoundaryGuardScanOutbound:
+ """scan_outbound should detect and redact secrets/PII."""
+
+ def test_redacts_openai_key(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="redact")
+ text = "Use this key: sk-proj-abc123def456ghi789jkl012mno345pqr678stu"
+ result = guard.scan_outbound(text, destination="openai")
+ assert "sk-proj-" not in result
+ assert "[REDACTED" in result
+
+ def test_redacts_aws_key(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="redact")
+ text = "AWS key: AKIAIOSFODNN7EXAMPLE"
+ result = guard.scan_outbound(text, destination="openai")
+ assert "AKIAIOSFODNN7EXAMPLE" not in result
+
+ def test_warn_mode_does_not_alter_text(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="warn")
+ text = "Use this key: sk-proj-abc123def456ghi789jkl012mno345pqr678stu"
+ result = guard.scan_outbound(text, destination="openai")
+ assert result == text
+
+ def test_block_mode_raises(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard, SecurityBlockError
+
+ guard = BoundaryGuard(mode="block")
+ text = "Use this key: sk-proj-abc123def456ghi789jkl012mno345pqr678stu"
+ with pytest.raises(SecurityBlockError):
+ guard.scan_outbound(text, destination="openai")
+
+ def test_clean_text_passes_through(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="redact")
+ text = "Hello, how are you?"
+ result = guard.scan_outbound(text, destination="openai")
+ assert result == text
+
+
+class TestBoundaryGuardCheckOutbound:
+ """check_outbound should redact secrets in tool call arguments."""
+
+ def test_redacts_tool_call_arguments(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="redact")
+ tc = ToolCall(
+ id="test_1",
+ name="web_search",
+ arguments=(
+ '{"query": "my key is sk-proj-abc123def456ghi789jkl012mno345pqr678stu"}'
+ ),
+ )
+ result = guard.check_outbound(tc)
+ assert "sk-proj-" not in result.arguments
+ assert result.id == "test_1"
+ assert result.name == "web_search"
+
+ def test_clean_args_pass_through(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="redact")
+ tc = ToolCall(id="test_2", name="web_search", arguments='{"query": "weather"}')
+ result = guard.check_outbound(tc)
+ assert result.arguments == tc.arguments
+
+ def test_block_mode_raises_on_tool_call(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard, SecurityBlockError
+
+ guard = BoundaryGuard(mode="block")
+ tc = ToolCall(
+ id="test_3",
+ name="web_search",
+ arguments='{"query": "AKIAIOSFODNN7EXAMPLE"}',
+ )
+ with pytest.raises(SecurityBlockError):
+ guard.check_outbound(tc)
+
+
+class TestBoundaryGuardDisabled:
+ """When disabled, BoundaryGuard should pass everything through."""
+
+ def test_disabled_passes_secrets_through(self) -> None:
+ from openjarvis.security.boundary import BoundaryGuard
+
+ guard = BoundaryGuard(mode="redact", enabled=False)
+ text = "sk-proj-abc123def456ghi789jkl012mno345pqr678stu"
+ result = guard.scan_outbound(text, destination="openai")
+ assert result == text