From 54bd7523956a26d60272f79089c104eccf656e57 Mon Sep 17 00:00:00 2001 From: Yuqing Yang Date: Wed, 14 Oct 2026 09:44:03 +0000 Subject: [PATCH] Restrict semantic variable lookup to session scope Prevent authenticated sessions from resolving global constant-prefix variables through the public semantic-variable API. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- parrot/serve/variable_manager.py | 6 +----- tests/serve/test_sv.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/parrot/serve/variable_manager.py b/parrot/serve/variable_manager.py index 852ed3d..9cb02b5 100644 --- a/parrot/serve/variable_manager.py +++ b/parrot/serve/variable_manager.py @@ -253,17 +253,13 @@ def create_var(self, session_id: int, var_name: str) -> SemanticVariable: ) def get_var(self, session_id: int, var_id: str) -> SemanticVariable: - """Get a Semantic Variable by ID. + """Get a session-local Semantic Variable by ID. Args: session_id: int. The session ID. var_id: str. The Semantic Variable ID. """ - cp_var = self.constant_prefix_namespace.get_var_by_id(var_id) - if cp_var is not None: - return cp_var - parrot_assert( session_id in self.session_namespaces, f"Local namespace of {session_id} does not exist.", diff --git a/tests/serve/test_sv.py b/tests/serve/test_sv.py index 24b20d6..52aba41 100644 --- a/tests/serve/test_sv.py +++ b/tests/serve/test_sv.py @@ -1,3 +1,6 @@ +import pytest + +from parrot.exceptions import ParrotCoreUserError from parrot.serve.graph import ( RequestChain, ConstantFill, @@ -22,6 +25,21 @@ def test_content_hash(): assert var1 == var2 +def test_constant_prefix_is_not_exposed_through_session_lookup(): + var_mgr = SemanticVariableManager(constant_prefix_var_timeout=10) + owner_session_id = 0 + other_session_id = 1 + var_mgr.register_local_var_space(owner_session_id) + var_mgr.register_local_var_space(other_session_id) + + prefix_var = var_mgr._get_constant_prefix_var("private system prompt") + + with pytest.raises(ParrotCoreUserError): + var_mgr.get_var(owner_session_id, prefix_var.id) + with pytest.raises(ParrotCoreUserError): + var_mgr.get_var(other_session_id, prefix_var.id) + + def test_request_chain_hash(): var_mgr = SemanticVariableManager(constant_prefix_var_timeout=10)