mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 05:34:32 +00:00
refactor(shared): extract reused path and normalization helpers
This commit is contained in:
@@ -19,39 +19,11 @@ import {
|
||||
requireRef,
|
||||
toAIFriendlyError,
|
||||
} from "./pw-tools-core.shared.js";
|
||||
|
||||
function sanitizeDownloadFileName(fileName: string): string {
|
||||
const trimmed = String(fileName ?? "").trim();
|
||||
if (!trimmed) {
|
||||
return "download.bin";
|
||||
}
|
||||
|
||||
// `suggestedFilename()` is untrusted (influenced by remote servers). Force a basename so
|
||||
// path separators/traversal can't escape the downloads dir on any platform.
|
||||
let base = path.posix.basename(trimmed);
|
||||
base = path.win32.basename(base);
|
||||
let cleaned = "";
|
||||
for (let i = 0; i < base.length; i++) {
|
||||
const code = base.charCodeAt(i);
|
||||
if (code < 0x20 || code === 0x7f) {
|
||||
continue;
|
||||
}
|
||||
cleaned += base[i];
|
||||
}
|
||||
base = cleaned.trim();
|
||||
|
||||
if (!base || base === "." || base === "..") {
|
||||
return "download.bin";
|
||||
}
|
||||
if (base.length > 200) {
|
||||
base = base.slice(0, 200);
|
||||
}
|
||||
return base;
|
||||
}
|
||||
import { sanitizeUntrustedFileName } from "./safe-filename.js";
|
||||
|
||||
function buildTempDownloadPath(fileName: string): string {
|
||||
const id = crypto.randomUUID();
|
||||
const safeName = sanitizeDownloadFileName(fileName);
|
||||
const safeName = sanitizeUntrustedFileName(fileName, "download.bin");
|
||||
return path.join(resolvePreferredOpenClawTmpDir(), "downloads", `${id}-${safeName}`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user