From 362a1f766c33a8eca70320c63f49b3c1201851ed Mon Sep 17 00:00:00 2001 From: Yule Date: Fri, 7 Aug 2026 01:35:03 +0800 Subject: [PATCH] fix(webui): quote dynamic shell arguments --- web/src/fontPicker.ts | 6 +++--- web/src/ksu.ts | 9 +++++++++ web/src/main.ts | 26 ++++++++++++++++++-------- web/src/tools.ts | 18 ++++++++++-------- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/web/src/fontPicker.ts b/web/src/fontPicker.ts index b59f55e..bd6b61f 100644 --- a/web/src/fontPicker.ts +++ b/web/src/fontPicker.ts @@ -4,7 +4,7 @@ import '@material/web/list/list-item.js'; import '@material/web/icon/icon.js'; import '@material/web/iconbutton/icon-button.js'; import '@material/web/button/text-button.js'; -import { exec } from './ksu'; +import { exec, shellQuote } from './ksu'; export interface FileItem { name: string; @@ -103,8 +103,8 @@ export class FontFilePicker { // (兼容 busybox 与 GNU find, 不解析 ls 的列输出) private async readDir(path: string): Promise<{ items: FileItem[]; failed: boolean }> { const [dirRes, fileRes] = await Promise.all([ - exec(`find "${path}" -maxdepth 1 -mindepth 1 -type d -print0`), - exec(`find "${path}" -maxdepth 1 -mindepth 1 -type f -print0`), + exec(`find ${shellQuote(path)} -maxdepth 1 -mindepth 1 -type d -print0`), + exec(`find ${shellQuote(path)} -maxdepth 1 -mindepth 1 -type f -print0`), ]); if (dirRes.errno !== 0 || fileRes.errno !== 0) { diff --git a/web/src/ksu.ts b/web/src/ksu.ts index b67a9e3..5936d82 100644 --- a/web/src/ksu.ts +++ b/web/src/ksu.ts @@ -6,6 +6,15 @@ export interface ExecResult { stderr: string; } +/** + * Quote one argument for the POSIX shell used by KernelSU.exec. + * Prefer spawn() when the command can be expressed as argv; use this helper + * whenever exec() must receive a shell command containing external input. + */ +export function shellQuote(value: string): string { + return `'${value.replace(/'/g, `'\\''`)}'`; +} + // dev 模式假文件系统 (用于文件选择器, 路径结构与 Android 一致) const FAKE_FS: Record = { '/': { dirs: ['storage'], fonts: [], files: [] }, diff --git a/web/src/main.ts b/web/src/main.ts index a262e9a..f59c517 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -18,7 +18,15 @@ import 'material-symbols/outlined.css'; import './theme.scss'; import './style.scss'; -import { amStart, exec, enableEdgeToEdge, getSystemInfo, moduleInfo, toast } from './ksu'; +import { + amStart, + exec, + enableEdgeToEdge, + getSystemInfo, + moduleInfo, + shellQuote, + toast, +} from './ksu'; import { FontFilePicker } from './fontPicker'; import { FontEditorToolDef, MiFontToolDef, ToolHost } from './tools'; import type { FontSlot } from './types'; @@ -132,7 +140,7 @@ async function readFontInfo( async function refreshSlotInfo(slot: FontSlot) { if (!slot.path) return; try { - await exec(`cp -f '${slot.path}' '${TEST_FONT_DIR}/${slot.key}.ttf'`); + await exec(`cp -f ${shellQuote(slot.path)} ${shellQuote(`${TEST_FONT_DIR}/${slot.key}.ttf`)}`); const info = await readFontInfo(`${slot.key}.ttf`); if (info.name) slot.fileName = info.name; if (info.sizeText) slot.sizeText = info.sizeText; @@ -239,7 +247,7 @@ function renderSlots() { } async function copyFont(src: string, dest: string) { - const { errno, stderr } = await exec(`cp -f '${src}' '${dest}'`); + const { errno, stderr } = await exec(`cp -f ${shellQuote(src)} ${shellQuote(dest)}`); if (errno !== 0) throw new Error(`复制失败: ${stderr}`); } @@ -258,7 +266,7 @@ async function apply() { await copyFont(slots.hant.path, `${FONTS_DIR}/hant.ttf`); } } else { - await exec(`rm -f '${FONTS_DIR}/hant.ttf'`); + await exec(`rm -f ${shellQuote(`${FONTS_DIR}/hant.ttf`)}`); } if (slots.en.path) { @@ -266,7 +274,7 @@ async function apply() { await copyFont(slots.en.path, `${FONTS_DIR}/en.ttf`); } } else { - await exec(`rm -f '${FONTS_DIR}/en.ttf'`); + await exec(`rm -f ${shellQuote(`${FONTS_DIR}/en.ttf`)}`); } if (slots.mono.path) { @@ -274,7 +282,7 @@ async function apply() { await copyFont(slots.mono.path, `${FONTS_DIR}/mono.ttf`); } } else { - await exec(`rm -f '${FONTS_DIR}/mono.ttf'`); + await exec(`rm -f ${shellQuote(`${FONTS_DIR}/mono.ttf`)}`); } if (slots.emoji.path) { @@ -282,7 +290,7 @@ async function apply() { await copyFont(slots.emoji.path, `${FONTS_DIR}/emoji.ttf`); } } else { - await exec(`rm -f '${FONTS_DIR}/emoji.ttf'`); + await exec(`rm -f ${shellQuote(`${FONTS_DIR}/emoji.ttf`)}`); } const { errno, stdout, stderr } = await exec('sh /data/adb/modules/FontMM/apply.sh'); @@ -574,7 +582,9 @@ async function ensureFontsCopy(): Promise { // 列出 fonts-test/ 下的字体文件 (每行一个完整路径) async function listFontsDir(): Promise { try { - const { errno, stdout } = await exec(`find '${TEST_FONT_DIR}' -maxdepth 1 -mindepth 1 -type f`); + const { errno, stdout } = await exec( + `find ${shellQuote(TEST_FONT_DIR)} -maxdepth 1 -mindepth 1 -type f`, + ); if (errno !== 0) return []; return stdout .split('\n') diff --git a/web/src/tools.ts b/web/src/tools.ts index 4e51360..70491f3 100644 --- a/web/src/tools.ts +++ b/web/src/tools.ts @@ -1,5 +1,5 @@ import '@material/web/button/filled-button.js'; -import { exec } from './ksu'; +import { exec, shellQuote } from './ksu'; import { FontFilePicker } from './fontPicker'; import { renderPreview, type PreviewData } from './previewRenderer'; @@ -195,7 +195,7 @@ export const MiFontToolDef: ToolDef = { resultEl.textContent = '搜索中...'; try { const url = `${SEARCH_API}${encodeURIComponent(keywordEl.value.trim() || '字体')}&page=${page}`; - const { errno, stdout } = await exec(`curl -s '${url}'`); + const { errno, stdout } = await exec(`curl -s ${shellQuote(url)}`); if (errno !== 0) throw new Error('curl 执行失败'); const data = JSON.parse(stdout); const items = data?.data?.items ?? []; @@ -219,7 +219,7 @@ export const MiFontToolDef: ToolDef = { let seen = 0; while (Date.now() < deadline) { await new Promise((r) => setTimeout(r, 1000)); - const { stdout } = await exec(`cat '${logFile}' 2>/dev/null || true`); + const { stdout } = await exec(`cat ${shellQuote(logFile)} 2>/dev/null || true`); if (stdout.length > seen) { logEl.textContent += stdout.slice(seen); seen = stdout.length; @@ -240,7 +240,7 @@ export const MiFontToolDef: ToolDef = { const script = '/data/adb/modules/FontMM/webroot/mi-font-download.sh'; const encodedTitle = encodeURIComponent(title); await exec( - `rm -f '${logFile}' && nohup sh '${script}' '${title}' '${link}' '${encodedTitle}' > '${logFile}' 2>&1 &`, + `rm -f ${shellQuote(logFile)} && nohup sh ${shellQuote(script)} ${shellQuote(title)} ${shellQuote(link)} ${shellQuote(encodedTitle)} > ${shellQuote(logFile)} 2>&1 &`, ); logEl.hidden = false; logEl.textContent = ''; @@ -470,7 +470,9 @@ class FontEditorPage { this.statusEl.textContent = '正在复制字体到工作目录...'; this.openProgress.hidden = false; try { - await exec(`mkdir -p '${WEBROOT_DIR}/work' && cp -f '${path}' '${WEBROOT_DIR}/work/${name}'`); + await exec( + `mkdir -p ${shellQuote(`${WEBROOT_DIR}/work`)} && cp -f ${shellQuote(path)} ${shellQuote(`${WEBROOT_DIR}/work/${name}`)}`, + ); await this.loadFont(`work/${encodeURIComponent(name)}`, name); } catch (e) { this.statusEl.textContent = `复制失败: ${(e as Error).message}`; @@ -647,15 +649,15 @@ class FontEditorPage { const outDir = '/storage/emulated/0/Download/FontMM'; const tmp = `${WEBROOT_DIR}/work/export.tmp`; - await exec(`rm -f '${tmp}' && mkdir -p '${outDir}'`); + await exec(`rm -f ${shellQuote(tmp)} && mkdir -p ${shellQuote(outDir)}`); const CHUNK = 60000; // base64 字符, 约 45KB 二进制 const total = Math.ceil(b64.length / CHUNK); for (let i = 0; i < b64.length; i += CHUNK) { const part = b64.slice(i, i + CHUNK); - await exec(`echo '${part}' | base64 -d >> '${tmp}'`); + await exec(`echo ${shellQuote(part)} | base64 -d >> ${shellQuote(tmp)}`); this.exportProgress.value = (i / CHUNK + 1) / total; } - await exec(`mv '${tmp}' '${outDir}/${outName}'`); + await exec(`mv ${shellQuote(tmp)} ${shellQuote(`${outDir}/${outName}`)}`); this.statusEl.textContent = `已导出: ${outDir}/${outName}`; // 弹窗反馈 (在操作附近, 明确可见) this.page.querySelector('#ft-export-path')!.textContent = `${outDir}/${outName}`;