fix(mcp): an unset ${VAR} override also clears the inherited baseline value
Greptile: a key in the safe baseline (XDG_*, secret-source names) kept the
parent value when the server env override was an unset ${VAR}, so the child
saw the parent setting instead of the key being unset.
This commit is contained in:
@@ -9,11 +9,13 @@ def test_unset_whole_placeholder_env_entry_is_dropped():
|
||||
from tools.mcp_tool_config import _build_safe_env, _interpolate_env_vars
|
||||
|
||||
user_env = {"OPT_DIR": "${OPT_DIR}", "OPT_ENV": "${env:OPT_ENV}", "SET_DIR": "${SET_DIR}",
|
||||
"PARTIAL": "${OPT_DIR}/bin", "PLAIN": "x"}
|
||||
with patch.dict("os.environ", {"PATH": "/usr/bin", "SET_DIR": "/opt/set"}, clear=True):
|
||||
"PARTIAL": "${OPT_DIR}/bin", "PLAIN": "x", "XDG_CONFIG_HOME": "${OPT_CONFIG_HOME}"}
|
||||
parent = {"PATH": "/usr/bin", "SET_DIR": "/opt/set", "XDG_CONFIG_HOME": "/parent/config"}
|
||||
with patch.dict("os.environ", parent, clear=True):
|
||||
result = _build_safe_env(_interpolate_env_vars(user_env))
|
||||
|
||||
assert "OPT_DIR" not in result and "OPT_ENV" not in result
|
||||
assert "XDG_CONFIG_HOME" not in result # unset override does not fall back to the parent's value
|
||||
assert result["SET_DIR"] == "/opt/set"
|
||||
assert result["PARTIAL"] == "${OPT_DIR}/bin"
|
||||
assert result["PLAIN"] == "x"
|
||||
|
||||
@@ -207,10 +207,14 @@ def _build_safe_env(user_env: Optional[dict]) -> dict:
|
||||
if key in os.environ:
|
||||
env[key] = os.environ[key]
|
||||
if user_env:
|
||||
# A value that is still a whole ``${VAR}`` here was unset (or empty) at render time: drop
|
||||
# the key so a plugin can forward an optional var without the child seeing the literal
|
||||
# placeholder (servers that validate it, e.g. as an absolute path, refuse to start).
|
||||
env.update({key: value for key, value in user_env.items() if not _is_unresolved_ref(value)})
|
||||
# A value that is still a whole ``${VAR}`` here was unset (or empty) at render time: the
|
||||
# key is left unset (baseline copy included) so a plugin can forward an optional var
|
||||
# without the child seeing the literal placeholder (servers that validate it refuse to start).
|
||||
for key, value in user_env.items():
|
||||
if _is_unresolved_ref(value):
|
||||
env.pop(key, None)
|
||||
else:
|
||||
env[key] = value
|
||||
from agent.delegation_context import delegated_child_subprocess_env
|
||||
return delegated_child_subprocess_env(env)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user