codekingpro/portable-devtools
114k
1"""Msgpack deserialization safety controls.2 3Set ``LANGGRAPH_STRICT_MSGPACK=true`` to restrict checkpoint deserialization4to the types listed in ``SAFE_MSGPACK_TYPES``. Without this, any Python5callable stored in checkpoint data will be imported and executed on load.6"""7 8import os9from collections.abc import Iterable10from typing import cast11 12STRICT_MSGPACK_ENABLED = os.getenv("LANGGRAPH_STRICT_MSGPACK", "false").lower() in (13 "1",14 "true",15 "yes",16)17 18 19_SENTINEL = cast(None, object())20 21SAFE_MSGPACK_TYPES: frozenset[tuple[str, ...]] = frozenset(22 {23 # datetime types24 ("datetime", "datetime"),25 ("datetime", "date"),26 ("datetime", "time"),27 ("datetime", "timedelta"),28 ("datetime", "timezone"),29 # uuid30 ("uuid", "UUID"),31 # numeric32 ("decimal", "Decimal"),33 # collections34 ("builtins", "set"),35 ("builtins", "frozenset"),36 ("collections", "deque"),37 # ip addresses38 ("ipaddress", "IPv4Address"),39 ("ipaddress", "IPv4Interface"),40 ("ipaddress", "IPv4Network"),41 ("ipaddress", "IPv6Address"),42 ("ipaddress", "IPv6Interface"),43 ("ipaddress", "IPv6Network"),44 # pathlib45 ("pathlib", "Path"),46 ("pathlib", "PosixPath"),47 ("pathlib", "WindowsPath"),48 # pathlib in Python 3.13+49 ("pathlib._local", "Path"),50 ("pathlib._local", "PosixPath"),51 ("pathlib._local", "WindowsPath"),52 # zoneinfo53 ("zoneinfo", "ZoneInfo"),54 # regex55 ("re", "compile"),56 # langchain-core messages (safe container types used by graph state)57 ("langchain_core.messages.base", "BaseMessage"),58 ("langchain_core.messages.base", "BaseMessageChunk"),59 ("langchain_core.messages.human", "HumanMessage"),60 ("langchain_core.messages.human", "HumanMessageChunk"),61 ("langchain_core.messages.ai", "AIMessage"),62 ("langchain_core.messages.ai", "AIMessageChunk"),63 ("langchain_core.messages.system", "SystemMessage"),64 ("langchain_core.messages.system", "SystemMessageChunk"),65 ("langchain_core.messages.chat", "ChatMessage"),66 ("langchain_core.messages.chat", "ChatMessageChunk"),67 ("langchain_core.messages.tool", "ToolMessage"),68 ("langchain_core.messages.tool", "ToolMessageChunk"),69 ("langchain_core.messages.function", "FunctionMessage"),70 ("langchain_core.messages.function", "FunctionMessageChunk"),71 ("langchain_core.messages.modifier", "RemoveMessage"),72 # langchain-core document model73 ("langchain_core.documents.base", "Document"),74 # langgraph75 ("langgraph.types", "Send"),76 ("langgraph.types", "TimeoutPolicy"),77 ("langgraph.types", "Interrupt"),78 ("langgraph.types", "Command"),79 ("langgraph.types", "StateSnapshot"),80 ("langgraph.types", "PregelTask"),81 ("langgraph.types", "Overwrite"),82 ("langgraph.store.base", "Item"),83 ("langgraph.store.base", "GetOp"),84 }85)86 87# Allowed (module, name, method) triples for EXT_METHOD_SINGLE_ARG.88# Only these specific method invocations are permitted during deserialization.89# This is separate from SAFE_MSGPACK_TYPES which only governs construction.90SAFE_MSGPACK_METHODS: frozenset[tuple[str, str, str]] = frozenset(91 {92 ("datetime", "datetime", "fromisoformat"),93 }94)95 96 97AllowedMsgpackModules = Iterable[tuple[str, ...] | type]98 