fix(security): eliminate SQL string formatting in execute() calls#2061
Merged
teknium1 merged 1 commit intoNousResearch:mainfrom Mar 19, 2026
Merged
Conversation
Closes NousResearch#1911 - insights.py: Pre-compute SELECT queries as class constants instead of f-string interpolation at runtime. _SESSION_COLS is now evaluated once at class definition time. - hermes_state.py: Add identifier quoting and whitelist validation for ALTER TABLE column names in schema migrations. - Add 4 tests verifying no injection vectors in SQL query construction.
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.
Summary
Closes #1911
Eliminates SQL string formatting anti-patterns in
execute()calls across the codebase. While current inputs are hardcoded constants (not directly exploitable), the f-string interpolation pattern is dangerous and violates parameterized query best practices.Changes
agent/insights.py_GET_SESSIONS_WITH_SOURCE,_GET_SESSIONS_ALL). F-string interpolation of_SESSION_COLSnow runs once at class definition time, never at runtime.hermes_state.pytests/test_sql_injection.pyHow to test
All 64 tests pass (4 new + 60 existing insights tests).
Security note
This PR affects SQL query construction. The fix ensures:
execute()calls_SESSION_COLScontains only safe[a-zA-Z_][a-zA-Z0-9_]*identifiers (verified by test)Platform
Tested on Linux (WSL2, Python 3.11). Changes are pure Python/SQLite — no platform-specific impact.
Full test suite: 4,449 passed, 214 failed (all pre-existing), 174 skipped, 0 new regressions.