Skip to content

sec: clear wizard-set API keys from os.environ after session ends#25

Merged
rosspeili merged 1 commit into
ARPAHLS:mainfrom
rosspeili:sec/clear-wizard-env-on-session-end
May 18, 2026
Merged

sec: clear wizard-set API keys from os.environ after session ends#25
rosspeili merged 1 commit into
ARPAHLS:mainfrom
rosspeili:sec/clear-wizard-env-on-session-end

Conversation

@rosspeili
Copy link
Copy Markdown
Contributor

Closes ARPAHLS/rooms#5.

When the CLI wizard collects an API key (e.g. OPENAI_API_KEY), it writes into os.environ. Those values previously persisted for the entire Python process, which is a security risk if the user runs another session in the same terminal or the process stays alive.

This PR tracks only keys introduced by the wizard during setup and removes them when the session loop ends (normal exit, exit/quit, or Ctrl+C). Keys that were already set in the environment before the wizard runs are never touched.

Changes

  • Add _set_session_env_key, _prompt_api_key_if_needed, and _cleanup_session_env in cli.py
  • Deduplicate API-key prompting for custom agents and the orchestrator
  • Pass tracked_env_keys from main_menu into run_session
  • Run _cleanup_session_env in a finally block after the session loop
  • Add tests/test_cli.py for tracking, skip-existing, and cleanup behavior

Test plan

  • .\venv\Scripts\python.exe -m pytest tests -q (15 passed)
  • Start CLI with a non-Ollama model, enter an API key when prompted, finish or interrupt the session, confirm the key is no longer in os.environ for that process
  • Export a key before starting the CLI; confirm it remains after the session ends

Track env vars the CLI wizard adds for LiteLLM models and remove them in a finally block when the session loop ends, including on Ctrl+C. Pre-existing keys are never deleted. Adds unit tests for the tracking and cleanup helpers.

Fixes ARPAHLS#5
@rosspeili rosspeili merged commit 9ea6d1b into ARPAHLS:main May 18, 2026
1 check passed
@rosspeili rosspeili deleted the sec/clear-wizard-env-on-session-end branch May 18, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sec: clear sensitive environment variables on session end

1 participant