Skip to content

validation

code_context_agent.tools.validation

Input validation utilities for tool functions.

This module provides validation functions to prevent path traversal, command injection, and other security issues in user-provided inputs.

ValidationError

Bases: ValueError

Raised when input validation fails.

validate_repo_path

validate_repo_path(path)

Validate 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 path is dangerous or invalid.

Example

validate_repo_path("/home/user/project") PosixPath('/home/user/project')

Source code in src/code_context_agent/tools/validation.py
def validate_repo_path(path: str) -> Path:
    """Validate repository path is safe to use.

    Args:
        path: User-provided path string.

    Returns:
        Resolved Path object.

    Raises:
        ValidationError: If path is dangerous or invalid.

    Example:
        >>> validate_repo_path("/home/user/project")
        PosixPath('/home/user/project')
    """
    resolved = Path(path).resolve()

    # Prevent path traversal
    if ".." in path:
        raise ValidationError(f"Path traversal detected: {path}")

    # Prevent system paths
    dangerous = {"/", "/etc", "/usr", "/var", "/bin", "/sbin", "/root", "/boot"}
    if str(resolved) in dangerous:
        raise ValidationError(f"Dangerous system path: {path}")

    # Must exist and be directory
    if not resolved.exists():
        raise ValidationError(f"Path does not exist: {path}")
    if not resolved.is_dir():
        raise ValidationError(f"Path is not a directory: {path}")

    return resolved

validate_file_path

validate_file_path(path, must_exist=True)

Validate file path is safe.

Parameters:

Name Type Description Default
path str

User-provided file path.

required
must_exist bool

If True, file must exist.

True

Returns:

Type Description
Path

Resolved Path object.

Raises:

Type Description
ValidationError

If path is dangerous or invalid.

Source code in src/code_context_agent/tools/validation.py
def validate_file_path(path: str, must_exist: bool = True) -> Path:
    """Validate file path is safe.

    Args:
        path: User-provided file path.
        must_exist: If True, file must exist.

    Returns:
        Resolved Path object.

    Raises:
        ValidationError: If path is dangerous or invalid.
    """
    resolved = Path(path).resolve()

    # Prevent path traversal
    if ".." in path:
        raise ValidationError(f"Path traversal detected: {path}")

    # Must exist if required
    if must_exist and not resolved.exists():
        raise ValidationError(f"File does not exist: {path}")

    # Must be file if exists
    if resolved.exists() and not resolved.is_file():
        raise ValidationError(f"Path is not a file: {path}")

    return resolved

validate_glob_pattern

validate_glob_pattern(pattern)

Validate 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 pattern contains dangerous characters.

Source code in src/code_context_agent/tools/validation.py
def validate_glob_pattern(pattern: str) -> str:
    """Validate glob pattern is safe.

    Args:
        pattern: User-provided glob pattern.

    Returns:
        Validated pattern string.

    Raises:
        ValidationError: If pattern contains dangerous characters.
    """
    # Prevent command injection via glob
    dangerous_chars = re.compile(r"[;&|`$(){}\\]")
    if dangerous_chars.search(pattern):
        raise ValidationError(f"Invalid characters in pattern: {pattern}")

    # Prevent path traversal
    if ".." in pattern:
        raise ValidationError(f"Path traversal in pattern: {pattern}")

    return pattern

validate_path_within_repo

validate_path_within_repo(path, repo_root)

Validate that a path is contained within the 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 path escapes the repository root.

Source code in src/code_context_agent/tools/validation.py
def validate_path_within_repo(path: str, repo_root: str) -> Path:
    """Validate that a path is contained within the repository root.

    Args:
        path: Path to validate.
        repo_root: Repository root path.

    Returns:
        Resolved Path object.

    Raises:
        ValidationError: If path escapes the repository root.
    """
    resolved = Path(path).resolve()
    root = Path(repo_root).resolve()
    if resolved != root and not str(resolved).startswith(str(root) + "/"):
        raise ValidationError(f"Path {resolved} is outside repository root {root}")
    return resolved

validate_search_pattern

validate_search_pattern(pattern, max_length=1000)

Validate 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 pattern is invalid or too long.

Source code in src/code_context_agent/tools/validation.py
def validate_search_pattern(pattern: str, max_length: int = 1000) -> str:
    """Validate search pattern (regex) is safe.

    Args:
        pattern: User-provided search pattern.
        max_length: Maximum allowed pattern length.

    Returns:
        Validated pattern string.

    Raises:
        ValidationError: If pattern is invalid or too long.
    """
    if len(pattern) > max_length:
        raise ValidationError(f"Pattern too long: {len(pattern)} > {max_length}")

    # Try to compile regex to catch syntax errors early
    try:
        re.compile(pattern)
    except re.error as e:
        raise ValidationError(f"Invalid regex pattern: {e}") from e

    return pattern