From 36f01d2e54435317282dfad1dfc0c7fc72d3115b Mon Sep 17 00:00:00 2001 From: Hao Zhe Date: Sun, 5 Jul 2026 05:33:22 +0800 Subject: [PATCH] fix(openviking): sanitize splitline env separators --- plugins/memory/openviking/__init__.py | 6 +++--- tests/plugins/memory/test_openviking_provider.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/plugins/memory/openviking/__init__.py b/plugins/memory/openviking/__init__.py index ec2f2772c066..1202e11a1604 100644 --- a/plugins/memory/openviking/__init__.py +++ b/plugins/memory/openviking/__init__.py @@ -972,11 +972,11 @@ def _env_line_safe(value: Any) -> str: ``read_text().splitlines()`` round-trip — letting a malformed or pasted secret (e.g. an api_key copied with a trailing record) inject an arbitrary additional variable into the persisted credentials file. Strip - the line terminators (``\r``, ``\n``) and the NUL byte so a value can - only ever occupy the single line it is written on. + the line separators recognized by ``splitlines()`` and the NUL byte so a + value can only ever occupy the single line it is written on. """ text = value if isinstance(value, str) else str(value) - return text.replace("\r", "").replace("\n", "").replace("\x00", "") + return "".join(text.replace("\x00", "").splitlines()) def _write_env_vars(env_path: Path, env_writes: dict, remove_keys: tuple[str, ...] = ()) -> None: diff --git a/tests/plugins/memory/test_openviking_provider.py b/tests/plugins/memory/test_openviking_provider.py index e7c7f666b44c..bad3ae9f9755 100644 --- a/tests/plugins/memory/test_openviking_provider.py +++ b/tests/plugins/memory/test_openviking_provider.py @@ -110,6 +110,18 @@ def test_openviking_env_writer_strips_embedded_newlines_in_values(tmp_path): assert "INJECTED_KEY" not in parsed +def test_openviking_env_writer_strips_splitline_separators_and_nul(tmp_path): + env_path = tmp_path / ".env" + + openviking_module._write_env_vars( + env_path, + {"OPENVIKING_API_KEY": "good\u2028INJECTED_KEY=attacker\x00tail"}, + ) + + lines = env_path.read_text(encoding="utf-8").splitlines() + assert lines == ["OPENVIKING_API_KEY=goodINJECTED_KEY=attackertail"] + + def test_openviking_env_writer_strips_newlines_when_updating_existing_key(tmp_path): env_path = tmp_path / ".env" env_path.write_text("OPENVIKING_API_KEY=old\n", encoding="utf-8")