π OpenHands: AI-Driven Development
fix: redact credentials from PluginSpec.source during serialization (#14795)
simonrosenbergf762012af371
diff --git a/openhands/app_server/app_conversation/app_conversation_models.py b/openhands/app_server/app_conversation/app_conversation_models.pyindex 37830b2b3f..aec97f6675 100644--- a/openhands/app_server/app_conversation/app_conversation_models.py+++ b/openhands/app_server/app_conversation/app_conversation_models.py@@ -1,9 +1,10 @@+import refrom datetime import datetimefrom enum import Enumfrom typing import Any, Literalfrom uuid import UUID, uuid4-from pydantic import BaseModel, Field, SecretStr+from pydantic import BaseModel, Field, SecretStr, field_serializerfrom openhands.agent_server.models import (ImageContent,@@ -41,6 +42,16 @@ class ConversationTrigger(Enum):AUTOMATION = 'automation'+def _redact_url_credentials(url: str) -> str:+ """Redact embedded credentials from a URL (https://user:token@host β https://****@host).++ # TODO: replace with `from openhands.sdk.utils.redact import redact_url_credentials`+ # once the SDK pin is bumped to include OpenHands/software-agent-sdk#2154.+ """+ m = re.match(r'^(https?://)([^@/]+)@(.+)$', url)+ return f'{m.group(1)}****@{m.group(3)}' if m else url++class AgentType(Enum):"""Agent type for conversation."""@@ -60,6 +71,11 @@ class PluginSpec(PluginSource):description='User-provided values for plugin input parameters',)+ @field_serializer('source')+ @classmethod+ def _serialize_source(cls, source: str) -> str:+ return _redact_url_credentials(source)+@propertydef display_name(self) -> str:"""Extract a friendly display name from the plugin source.diff --git a/tests/unit/app_server/test_models.py b/tests/unit/app_server/test_models.pyindex d410366bb0..7eb475d503 100644--- a/tests/unit/app_server/test_models.py+++ b/tests/unit/app_server/test_models.py@@ -7,6 +7,7 @@ from openhands.app_server.app_conversation.app_conversation_models import (AppConversationInfo,AppConversationStartRequest,AppConversationUpdateRequest,+ PluginSpec,)from openhands.app_server.event_callback.event_callback_models import (EventCallback,@@ -98,3 +99,39 @@ def test_app_conversation_update_request_title_field_updates_conversation_info()'Title was not updated on AppConversationInfo. '"Ensure 'title' is in AppConversationUpdateRequest.model_fields_set.")+++class TestPluginSpecSourceRedaction:+ """Verify PluginSpec.source credentials are redacted on serialization."""++ def test_model_dump_redacts_credentials(self):+ spec = PluginSpec(source='https://oauth2:[email protected]/org/repo.git')+ dumped = spec.model_dump()+ assert '****' in dumped['source']+ assert 'SECRET' not in dumped['source']++ def test_model_dump_json_redacts_credentials(self):+ spec = PluginSpec(source='https://user:[email protected]/org/repo.git')+ json_str = spec.model_dump_json()+ assert 'pass' not in json_str+ assert '****' in json_str++ def test_source_attribute_retains_raw_value(self):+ url = 'https://oauth2:[email protected]/org/repo.git'+ spec = PluginSpec(source=url)+ assert spec.source == url++ def test_clean_url_passes_through_unchanged(self):+ url = 'https://github.com/org/repo.git'+ spec = PluginSpec(source=url)+ assert spec.model_dump()['source'] == url++ def test_redaction_nested_in_start_request(self):+ """Credentials stay out of model_dump when PluginSpec is nested."""+ req = AppConversationStartRequest(+ plugins=[PluginSpec(source='https://[email protected]/org/repo.git')]+ )+ dumped = req.model_dump()+ plugin_source = dumped['plugins'][0]['source']+ assert 'token' not in plugin_source+ assert '****' in plugin_source