Home / Class/ TestConstructorInjection Class — langchain Architecture

TestConstructorInjection Class — langchain Architecture

Architecture documentation for the TestConstructorInjection class in test_secret_injection.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  78bbdb64_3b35_32c5_4637_ce07649639b9["TestConstructorInjection"]
  901a6c0c_2813_c391_3a48_4db4b001544d["test_secret_injection.py"]
  78bbdb64_3b35_32c5_4637_ce07649639b9 -->|defined in| 901a6c0c_2813_c391_3a48_4db4b001544d
  92c82797_a79e_d245_e36a_09f190f49fda["test_constructor_in_metadata_not_instantiated()"]
  78bbdb64_3b35_32c5_4637_ce07649639b9 -->|method| 92c82797_a79e_d245_e36a_09f190f49fda
  df624300_8909_b2f6_3cc5_96ca93ed0e52["test_constructor_in_content_not_instantiated()"]
  78bbdb64_3b35_32c5_4637_ce07649639b9 -->|method| df624300_8909_b2f6_3cc5_96ca93ed0e52

Relationship Graph

Source Code

libs/core/tests/unit_tests/load/test_secret_injection.py lines 366–417

class TestConstructorInjection:
    """Tests for constructor-type injection (not just secrets)."""

    def test_constructor_in_metadata_not_instantiated(self) -> None:
        """Verify constructor-like dict in metadata is not instantiated."""
        malicious_constructor = {
            "lc": 1,
            "type": "constructor",
            "id": ["langchain_core", "messages", "ai", "AIMessage"],
            "kwargs": {"content": "injected"},
        }

        doc = Document(
            page_content="Hello",
            metadata={"data": malicious_constructor},
        )

        serialized = dumpd(doc)
        deserialized = load(
            serialized,
            secrets_from_env=True,
            allowed_objects=[Document, AIMessage],
        )

        # The constructor-like dict should be a plain dict, NOT an AIMessage
        assert isinstance(deserialized.metadata["data"], dict)
        assert deserialized.metadata["data"] == malicious_constructor

    def test_constructor_in_content_not_instantiated(self) -> None:
        """Verify constructor-like dict in message content is not instantiated."""
        malicious_constructor = {
            "lc": 1,
            "type": "constructor",
            "id": ["langchain_core", "messages", "human", "HumanMessage"],
            "kwargs": {"content": "injected"},
        }

        msg = AIMessage(
            content="Hello",
            additional_kwargs={"nested": malicious_constructor},
        )

        serialized = dumpd(msg)
        deserialized = load(
            serialized,
            secrets_from_env=True,
            allowed_objects=[AIMessage, HumanMessage],
        )

        # The constructor-like dict should be a plain dict, NOT a HumanMessage
        assert isinstance(deserialized.additional_kwargs["nested"], dict)
        assert deserialized.additional_kwargs["nested"] == malicious_constructor

Frequently Asked Questions

What is the TestConstructorInjection class?
TestConstructorInjection is a class in the langchain codebase, defined in libs/core/tests/unit_tests/load/test_secret_injection.py.
Where is TestConstructorInjection defined?
TestConstructorInjection is defined in libs/core/tests/unit_tests/load/test_secret_injection.py at line 366.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free