Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions openhands-agent-server/openhands/agent_server/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
them through ``app.dependency_overrides``.
"""

import re
from typing import Annotated
from uuid import UUID

Expand Down Expand Up @@ -48,6 +49,10 @@
_WORKSPACE_SESSION_COOKIE = APIKeyCookie(
name=WORKSPACE_SESSION_COOKIE_NAME, auto_error=False
)
_RUNTIME_CREDENTIAL_BINDING_PATH = re.compile(
r"^/api/(?:_docker/)?conversations/[0-9a-fA-F-]{36}/"
r"runtime-credential-bindings/[^/]+$"
)


# Declared with ``Annotated`` so that they are required arguments: calling a
Expand Down Expand Up @@ -124,11 +129,22 @@ def _unavailable(connection: HTTPConnection, detail: str) -> Exception:


def check_session_api_key(
connection: HTTPConnection,
config: Annotated[Config, Depends(get_config)],
session_api_key: Annotated[str | None, Depends(_SESSION_API_KEY_HEADER)],
) -> None:
"""Reject the request if the supplied key is not in the current session keys."""
if config.session_api_keys and session_api_key not in config.session_api_keys:
# Docker runtimes call this one broker route with their per-runtime key.
# The route itself verifies the key against the requested conversation;
# this exception only lets that exact request reach that validation.
if (
session_api_key is not None
and connection.scope["type"] == "http"
and connection.scope.get("method") in {"GET", "PUT"}
and _RUNTIME_CREDENTIAL_BINDING_PATH.fullmatch(connection.url.path)
):
return
raise Unauthenticated()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ def cleanup_stale_containers(self) -> None:
def has_active_leases(self, conversation_id: UUID) -> bool:
return self._leases.get(conversation_id, 0) > 0

def idle_token(self, conversation_id: UUID) -> int:
"""Snapshot runtime activity for a later ``stop_if_idle`` call."""
return self._lease_generations.get(conversation_id, 0)

def release_lease(self, conversation_id: UUID) -> None:
"""Release a previously acquired lease on a container runtime."""
count = self._leases.get(conversation_id, 0) - 1
Expand Down
Loading