feat(tests): add unit tests for readValidDirectory function to validate path traversal logic

This commit is contained in:
Mauricio Siu
2026-02-17 14:22:20 -06:00
parent 06fd561bb1
commit b58f2b236f
8 changed files with 269 additions and 18 deletions

View File

@@ -1,4 +1,6 @@
import os from "node:os";
import path from "node:path";
import { paths } from "@dokploy/server/constants";
import { publicIpv4, publicIpv6 } from "public-ip";
export const getShell = () => {
@@ -33,3 +35,18 @@ export const getPublicIpWithFallback = async () => {
}
return ip;
};
export const readValidDirectory = (
directory: string,
serverId?: string | null,
) => {
const { BASE_PATH } = paths(!!serverId);
const resolvedBase = path.resolve(BASE_PATH);
const resolvedDir = path.resolve(directory);
return (
resolvedDir === resolvedBase ||
resolvedDir.startsWith(resolvedBase + path.sep)
);
};