Permissions API¶
Types¶
PermissionAction¶
pydantic_ai_backends.permissions.types.PermissionAction = Literal['allow', 'deny', 'ask']
module-attribute
¶
PermissionOperation¶
pydantic_ai_backends.permissions.types.PermissionOperation = Literal['read', 'write', 'edit', 'execute', 'glob', 'grep', 'ls']
module-attribute
¶
PermissionRule¶
pydantic_ai_backends.permissions.types.PermissionRule
¶
Bases: BaseModel
A rule that matches paths/commands and specifies an action.
Rules are evaluated in order - first matching rule wins.
Patterns use fnmatch-style matching with support for ** (recursive).
Example
Source code in src/pydantic_ai_backends/permissions/types.py
pattern
instance-attribute
¶
Glob pattern to match against paths or commands.
Supports fnmatch patterns:
- * matches any characters except /
- ** matches any characters including / (recursive)
- ? matches any single character
- [seq] matches any character in seq
action
instance-attribute
¶
Action to take when pattern matches: "allow", "deny", or "ask".
description = ''
class-attribute
instance-attribute
¶
Human-readable description of why this rule exists.
OperationPermissions¶
pydantic_ai_backends.permissions.types.OperationPermissions
¶
Bases: BaseModel
Permissions configuration for a single operation type.
Contains a default action and a list of rules that override the default for specific patterns.
Example
Source code in src/pydantic_ai_backends/permissions/types.py
PermissionRuleset¶
pydantic_ai_backends.permissions.types.PermissionRuleset
¶
Bases: BaseModel
Complete permissions configuration for all operations.
Defines default behavior and per-operation permissions. Each operation can have its own default and rules.
Example
Source code in src/pydantic_ai_backends/permissions/types.py
default = 'ask'
class-attribute
instance-attribute
¶
Global default action when operation has no specific config.
read = None
class-attribute
instance-attribute
¶
Permissions for read operations.
write = None
class-attribute
instance-attribute
¶
Permissions for write operations.
edit = None
class-attribute
instance-attribute
¶
Permissions for edit operations.
execute = None
class-attribute
instance-attribute
¶
Permissions for execute operations (shell commands).
glob = None
class-attribute
instance-attribute
¶
Permissions for glob operations.
grep = None
class-attribute
instance-attribute
¶
Permissions for grep operations.
ls = None
class-attribute
instance-attribute
¶
Permissions for ls operations.
get_operation_permissions(operation)
¶
Get permissions for a specific operation.
Returns the operation-specific permissions if defined, otherwise creates default permissions using the global default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
PermissionOperation
|
The operation type to get permissions for. |
required |
Returns:
| Type | Description |
|---|---|
OperationPermissions
|
OperationPermissions for the specified operation. |
Source code in src/pydantic_ai_backends/permissions/types.py
Checker¶
PermissionChecker¶
pydantic_ai_backends.permissions.checker.PermissionChecker
¶
Checks operations against a permission ruleset.
The checker evaluates rules in order and uses the first matching rule's action. If no rule matches, the operation's default action is used. If no operation-specific permissions exist, the global default is used.
Example
from pydantic_ai_backends.permissions import (
PermissionChecker,
DEFAULT_RULESET,
)
async def ask_user(op: str, target: str, reason: str) -> bool:
return input(f"Allow {op} on {target}? ").lower() == "y"
checker = PermissionChecker(
ruleset=DEFAULT_RULESET,
ask_callback=ask_user,
)
# Synchronous check (returns action without asking)
action = checker.check_sync("read", "/path/to/file")
# Async check (handles "ask" via callback)
allowed = await checker.check("write", "/path/to/file", "Save changes")
Source code in src/pydantic_ai_backends/permissions/checker.py
| Python | |
|---|---|
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
ruleset
property
¶
The permission ruleset being used.
__init__(ruleset, ask_callback=None, ask_fallback='error')
¶
Initialize the permission checker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ruleset
|
PermissionRuleset
|
The permission ruleset to check against. |
required |
ask_callback
|
AskCallback | None
|
Async callback for "ask" actions. Receives (operation, target, reason) and returns True to allow. |
None
|
ask_fallback
|
AskFallback
|
What to do when ask_callback is None or needed but not available. "deny" returns False, "error" raises. |
'error'
|
Source code in src/pydantic_ai_backends/permissions/checker.py
check_sync(operation, target)
¶
Check permission synchronously without invoking callbacks.
This method evaluates rules and returns the action without executing any callbacks. Use this when you need to know what action would be taken.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
PermissionOperation
|
The operation type (read, write, etc.). |
required |
target
|
str
|
The path or command being accessed. |
required |
Returns:
| Type | Description |
|---|---|
PermissionAction
|
The permission action: "allow", "deny", or "ask". |
Source code in src/pydantic_ai_backends/permissions/checker.py
check(operation, target, reason='')
async
¶
Check permission asynchronously with callback support.
Evaluates rules and handles "ask" actions via the callback. For "allow" returns True, for "deny" raises PermissionDeniedError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
PermissionOperation
|
The operation type (read, write, etc.). |
required |
target
|
str
|
The path or command being accessed. |
required |
reason
|
str
|
Human-readable reason for the operation. |
''
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the operation is allowed. |
Raises:
| Type | Description |
|---|---|
PermissionDeniedError
|
If the operation is explicitly denied. |
PermissionError
|
If ask_fallback="error" and callback unavailable. |
Source code in src/pydantic_ai_backends/permissions/checker.py
is_allowed(operation, target)
¶
Check if an operation would be immediately allowed.
This is a convenience method that returns True only if the operation would be allowed without needing to ask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
PermissionOperation
|
The operation type. |
required |
target
|
str
|
The path or command. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if action is "allow", False otherwise. |
Source code in src/pydantic_ai_backends/permissions/checker.py
is_denied(operation, target)
¶
Check if an operation would be immediately denied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
PermissionOperation
|
The operation type. |
required |
target
|
str
|
The path or command. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if action is "deny", False otherwise. |
Source code in src/pydantic_ai_backends/permissions/checker.py
requires_approval(operation, target)
¶
Check if an operation would require user approval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
PermissionOperation
|
The operation type. |
required |
target
|
str
|
The path or command. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if action is "ask", False otherwise. |
Source code in src/pydantic_ai_backends/permissions/checker.py
PermissionError¶
pydantic_ai_backends.permissions.checker.PermissionError
¶
Bases: Exception
Raised when a permission check fails with ask_fallback="error".
Source code in src/pydantic_ai_backends/permissions/checker.py
PermissionDeniedError¶
pydantic_ai_backends.permissions.checker.PermissionDeniedError
¶
Bases: Exception
Raised when a permission is explicitly denied.
Source code in src/pydantic_ai_backends/permissions/checker.py
Presets¶
DEFAULT_RULESET¶
pydantic_ai_backends.permissions.presets.DEFAULT_RULESET = PermissionRuleset(default='ask', read=(OperationPermissions(default='allow', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), write=(OperationPermissions(default='ask', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), edit=(OperationPermissions(default='ask', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), execute=(OperationPermissions(default='ask', rules=(_create_deny_rules(DANGEROUS_COMMANDS, 'Block dangerous commands')))), glob=(OperationPermissions(default='allow')), grep=(OperationPermissions(default='allow')), ls=(OperationPermissions(default='allow')))
module-attribute
¶
PERMISSIVE_RULESET¶
pydantic_ai_backends.permissions.presets.PERMISSIVE_RULESET = PermissionRuleset(default='allow', read=(OperationPermissions(default='allow', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), write=(OperationPermissions(default='allow', rules=(_create_deny_rules(SECRETS_PATTERNS + SYSTEM_PATTERNS, 'Protect sensitive and system files')))), edit=(OperationPermissions(default='allow', rules=(_create_deny_rules(SECRETS_PATTERNS + SYSTEM_PATTERNS, 'Protect sensitive and system files')))), execute=(OperationPermissions(default='allow', rules=(_create_deny_rules(DANGEROUS_COMMANDS, 'Block dangerous commands')))), glob=(OperationPermissions(default='allow')), grep=(OperationPermissions(default='allow')), ls=(OperationPermissions(default='allow')))
module-attribute
¶
READONLY_RULESET¶
pydantic_ai_backends.permissions.presets.READONLY_RULESET = PermissionRuleset(default='deny', read=(OperationPermissions(default='allow', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), write=(OperationPermissions(default='deny')), edit=(OperationPermissions(default='deny')), execute=(OperationPermissions(default='deny')), glob=(OperationPermissions(default='allow')), grep=(OperationPermissions(default='allow')), ls=(OperationPermissions(default='allow')))
module-attribute
¶
STRICT_RULESET¶
pydantic_ai_backends.permissions.presets.STRICT_RULESET = PermissionRuleset(default='ask', read=(OperationPermissions(default='ask', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), write=(OperationPermissions(default='ask', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), edit=(OperationPermissions(default='ask', rules=(_create_deny_rules(SECRETS_PATTERNS, 'Protect sensitive files')))), execute=(OperationPermissions(default='ask', rules=(_create_deny_rules(DANGEROUS_COMMANDS, 'Block dangerous commands')))), glob=(OperationPermissions(default='ask')), grep=(OperationPermissions(default='ask')), ls=(OperationPermissions(default='ask')))
module-attribute
¶
create_ruleset¶
pydantic_ai_backends.permissions.presets.create_ruleset(*, default='ask', allow_read=True, allow_write=False, allow_edit=False, allow_execute=False, allow_glob=True, allow_grep=True, allow_ls=True, deny_secrets=True)
¶
Create a custom permission ruleset.
A convenience factory for creating rulesets with common configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
str
|
Global default action ("allow", "deny", or "ask"). |
'ask'
|
allow_read
|
bool
|
Whether to allow read operations by default. |
True
|
allow_write
|
bool
|
Whether to allow write operations by default. |
False
|
allow_edit
|
bool
|
Whether to allow edit operations by default. |
False
|
allow_execute
|
bool
|
Whether to allow execute operations by default. |
False
|
allow_glob
|
bool
|
Whether to allow glob operations by default. |
True
|
allow_grep
|
bool
|
Whether to allow grep operations by default. |
True
|
allow_ls
|
bool
|
Whether to allow ls operations by default. |
True
|
deny_secrets
|
bool
|
Whether to deny access to sensitive file patterns. |
True
|
Returns:
| Type | Description |
|---|---|
PermissionRuleset
|
A configured PermissionRuleset. |