AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

RunnableWithMessageHistory Revives Untrusted Constructor Payloads Into Live SystemMessage And Persists Them To Chat History

RunnableWithMessageHistory deserializes dict values in run inputs/outputs with allowed_objects="all", allowing attacker-controlled constructor payloads (e.g., SystemMessage) to be revived into live message objects and written to session history. This can inject malicious system prompts into conversations.

highConfidence 85%LangchainChainsEmbeddingsAffected Vlangchain-Core==1.2.23

Origin Analysis

In langchain_core/runnables/history.py, _get_input_messages and _get_output_messages attempt to deserialize any dict value found under the configured message keys by calling load(value, allowed_objects="all"). This unrestricted deserialization revives any LangChain Serializable constructor payload, including SystemMessage, instead of treating it as inert data.
1. Create a RunnableWithMessageHistory wrapping a RunnableLambda that returns {'output': json.loads(dumps(SystemMessage(content='POISONED_SYSTEM_MESSAGE')))}. 2. Invoke with a session_id and a normal input. 3. Inspect the session history: it contains a live SystemMessage with content 'POISONED_SYSTEM_MESSAGE' in addition to the HumanMessage.

Fixing Code Block

Edge Case Audit

This hotfix may break legitimate cases where users pass serialized message dictionaries (constructor payloads) as input/output under the configured message keys. Such usage was likely rare and not guaranteed; users should pass real BaseMessage objects instead. Rollback: reintroduce the removed branch but restrict allowed_objects to a safe list of concrete message classes (e.g., ['langchain_core.messages.human.HumanMessage', 'langchain_core.messages.ai.AIMessage']) instead of 'all'. Concurrent sessions calling the same session_id remain subject to race conditions inherent in the underlying history store; this fix does not add locking.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state