Skip to content

πŸ™Œ OpenHands: AI-Driven Development

chore: delete _redact_compat shim and import directly from openhands-sdk (#14083)

OpenHands Bot4e5ae36ff23b
diff --git a/openhands/app_server/app_conversation/live_status_app_conversation_service.py b/openhands/app_server/app_conversation/live_status_app_conversation_service.py
index 3567a11474..21c1b73cef 100644
--- a/openhands/app_server/app_conversation/live_status_app_conversation_service.py
+++ b/openhands/app_server/app_conversation/live_status_app_conversation_service.py
@@ -87,7 +87,6 @@ from openhands.app_server.services.injector import InjectorState
from openhands.app_server.services.jwt_service import JwtService
from openhands.app_server.user.user_context import UserContext
from openhands.app_server.user.user_models import UserInfo
-from openhands.app_server.utils._redact_compat import sanitize_config
from openhands.app_server.utils.docker_utils import (
replace_localhost_hostname_for_docker,
)
@@ -104,6 +103,7 @@ from openhands.sdk.plugin import PluginSource
from openhands.sdk.secret import LookupSecret, StaticSecret
from openhands.sdk.settings import ACPAgentSettings
from openhands.sdk.utils.paging import page_iterator
+from openhands.sdk.utils.redact import sanitize_config
from openhands.sdk.workspace.remote.async_remote_workspace import AsyncRemoteWorkspace
from openhands.tools.preset.default import (
get_default_tools,
diff --git a/openhands/app_server/event_callback/event_callback_models.py b/openhands/app_server/event_callback/event_callback_models.py
index 545f90644a..e8aac87ba0 100644
--- a/openhands/app_server/event_callback/event_callback_models.py
+++ b/openhands/app_server/event_callback/event_callback_models.py
@@ -15,15 +15,13 @@ from openhands.app_server.event_callback.event_callback_result_models import (
EventCallbackResult,
EventCallbackResultStatus,
)
-
-# TODO(OpenHands/evaluation#418): import from openhands.sdk.utils.redact
-from openhands.app_server.utils._redact_compat import redact_text_secrets
from openhands.sdk import Event
from openhands.sdk.utils.models import (
DiscriminatedUnionMixin,
OpenHandsModel,
get_known_concrete_subclasses,
)
+from openhands.sdk.utils.redact import redact_text_secrets
_logger = logging.getLogger(__name__)
if TYPE_CHECKING:
diff --git a/openhands/app_server/event_callback/set_title_callback_processor.py b/openhands/app_server/event_callback/set_title_callback_processor.py
index d2dc3dfe26..a29c94a4b9 100644
--- a/openhands/app_server/event_callback/set_title_callback_processor.py
+++ b/openhands/app_server/event_callback/set_title_callback_processor.py
@@ -18,13 +18,11 @@ from openhands.app_server.event_callback.event_callback_result_models import (
)
from openhands.app_server.services.injector import InjectorState
from openhands.app_server.user.specifiy_user_context import ADMIN, USER_CONTEXT_ATTR
-
-# TODO(OpenHands/evaluation#418): import from openhands.sdk.utils.redact
-from openhands.app_server.utils._redact_compat import redact_text_secrets
from openhands.app_server.utils.docker_utils import (
replace_localhost_hostname_for_docker,
)
from openhands.sdk import Event, MessageEvent
+from openhands.sdk.utils.redact import redact_text_secrets
_logger = logging.getLogger(__name__)
diff --git a/openhands/app_server/utils/_redact_compat.py b/openhands/app_server/utils/_redact_compat.py
deleted file mode 100644
index b920f850be..0000000000
--- a/openhands/app_server/utils/_redact_compat.py
+++ /dev/null
@@ -1,170 +0,0 @@
-# TODO(OpenHands/evaluation#418): Delete this file and import directly from
-# openhands.sdk.utils.redact once openhands-sdk >1.16.1 is released.
-# These functions are copied from the SDK's redact.py to unblock PRs while
-# waiting for the next SDK release.
-#
-# Source of truth: openhands-sdk/openhands/sdk/utils/redact.py
-# in repo: https://github.com/OpenHands/software-agent-sdk
-
-import copy
-import re
-from typing import Any
-from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
-
-from openhands.sdk.utils.redact import sanitize_dict
-
-# ---------------------------------------------------------------------------
-# URL param redaction
-# ---------------------------------------------------------------------------
-
-SENSITIVE_URL_PARAMS = frozenset(
- {
- 'tavilyapikey',
- 'apikey',
- 'api_key',
- 'token',
- 'access_token',
- 'secret',
- 'key',
- }
-)
-
-
-def _is_secret_key(key: str) -> bool:
- key_upper = key.upper()
- return any(
- p in key_upper
- for p in (
- 'AUTHORIZATION',
- 'COOKIE',
- 'CREDENTIAL',
- 'KEY',
- 'PASSWORD',
- 'SECRET',
- 'SESSION',
- 'TOKEN',
- )
- )
-
-
-def redact_url_params(url: str) -> str:
- """Redact sensitive query parameter values from a URL string."""
- try:
- parsed = urlparse(url)
- except Exception:
- return url
- if not parsed.query:
- return url
- params = parse_qs(parsed.query, keep_blank_values=True)
- redacted_params: dict[str, list[str]] = {}
- for param_name, values in params.items():
- if param_name.lower() in SENSITIVE_URL_PARAMS or _is_secret_key(param_name):
- redacted_params[param_name] = ['<redacted>'] * len(values)
- else:
- redacted_params[param_name] = values
- redacted_query = urlencode(redacted_params, doseq=True)
- return urlunparse(parsed._replace(query=redacted_query))
-
-
-def _walk_redact_urls(obj: Any) -> Any:
- if isinstance(obj, dict):
- return {k: _walk_redact_urls(v) for k, v in obj.items()}
- if isinstance(obj, list):
- return [_walk_redact_urls(item) for item in obj]
- if isinstance(obj, str) and '?' in obj:
- return redact_url_params(obj)
- return obj
-
-
-# ---------------------------------------------------------------------------
-# sanitize_config
-# ---------------------------------------------------------------------------
-
-
-def sanitize_config(config: dict[str, Any]) -> dict[str, Any]:
- """Deep-copy a config dict, redact secret keys and URL query params."""
- config = copy.deepcopy(config)
- config = sanitize_dict(config)
- config = _walk_redact_urls(config)
- return config
-
-
-# ---------------------------------------------------------------------------
-# Text / string redaction
-# ---------------------------------------------------------------------------
-
-_API_KEY_LITERAL_RE = re.compile(
- r'\b('
- # OpenRouter / OpenAI / Anthropic
- r'sk-(?:or-v1|proj|ant-(?:api|oat)\d{2})-[A-Za-z0-9_-]{20,}'
- r'|gsk_[A-Za-z0-9]{20,}' # GROQ
- r'|hf_[A-Za-z0-9]{20,}' # HuggingFace
- r'|tgp_v1_[A-Za-z0-9_-]{20,}' # Together AI
- r'|ghp_[A-Za-z0-9]{20,}' # GitHub PAT (classic)
- r'|github_pat_[A-Za-z0-9_]{20,}' # GitHub PAT (fine-grained)
- r'|sk-oh-[A-Za-z0-9]{20,}' # OpenHands session tokens
- r'|ctx7sk-[A-Za-z0-9_-]{10,}' # Context7 MCP keys
- r'|cla_[A-Za-z0-9_-]{20,}' # Claude.ai MCP tokens
- r'|sntryu_[A-Za-z0-9]{10,}' # Sentry tokens
- r'|lin_api_[A-Za-z0-9]{10,}' # Linear API tokens
- r'|tvly-[A-Za-z0-9_-]{10,}' # Tavily keys
- r'|ATATT3x[A-Za-z0-9_-]{10,}' # Jira/Atlassian tokens
- r'|xoxb-[A-Za-z0-9_-]{20,}' # Slack bot tokens
- r'|xoxp-[A-Za-z0-9_-]{20,}' # Slack user tokens
- r'|Bearer\s+[A-Za-z0-9_.-]{20,}' # Bearer tokens
- r')'
-)
-
-
-def redact_api_key_literals(text: str) -> str:
- """Replace bare API key literals from common providers with <redacted>."""
- return _API_KEY_LITERAL_RE.sub('<redacted>', text)
-
-
-def redact_text_secrets(text: str) -> str:
- """Redact secrets from a string representation of a config object."""
- # api_key='...' patterns (attribute style)
- text = re.sub(r"api_key='[^']*'", "api_key='<redacted>'", text)
- text = re.sub(r'api_key="[^"]*"', 'api_key="<redacted>"', text)
- # 'api_key': '...' patterns (dict style from str())
- text = re.sub(r"('api_key':\s*')[^']*(')", r'\g<1><redacted>\2', text)
-
- # Dict entries with sensitive key names (uppercase)
- text = re.sub(
- r"('[A-Z_]*(?:KEY|SECRET|TOKEN|PASSWORD)[A-Z_]*':\s*')[^']*(')",
- r'\g<1><redacted>\2',
- text,
- )
- text = re.sub(
- r'("[A-Z_]*(?:KEY|SECRET|TOKEN|PASSWORD)[A-Z_]*":\s*")[^"]*(")',
- r'\g<1><redacted>\2',
- text,
- )
-
- # URL query params
- text = re.sub(
- r'((?:tavilyApiKey|apiKey|api_key|token|access_token|secret|key)=)'
- r"[^&\s'\")\]]+",
- r'\g<1><redacted>',
- text,
- flags=re.IGNORECASE,
- )
-
- # Authorization header values
- text = re.sub(
- r"('Authorization':\s*')[^']*(')",
- r'\g<1><redacted>\2',
- text,
- )
-
- # X-Session-API-Key header values
- text = re.sub(
- r"('X-Session-API-Key':\s*')[^']*(')",
- r'\g<1><redacted>\2',
- text,
- )
-
- # Bare API key literals
- text = redact_api_key_literals(text)
-
- return text
diff --git a/openhands/app_server/utils/logger.py b/openhands/app_server/utils/logger.py
index 08163aabd1..b67b028789 100644
--- a/openhands/app_server/utils/logger.py
+++ b/openhands/app_server/utils/logger.py
@@ -542,7 +542,7 @@ class RedactURLParamsFilter(logging.Filter):
_URL_WITH_QS_RE = re.compile(r'\S+\?\S+')
def filter(self, record: logging.LogRecord) -> bool:
- from openhands.app_server.utils._redact_compat import redact_url_params
+ from openhands.sdk.utils.redact import redact_url_params
if record.args:
if isinstance(record.args, (tuple, list)):
diff --git a/tests/unit/utils/test_redact_compat.py b/tests/unit/utils/test_redact.py
similarity index 95%
rename from tests/unit/utils/test_redact_compat.py
rename to tests/unit/utils/test_redact.py
index df065481fb..b9800b0a62 100644
--- a/tests/unit/utils/test_redact_compat.py
+++ b/tests/unit/utils/test_redact.py
@@ -1,9 +1,9 @@
-"""Tests for openhands.app_server.utils._redact_compat redaction utilities.
+"""Tests for openhands.sdk.utils.redact redaction utilities.
These tests verify that MCP config secrets are properly redacted before logging.
"""
-from openhands.app_server.utils._redact_compat import (
+from openhands.sdk.utils.redact import (
redact_api_key_literals,
redact_text_secrets,
redact_url_params,
@@ -107,16 +107,17 @@ class TestMCPConfigLoggingIntegration:
def test_mcp_sse_server_logging_is_safe(self):
"""Simulate logging MCP SSE server configs as done in action_execution_client.py."""
+ # Note: The SDK regex requires at least 20 chars after 'sk-oh-' prefix
sse_servers = [
{
'url': 'http://localhost:8000/mcp/sse',
- 'api_key': 'sk-oh-realSessionKey456',
+ 'api_key': 'sk-oh-realSessionKey4567890abc',
}
]
log_output = redact_text_secrets(str(sse_servers))
- assert 'sk-oh-realSessionKey456' not in log_output
+ assert 'sk-oh-realSessionKey4567890abc' not in log_output
assert REDACTED in log_output
assert 'http://localhost:8000/mcp/sse' in log_output # URL should be visible