211 lines
6.4 KiB
JavaScript
211 lines
6.4 KiB
JavaScript
"use strict";
|
|
|
|
const POLLUTION_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
|
|
function cleanString(value) {
|
|
return String(value ?? "").trim();
|
|
}
|
|
|
|
function cleanScript(value, maxLength = 262144) {
|
|
return String(value ?? "")
|
|
.replace(/\0/g, "")
|
|
.replace(/\r\n?/g, "\n")
|
|
.trim()
|
|
.slice(0, maxLength);
|
|
}
|
|
|
|
function toFiniteNumber(value) {
|
|
const numeric = Number(value);
|
|
return Number.isFinite(numeric) ? numeric : null;
|
|
}
|
|
|
|
function normalizePortProtocol(value) {
|
|
const protocol = cleanString(value).toLowerCase();
|
|
return protocol === "tcp" || protocol === "udp" || protocol === "sctp" ? protocol : "";
|
|
}
|
|
|
|
function sanitizePortProtocolPayload(portProtocols, ports = []) {
|
|
if (!portProtocols || typeof portProtocols !== "object" || Array.isArray(portProtocols)) return null;
|
|
|
|
const allowedPorts = new Set(
|
|
(Array.isArray(ports) ? ports : [])
|
|
.map((entry) => toFiniteNumber(entry))
|
|
.filter((entry) => Number.isInteger(entry) && entry > 0)
|
|
.map((entry) => String(entry))
|
|
);
|
|
const out = {};
|
|
for (const [rawPort, rawProtocols] of Object.entries(portProtocols)) {
|
|
const port = toFiniteNumber(rawPort);
|
|
if (!Number.isInteger(port) || port < 1 || port > 65535) continue;
|
|
const portKey = String(port);
|
|
if (allowedPorts.size && !allowedPorts.has(portKey)) continue;
|
|
|
|
const source = Array.isArray(rawProtocols) ? rawProtocols : [rawProtocols];
|
|
const protocols = [];
|
|
const seen = new Set();
|
|
for (const entry of source) {
|
|
const protocol = normalizePortProtocol(entry);
|
|
if (!protocol || seen.has(protocol)) continue;
|
|
seen.add(protocol);
|
|
protocols.push(protocol);
|
|
}
|
|
if (protocols.length) out[portKey] = protocols;
|
|
}
|
|
return Object.keys(out).length ? out : null;
|
|
}
|
|
|
|
function cleanAbsoluteContainerPath(value, fallback = "") {
|
|
let raw = cleanString(value).replace(/\0/g, "").replace(/\\/g, "/");
|
|
if (!raw) return fallback;
|
|
if (!raw.startsWith("/")) return fallback;
|
|
raw = raw.replace(/\/+/g, "/");
|
|
if (raw.length > 1) raw = raw.replace(/\/+$/, "");
|
|
return raw || fallback;
|
|
}
|
|
|
|
function sanitizeEnvPayload(env) {
|
|
if (!env || typeof env !== "object" || Array.isArray(env)) return null;
|
|
const out = {};
|
|
for (const [key, value] of Object.entries(env)) {
|
|
const envKey = cleanString(key);
|
|
if (POLLUTION_KEYS.has(envKey)) continue;
|
|
if (!envKey) continue;
|
|
out[envKey] = String(value ?? "");
|
|
}
|
|
return Object.keys(out).length ? out : null;
|
|
}
|
|
|
|
function sanitizeDockerInstallPayload(install) {
|
|
if (!install || typeof install !== "object" || Array.isArray(install)) return null;
|
|
|
|
const script = cleanScript(install.script || install.installScript || install.install_script);
|
|
if (!script) return null;
|
|
|
|
const image = cleanString(install.image || install.container) || "ghcr.io/pterodactyl/installers:debian";
|
|
const mountPath = cleanAbsoluteContainerPath(
|
|
install.mountPath || install.containerPath || install.container_path || install.serverPath,
|
|
"/mnt/server"
|
|
);
|
|
const runtimePath = cleanAbsoluteContainerPath(
|
|
install.runtimePath || install.runtime_path || install.homePath || install.home_path,
|
|
"/home/container"
|
|
);
|
|
|
|
const minimumStorageRaw = toFiniteNumber(install.minimumStorageMb || install.minimum_storage_mb);
|
|
const minimumStorageMb = minimumStorageRaw && minimumStorageRaw > 0
|
|
? Math.max(1, Math.min(Math.trunc(minimumStorageRaw), 1024 * 1024))
|
|
: 0;
|
|
const timeoutRaw = toFiniteNumber(install.timeoutSeconds || install.timeout_seconds);
|
|
const timeoutSeconds = timeoutRaw && timeoutRaw > 0
|
|
? Math.max(60, Math.min(Math.trunc(timeoutRaw), 7200))
|
|
: 1800;
|
|
|
|
const payload = {
|
|
image,
|
|
script,
|
|
mountPath,
|
|
runtimePath,
|
|
timeoutSeconds,
|
|
};
|
|
if (minimumStorageMb > 0) payload.minimumStorageMb = minimumStorageMb;
|
|
if (install.allowEmpty === true || install.allowEmptyResult === true) {
|
|
payload.allowEmpty = true;
|
|
}
|
|
|
|
const env = sanitizeEnvPayload(install.env);
|
|
if (env) payload.env = env;
|
|
|
|
return payload;
|
|
}
|
|
|
|
function sanitizeDockerTemplatePayload(docker) {
|
|
if (!docker || typeof docker !== "object") return null;
|
|
|
|
const image = cleanString(docker.image);
|
|
if (!image) return null;
|
|
|
|
const payload = {
|
|
image,
|
|
};
|
|
|
|
const tag = cleanString(docker.tag) || "latest";
|
|
if (tag) payload.tag = tag;
|
|
|
|
const command = cleanString(docker.command);
|
|
if (command) payload.command = command;
|
|
|
|
const workdir = cleanString(docker.workdir || docker.workingDir || docker.working_dir);
|
|
if (workdir) payload.workdir = workdir;
|
|
|
|
const restart = cleanString(docker.restart);
|
|
if (restart) payload.restart = restart;
|
|
|
|
const restartPolicy = cleanString(docker.restartPolicy);
|
|
if (restartPolicy) payload.restartPolicy = restartPolicy;
|
|
|
|
if (docker.runAsRoot === true || docker.run_as_root === true) {
|
|
payload.runAsRoot = true;
|
|
}
|
|
|
|
const ports = Array.isArray(docker.ports)
|
|
? docker.ports
|
|
.map((entry) => {
|
|
const port = toFiniteNumber(entry);
|
|
return Number.isInteger(port) && port > 0 ? port : null;
|
|
})
|
|
.filter((entry) => entry != null)
|
|
: [];
|
|
if (ports.length) payload.ports = ports;
|
|
|
|
const portProtocols = sanitizePortProtocolPayload(
|
|
docker.portProtocols || docker.port_protocols || docker.protocols,
|
|
ports
|
|
);
|
|
if (portProtocols) payload.portProtocols = portProtocols;
|
|
|
|
const volumes = Array.isArray(docker.volumes)
|
|
? docker.volumes.map((entry) => cleanString(entry)).filter(Boolean)
|
|
: [];
|
|
if (volumes.length) payload.volumes = volumes;
|
|
|
|
const env = sanitizeEnvPayload(docker.env);
|
|
if (env && Object.keys(env).length) payload.env = env;
|
|
|
|
const adpanelStartup = cleanScript(
|
|
docker.adpanelStartup ||
|
|
docker.adpanel_startup ||
|
|
docker.pterodactylStartup ||
|
|
docker.pterodactyl_startup ||
|
|
docker.startup,
|
|
32768
|
|
);
|
|
if (adpanelStartup) payload.adpanelStartup = adpanelStartup;
|
|
|
|
const startupDisplay = cleanScript(
|
|
docker.startupDisplay ||
|
|
docker.startup_display ||
|
|
docker.startupPreview ||
|
|
docker.startup_preview ||
|
|
docker.imageDefaultStartup ||
|
|
docker.image_default_startup,
|
|
32768
|
|
);
|
|
if (startupDisplay) payload.startupDisplay = startupDisplay;
|
|
|
|
const install = sanitizeDockerInstallPayload(docker.install || docker.installation);
|
|
if (install) payload.install = install;
|
|
|
|
if (docker.console !== undefined) {
|
|
payload.console = docker.console;
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
module.exports = {
|
|
sanitizeDockerTemplatePayload,
|
|
sanitizeDockerInstallPayload,
|
|
sanitizeEnvPayload,
|
|
sanitizePortProtocolPayload,
|
|
};
|