Repository navigation
fix(mcp): restrict catalog environment writes #91139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,25 +215,59 @@ def _warn_config_parse_failure( | |
| # NOT a HERMES_* blanket: integration credentials (HERMES_GEMINI_*, | ||
| # HERMES_LANGFUSE_*, HERMES_SPOTIFY_*, ...) ARE allowed. | ||
| "HERMES_HOME", "HERMES_PROFILE", "HERMES_CONFIG", "HERMES_ENV", | ||
| "HERMES_CONFIG_PATH", "HERMES_ENV_PATH", | ||
| # MCP catalog trust root. Package-manager wrappers may still provide this | ||
| # in the process environment; only generic persistence writes are blocked. | ||
| "HERMES_OPTIONAL_MCPS", | ||
| # Hermes security policy / approval-routing context. These remain available | ||
| # through their dedicated CLI/config/session controls, but a generic | ||
| # credential writer must not persist them for the next process startup. | ||
| "HERMES_YOLO_MODE", "HERMES_ACCEPT_HOOKS", "HERMES_REDACT_SECRETS", | ||
| "HERMES_INTERACTIVE", "HERMES_EXEC_ASK", "HERMES_GATEWAY_SESSION", | ||
| "HERMES_CRON_SESSION", "HERMES_SINGLE_QUERY_SESSION", | ||
| "HERMES_SESSION_KEY", "HERMES_SESSION_PLATFORM", | ||
| }) | ||
|
|
||
|
|
||
| def _env_var_policy_name(key: str, *, is_windows: Optional[bool] = None) -> str: | ||
| """Return the name used for environment policy comparisons. | ||
|
|
||
| Windows environment names are case-insensitive; POSIX names are not. The | ||
| explicit override keeps both semantics directly testable without pretending | ||
| the test interpreter is running on another host OS. | ||
| """ | ||
| windows = _IS_WINDOWS if is_windows is None else is_windows | ||
| return key.upper() if windows else key | ||
|
|
||
|
|
||
| def _reject_denylisted_env_var(key: str) -> None: | ||
| """Raise if ``key`` is in :data:`_ENV_VAR_NAME_DENYLIST`. | ||
|
|
||
| Centralised so both the regular and "secure" env writers share the | ||
| same gate, and so the message is consistent for callers. | ||
| """ | ||
| if key in _ENV_VAR_NAME_DENYLIST: | ||
| if _env_var_policy_name(key) in _ENV_VAR_NAME_DENYLIST: | ||
| raise ValueError( | ||
| f"Environment variable {key!r} is on the writer denylist. " | ||
| "Names that influence subprocess execution (LD_PRELOAD, " | ||
| "PYTHONPATH, PATH, EDITOR, ...) or Hermes runtime location " | ||
| "(HERMES_HOME, HERMES_PROFILE, ...) cannot be persisted via " | ||
| "and security policy (HERMES_HOME, HERMES_YOLO_MODE, ...) " | ||
| "cannot be persisted via " | ||
| "the env writer. If you really need this, edit " | ||
| "~/.hermes/.env directly." | ||
| ) | ||
|
|
||
|
|
||
| def validate_env_var_name_for_write(key: str) -> None: | ||
| """Validate an environment name before a generic persistence write. | ||
|
|
||
| Exposed separately from :func:`save_env_value` so batch-style callers can | ||
| validate their complete request before writing the first value. | ||
| """ | ||
| if not _ENV_VAR_NAME_RE.match(key): | ||
| raise ValueError(f"Invalid environment variable name: {key!r}") | ||
| _reject_denylisted_env_var(key) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] Canonicalize the name before the policy lookup.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Commit Coverage includes pure Windows/POSIX policy tests without faking the host OS,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved on exact head |
||
|
|
||
| _LAST_EXPANDED_CONFIG_BY_PATH: Dict[str, Any] = {} | ||
| # (path, mtime_ns, size) -> cached expanded config dict. | ||
| # load_config() returns a deepcopy of the cached value when the file | ||
|
|
@@ -4190,7 +4224,12 @@ def _quote_env_value(value: str) -> str: | |
| return f'"{escaped}"' | ||
|
|
||
|
|
||
| def _env_line_defines_key(line: str, key: str) -> bool: | ||
| def _env_line_defines_key( | ||
| line: str, | ||
| key: str, | ||
| *, | ||
| is_windows: Optional[bool] = None, | ||
| ) -> bool: | ||
| """True when a .env line assigns ``key`` — plain or ``export``-prefixed. | ||
|
|
||
| ``load_env()`` accepts the bash-compatible ``export KEY=value`` form | ||
|
|
@@ -4201,7 +4240,13 @@ def _env_line_defines_key(line: str, key: str) -> bool: | |
| stripped = line.strip() | ||
| if stripped.startswith("export "): | ||
| stripped = stripped[7:].lstrip() | ||
| return stripped.startswith(f"{key}=") | ||
| assigned_key, separator, _value = stripped.partition("=") | ||
| if not separator: | ||
| return False | ||
| return _env_var_policy_name( | ||
| assigned_key, | ||
| is_windows=is_windows, | ||
| ) == _env_var_policy_name(key, is_windows=is_windows) | ||
|
|
||
|
|
||
| def save_env_value(key: str, value: str): | ||
|
|
@@ -4222,9 +4267,7 @@ def save_env_value(key: str, value: str): | |
| file=sys.stderr, | ||
| ) | ||
| return | ||
| if not _ENV_VAR_NAME_RE.match(key): | ||
| raise ValueError(f"Invalid environment variable name: {key!r}") | ||
| _reject_denylisted_env_var(key) | ||
| validate_env_var_name_for_write(key) | ||
| value = value.replace("\n", "").replace("\r", "") | ||
| # API keys / tokens must be ASCII — strip non-ASCII with a warning. | ||
| value = _check_non_ascii_credential(key, value) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Block the complete execution-control class, not selected names.
HERMES_OPTIONAL_MCPSis still writable here;mcp_catalog._catalog_root()trusts it as the catalog root, and a manifest from that directory can supplyinstall.bootstrap, which_run_bootstrap()executes withshell=True. The shared writer also still acceptsHERMES_COPILOT_ACP_COMMAND/HERMES_COPILOT_ACP_ARGS, whichagent/copilot_acp_client.pyuses to construct a subprocess invocation. Becausesave_env_value()updatesos.environimmediately, these are live in the current dashboard process as well as future children. Define one authoritative non-writable set covering catalog/skill trust roots and the behavioral keys already identified by_PROFILE_MANAGED_ENV_KEYS, then add public/api/envregressions. Otherwise the new catalog schema blocks one route while equivalent execution paths remain open.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks — I agree with the MCP-specific trust-root gap. Commit
6818ea0e4faddsHERMES_OPTIONAL_MCPSto the shared writer denylist, so authenticated/api/envwrites can no longer replace the catalog root in either the current process or a later restart. The restriction is deliberately write-only: pre-existing.envvalues still load, and package-manager/process-suppliedHERMES_OPTIONAL_MCPSstill resolves. The public endpoint and both compatibility paths now have regression coverage (test_generic_env_endpoint_rejects_protected_key,test_preexisting_optional_mcps_override_still_loads, andtest_process_supplied_catalog_root_remains_supported).I did not fold all of
_PROFILE_MANAGED_ENV_KEYSinto this MCP PR. That set includes documented Copilot ACP command/path/base-URL overrides; blocking all of them through the generic writer would change an unrelated provider configuration surface and widen the blast radius for MCP users. The catalog route is closed against undeclared keys, and this follow-up closes its alternate MCP trust-root path. The Copilot/shared-writer execution-control class should be handled separately with its own compatibility analysis rather than being bundled into this MCP fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-review at exact head
6818ea0e4fb1820916a1f76bbc018cdb521cfadc: the MCP trust-root half is fixed, but this P1 is only partially closed.HERMES_OPTIONAL_MCPSis now protected and the Windows-name bypass is handled, but the same shared writer still acceptsHERMES_COPILOT_ACP_COMMAND/HERMES_COPILOT_ACP_ARGS;agent/copilot_acp_client.pyreads those values directly to choose the executable/argv for a subprocess. Becausesave_env_value()also updates the live process environment, the generic authenticated env-writing surface still has an execution-control path.If that class is intentionally split out of this MCP patch, it needs an explicit interlock to a concrete follow-up rather than treating the original finding as closed. I am leaving this thread unresolved until either the shared boundary is closed here or that follow-up exists and this PR is scoped/proven as an MCP-only slice.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved on exact head
6b94252f4226bed24f0a3152b5165e4f9486ec66. The PR now absorbs #91565 directly:HERMES_COPILOT_ACP_COMMANDandHERMES_COPILOT_ACP_ARGSare blocked by the shared writer, Windows aliases are covered, rejected public/api/envwrites cannot change the live ACP command/argv resolvers, and pre-existing operator configuration remains readable. The original P1 execution-control finding is closed on this head.