Skip to content

API Reference parrot.utils.paths

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

parrot.utils.paths

View source

Filesystem path helpers with path-traversal protection.

Several identifiers used to build on-disk paths (agent_id, chatbot_id, filenames, …) reach the framework straight from an HTTP payload, which makes every base / identifier join a path-traversal sink (CWE-22 / CodeQL py/path-injection). The helpers here apply two independent barriers:

  1. every untrusted segment must match :data:SAFE_SEGMENT_RE — a single path component: no separators, no .., no leading dot;
  2. the joined path is normalised and verified to stay inside base before it is used for any filesystem access.

The containment check deliberately uses :func:os.path.normpath (pure string normalisation) instead of :meth:pathlib.Path.resolve: resolve() touches the filesystem, so calling it on a not-yet-validated path would be the very access the guard is meant to prevent.

Attribute SAFE_SEGMENT_RE

Function secure_path

Join segments under base, refusing anything that escapes it.

secure_path(base: Union[str, Path], *segments: str, name: str = 'path segment') -> Path

Parameter Type Default Description
base Union[str, Path] Trusted root directory the result must stay within.
*segments str () Path components to append. Each one is validated with :func:validate_path_segment.
name str 'path segment' Human-readable name of the untrusted value, used in error messages.

Returns: Path — The canonical path — normalised and symlink-resolved, as meth:pathlib.Path.resolve would return — guaranteed to be base itself or a descendant of it.

Raises:

  • ValueError — If a segment is unsafe or the joined path would escape base.

Function validate_path_segment

Validate that value is a safe single path component.

validate_path_segment(value: str, name: str = 'path segment') -> str

Parameter Type Default Description
value str The candidate path component (typically untrusted input).
name str 'path segment' Human-readable name of the value, used in the error message.

Returns: str — The validated value, unchanged.

Raises:

  • ValueError — If value is not a string or does not match :data:SAFE_SEGMENT_RE.

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