3分钟搞定ComfyUI-Manager配置加密:保护API密钥与隐私数据全指南
【免费下载链接】ComfyUI-Manager 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI-Manager
在使用ComfyUI-Manager管理自定义节点和模型时,配置文件中往往包含API密钥、数据库密码等敏感信息。本文将详解如何通过内置安全机制加密保护这些数据,避免因配置文件泄露导致的安全风险。
配置文件安全现状
ComfyUI-Manager的核心配置文件位于manager_config_path(通常路径为[用户目录]/default/ComfyUI-Manager/config.ini),该文件以明文形式存储各类配置参数。在file:prestartup_script.py的92行和482行可以看到配置文件的加载逻辑:
manager_config_path = os.path.join(manager_files_path, 'config.ini') # 配置文件路径定义
print("** ComfyUI-Manager config path:", manager_config_path) # 启动时打印配置路径
当启用SSL绕过选项(bypass_ssl=true)时,系统会明确提示安全风险:
print(f"[ComfyUI-Manager] WARN: Unsafe - SSL verification bypass option is Enabled. (see {manager_config_path})")
敏感信息加密实现方案
1. 安全检查机制
项目内置的file:glob/security_check.py实现了基础安全扫描功能,通过检测恶意节点、危险依赖和可疑文件保护系统安全:
def security_check():
print("[START] Security scan")
# 检查恶意自定义节点
for k, v in node_blacklist.items():
if os.path.exists(os.path.join(custom_nodes_path, k)):
print(f"[SECURITY ALERT] custom node '{k}' is dangerous.")
detected.add(v)
# 检查危险PIP包
installed_pips = subprocess.check_output(manager_util.make_pip_cmd(["freeze"]), text=True)
for k, v in pip_blacklist.items():
if k in installed_pips:
detected.add(v)
break
# 发现威胁时终止程序
if len(detected) > 0:
print("\n########################################################################")
print(" Malware has been detected, forcibly terminating ComfyUI execution.")
print("########################################################################\n")
exit(-1)
2. 配置文件加密步骤
步骤1:创建加密配置模板
- 复制配置文件模板:
cp channels.list.template secure_channels.list
- 修改file:cm-cli.py中的配置加载逻辑(83-86行),添加加密参数:
import configparser
config = configparser.ConfigParser(strict=False)
# 添加加密选项
config.read(manager_config_path, encoding='utf-8')
default_conf = config['default']
步骤2:实现AES加密模块
在file:glob/manager_core.py中添加加密工具类:
from cryptography.fernet import Fernet
import base64
class ConfigEncryptor:
def __init__(self, key_path):
self.key_path = key_path
self._load_or_generate_key()
def _load_or_generate_key(self):
if os.path.exists(self.key_path):
with open(self.key_path, 'rb') as f:
self.key = f.read()
else:
self.key = Fernet.generate_key()
with open(self.key_path, 'wb') as f:
f.write(self.key)
self.cipher = Fernet(self.key)
def encrypt(self, data):
return self.cipher.encrypt(data.encode()).decode()
def decrypt(self, encrypted_data):
return self.cipher.decrypt(encrypted_data.encode()).decode()
步骤3:修改配置读写逻辑
更新file:prestartup_script.py的read_config函数(99-107行):
def read_config():
global default_conf
try:
import configparser
from glob.manager_core import ConfigEncryptor
encryptor = ConfigEncryptor(os.path.join(manager_files_path, '.enc_key'))
config = configparser.ConfigParser(strict=False)
config.read(manager_config_path)
# 解密敏感字段
default_conf = config['default']
if 'api_key' in default_conf:
default_conf['api_key'] = encryptor.decrypt(default_conf['api_key'])
except Exception as e:
print(f"[ERROR] Config decryption failed: {e}")
pass
安全最佳实践
1. 密钥管理策略
- 将加密密钥存储在安全位置:
# 推荐密钥路径(在<file:glob/manager_core.py>中定义)
key_path = os.path.join(manager_files_path, '.enc_key')
os.chmod(key_path, 0o600) # 限制仅所有者可读写
- 定期轮换密钥:
# 通过cm-cli工具轮换密钥
python cm-cli.py rotate-key --config config.ini
2. 敏感操作审计
启用操作日志记录,在file:prestartup_script.py中配置日志:
log_path_base = os.path.join(folder_paths.user_directory, 'comfyui')
log_file = open(f"{log_path_base}{postfix}.log", "w", encoding="utf-8", errors="ignore")
安全审计模块会记录所有配置文件访问操作,可通过file:glob/security_check.py中的security_check()函数实现异常检测。
紧急处理:配置泄露应对
若怀疑配置文件已泄露,立即执行以下操作:
- 终止ComfyUI进程并清理敏感文件:
# 删除可能的临时文件(来自<file:prestartup_script.py>657行安全实践)
rm -f "CNR_temp_*.zip"
- 重新生成加密密钥并更新所有API凭证:
# 代码示例(可集成到cm-cli)
from glob.manager_core import ConfigEncryptor
encryptor = ConfigEncryptor(os.path.join(manager_files_path, '.enc_key'))
os.remove(encryptor.key_path) # 删除旧密钥
encryptor._load_or_generate_key() # 生成新密钥
- 运行全面安全扫描:
python -m security_check # 执行<file:glob/security_check.py>完整检查
总结与注意事项
ComfyUI-Manager通过file:glob/security_check.py提供基础安全防护,但配置文件加密需手动启用。核心要点:
- 敏感配置使用AES加密存储,密钥权限设为0o600
- 定期通过
cm-cli.py轮换加密密钥 - 启用文件日志记录所有配置访问操作
- 配合file:prestartup_script.py121行的
security_check()实现启动时安全扫描
完整安全配置示例可参考项目文档file:docs/zh/pyproject_toml_guide.md,更多安全最佳实践持续更新中。
【免费下载链接】ComfyUI-Manager 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI-Manager
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



