security
strands_teams.security ¶
Security guards: path validation, bounded shell execution, write confinement.
Adapted from the pre-split code_context_agent package: - Path validators from tools/validation.py. - run_command / CommandResult from tools/shell.py. - confine_write_path is new: it confines artifact writes to an output-dir root.
Domain-agnostic: no use-case vocabulary. Keeps the framework layer testable without any strands import.
ValidationError ¶
Bases: ValueError
Raised when input validation fails.
CommandResult ¶
Bases: TypedDict
Result of a shell command execution.
validate_repo_path ¶
Validate a repository path is safe to use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | User-provided path string. | required |
Returns:
| Type | Description |
|---|---|
Path | Resolved Path object. |
Raises:
| Type | Description |
|---|---|
ValidationError | If the path is dangerous or invalid. |
Example
validate_repo_path("/home/user/project") # doctest: +SKIP PosixPath('/home/user/project')
Source code in packages/strands-teams/src/strands_teams/security.py
validate_file_path ¶
Validate a file path is safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | User-provided file path. | required |
must_exist | bool | If True, the file must exist. | True |
Returns:
| Type | Description |
|---|---|
Path | Resolved Path object. |
Raises:
| Type | Description |
|---|---|
ValidationError | If the path is dangerous or invalid. |
Source code in packages/strands-teams/src/strands_teams/security.py
validate_glob_pattern ¶
Validate a glob pattern is safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern | str | User-provided glob pattern. | required |
Returns:
| Type | Description |
|---|---|
str | Validated pattern string. |
Raises:
| Type | Description |
|---|---|
ValidationError | If the pattern contains dangerous characters. |
Source code in packages/strands-teams/src/strands_teams/security.py
validate_path_within_repo ¶
Validate that a path is contained within a repository root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | Path to validate. | required |
repo_root | str | Repository root path. | required |
Returns:
| Type | Description |
|---|---|
Path | Resolved Path object. |
Raises:
| Type | Description |
|---|---|
ValidationError | If the path escapes the repository root. |
Source code in packages/strands-teams/src/strands_teams/security.py
validate_search_pattern ¶
Validate a search pattern (regex) is safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern | str | User-provided search pattern. | required |
max_length | int | Maximum allowed pattern length. | 1000 |
Returns:
| Type | Description |
|---|---|
str | Validated pattern string. |
Raises:
| Type | Description |
|---|---|
ValidationError | If the pattern is invalid or too long. |
Source code in packages/strands-teams/src/strands_teams/security.py
confine_write_path ¶
Confine an artifact write target to the output-directory root.
Guards the artifact-writing tools so a team agent cannot write outside the run's output directory. Rejects traversal and absolute escapes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | Requested write path (absolute or relative to | required |
output_root | str | Path | The run's output-directory root that writes must stay under. | required |
Returns:
| Type | Description |
|---|---|
Path | Resolved Path object guaranteed to be at or under |
Raises:
| Type | Description |
|---|---|
ValidationError | If the resolved path escapes |
Source code in packages/strands-teams/src/strands_teams/security.py
run_command ¶
Run a shell command with bounds.
Uses shell=False with shlex parsing for security. For commands requiring shell features (pipes, redirects), pass a list like ["sh", "-c", "cmd"].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd | str | list[str] | Command string or list of arguments. | required |
cwd | str | None | Working directory. | None |
timeout | int | Maximum execution time in seconds. | 120 |
max_output | int | Maximum characters to capture. | 100000 |
input_data | str | None | Optional string to send to stdin. | None |
Returns:
| Type | Description |
|---|---|
CommandResult | Dict with status, stdout, stderr, return_code, and truncated flag. |