Skip to content

API Reference parrot.security.python_sanitizer

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.security.python_sanitizer

View source

Allowlist-first AST gate for Python code executed in the REPL sandbox.

Introduced in FEAT-252 (TASK-1614) as WS1 — the primary code containment layer. An allowlist-first policy decides which import names, builtins, and operations are permitted. Categorical denials (env access, introspection, dynamic exec, data IO) fire regardless of the allowlist as a defence-in-depth layer alongside the existing PythonREPLTool._check_ast_security denylist.

Alias CommandVerdict

Re-exported from parrot.security.command_sanitizer.CommandVerdict.

Class PythonCodeSanitizer

Allowlist-first AST gate for Python code.

Walks the AST of code and checks each import statement, name reference, and function call against the active PythonExecutionPolicy.

Categorical denials (env / introspection / dynamic-exec / data-IO) fire regardless of the allowlist, providing a belt-and-suspenders defence on top of the existing PythonREPLTool._check_ast_security denylist.

Parameter Type Default Description
policy Optional[PythonExecutionPolicy] None Execution policy to apply. Defaults to general_profile().

Method PythonCodeSanitizer.validate

Validate code against the active policy.

validate(code: str) -> ValidationResult

Parameter Type Default Description
code str Python source code to validate.

Returns: ValidationResult — ValidationResult with verdict=ALLOW or verdict=DENY. On deny, reasons lists why each denial fired.

Class PythonExecutionPolicy

Policy controlling the PythonCodeSanitizer allowlist-first gate.

Parameter Type Default Description
level SecurityLevel SecurityLevel.RESTRICTIVE
default_deny bool True
allowed_imports FrozenSet[str] frozenset()
allowed_builtins FrozenSet[str] frozenset()
deny_env_access bool True
deny_introspection bool True
deny_dynamic_exec bool True
deny_data_io bool True
isolation str 'in_process'
max_output_bytes int 1048576

Fields:

Field Type Default Description
level SecurityLevel SecurityLevel.RESTRICTIVE The SecurityLevel posture (default RESTRICTIVE).
default_deny bool True When True (default), any import / call / name NOT on the allowlist is denied. Set to False only for trusted contexts.
allowed_imports FrozenSet[str] field(default_factory=frozenset) Frozenset of module root names that may be imported.
allowed_builtins FrozenSet[str] field(default_factory=frozenset) Frozenset of builtin names that may be called. Wired into _check_name: when default_deny=True and this set is non-empty, any name not in this allowlist is denied (spec §WS1).
deny_env_access bool True Deny all os.environ / os.getenv access (default True).
deny_introspection bool True Deny globals, locals, __class__.__bases__ etc. (default True).
deny_dynamic_exec bool True Deny eval, exec, compile, __import__ (default True).
deny_data_io bool True Deny file/network/DB IO (default True).
isolation str 'in_process' Execution isolation mode ("in_process" — subprocess is a Non-Goal).
max_output_bytes int 1048576 Maximum allowed output size in bytes.

Alias SecurityLevel

Re-exported from parrot.security.command_sanitizer.SecurityLevel.

Alias ValidationResult

Re-exported from parrot.security.command_sanitizer.ValidationResult.

Function data_analysis_profile

Return the data-analysis execution policy.

Widens the allowlist for pandas/numpy compute on already-materialised DataFrames injected via REPL locals. File / network IO remains denied.

data_analysis_profile() -> PythonExecutionPolicy

Returns: PythonExecutionPolicy — PythonExecutionPolicy with a broader but still restricted import allowlist.

Function general_profile

Return the general (tightest) execution policy.

Suitable for Jira/GitHub/tool-orchestration agents that consume data via structured tools rather than raw REPL IO.

general_profile() -> PythonExecutionPolicy

Returns: PythonExecutionPolicy — PythonExecutionPolicy with restricted import/builtin allowlists.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally