Files
Kelx/utils/minecraft-versions.js
2026-11-30 16:32:37 +00:00

69 lines
2.8 KiB
JavaScript

"use strict";
// F8 (unvalidated mcVersion/mcFork reach the node): the panel used to forward
// these fields with only a lowercase/trim, and the Go daemon interpolated the
// version straight into the PaperMC/Purpur API URL path. The upstream hosts
// are fixed, so this was never SSRF — but a crafted value could manipulate
// the request path/query against api.papermc.io / api.purpurmc.org and land
// unvalidated in the server scaffold. These helpers are the single source of
// truth for what a "fork" and a "version" are, used by the create endpoint,
// the dashboard assistant create tool, and mirrored by the daemon (main.go
// resolveMinecraftFork/resolveMinecraftVersion must stay in sync).
//
// Validation is a strict allowlist, not sanitization: a value that does not
// parse is rejected (null) so the caller can return a clear error, instead of
// silently creating a server that differs from what was asked for.
const SUPPORTED_MC_FORKS = Object.freeze(["paper", "pufferfish", "vanilla", "purpur"]);
// Legacy names that map onto a supported fork. Spigot/Bukkit servers are
// Paper-compatible (Paper is the maintained successor); before this validator
// an unknown fork fell through to a Purpur download, which was never what a
// "spigot" request meant.
const MC_FORK_ALIASES = Object.freeze({
spigot: "paper",
bukkit: "paper",
});
// Minecraft versions that the upstream providers can actually serve:
// releases 1.21, 1.21.8, 1.20.4
// pre-releases / RCs 1.21.8-pre1, 1.21.9-rc2
// snapshots 24w14a
// Anything else (paths, query/fragment separators, encoded traversal, future
// unknown formats) is rejected. Kept in sync with the Go daemon's
// minecraftVersionPattern in main.go.
const MC_VERSION_PATTERN = /^(?:1\.\d{1,2}(?:\.\d{1,2})?(?:-(?:pre|rc)\d{1,2})?|\d{1,2}w\d{1,2}[a-z])$/;
const MC_VERSION_MAX_LEN = 20;
const DEFAULT_MC_FORK = "paper";
const DEFAULT_MC_VERSION = "1.21.8";
// "" / undefined → default fork; aliases mapped; unsupported → null.
function resolveMcFork(raw) {
const value = String(raw ?? "").trim().toLowerCase();
if (value === "") return DEFAULT_MC_FORK;
const aliased = MC_FORK_ALIASES[value] || value;
return SUPPORTED_MC_FORKS.includes(aliased) ? aliased : null;
}
// "" / undefined → default version; lowercased (all real ids are lowercase,
// so case tricks can't smuggle anything past downstream comparisons);
// malformed → null.
function resolveMcVersion(raw) {
const value = String(raw ?? "").trim().toLowerCase();
if (value === "") return DEFAULT_MC_VERSION;
if (value.length > MC_VERSION_MAX_LEN) return null;
return MC_VERSION_PATTERN.test(value) ? value : null;
}
module.exports = {
SUPPORTED_MC_FORKS,
MC_FORK_ALIASES,
MC_VERSION_PATTERN,
MC_VERSION_MAX_LEN,
DEFAULT_MC_FORK,
DEFAULT_MC_VERSION,
resolveMcFork,
resolveMcVersion,
};