fix: harden URL-decoded image paths

Co-authored-by: zcqqq <10296164+zcqqq@users.noreply.github.com>
This commit is contained in:
Jim Liu 宝玉
2026-05-27 21:34:59 -05:00
parent 876f01ac19
commit 84cefc2784
6 changed files with 130 additions and 13 deletions
+17 -2
View File
@@ -72757,8 +72757,13 @@ async function resolveImagePath(imagePath, baseDir, tempDir, logLabel = "baoyu-m
}
return localPath;
}
const resolved = import_node_path6.default.isAbsolute(imagePath) ? imagePath : import_node_path6.default.resolve(baseDir, imagePath);
return resolveLocalWithFallback(resolved, logLabel);
const decoded = safeDecodeImagePath(imagePath);
const resolved = resolveAgainstBaseDir(decoded, baseDir);
const resolvedWithFallback = resolveLocalWithFallback(resolved, logLabel);
if (decoded === imagePath || import_node_fs5.default.existsSync(resolvedWithFallback)) {
return resolvedWithFallback;
}
return resolveLocalWithFallback(resolveAgainstBaseDir(imagePath, baseDir), logLabel);
}
async function resolveContentImages(images, baseDir, tempDir, logLabel = "baoyu-md") {
const resolved = [];
@@ -72793,6 +72798,16 @@ function resolveLocalWithFallback(resolved, logLabel) {
}
return resolved;
}
function safeDecodeImagePath(imagePath) {
try {
return decodeURIComponent(imagePath);
} catch {
return imagePath;
}
}
function resolveAgainstBaseDir(imagePath, baseDir) {
return import_node_path6.default.isAbsolute(imagePath) ? imagePath : import_node_path6.default.resolve(baseDir, imagePath);
}
// src/mermaid-preprocess.ts
var import_node_fs6 = __toESM(require("node:fs"));
var import_node_path7 = __toESM(require("node:path"));
+17 -2
View File
@@ -72683,8 +72683,13 @@ async function resolveImagePath(imagePath, baseDir, tempDir, logLabel = "baoyu-m
}
return localPath;
}
const resolved = path5.isAbsolute(imagePath) ? imagePath : path5.resolve(baseDir, imagePath);
return resolveLocalWithFallback(resolved, logLabel);
const decoded = safeDecodeImagePath(imagePath);
const resolved = resolveAgainstBaseDir(decoded, baseDir);
const resolvedWithFallback = resolveLocalWithFallback(resolved, logLabel);
if (decoded === imagePath || fs5.existsSync(resolvedWithFallback)) {
return resolvedWithFallback;
}
return resolveLocalWithFallback(resolveAgainstBaseDir(imagePath, baseDir), logLabel);
}
async function resolveContentImages(images, baseDir, tempDir, logLabel = "baoyu-md") {
const resolved = [];
@@ -72719,6 +72724,16 @@ function resolveLocalWithFallback(resolved, logLabel) {
}
return resolved;
}
function safeDecodeImagePath(imagePath) {
try {
return decodeURIComponent(imagePath);
} catch {
return imagePath;
}
}
function resolveAgainstBaseDir(imagePath, baseDir) {
return path5.isAbsolute(imagePath) ? imagePath : path5.resolve(baseDir, imagePath);
}
// src/mermaid-preprocess.ts
import fs6 from "node:fs";
import path6 from "node:path";