Index
security
¶
Security guardrails — scanners, engine wrapper, audit, SSRF.
Classes¶
BaseScanner
¶
Bases: ABC
Base class for all security scanners.
Subclasses implement pattern-based scanning for secrets, PII, or other sensitive content.
Methods:¶
scan
abstractmethod
¶
scan(text: str) -> ScanResult
AuditLogger
¶
AuditLogger(
db_path: Union[str, Path] = DEFAULT_CONFIG_DIR
/ "audit.db",
bus: Optional[EventBus] = None,
)
Append-only SQLite audit log for security events.
| PARAMETER | DESCRIPTION |
|---|---|
db_path
|
Path to the SQLite database file.
TYPE:
|
bus
|
Optional event bus — if provided, subscribes to security events
(
TYPE:
|
Source code in src/diapason/security/audit.py
Methods:¶
log
¶
log(event: SecurityEvent) -> None
Insert a security event into the audit log with Merkle hash chain.
Source code in src/diapason/security/audit.py
query
¶
query(
*,
event_type: Optional[str] = None,
since: Optional[float] = None,
limit: int = 100,
) -> List[SecurityEvent]
Query logged security events with optional filters.
Source code in src/diapason/security/audit.py
tail_hash
¶
Return the hash of the last row in the chain, or empty string.
Source code in src/diapason/security/audit.py
verify_chain
¶
Verify the Merkle hash chain integrity.
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
|
Source code in src/diapason/security/audit.py
count
¶
Return the total number of logged security events.
BoundaryGuard
¶
BoundaryGuard(
mode: str = "redact",
*,
enabled: bool = True,
bus: Optional["EventBus"] = None,
scanners: Optional[List["BaseScanner"]] = None,
)
Scans outbound content for secrets and PII at device boundaries.
| PARAMETER | DESCRIPTION |
|---|---|
mode
|
Action on findings:
TYPE:
|
enabled
|
Master switch. When
TYPE:
|
bus
|
Optional event bus for publishing SECURITY_ALERT events.
TYPE:
|
scanners
|
Custom scanners. Defaults to SecretScanner + PIIScanner.
TYPE:
|
Source code in src/diapason/security/boundary.py
Methods:¶
scan_outbound
¶
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.
Source code in src/diapason/security/boundary.py
check_outbound
¶
Scan tool call arguments before execution.
Returns a new ToolCall with redacted arguments if needed.
Source code in src/diapason/security/boundary.py
redact_for_storage
¶
Always redact secrets and PII before telemetry or trace storage.
Source code in src/diapason/security/boundary.py
GuardrailsEngine
¶
GuardrailsEngine(
engine: InferenceEngine,
*,
scanners: Optional[List[BaseScanner]] = None,
mode: RedactionMode = REDACT,
scan_input: bool = True,
scan_output: bool = True,
bus: Optional[EventBus] = None,
)
Bases: InferenceEngine
Wraps an existing InferenceEngine with security scanning.
Not registered in EngineRegistry — instantiated dynamically to wrap
any engine at runtime.
| PARAMETER | DESCRIPTION |
|---|---|
engine
|
The wrapped inference engine.
TYPE:
|
scanners
|
List of scanners to run. Defaults to
TYPE:
|
mode
|
Action taken on findings: WARN, REDACT, or BLOCK.
TYPE:
|
scan_input
|
Whether to scan input messages.
TYPE:
|
scan_output
|
Whether to scan output content.
TYPE:
|
bus
|
Optional event bus for publishing security events.
TYPE:
|
Source code in src/diapason/security/guardrails.py
Attributes¶
Methods:¶
generate
¶
generate(
messages: Sequence[Message],
*,
model: str,
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs: Any,
) -> Dict[str, Any]
Scan input, call wrapped engine, scan output.
Source code in src/diapason/security/guardrails.py
stream
async
¶
stream(
messages: Sequence[Message],
*,
model: str,
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs: Any,
) -> AsyncIterator[str]
Use the same safe boundaries as rich output, including long secrets.
Source code in src/diapason/security/guardrails.py
stream_full
async
¶
stream_full(
messages: Sequence[Message],
*,
model: str,
temperature: float = 0.7,
max_tokens: int = 1024,
**kwargs: Any,
) -> AsyncIterator[StreamChunk]
Stream verified text without releasing tools or terminal data early.
Source code in src/diapason/security/guardrails.py
list_models
¶
SecurityBlockError
¶
Bases: Exception
Raised when mode is BLOCK and security findings are detected.
PIIScanner
¶
Bases: BaseScanner
Detect personally identifiable information in text.
Source code in src/diapason/security/scanner.py
Methods:¶
scan
¶
scan(text: str) -> ScanResult
Scan text for PII patterns using Rust or the safe fallback.
Source code in src/diapason/security/scanner.py
redact
¶
Replace PII matches with [REDACTED:{pattern_name}].
SecretScanner
¶
Bases: BaseScanner
Detect API keys, tokens, passwords, and other secrets in text.
Source code in src/diapason/security/scanner.py
Methods:¶
scan
¶
scan(text: str) -> ScanResult
Scan text for secret patterns using Rust or the safe fallback.
Source code in src/diapason/security/scanner.py
redact
¶
Replace secret matches with [REDACTED:{pattern_name}].
RedactionMode
¶
Bases: str, Enum
Action mode when findings are detected.
ScanFinding
dataclass
¶
ScanFinding(
pattern_name: str,
matched_text: str,
threat_level: ThreatLevel,
start: int,
end: int,
description: str = "",
)
A single finding from a security scanner.
ScanResult
dataclass
¶
ScanResult(findings: List[ScanFinding] = list())
Aggregated result from one or more scanners.
Attributes¶
highest_threat
property
¶
highest_threat: Optional[ThreatLevel]
Return the highest threat level among findings, or None.
SecurityEvent
dataclass
¶
SecurityEvent(
event_type: SecurityEventType,
timestamp: float,
findings: List[ScanFinding] = list(),
content_preview: str = "",
action_taken: str = "",
)
A recorded security event for audit logging.
SecurityEventType
¶
Bases: str, Enum
Categories of security events.
ThreatLevel
¶
Bases: str, Enum
Severity classification for security findings.
SecurityContext
dataclass
¶
SecurityContext(
engine: Any,
capability_policy: Any = None,
audit_logger: Any = None,
boundary_guard: Any = None,
rate_limiter: Any = None,
)
Result of setup_security() — wrapped engine, policy, audit.
Functions:¶
filter_sensitive_paths
¶
is_sensitive_file
¶
Return True if path matches a sensitive file pattern.
Checks both the filename and the full name against
DEFAULT_SENSITIVE_PATTERNS using :func:fnmatch.fnmatch.
Uses the Rust implementation when available, falls back to Python.
Source code in src/diapason/security/file_policy.py
check_ssrf
¶
Check a URL for SSRF vulnerabilities.
Prefers the Rust backend, but falls back to the pure-Python
implementation when the compiled extension is unavailable. The SSRF
guard is security-critical, so it must never be silently skipped — or
crash with ImportError — merely because Rust was not built.
Source code in src/diapason/security/ssrf.py
is_private_ip
¶
Check if an IP address is private/reserved.
Source code in src/diapason/security/ssrf.py
setup_security
¶
setup_security(
config: Any, engine: Any, bus: Optional[EventBus] = None
) -> SecurityContext
Apply security guardrails to an engine based on config.
Returns a SecurityContext. No-ops if config.security.enabled is False.