Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(security): strip every conversational-state field from workflow exports
trohitgc695f1c144f1
diff --git a/client/src/utils/__tests__/parameterSanitizer.test.ts b/client/src/utils/__tests__/parameterSanitizer.test.tsindex 19463d7f..f555a7d6 100644--- a/client/src/utils/__tests__/parameterSanitizer.test.ts+++ b/client/src/utils/__tests__/parameterSanitizer.test.ts@@ -28,6 +28,24 @@ describe('sanitizeParameters', () => {expect(out).not.toHaveProperty('last_session_id');});+ it('strips the structured JSONL transcript behind the markdown view', () => {+ const out = sanitizeParameters({+ memory_jsonl: '{"role":"user","content":"my card is 4111 1111 1111 1111"}',+ });+ expect(out).toEqual({});+ });++ it('strips vertex cloud-conversation handles', () => {+ // These point into the *author's* Vertex session; an exported workflow+ // must not carry them to whoever imports it.+ const out = sanitizeParameters({+ vertex_interaction_id: 'interactions/abc123',+ vertex_environment_id: 'environments/xyz789',+ provider: 'vertex',+ });+ expect(out).toEqual({ provider: 'vertex' });+ });+it('keeps genuine configuration on the same node', () => {const out = sanitizeParameters({memory_content: 'secrets in here',diff --git a/client/src/utils/parameterSanitizer.ts b/client/src/utils/parameterSanitizer.tsindex ea95c3f0..a4ea0af0 100644--- a/client/src/utils/parameterSanitizer.ts+++ b/client/src/utils/parameterSanitizer.ts@@ -105,12 +105,26 @@ function isSensitiveKey(key: string): boolean {// Names below are the backend field names. Do NOT add a key here on the basis// of a `hidden: true` UI flag -- that marks merely-advanced configuration// (`model`, `max_parallel`, `effort`), which an export legitimately needs.+// The first four mirror, exactly, what the backend's own "clear memory"+// routine resets or removes -- `clear_agent_session_state` in+// `server/services/memory/state.py`. That function is the authoritative+// definition of "everything an agent reuses across a conversation", so it is+// the right list to track, and a backend test asserts this file stays in sync+// with it.const RUNTIME_KEYS = normalizedSet([// simpleMemory: the whole conversation, verbatim, including anything the// user or the model said.'memory_content',+ // simpleMemory: the structured JSONL transcript behind the markdown view.+ // No current writer, but older databases still carry it.+ 'memory_jsonl',// simpleMemory: the Claude Code session UUID from the last successful run.'last_session_id',+ // vertexManagedAgent: handles into a cloud-side conversation and its+ // environment. Exporting them hands the recipient a pointer into the+ // author's own Vertex session.+ 'vertex_interaction_id',+ 'vertex_environment_id','token_usage', // Execution token metrics'execution_time', // Runtime timing'last_execution', // Last execution resultdiff --git a/server/tests/test_export_sanitizer_sync.py b/server/tests/test_export_sanitizer_sync.pynew file mode 100644index 00000000..bdf5c998--- /dev/null+++ b/server/tests/test_export_sanitizer_sync.py@@ -0,0 +1,112 @@+"""The export sanitizer must cover everything "clear memory" wipes.++Two lists describe the same concept in two languages:++- ``clear_agent_session_state`` (``services/memory/state.py``) is the+ backend's authoritative definition of "every piece of state an agent+ reuses across a conversation". It resets some fields and removes others.+- ``RUNTIME_KEYS`` (``client/src/utils/parameterSanitizer.ts``) decides what+ never leaves the instance in an exported workflow.++They drifted once already, and silently: the sanitizer held the camelCase+``memoryContent`` while the field is ``memory_content``, so every export+carried the full conversation history -- into the shipped example workflows,+and from there into a public repository.++Cross-language, so it cannot be a type. This test reads both sources and+asserts the second covers the first.+"""++from __future__ import annotations++import inspect+import re+from pathlib import Path++import pytest++pytestmark = pytest.mark.unit++REPO_ROOT = Path(__file__).resolve().parents[2]+SANITIZER = REPO_ROOT / "client" / "src" / "utils" / "parameterSanitizer.ts"+++def _memory_state_fields() -> set[str]:+ """Field names ``clear_agent_session_state`` resets or removes."""+ from services.memory.state import clear_agent_session_state++ source = inspect.getsource(clear_agent_session_state)++ updates = re.search(r"parameter_updates=\{(.*?)\}", source, re.S)+ removals = re.search(r"remove_parameters=\((.*?)\)", source, re.S)+ assert updates and removals, (+ "Could not locate parameter_updates / remove_parameters in "+ "clear_agent_session_state. If that function was restructured, update "+ "this extraction -- do not delete the test, the invariant still holds."+ )++ fields = set(re.findall(r'"([a-z_]+)"', updates.group(1)))+ fields |= set(re.findall(r'"([a-z_]+)"', removals.group(1)))+ return fields+++def _normalize(key: str) -> str:+ return re.sub(r"[_\-\s]", "", key.lower())+++def _sanitizer_runtime_keys() -> set[str]:+ """The normalized RUNTIME_KEYS entries declared in the TS sanitizer."""+ assert SANITIZER.is_file(), f"sanitizer not found at {SANITIZER}"+ source = SANITIZER.read_text(encoding="utf-8")++ block = re.search(r"const RUNTIME_KEYS = normalizedSet\(\[(.*?)\]\)", source, re.S)+ assert block, "Could not locate the RUNTIME_KEYS array in parameterSanitizer.ts"++ # Strip `//` comments first. Prose in there legitimately contains+ # apostrophes ("the author's own session"), and a naive quote-pair scan+ # would treat one as a string delimiter and mis-read the whole list.+ body = re.sub(r"//[^\n]*", "", block.group(1))+ return {_normalize(k) for k in re.findall(r"'([^']+)'", body)}+++class TestExportSanitizerCoversConversationalState:+ def test_extraction_actually_found_something(self):+ """Guard the guard: a silently-empty set would pass everything."""+ backend = _memory_state_fields()+ assert len(backend) >= 5, backend+ assert len(_sanitizer_runtime_keys()) >= 5++ def test_every_cleared_field_is_stripped_from_exports(self):+ missing = sorted(+ field+ for field in _memory_state_fields()+ if _normalize(field) not in _sanitizer_runtime_keys()+ )+ assert not missing, (+ f"{missing} are wiped by clear_agent_session_state -- they are "+ "conversational state -- but the export sanitizer does not strip "+ f"them. Add them to RUNTIME_KEYS in {SANITIZER.name}."+ )++ @pytest.mark.parametrize(+ "field",+ [+ "memory_content",+ "memory_jsonl",+ "last_session_id",+ "vertex_interaction_id",+ "vertex_environment_id",+ ],+ )+ def test_known_conversational_fields_are_named_explicitly(self, field):+ """Pin the individual names, so a regression reads clearly."""+ assert _normalize(field) in _sanitizer_runtime_keys()++ def test_sanitizer_compares_normalized_keys(self):+ """The camelCase/snake_case seam that caused the original leak."""+ source = SANITIZER.read_text(encoding="utf-8")+ assert "function normalizeKey" in source, (+ "parameterSanitizer.ts must fold case and drop separators before "+ "comparing. Exact-match Set.has() is what let `memoryContent` "+ "shadow `memory_content` and leak every conversation."+ )