feat: 新增侧边栏常驻cnb入口,新增关闭环境、文件漫游、文档说明等功能#46
以下内容由 AI 生成
新增 CNB 开发面板及关闭环境功能,优化 API 与常量支持
CodeBuddy Code
问题: ## 路径遍历漏洞
loadDirectoryChildren 函数接收任意路径参数 dirPath,没有任何验证或路径限制。攻击者可以通过 webview 发送恶意路径(如 ../../../etc/passwd)读取系统敏感文件。
loadDirectoryChildren
dirPath
../../../etc/passwd
调用链:
sidebar-manage.ts:39-41
data.path
sidebar-manage.ts:41
loadDirectoryChildren(data.path)
攻击者可在 webview 中执行:
vscode.postMessage({ type: 'loadDirectoryChildren', path: '/etc/passwd' });
建议: ## 修复建议
添加路径验证,确保只能访问预期的目录:
const ALLOWED_BASE_DIR = '/run/.userdata/'; export function loadDirectoryChildren(dirPath: string): any[] { const fs = require('fs'); const path = require('path'); // 路径规范化并验证 const normalizedPath = path.normalize(dirPath); if (!normalizedPath.startsWith(ALLOWED_BASE_DIR)) { console.error('Invalid path access attempt:', dirPath); return []; } // ... 其余代码 }
问题: ## 路径遍历漏洞 - 入口点
webview 消息处理中,data.path 来自不可信来源(webview),直接传递给 loadDirectoryChildren 而未验证。这是上述路径遍历漏洞的入口点。 建议: ## 修复建议
在调用 loadDirectoryChildren 前验证路径:
case 'loadDirectoryChildren': // 验证路径合法性 if (!data.path || typeof data.path !== 'string') { return; } const children = loadDirectoryChildren(data.path); // ...
问题: ## CSS 选择器注入风险
使用用户可控数据构建 CSS 选择器:
const folderPath = expandHint.getAttribute('data-expand-hint'); const folderItem = document.querySelector(`[data-path="${folderPath}"]`);
如果 folderPath 包含双引号 ",选择器会被提前终止,可能导致:
folderPath
"
同样的问题存在于第 62、137-138 行。 建议: ## 修复建议
使用 CSS 转义或通过属性选择器转义:
function escapeCssSelector(value) { return value.replace(/["'\\]/g, '\\$&'); } const folderPath = expandHint.getAttribute('data-expand-hint'); const folderItem = document.querySelector(`[data-path="${escapeCssSelector(folderPath)}"]`);
或使用 document.evaluate / dataset 查询方式避免字符串拼接。
document.evaluate
dataset
新增 CNB 开发面板及关闭环境功能,优化 API 与常量支持