fix(cli): prevent .env sanitizer from splitting GLM_API_KEY by LM_API_KEY suffix#17141
Closed
jackjin1997 wants to merge 1 commit into
Closed
fix(cli): prevent .env sanitizer from splitting GLM_API_KEY by LM_API_KEY suffix#17141jackjin1997 wants to merge 1 commit into
jackjin1997 wants to merge 1 commit into
Conversation
…_KEY suffix The known-key splitter in `_sanitize_env_lines` used substring matching to find concatenated KEY=VALUE pairs. When a registered key was a suffix of another (LM_API_KEY is a suffix of GLM_API_KEY), the shorter key's needle would match inside the longer one, causing the sanitizer to rewrite `GLM_API_KEY=...` as `G\nLM_API_KEY=...` and silently break Z.AI/GLM auth (and similarly `GLM_BASE_URL` -> `G\nLM_BASE_URL`). Drop matches whose needle range is fully contained within a longer overlapping match. Two regression tests cover the suffix-collision case and confirm a real concatenation that happens to start with the longer key still splits where it should. Fixes NousResearch#17138
This was referenced Apr 29, 2026
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fix a corruption bug in
_sanitize_env_lineswhere any registered env var name that is a suffix of another (e.g.LM_API_KEYis a suffix ofGLM_API_KEY) would cause the longer key's line to be silently split into corrupted halves likeG\nLM_API_KEY=.... This rewrote the user's.envfile on disk and disabled provider auth for the affected key (Z.AI/GLM in the report; same shape applies toGLM_BASE_URLvsLM_BASE_URL).The reporter pinpointed the exact mechanism: the splitter walks each
.envline scanning for any known-key name as a substring, with no word-boundary check.stripped.find(\"LM_API_KEY=\")returns 1 inside\"GLM_API_KEY=...\"becauseLM_API_KEY=is a literal suffix ofGLM_API_KEY=.Related Issue
Fixes #17138
Type of Change
Changes Made
hermes_cli/config.py—_sanitize_env_lines: collect full needle ranges(start, end)for every known-key match, then drop matches whose range is fully contained within a longer overlapping match. This kills suffix-collision splits while keeping every legitimate concatenation case (where matches do not nest).tests/hermes_cli/test_config.py— two new tests underTestSanitizeEnvLines:test_glm_suffix_collision_not_split— direct regression for [Bug]: On start, Hermes Agent is santizing GLM_API_KEY/GLM_BASE_URL into G\nLM_API_KEY and G\nLM_BASE_URL #17138.test_suffix_collision_does_not_break_real_concatenation— confirms a genuine concatGLM_API_KEY=glmLM_API_KEY=lm-keystill splits at the second key, since that match is not nested inside the first.How to Test
All 13 tests (11 existing + 2 new) pass. Full `tests/hermes_cli/test_config.py` suite (52 tests) also passes.
Manual repro:
Checklist
AI Disclosure
This bug was identified and fixed with AI assistance.