1 Star 0 Fork 239

JavaAgent/Toonflow-app

forked from HBAI-Ltd/Toonflow-app 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
replaceUrl.ts 1012 Bytes
一键复制 编辑 原始数据 按行查看 历史
zcb555 提交于 2026-06-05 16:20 +08:00 . 修复docker 下图片无法显示问题
import path from "node:path";
export default function replaceUrl(url: string): string {
if (typeof url !== "string" || !url.trim()) return "";
let cleanedPath = "";
try {
const pathname = new URL(url).pathname;
cleanedPath = pathname;
} catch (e) {
// 如果不是有效的URL,则直接使用原字符串
cleanedPath = url;
}
cleanedPath = cleanedPath.replace(/^\/oss/, "").replace(/^\/smallImage/, "");
// 去除 query 参数
cleanedPath = cleanedPath.split("?")[0];
// 防止路径穿越:对路径进行规范化后,确保不含上溯分量
// 使用 posix 规范化(保持 / 分隔符),去除所有 .. 和 .
const normalized = path.posix.normalize(cleanedPath);
// 规范化后若路径以 ../ 开头或等于 .. 则说明发生了路径穿越,拒绝并返回空字符串
if (normalized.startsWith("../") || normalized === "..") {
return "";
}
// 去除前导斜杠,保证返回的是相对路径
return normalized.replace(/^\/+/, "");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/JavaAgent_1/Toonflow-app.git
git@gitee.com:JavaAgent_1/Toonflow-app.git
JavaAgent_1
Toonflow-app
Toonflow-app
master

搜索帮助