Skip to content

The agent that grows with you

fix(agent): strip UTF-8 BOM when loading profile .env into secret scope

Fangliquan696fae8e78c0
diff --git a/agent/secret_scope.py b/agent/secret_scope.py
index 8b376d5fcef..1e4b30e3a70 100644
--- a/agent/secret_scope.py
+++ b/agent/secret_scope.py
@@ -184,10 +184,14 @@ def load_env_file(env_path: Path) -> Dict[str, str]:
``set_secret_scope``. Mirrors python-dotenv's basic parsing (KEY=VALUE,
``export`` prefix, ``#`` comments, optional matching quotes) but never
mutates the process environment — that isolation is the whole point.
+
+ Encoding is ``utf-8-sig`` so a leading UTF-8 BOM (Windows Notepad /
+ PowerShell ``Set-Content -Encoding UTF8``) does not prefix the first
+ key as ``\\ufeffNAME`` and make ``get_secret('NAME')`` miss under scope.
"""
secrets: Dict[str, str] = {}
try:
- text = env_path.read_text(encoding="utf-8")
+ text = env_path.read_text(encoding="utf-8-sig")
except (FileNotFoundError, OSError, UnicodeDecodeError):
return secrets
diff --git a/tests/agent/test_secret_scope.py b/tests/agent/test_secret_scope.py
index 1ac078d6eb9..29f82a334db 100644
--- a/tests/agent/test_secret_scope.py
+++ b/tests/agent/test_secret_scope.py
@@ -116,6 +116,33 @@ class TestEnvFileParsing:
+ def test_strips_utf8_bom_from_first_key(self, tmp_path):
+ """Windows editors often save .env as UTF-8 with BOM (EF BB BF).
+
+ Plain utf-8 keeps U+FEFF on the first key name, so get_secret('NAME')
+ misses under an installed scope. utf-8-sig strips the leading BOM.
+ """
+ env = tmp_path / ".env"
+ env.write_bytes(
+ b"\xef\xbb\xbfANTHROPIC_API_KEY=sk-x\nOPENAI_API_KEY=sk-y\n"
+ )
+ out = ss.load_env_file(env)
+ assert out == {
+ "ANTHROPIC_API_KEY": "sk-x",
+ "OPENAI_API_KEY": "sk-y",
+ }
+ assert "\ufeffANTHROPIC_API_KEY" not in out
+
+ scope = ss.build_profile_secret_scope(tmp_path)
+ ss.set_multiplex_active(True)
+ token = ss.set_secret_scope(scope)
+ try:
+ assert ss.get_secret("ANTHROPIC_API_KEY") == "sk-x"
+ assert ss.get_secret("OPENAI_API_KEY") == "sk-y"
+ finally:
+ ss.reset_secret_scope(token)
+ ss.set_multiplex_active(False)
+
def test_build_profile_secret_scope(self, tmp_path):
(tmp_path / ".env").write_text("ANTHROPIC_API_KEY=sk-profile\n")
assert ss.build_profile_secret_scope(tmp_path) == {