Skip to content

Conversation

@mikejmorgan-ai
Copy link
Member

Potential fix for https://github.com/cortexlinux/cortex/security/code-scanning/7

General approach: Avoid storing raw API keys directly in long‑lived, plain‑text shell configuration files. Either (1) don’t persist the key at all (use it only in‑memory), (2) store it in a dedicated config file with restricted permissions, or (3) if we must assist the user in adding it to their shell, print instructions rather than editing the rc file automatically.

Best fix with minimal functional change: Keep setting the environment variable for the current process (so the rest of the wizard behaves as before) but stop appending the raw secret to shell rc files. Instead, _save_env_var will set os.environ[name] = value and log a message telling the user to add the appropriate export line themselves if they want persistence. This avoids writing secrets to disk while still giving the user the information they need.

Concretely:

  • Edit FirstRunWizard._save_env_var in cortex/first_run_wizard.py:
    • Remove the logic that computes the shell, finds the config file, builds export_line, and appends it to that file.
    • Keep setting os.environ[name] = value.
    • Optionally, log or print a short message indicating how the user can persist the variable manually (using the same export line), but without writing it automatically.
  • No changes are needed to the call sites (_setup_claude_api, _setup_openai_api), because they already go through _save_env_var.

Needed elements:

  • No new external libraries are necessary.
  • We only modify _save_env_var and can rely on existing imports (os, logger).

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…nsitive information

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 26, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch alert-autofix-7

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

Comment on lines +775 to +776
f"\nTo persist this setting, add the following line to your shell config "
f"(e.g. ~/.bashrc or ~/.zshrc):\n {export_line}\n"

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This expression logs
sensitive data (password)
as clear text.
This expression logs
sensitive data (password)
as clear text.

Copilot Autofix

AI 3 days ago

In general, the fix is to ensure that sensitive values (API keys) are never included in log or print output. If we still want to help the user persist the variable, we can print only the variable name and a template/export command without the actual secret, or instruct the user in prose. This maintains functionality (guidance on how to persist) without echoing the secret back.

For this specific code, the best change is within _save_env_var in cortex/first_run_wizard.py. Instead of building export_line with the full value and printing it, we should either (a) print an export command with a placeholder like <YOUR_API_KEY_HERE> or (b) simply instruct the user to add an appropriate line with their value, without constructing a string that contains value. The environment variable still gets set for the current process via os.environ[name] = value, so existing runtime behavior is preserved. Only user-facing messaging changes.

Concretely:

  • Keep os.environ[name] = value unchanged.

  • Remove or change export_line = f'export {name}="{value}"'.

  • Replace the print call so that it no longer interpolates value, and instead uses a placeholder or generic instruction, e.g.:

    print(
        f"\nTo persist this setting, add a line like the following to your shell "
        f"config (e.g. ~/.bashrc or ~/.zshrc), replacing <YOUR_SECRET> with your "
        f"actual value:\n  export {name}=\"<YOUR_SECRET>\"\n"
    )

No new imports or helper methods are required; we only adjust this one method’s body.


Suggested changeset 1
cortex/first_run_wizard.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/cortex/first_run_wizard.py b/cortex/first_run_wizard.py
--- a/cortex/first_run_wizard.py
+++ b/cortex/first_run_wizard.py
@@ -763,17 +763,18 @@
 
         To avoid storing sensitive data in clear text on disk, this method no longer
         appends the value to shell configuration files. Instead, it sets the variable
-        for the current process and prints the export command so the user can add it
-        manually if they choose.
+        for the current process and prints guidance so the user can add it
+        manually if they choose, without echoing the secret value.
         """
         # Set for current session only
         os.environ[name] = value
 
-        # Show user how to persist it without doing so automatically
-        export_line = f'export {name}="{value}"'
+        # Show user how to persist it without doing so automatically,
+        # and without printing the sensitive value.
         print(
-            f"\nTo persist this setting, add the following line to your shell config "
-            f"(e.g. ~/.bashrc or ~/.zshrc):\n  {export_line}\n"
+            f"\nTo persist this setting, add a line like the following to your shell "
+            f"config (e.g. ~/.bashrc or ~/.zshrc), replacing <YOUR_SECRET> with your "
+            f"actual value:\n  export {name}=\"<YOUR_SECRET>\"\n"
         )
 
 
EOF
@@ -763,17 +763,18 @@

To avoid storing sensitive data in clear text on disk, this method no longer
appends the value to shell configuration files. Instead, it sets the variable
for the current process and prints the export command so the user can add it
manually if they choose.
for the current process and prints guidance so the user can add it
manually if they choose, without echoing the secret value.
"""
# Set for current session only
os.environ[name] = value

# Show user how to persist it without doing so automatically
export_line = f'export {name}="{value}"'
# Show user how to persist it without doing so automatically,
# and without printing the sensitive value.
print(
f"\nTo persist this setting, add the following line to your shell config "
f"(e.g. ~/.bashrc or ~/.zshrc):\n {export_line}\n"
f"\nTo persist this setting, add a line like the following to your shell "
f"config (e.g. ~/.bashrc or ~/.zshrc), replacing <YOUR_SECRET> with your "
f"actual value:\n export {name}=\"<YOUR_SECRET>\"\n"
)


Copilot is powered by AI and may make mistakes. Always verify output.
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.

2 participants