fix(actor): Validate and sanitize environment variable interpolation in config #10570

Open
opened 2026-04-18 17:22:29 +00:00 by HAL9000 · 0 comments
Owner

Metadata

Commit: April 6, 2026
Branch: main

Background and Context

The ActorConfiguration._interpolate_env_vars() method in src/cleveragents/actor/config.py reads environment variables and interpolates them into actor configurations without validation or sanitization. This creates a security risk if environment variables are set by untrusted sources or if the interpolated values are used in sensitive contexts (e.g., database queries, file paths, or system commands).

Code Evidence

Lines 155-175 in src/cleveragents/actor/config.py:

@staticmethod
def _interpolate_env_vars(config: Any) -> Any:
    if isinstance(config, dict):
        config_dict = cast(dict[str, Any], config)
        return {
            k: ActorConfiguration._interpolate_env_vars(v)
            for k, v in config_dict.items()
        }
    if isinstance(config, list):
        config_list = cast(list[Any], config)
        return [ActorConfiguration._interpolate_env_vars(i) for i in config_list]
    if isinstance(config, str):
        env_var_pattern = r"\${([A-Za-z0-9_]+)(?::([^}]*))?\}"

        def replace_env_var(match: re.Match[str]) -> str:
            env_var = match.group(1)
            default_value = match.group(2) if match.group(2) is not None else None

            env_value = os.environ.get(env_var)
            if env_value is None:
                if default_value is not None:
                    # ... use default ...
                    return default_value
                raise ValueError(f"Environment variable '{env_var}' is not set")
            return env_value  # <-- No validation or sanitization!

The function directly returns environment variable values without any validation. An attacker who can set environment variables could inject:

  • Malicious file paths
  • SQL injection payloads
  • Command injection payloads
  • YAML syntax that breaks the configuration

Environment Verification

This is reproducible by:

  1. Setting an environment variable with malicious content: export ACTOR_PROMPT="'; DROP TABLE actors; --"
  2. Creating an actor config that uses ${ACTOR_PROMPT}
  3. Loading the config via ActorConfiguration.from_blob()
  4. The malicious content is directly interpolated into the config without validation

Expected Behavior

Environment variable values should be validated before interpolation into actor configurations. The system should:

  • Reject malicious or unexpected values with clear error messages
  • Log environment variable usage for audit trails
  • Document security implications for developers
  • Provide validation rules for different config field types

Acceptance Criteria

  • Environment variable values are validated before interpolation
  • Validation rules are documented for each config field type
  • Malicious environment variable values are rejected with clear error messages
  • Environment variable usage is logged for audit trails
  • Security implications are documented in docstrings

Subtasks

  • Define validation rules for common config field types (URLs, file paths, prompts, etc.)
  • Implement validation function for interpolated values
  • Add logging when environment variables are used
  • Create test cases with malicious environment variable values
  • Document security implications in module docstring

Definition of Done

This issue is complete when:

  • Environment variable values are validated before interpolation
  • Malicious values are rejected with clear error messages
  • Test cases cover common injection attack patterns
  • All existing tests pass
  • Code coverage remains >=97%
  • Security implications are documented

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata **Commit**: April 6, 2026 **Branch**: main ## Background and Context The `ActorConfiguration._interpolate_env_vars()` method in `src/cleveragents/actor/config.py` reads environment variables and interpolates them into actor configurations without validation or sanitization. This creates a security risk if environment variables are set by untrusted sources or if the interpolated values are used in sensitive contexts (e.g., database queries, file paths, or system commands). ### Code Evidence Lines 155-175 in `src/cleveragents/actor/config.py`: ```python @staticmethod def _interpolate_env_vars(config: Any) -> Any: if isinstance(config, dict): config_dict = cast(dict[str, Any], config) return { k: ActorConfiguration._interpolate_env_vars(v) for k, v in config_dict.items() } if isinstance(config, list): config_list = cast(list[Any], config) return [ActorConfiguration._interpolate_env_vars(i) for i in config_list] if isinstance(config, str): env_var_pattern = r"\${([A-Za-z0-9_]+)(?::([^}]*))?\}" def replace_env_var(match: re.Match[str]) -> str: env_var = match.group(1) default_value = match.group(2) if match.group(2) is not None else None env_value = os.environ.get(env_var) if env_value is None: if default_value is not None: # ... use default ... return default_value raise ValueError(f"Environment variable '{env_var}' is not set") return env_value # <-- No validation or sanitization! ``` The function directly returns environment variable values without any validation. An attacker who can set environment variables could inject: - Malicious file paths - SQL injection payloads - Command injection payloads - YAML syntax that breaks the configuration ### Environment Verification This is reproducible by: 1. Setting an environment variable with malicious content: `export ACTOR_PROMPT="'; DROP TABLE actors; --"` 2. Creating an actor config that uses `${ACTOR_PROMPT}` 3. Loading the config via `ActorConfiguration.from_blob()` 4. The malicious content is directly interpolated into the config without validation ## Expected Behavior Environment variable values should be validated before interpolation into actor configurations. The system should: - Reject malicious or unexpected values with clear error messages - Log environment variable usage for audit trails - Document security implications for developers - Provide validation rules for different config field types ## Acceptance Criteria - [ ] Environment variable values are validated before interpolation - [ ] Validation rules are documented for each config field type - [ ] Malicious environment variable values are rejected with clear error messages - [ ] Environment variable usage is logged for audit trails - [ ] Security implications are documented in docstrings ## Subtasks - [ ] Define validation rules for common config field types (URLs, file paths, prompts, etc.) - [ ] Implement validation function for interpolated values - [ ] Add logging when environment variables are used - [ ] Create test cases with malicious environment variable values - [ ] Document security implications in module docstring ## Definition of Done This issue is complete when: - Environment variable values are validated before interpolation - Malicious values are rejected with clear error messages - Test cases cover common injection attack patterns - All existing tests pass - Code coverage remains >=97% - Security implications are documented --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10570
No description provided.