diff --git a/lib/routes-v07.js b/lib/routes-v07.js index b576725..83edcdf 100644 --- a/lib/routes-v07.js +++ b/lib/routes-v07.js @@ -7,7 +7,7 @@ * - 首次引导:检查清单 + 不再显示。 */ import { existsSync, readFileSync, createWriteStream, unlinkSync, mkdirSync } from 'node:fs'; -import { join, basename, resolve } from 'node:path'; +import { join, basename } from 'node:path'; import { pipeline } from 'node:stream/promises'; import { Transform } from 'node:stream'; import { randomUUID } from 'node:crypto'; @@ -21,6 +21,7 @@ import { WEB_PROVIDERS, webSearchConfig, ownSearchReady } from './websearch.js'; import { listChatSessions } from './db.js'; import { previewMaiBotExtras, importMaiBotExtras, sniffImage } from './maibot-extras.js'; import { maskKey } from './routes.js'; +import { sameWorkspace, knownWorkspace as _knownWorkspace } from './workspace-guard.js'; const BODY_MAX = 256 * 1024; const IMPORT_BODY_MAX = 6 * 1024 * 1024; // 旧路(JSON + base64)的 body 上限 @@ -103,19 +104,8 @@ export async function handleV07Route({ method, path, url, req, res, send, readBo const renamed = stickers?.autoRename?.(r.file, tags, args.workspacePath) ?? { ok: false }; return { ...r, ...(renamed.ok ? { file: renamed.file } : {}), tags }; }; - /** 同一个工作区?路径写法可能不一样(相对 / 绝对),比绝对路径。 */ - const sameWorkspace = (a, b) => { - const x = String(a ?? ''); const y = String(b ?? ''); - if (x === '' || y === '') return false; - try { return resolve(x) === resolve(y); } catch { return x === y; } - }; - /** ws 得是认得的工作区(当前工作区 / 收藏记录里出现过的),不能随便指个目录来读、改、删文件。 */ - const knownWorkspace = (p) => { - const ws = String(p ?? ''); - if (ws === '') return false; - if (sameWorkspace(ws, gateway?.workspacePath?.() ?? '')) return true; - return (stickers?.collected() ?? []).some((x) => sameWorkspace(x.workspacePath, ws)); - }; + // 工作区校验从 workspace-guard.js 来(设计消除:不 import 就没有) + const knownWorkspace = (p) => _knownWorkspace(p, gateway, stickers); const webState = () => { const w = webConfig?.() ?? webSearchConfig({}); return { diff --git a/lib/workspace-guard.js b/lib/workspace-guard.js new file mode 100644 index 0000000..bb2dc28 --- /dev/null +++ b/lib/workspace-guard.js @@ -0,0 +1,31 @@ +/** + * 工作区路径守卫:sameWorkspace / knownWorkspace。 + * + * 为什么抽出来: + * routes-v07.js 的 _stickers 路由靠这两个函数挡「随便指个目录来读改删文件」。 + * 抽成独立模块让其他需要校验工作区路径的路由直接 import, + * 让「不校验」变成「import 不到」——设计消除,不靠记得调。 + * + * 参照:D40(/stickers 写路由加 knownWorkspace 白名单)。 + */ +import { resolve } from 'node:path'; + +/** 同一个工作区?路径写法可能不一样(相对 / 绝对),比绝对路径。 */ +export function sameWorkspace(a, b) { + const x = String(a ?? ''); const y = String(b ?? ''); + if (x === '' || y === '') return false; + try { return resolve(x) === resolve(y); } catch { return x === y; } +} + +/** + * ws 得是认得的工作区(当前工作区 / 收藏记录里出现过的),不能随便指个目录来读、改、删文件。 + * @param {string} p 待校验的工作区路径 + * @param {{ workspacePath?: () => string }} gateway 网关(拿当前工作区) + * @param {{ collected?: () => Array<{workspacePath: string}> }} stickers 表情包(拿历史工作区) + */ +export function knownWorkspace(p, gateway, stickers) { + const ws = String(p ?? ''); + if (ws === '') return false; + if (sameWorkspace(ws, gateway?.workspacePath?.() ?? '')) return true; + return (stickers?.collected() ?? []).some((x) => sameWorkspace(x.workspacePath, ws)); +} diff --git a/scripts/safe-patch.mjs b/scripts/safe-patch.mjs new file mode 100644 index 0000000..76b62d9 --- /dev/null +++ b/scripts/safe-patch.mjs @@ -0,0 +1,73 @@ +/** + * 安全的文件补丁脚本:读文件 → 替换 → 写回 → 语法检查。 + * + * 为什么要有这个: + * `node -e "..."` 里写正则会被 shell 吃掉反斜杠(\s → s、\d → d), + * 导致「回退脚本写出来的代码是坏的,回退后测试仍然绿」——假安全。 + * 这个脚本把「改代码」变成「写文件再跑」,不会被 shell 转义坑。 + * + * 用法: + * node scripts/safe-patch.mjs <文件> <旧字符串> <新字符串> + * node scripts/safe-patch.mjs --check <文件> # 只检查语法不改 + * + * 替换是精确匹配(不是正则),避免转义问题。 + * 替换后自动跑 node --check(如果是 .js/.mjs 文件)。 + */ +import { readFileSync, writeFileSync } from 'node:fs'; +import { execSync } from 'node:child_process'; + +const [,, ...args] = process.argv; + +if (args.length === 0 || args[0] === '--help') { + console.log('用法:'); + console.log(' node scripts/safe-patch.mjs <文件> <旧字符串> <新字符串>'); + console.log(' node scripts/safe-patch.mjs --check <文件>'); + process.exit(0); +} + +if (args[0] === '--check') { + const file = args[1]; + if (!file) { console.error('缺文件名'); process.exit(1); } + try { + execSync(`node --check "${file}"`, { stdio: 'pipe' }); + console.log(`✓ ${file} 语法检查通过`); + } catch { + console.error(`✗ ${file} 语法检查失败`); + process.exit(1); + } + process.exit(0); +} + +const [file, oldStr, newStr] = args; +if (!file || oldStr === undefined || newStr === undefined) { + console.error('用法:node scripts/safe-patch.mjs <文件> <旧字符串> <新字符串>'); + process.exit(1); +} + +let src; +try { src = readFileSync(file, 'utf8'); } catch { console.error(`读不到 ${file}`); process.exit(1); } + +const count = src.split(oldStr).length - 1; +if (count === 0) { + console.error(`在 ${file} 里找不到旧字符串`); + process.exit(1); +} +if (count > 1) { + console.error(`在 ${file} 里找到 ${count} 处旧字符串,不够精确——请加更多上下文让它唯一`); + process.exit(1); +} + +const patched = src.replace(oldStr, newStr); +writeFileSync(file, patched, 'utf8'); +console.log(`✓ ${file} 替换完成(1 处)`); + +// 自动语法检查 +if (file.endsWith('.js') || file.endsWith('.mjs')) { + try { + execSync(`node --check "${file}"`, { stdio: 'pipe' }); + console.log(`✓ ${file} 语法检查通过`); + } catch { + console.error(`✗ ${file} 替换后语法检查失败——请立即检查`); + process.exit(1); + } +}