fix(config): prevent .env sanitizer from corrupting GLM_API_KEY and other suffix-trap keys#17273
fix(config): prevent .env sanitizer from corrupting GLM_API_KEY and other suffix-trap keys#17273searchonedev wants to merge 1 commit into
Conversation
…nd other suffix-trap keys The .env sanitizer uses str.find() to detect concatenated KEY=VALUE pairs. Since LM_API_KEY is a suffix of GLM_API_KEY, scanning a line like GLM_API_KEY=abc123 finds LM_API_KEY= at position 1, triggering a false split into 'G' and 'LM_API_KEY=abc123'. This corrupts the key on every startup because sanitize_env_file() is called from migrate_config(). Same issue affects LM_BASE_URL/GLM_BASE_URL and the three LANGFUSE_* / HERMES_LANGFUSE_* pairs. Fix: pre-compute a set of 'suffix trap' keys (known keys that are suffixes of other known keys) and skip them during the scan. This eliminates substring false-positives while still correctly splitting truly concatenated lines. Affected pairs: - LM_API_KEY inside GLM_API_KEY - LM_BASE_URL inside GLM_BASE_URL - LANGFUSE_PUBLIC_KEY inside HERMES_LANGFUSE_PUBLIC_KEY - LANGFUSE_SECRET_KEY inside HERMES_LANGFUSE_SECRET_KEY - LANGFUSE_BASE_URL inside HERMES_LANGFUSE_BASE_URL
|
Thanks @searchonedev — closing as already-fixed on Your PR identified a real bug: The fix landed on Closing as redundant. Your diagnosis pinpointed the issue correctly. |
Summary
The
_sanitize_env_lines()function inhermes_cli/config.pysilently corrupts API keys on every startup. It usesstr.find()to detect accidentally-concatenatedKEY=VALUEpairs, but sinceLM_API_KEYis a substring ofGLM_API_KEY, scanningGLM_API_KEY=abc123findsLM_API_KEY=at position 1, triggering a false split intoGandLM_API_KEY=abc123.Because
sanitize_env_file()is called unconditionally frommigrate_config()on every startup, the.envfile gets rewritten with the corruption each time.Affected key pairs (5 total)
LM_API_KEYGLM_API_KEYLM_BASE_URLGLM_BASE_URLLANGFUSE_PUBLIC_KEYHERMES_LANGFUSE_PUBLIC_KEYLANGFUSE_SECRET_KEYHERMES_LANGFUSE_SECRET_KEYLANGFUSE_BASE_URLHERMES_LANGFUSE_BASE_URLFix
Pre-compute a "suffix traps" set of known keys that are suffixes of other known keys, and skip them during the scan. This eliminates substring false-positives while still correctly splitting truly concatenated lines.
Testing
GLM_API_KEY=value→ unchanged (was split intoG+LM_API_KEY=value)LM_API_KEY=value→ unchanged (still works standalone)HERMES_LANGFUSE_PUBLIC_KEY=value→ unchangedOPENROUTER_API_KEY=abcGOOGLE_API_KEY=def→ correctly split into 2 lines (real concatenation still handled)