-
Notifications
You must be signed in to change notification settings - Fork 458
fix(agent-server): detect all secret-bearing fields for the plaintext-save warning, not just llm.api_key #4618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
50ec4a0
feat(agent-server): add require_secret_key to refuse plaintext secret…
simonrosenberg fad27ba
chore: address PR review feedback (#4618)
simonrosenberg 6ee2092
fix: guard require_cipher against critic_api_key, not just llm.api_key
simonrosenberg 091ceb5
fix: detect secrets via the real serialization pipeline, not a field …
simonrosenberg b481a03
simplify: drop require_secret_key, keep only the has_any_secret fix
simonrosenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
tests/agent_server/test_persistence_secret_detection.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| """Tests for has_any_secret: detect secret-bearing fields via the real | ||
| serialization pipeline, not a hardcoded field checklist. | ||
| """ | ||
|
|
||
| import logging | ||
| import tempfile | ||
| from base64 import urlsafe_b64encode | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from pydantic import SecretStr | ||
|
|
||
| from openhands.agent_server.persistence import ( | ||
| CustomSecret, | ||
| FileSecretsStore, | ||
| FileSettingsStore, | ||
| PersistedSettings, | ||
| Secrets, | ||
| ) | ||
| from openhands.sdk.utils.cipher import Cipher | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def persistence_dir(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| yield Path(tmpdir) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cipher(): | ||
| return Cipher(urlsafe_b64encode(b"a" * 32).decode("ascii")) | ||
|
|
||
|
|
||
| def test_has_any_secret_detects_llm_api_key(): | ||
| settings = PersistedSettings.model_validate( | ||
| {"agent_settings": {"llm": {"model": "gpt-4o", "api_key": "sk-test"}}} | ||
| ) | ||
| assert settings.has_any_secret | ||
|
|
||
|
|
||
| def test_has_any_secret_detects_critic_api_key(): | ||
| """critic_api_key is a separate secret from llm.api_key.""" | ||
| settings = PersistedSettings.model_validate( | ||
| {"agent_settings": {"verification": {"critic_api_key": "sk-critic"}}} | ||
| ) | ||
| assert settings.has_any_secret | ||
|
|
||
|
|
||
| def test_has_any_secret_detects_mcp_server_secret(): | ||
| settings = PersistedSettings.model_validate( | ||
| { | ||
| "agent_settings": { | ||
| "mcp_config": { | ||
| "mcpServers": { | ||
| "github": { | ||
| "command": "uvx", | ||
| "args": ["mcp-server-github"], | ||
| "env": {"GITHUB_TOKEN": "ghp-test"}, | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| assert settings.has_any_secret | ||
|
|
||
|
|
||
| def test_has_any_secret_detects_agent_context_secret(): | ||
| """agent_context.secrets values are plain str at rest (only secret-shaped | ||
| at serialize time via their own field serializer), so a naive | ||
| isinstance(v, SecretStr) walk would miss this -- must still be caught.""" | ||
| settings = PersistedSettings.model_validate( | ||
| {"agent_settings": {"agent_context": {"secrets": {"MY_TOKEN": "sk-ctx"}}}} | ||
| ) | ||
| assert settings.has_any_secret | ||
|
|
||
|
|
||
| def test_has_any_secret_false_for_empty_settings(): | ||
| assert not PersistedSettings().has_any_secret | ||
|
|
||
|
|
||
| def test_secrets_has_any_secret_detects_custom_secret(): | ||
| secrets = Secrets( | ||
| custom_secrets={ | ||
| "MY_SECRET": CustomSecret(name="MY_SECRET", secret=SecretStr("sk-test")) | ||
| } | ||
| ) | ||
| assert secrets.has_any_secret | ||
|
|
||
|
|
||
| def test_secrets_has_any_secret_false_for_empty_secrets(): | ||
| assert not Secrets().has_any_secret | ||
|
|
||
|
|
||
| def test_settings_save_warns_for_critic_key_only_without_cipher( | ||
| persistence_dir, caplog | ||
| ): | ||
| """Regression test: before has_any_secret, the plaintext warning was | ||
| gated on llm_api_key_is_set alone, so a critic_api_key-only settings | ||
| object triggered no warning at all.""" | ||
| caplog.set_level(logging.WARNING) | ||
| store = FileSettingsStore(persistence_dir=persistence_dir) | ||
| settings = PersistedSettings.model_validate( | ||
| {"agent_settings": {"verification": {"critic_api_key": "sk-critic"}}} | ||
| ) | ||
|
|
||
| store.save(settings) | ||
|
|
||
| assert "PLAINTEXT" in caplog.text | ||
|
|
||
|
|
||
| def test_settings_save_no_warning_when_no_secrets_present(persistence_dir, caplog): | ||
| caplog.set_level(logging.WARNING) | ||
| store = FileSettingsStore(persistence_dir=persistence_dir) | ||
|
|
||
| store.save(PersistedSettings()) | ||
|
|
||
| assert "PLAINTEXT" not in caplog.text | ||
|
|
||
|
|
||
| def test_settings_save_with_cipher_round_trips_critic_key(persistence_dir, cipher): | ||
| store = FileSettingsStore(persistence_dir=persistence_dir, cipher=cipher) | ||
| settings = PersistedSettings.model_validate( | ||
| {"agent_settings": {"verification": {"critic_api_key": "sk-critic"}}} | ||
| ) | ||
|
|
||
| store.save(settings) | ||
|
|
||
| reloaded = store.load() | ||
| assert reloaded is not None | ||
| assert reloaded.has_any_secret | ||
|
|
||
|
|
||
| def test_secrets_save_warns_without_cipher(persistence_dir, caplog): | ||
| caplog.set_level(logging.WARNING) | ||
| store = FileSecretsStore(persistence_dir=persistence_dir) | ||
| secrets = Secrets( | ||
| custom_secrets={ | ||
| "MY_SECRET": CustomSecret(name="MY_SECRET", secret=SecretStr("sk-test")) | ||
| } | ||
| ) | ||
|
|
||
| store.save(secrets) | ||
|
|
||
| assert "PLAINTEXT" in caplog.text |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.