已合并
【模型分级可视化】调用命令前,增加安全校验 #722
sun-chao创建于 5月28日
【模型分级可视化】调用命令前,增加安全校验 #722
已合并
共 4 个文件变更+102-6
| @@ -16,7 +16,7 @@ import os | |||
| 16 | import json | 16 | import json |
| 17 | import time | 17 | import time |
| 18 | import threading | 18 | import threading |
| 19 | -import subprocess | 19 | +import subprocess # nosec B404 |
| 20 | from abc import ABC, abstractmethod | 20 | from abc import ABC, abstractmethod |
| 21 | from tensorboard.util import tb_logging | 21 | from tensorboard.util import tb_logging |
| 22 | from ..utils.graph_utils import GraphUtils | 22 | from ..utils.graph_utils import GraphUtils |
| @@ -133,7 +133,13 @@ class GraphServiceStrategy(ABC): | |||
| 133 | run_list.extend([param, str(value_n)]) | 133 | run_list.extend([param, str(value_n)]) |
| 134 | if value_b: | 134 | if value_b: |
| 135 | run_list.append(str(value_b)) | 135 | run_list.append(str(value_b)) |
| 136 | - proc = subprocess.Popen(run_list, stdout=subprocess.PIPE, text=True) | 136 | + success, result = GraphUtils.safe_run_command(run_list, stdout=subprocess.PIPE, text=True) |
| 137 | + if not success: | ||
| 138 | + logger.error(f"Failed to run command: {result}") | ||
| 139 | + ProgressInfo.error_msg = result | ||
| 140 | + ProgressInfo.process_running = False | ||
| 141 | + return | ||
| 142 | + proc = result | ||
| 137 | update_progress_info(proc, ProgressInfo) | 143 | update_progress_info(proc, ProgressInfo) |
| 138 | 144 | ||
| 139 | thread = threading.Thread(target=call_path_api) | 145 | thread = threading.Thread(target=call_path_api) |
| @@ -14,6 +14,23 @@ | |||
| 14 | 14 | ||
| 15 | from enum import Enum | 15 | from enum import Enum |
| 16 | 16 | ||
| 17 | +# 允许执行的命令白名单 | ||
| 18 | +ALLOWED_COMMANDS = {"msprobe"} | ||
| 19 | +# 禁止的参数模式(注入攻击特征) | ||
| 20 | +FORBIDDEN_ARG_PATTERNS = [ | ||
| 21 | + r"\|", # 管道符 | ||
| 22 | + r";", # 命令分隔符 | ||
| 23 | + r"&&", # 逻辑与 | ||
| 24 | + r"\|\|", # 逻辑或 | ||
| 25 | + r"`", # 反引号命令替换 | ||
| 26 | + r"\$\(", # 命令替换 | ||
| 27 | + r"\n", # 换行符注入 | ||
| 28 | + r"\r", # 回车符注入 | ||
| 29 | + r">>", # 追加重定向 | ||
| 30 | + r"[<>]", # 重定向 | ||
| 31 | + r"\\x", # 十六进制转义 | ||
| 32 | +] | ||
| 33 | + | ||
| 17 | security_headers = { | 34 | security_headers = { |
| 18 | "Content-Security-Policy": ( | 35 | "Content-Security-Policy": ( |
| 19 | "default-src 'self'; connect-src 'self'; script-src 'unsafe-inline'; " | 36 | "default-src 'self'; connect-src 'self'; script-src 'unsafe-inline'; " |
| @@ -54,11 +54,14 @@ def build_frontend(plugin_name): | |||
| 54 | raise RuntimeError(f"{failed_message} file 'package.json' is not exist!") | 54 | raise RuntimeError(f"{failed_message} file 'package.json' is not exist!") |
| 55 | 55 | ||
| 56 | # 安装依赖 | 56 | # 安装依赖 |
| 57 | - install_result = subprocess.run( # nosec | 57 | + install_result = subprocess.run( # nosec B603, B607 |
| 58 | - ["npm", "install", "--force"], capture_output=True, text=True, check=False | 58 | + ["npm", "ci"], |
【review】【Bug】 【文件和行号】setup.py:57-60 【检视意见】将 npm install --force 替换为 npm ci 属于行为变更:npm ci 要求项目目录下存在且仅依赖 package-lock.json,且当 node_modules 已存在时会直接失败(除非删除),npm install --force 则更宽容。若构建环境的 package-lock.json 与 package.json 不同步,或 node_modules 残留未被清理,构建将失败。建议确认项目中存在 package-lock.json 且 CI 环境执行了 clean 步骤,否则应保留 npm install --force 或添加兼容判断。 ![]() ![]() | |||
| 59 | + capture_output=True, | ||
| 60 | + text=True, | ||
| 61 | + check=False, | ||
| 59 | ) | 62 | ) |
| 60 | if install_result.returncode != 0: | 63 | if install_result.returncode != 0: |
| 61 | - raise RuntimeError(f"{failed_message} run 'npm install --force' failed!") | 64 | + raise RuntimeError(f"{failed_message} run 'npm ci' failed!") |
| 62 | 65 | ||
| 63 | # 执行构建 | 66 | # 执行构建 |
| 64 | build_result = subprocess.run( # nosec | 67 | build_result = subprocess.run( # nosec |


ProgressInfo.error_msg 类型不匹配 — 将字符串赋值给列表字段
plugins/tb_graph_ascend/hierarchy_plugin/server/app/service/graph_service_base.py:140ProgressInfo类的error_msg字段在代码中定义为[](列表类型,见python/msprobe/visualization/utils.py:409),且该类的update_error_msg()方法(utils.py:432-433) 通过cls.error_msg.append(value)操作其内容。本 PR 中ProgressInfo.error_msg = result直接赋值为字符串,改变了字段的类型。后续若有其他代码调用update_error_msg()或error_msg.append(),将触发AttributeError: 'str' object has no attribute 'append',导致服务崩溃。建议改为ProgressInfo.error_msg = [result]以保持列表类型一致性。