Home / File/ test_secret_injection.py — langchain Source File

test_secret_injection.py — langchain Source File

Architecture documentation for test_secret_injection.py, a python file in the langchain codebase. 11 imports, 0 dependents.

File python CoreAbstractions RunnableInterface 11 imports 3 functions 10 classes

Entity Profile

Dependency Diagram

graph LR
  901a6c0c_2813_c391_3a48_4db4b001544d["test_secret_injection.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  9e98f0a7_ec6e_708f_4f1b_e9428b316e1c["os"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 9e98f0a7_ec6e_708f_4f1b_e9428b316e1c
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6ebcaae2_3bc1_badf_b751_e164ff2776c4["unittest"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 6ebcaae2_3bc1_badf_b751_e164ff2776c4
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 120e2591_3e15_b895_72b6_cb26195e40a6
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  36cce5da_d805_04c3_7e86_e1b4dd49b497["langchain_core.load"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> 36cce5da_d805_04c3_7e86_e1b4dd49b497
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> d758344f_537f_649e_f467_b9d7442e86df
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  901a6c0c_2813_c391_3a48_4db4b001544d --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  style 901a6c0c_2813_c391_3a48_4db4b001544d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Tests for secret injection prevention in serialization.

Verify that user-provided data containing secret-like structures cannot be used to
extract environment variables during deserialization.
"""

import json
import os
import re
from typing import Any
from unittest import mock

import pytest
from pydantic import BaseModel

from langchain_core.documents import Document
from langchain_core.load import dumpd, dumps, load
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.outputs import ChatGeneration

SENTINEL_ENV_VAR = "TEST_SECRET_INJECTION_VAR"
"""Sentinel value that should NEVER appear in serialized output."""

SENTINEL_VALUE = "LEAKED_SECRET_MEOW_12345"
"""Sentinel value that should NEVER appear in serialized output."""

MALICIOUS_SECRET_DICT: dict[str, Any] = {
    "lc": 1,
    "type": "secret",
    "id": [SENTINEL_ENV_VAR],
}
"""The malicious secret-like dict that tries to read the env var"""


@pytest.fixture(autouse=True)
def _set_sentinel_env_var() -> Any:
    """Set the sentinel env var for all tests in this module."""
    with mock.patch.dict(os.environ, {SENTINEL_ENV_VAR: SENTINEL_VALUE}):
        yield


def _assert_no_secret_leak(payload: Any) -> None:
    """Assert that serializing/deserializing payload doesn't leak the secret."""
    # First serialize
    serialized = dumps(payload)

    # Deserialize with secrets_from_env=True (the dangerous setting)
    deserialized = load(serialized, secrets_from_env=True)

    # Re-serialize to string
    reserialized = dumps(deserialized)

    assert SENTINEL_VALUE not in reserialized, (
        f"Secret was leaked! Found '{SENTINEL_VALUE}' in output.\n"
        f"Original payload type: {type(payload)}\n"
        f"Reserialized output: {reserialized[:500]}..."
    )

    assert SENTINEL_VALUE not in repr(deserialized), (
        f"Secret was leaked in deserialized object! Found '{SENTINEL_VALUE}'.\n"
// ... (372 more lines)

Subdomains

Dependencies

  • json
  • langchain_core.documents
  • langchain_core.load
  • langchain_core.messages
  • langchain_core.outputs
  • os
  • pydantic
  • pytest
  • re
  • typing
  • unittest

Frequently Asked Questions

What does test_secret_injection.py do?
test_secret_injection.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_secret_injection.py?
test_secret_injection.py defines 3 function(s): _assert_no_secret_leak, _set_sentinel_env_var, test_allowed_objects.
What does test_secret_injection.py depend on?
test_secret_injection.py imports 11 module(s): json, langchain_core.documents, langchain_core.load, langchain_core.messages, langchain_core.outputs, os, pydantic, pytest, and 3 more.
Where is test_secret_injection.py in the architecture?
test_secret_injection.py is located at libs/core/tests/unit_tests/load/test_secret_injection.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/tests/unit_tests/load).

Analyze Your Own Codebase

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

Try Supermodel Free