T09 · Insecure Skill Coding Practices
- Location
context-switch-token-optimizer.py:124- Finding
Reset Operations Preserve Persisted Conversation State While Reporting Successful Deletion
- Content
View full analysis
Vulnerability Details
File Location:
context-switch-token-optimizer.py:124-134
Additional Location:token_optimizer.py:227-245
Vulnerability Type: Improper clearing of persisted sensitive state
Risk Level: MediumComplete Code Snippets
context-switch-token-optimizer.py:124-134:python def reset_context(self) -> Dict: """重置上下文""" # 重置上下文管理器 self.context_manager.state = self.context_manager._load_context_state() # 生成重置报告 return { 'action': 'reset_context', 'timestamp': '2026-03-17T11:00:00', 'context_cleared': True, 'token_optimization_active': True, 'message': '上下文已重置,可以开始新的对话主题' }token_optimizer.py:227-245:python def _reset_context(self) -> Dict: """重置上下文""" original_state = { 'topic': self.context_manager.state.current_topic, 'history_size': len(self.context_manager.state.topic_history), 'memory_loaded': bool(self.context_manager.state.memory_context), 'switch_count': self.context_manager.state.switch_count } # 重置上下文状态 self.context_manager.state = self.context_manager._load_context_state() return { 'action': 'reset_context', 'original_state': original_state, 'memory_cleared': True, 'context_cleared': True, 'token_savings': self._estimate_token_savings() }Technical Analysis
Both reset implementations call
_load_context_state(). That method readsmemory/context_switch_state.jsonand reconstructs the previously persisted state; it does not create a new empty state, delete the file, or overwrite it with an empty structure.Consequently, the reset operation restores existing topic history, conversation snippets, loaded memory context, and counters while returning
context_cleared: Trueand, in the optimizer path,memory_cleared: True. The primary CLI--resetoption invokes the affected method in `context-switch-token-op ...[truncated 1556 chars]- Remediation
View remediation
Remediation Suggestions
- Replace state reloading with construction of a new empty
ContextState:
python self.context_manager.state = ContextState( current_topic=None, topic_history=[], memory_context=None, last_switch_time=None, switch_count=0, total_tokens=0, )-
Persist the empty state immediately with
save_context_state(), or securely delete the state file if absence represents an empty state. -
Use atomic replacement when rewriting the file: write the empty state to a temporary file in the same directory, set restrictive permissions, flush it, and replace the original file.
-
Clear related in-memory token usage and optimization logs where the reset contract requires a complete reset.
-
Consolidate the two reset implementations into one shared method to prevent behavioral divergence.
-
Return
context_cleared: trueandmemory_cleared: trueonly after persistent clearing succeeds. Propagate write or deletion failures rather than reporting success. -
Add regression tests that populate the state, invoke each reset path, instantiate a new manager, and verify that no topic history, snippet, memory context, or counters can be recovered.
- Replace state reloading with construction of a new empty
