Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,340 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import vm from "node:vm";
|
||||
|
||||
const sandbox = {
|
||||
console,
|
||||
toInt: (n, def = 0) => { const x = Number(n); return Number.isFinite(x) ? Math.round(x) : def; },
|
||||
clampPort: (p) => { const n = Number(p); return (n < 1 || n > 65535) ? 8080 : n; },
|
||||
};
|
||||
vm.createContext(sandbox);
|
||||
|
||||
const scriptCode = `
|
||||
const MIN_NODE_PORT = 1024;
|
||||
const MAX_NODE_PORT = 65535;
|
||||
// Matches the create/edit UI cap ("Port Count ≤ 1000"); enforced server-side so
|
||||
// a direct API call cannot balloon a node's allocation JSON.
|
||||
const MAX_NODE_PORT_ALLOCATION_COUNT = 1000;
|
||||
|
||||
function validateNodePortsInput(input) {
|
||||
if (!input) return { ok: true };
|
||||
const tooMany = (n) => ({ ok: false, error: "At most 1000 ports can be allocated per node." });
|
||||
|
||||
const checkPort = (p) => {
|
||||
const num = Number(p);
|
||||
if (!Number.isFinite(num)) return { ok: false, error: "Invalid port number." };
|
||||
if (num < MIN_NODE_PORT) {
|
||||
return { ok: false, error: "Ports below 1024 cannot be allocated." };
|
||||
}
|
||||
if (num > MAX_NODE_PORT) {
|
||||
return { ok: false, error: "Ports cannot exceed 65535." };
|
||||
}
|
||||
return { ok: true };
|
||||
};
|
||||
|
||||
if (Array.isArray(input)) {
|
||||
if (input.length > MAX_NODE_PORT_ALLOCATION_COUNT) return tooMany();
|
||||
for (const item of input) {
|
||||
if (typeof item === "object" && item !== null) {
|
||||
const res = checkPort(item.port);
|
||||
if (!res.ok) return res;
|
||||
} else if (typeof item === "string" && item.includes(":")) {
|
||||
const parts = item.split(":");
|
||||
const res = checkPort(parts[parts.length - 1]);
|
||||
if (!res.ok) return res;
|
||||
} else {
|
||||
const res = checkPort(item);
|
||||
if (!res.ok) return res;
|
||||
}
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
if (typeof input === "object") {
|
||||
if (Array.isArray(input.allocations)) {
|
||||
if (input.allocations.length > MAX_NODE_PORT_ALLOCATION_COUNT) return tooMany();
|
||||
for (const item of input.allocations) {
|
||||
if (typeof item === "object" && item !== null) {
|
||||
const res = checkPort(item.port);
|
||||
if (!res.ok) return res;
|
||||
} else if (typeof item === "string" && item.includes(":")) {
|
||||
const parts = item.split(":");
|
||||
const res = checkPort(parts[parts.length - 1]);
|
||||
if (!res.ok) return res;
|
||||
} else {
|
||||
const res = checkPort(item);
|
||||
if (!res.ok) return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Array.isArray(input.ports)) {
|
||||
if (input.ports.length > MAX_NODE_PORT_ALLOCATION_COUNT) return tooMany();
|
||||
for (const p of input.ports) {
|
||||
const res = checkPort(p);
|
||||
if (!res.ok) return res;
|
||||
}
|
||||
}
|
||||
if (input.mode === "range" || typeof input.start !== "undefined") {
|
||||
const start = Number(input.start);
|
||||
const count = Number(input.count || 1);
|
||||
if (Number.isFinite(start)) {
|
||||
if (start < MIN_NODE_PORT) {
|
||||
return { ok: false, error: "Ports below 1024 cannot be allocated." };
|
||||
}
|
||||
if (start > MAX_NODE_PORT) {
|
||||
return { ok: false, error: "Ports cannot exceed 65535." };
|
||||
}
|
||||
if (Number.isFinite(count) && count > 0 && start + count - 1 > MAX_NODE_PORT) {
|
||||
return { ok: false, error: "Port range exceeds 65535." };
|
||||
}
|
||||
}
|
||||
if (Number.isFinite(count) && count > MAX_NODE_PORT_ALLOCATION_COUNT) return tooMany();
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
if (typeof input === "string") {
|
||||
const parts = input.split(",").map(s => s.trim()).filter(Boolean);
|
||||
if (parts.length > MAX_NODE_PORT_ALLOCATION_COUNT) return tooMany();
|
||||
for (const part of parts) {
|
||||
const res = checkPort(part);
|
||||
if (!res.ok) return res;
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
function normalizePorts(input, defaultIp = "") {
|
||||
if (!input) return { mode: "range", start: 25565, count: 10, allocations: [], ports: [] };
|
||||
if (Array.isArray(input)) {
|
||||
const allocations = [];
|
||||
const portsSet = new Set();
|
||||
for (const item of input) {
|
||||
if (typeof item === "object" && item !== null) {
|
||||
const p = toInt(item.port);
|
||||
const ip = String(item.ip || defaultIp || "").trim();
|
||||
if (p >= MIN_NODE_PORT && p <= MAX_NODE_PORT) {
|
||||
allocations.push({ ip, port: p });
|
||||
portsSet.add(p);
|
||||
}
|
||||
} else {
|
||||
const p = toInt(item);
|
||||
if (p >= MIN_NODE_PORT && p <= MAX_NODE_PORT) {
|
||||
allocations.push({ ip: defaultIp || "", port: p });
|
||||
portsSet.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
const uniqueMap = new Map();
|
||||
for (const a of allocations) {
|
||||
const key = \`\${a.ip || defaultIp}:\${a.port}\`;
|
||||
if (!uniqueMap.has(key)) uniqueMap.set(key, a);
|
||||
}
|
||||
const uniqueAllocations = Array.from(uniqueMap.values());
|
||||
uniqueAllocations.sort((a, b) => a.port - b.port);
|
||||
return {
|
||||
mode: "list",
|
||||
allocations: uniqueAllocations,
|
||||
ports: Array.from(portsSet).sort((a, b) => a - b)
|
||||
};
|
||||
}
|
||||
if (typeof input === "object") {
|
||||
const targetIp = String(input.ip || defaultIp || "").trim() || defaultIp;
|
||||
if (Array.isArray(input.allocations)) {
|
||||
const allocations = [];
|
||||
const portsSet = new Set();
|
||||
for (const item of input.allocations) {
|
||||
if (typeof item === "object" && item !== null) {
|
||||
const p = toInt(item.port);
|
||||
const ip = String(item.ip || targetIp || defaultIp || "").trim();
|
||||
if (p >= MIN_NODE_PORT && p <= MAX_NODE_PORT) {
|
||||
allocations.push({ ip, port: p });
|
||||
portsSet.add(p);
|
||||
}
|
||||
} else if (typeof item === "string" && item.includes(":")) {
|
||||
const parts = item.split(":");
|
||||
const p = toInt(parts[parts.length - 1]);
|
||||
const ip = parts.slice(0, -1).join(":").trim();
|
||||
if (p >= MIN_NODE_PORT && p <= MAX_NODE_PORT) {
|
||||
allocations.push({ ip: ip || targetIp || defaultIp || "", port: p });
|
||||
portsSet.add(p);
|
||||
}
|
||||
} else {
|
||||
const p = toInt(item);
|
||||
if (p >= MIN_NODE_PORT && p <= MAX_NODE_PORT) {
|
||||
allocations.push({ ip: targetIp || defaultIp || "", port: p });
|
||||
portsSet.add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
const uniqueMap = new Map();
|
||||
for (const a of allocations) {
|
||||
const key = \`\${a.ip || defaultIp}:\${a.port}\`;
|
||||
if (!uniqueMap.has(key)) uniqueMap.set(key, a);
|
||||
}
|
||||
const uniqueAllocations = Array.from(uniqueMap.values());
|
||||
uniqueAllocations.sort((a, b) => a.port - b.port);
|
||||
return {
|
||||
mode: input.mode || "list",
|
||||
allocations: uniqueAllocations,
|
||||
ports: Array.from(portsSet).sort((a, b) => a - b),
|
||||
start: toInt(input.start, 25565),
|
||||
count: toInt(input.count, 10)
|
||||
};
|
||||
}
|
||||
if (input.mode === "range") {
|
||||
let start = toInt(input.start, 25565);
|
||||
let count = toInt(input.count, 10);
|
||||
if (start < MIN_NODE_PORT || start > MAX_NODE_PORT) start = 25565;
|
||||
if (count < 1) count = 1;
|
||||
if (start + count - 1 > MAX_NODE_PORT) count = MAX_NODE_PORT - start + 1;
|
||||
const allocations = [];
|
||||
const ports = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
const p = start + i;
|
||||
allocations.push({ ip: targetIp || defaultIp || "", port: p });
|
||||
ports.push(p);
|
||||
}
|
||||
return { mode: "range", start, count, allocations, ports };
|
||||
}
|
||||
if (input.mode === "list") {
|
||||
const ports = Array.from(new Set((input.ports || [])
|
||||
.map(p => toInt(p))
|
||||
.filter(p => p >= MIN_NODE_PORT && p <= MAX_NODE_PORT)));
|
||||
const allocations = ports.map(p => ({ ip: targetIp || defaultIp || "", port: p }));
|
||||
return { mode: "list", ports, allocations };
|
||||
}
|
||||
}
|
||||
return { mode: "range", start: 25565, count: 10, allocations: [], ports: [] };
|
||||
}
|
||||
|
||||
function isPortInNodeAllocation(node, port) {
|
||||
const p = Number(port);
|
||||
if (!Number.isFinite(p) || p < MIN_NODE_PORT || p > MAX_NODE_PORT) return false;
|
||||
const alloc = node && node.ports;
|
||||
// Fail closed in every degenerate state: an unknown or empty allocation
|
||||
// must mean "nothing is assignable", never "everything is assignable".
|
||||
// The Ports pane's own Select All → Delete persists exactly
|
||||
// {mode:"list", allocations:[], ports:[]}; treating that empty pool as an
|
||||
// allow-all would let server creation bind any free host port on the node,
|
||||
// including ports the admin reserved for node services (the daemon's live
|
||||
// bind test only rejects ports that are already bound).
|
||||
if (!alloc || typeof alloc !== "object") return false;
|
||||
if (Array.isArray(alloc.allocations) && alloc.allocations.length > 0) {
|
||||
return alloc.allocations.some(a => Number(a.port) === p);
|
||||
}
|
||||
if (alloc.mode === "range") {
|
||||
const start = Number(alloc.start || 0);
|
||||
const count = Number(alloc.count || 0);
|
||||
if (start <= 0 || count <= 0) return false;
|
||||
return p >= start && p < start + count;
|
||||
}
|
||||
if (alloc.mode === "list" && Array.isArray(alloc.ports)) {
|
||||
if (alloc.ports.length === 0) return false;
|
||||
return alloc.ports.includes(p);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
`;
|
||||
vm.runInContext(scriptCode, sandbox);
|
||||
|
||||
test("validateNodePortsInput rejects ports below 1024 and allows 1024+", () => {
|
||||
const { validateNodePortsInput } = sandbox;
|
||||
|
||||
// Single port 1024 is allowed
|
||||
assert.equal(validateNodePortsInput([1024]).ok, true);
|
||||
// Single port 1023 is rejected
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput([1023]))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
// Privileged ports 80, 443, 22 are rejected
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput([80, 25565]))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput([443]))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
|
||||
// Mode range: start 1024 allowed
|
||||
assert.equal(validateNodePortsInput({ mode: "range", start: 1024, count: 10 }).ok, true);
|
||||
// Mode range: start 1023 rejected
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput({ mode: "range", start: 1023, count: 10 }))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
|
||||
// Mode list: ports >= 1024 allowed
|
||||
assert.equal(validateNodePortsInput({ mode: "list", ports: [1024, 25565] }).ok, true);
|
||||
// Mode list: port < 1024 rejected
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput({ mode: "list", ports: [1023, 25565] }))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
|
||||
// Allocations array with IP
|
||||
assert.equal(validateNodePortsInput({ allocations: [{ ip: "1.2.3.4", port: 1024 }] }).ok, true);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput({ allocations: [{ ip: "1.2.3.4", port: 80 }] }))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
|
||||
// Comma-separated string
|
||||
assert.equal(validateNodePortsInput("1024, 25565").ok, true);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput("1023, 25565"))), { ok: false, error: "Ports below 1024 cannot be allocated." });
|
||||
});
|
||||
|
||||
test("validateNodePortsInput caps port counts at 1000 like the UI", () => {
|
||||
const { validateNodePortsInput } = sandbox;
|
||||
const err = { ok: false, error: "At most 1000 ports can be allocated per node." };
|
||||
// range
|
||||
assert.equal(validateNodePortsInput({ mode: "range", start: 1024, count: 1000 }).ok, true);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput({ mode: "range", start: 1024, count: 1001 }))), err);
|
||||
// range just inside the 65535 boundary with max count still allowed
|
||||
assert.equal(validateNodePortsInput({ mode: "range", start: 64536, count: 1000 }).ok, true);
|
||||
// list
|
||||
assert.equal(validateNodePortsInput({ mode: "list", ports: Array.from({ length: 1000 }, (_, i) => 2000 + i) }).ok, true);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput({ mode: "list", ports: Array.from({ length: 1001 }, (_, i) => 2000 + i) }))), err);
|
||||
// allocations array
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput({ allocations: Array.from({ length: 1001 }, (_, i) => ({ ip: "1.2.3.4", port: 2000 + i })) }))), err);
|
||||
// bare array and string forms
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput(Array.from({ length: 1001 }, (_, i) => 2000 + i)))), err);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(validateNodePortsInput(Array.from({ length: 1001 }, (_, i) => String(2000 + i)).join(",")))), err);
|
||||
});
|
||||
|
||||
test("validateNodePortsInput still caps counts when range start is non-numeric", () => {
|
||||
const { validateNodePortsInput } = sandbox;
|
||||
// Number("not-a-number") is NaN, which skips the inner 1024/65535 checks; the
|
||||
// count cap must still apply since normalizePorts would default start to 25565.
|
||||
assert.deepEqual(
|
||||
JSON.parse(JSON.stringify(validateNodePortsInput({ mode: "range", start: "not-a-number", count: 60000 }))),
|
||||
{ ok: false, error: "At most 1000 ports can be allocated per node." }
|
||||
);
|
||||
});
|
||||
|
||||
test("normalizePorts strips ports below 1024", () => {
|
||||
const { normalizePorts } = sandbox;
|
||||
|
||||
const result = normalizePorts([80, 443, 1023, 1024, 25565], "1.2.3.4");
|
||||
assert.deepEqual(Array.from(result.ports), [1024, 25565]);
|
||||
assert.equal(result.allocations.length, 2);
|
||||
assert.equal(result.allocations[0].port, 1024);
|
||||
assert.equal(result.allocations[1].port, 25565);
|
||||
});
|
||||
|
||||
test("isPortInNodeAllocation rejects ports below 1024", () => {
|
||||
const { isPortInNodeAllocation } = sandbox;
|
||||
|
||||
const node = { ports: { mode: "range", start: 1024, count: 10 } };
|
||||
assert.equal(isPortInNodeAllocation(node, 1023), false);
|
||||
assert.equal(isPortInNodeAllocation(node, 1024), true);
|
||||
assert.equal(isPortInNodeAllocation(node, 80), false);
|
||||
});
|
||||
|
||||
test("isPortInNodeAllocation fails closed on empty/degenerate allocation pools", () => {
|
||||
const { isPortInNodeAllocation } = sandbox;
|
||||
|
||||
// The exact shape the Ports pane's "Select All → Delete" persists: an empty
|
||||
// pool must allow NOTHING, not fall open to every port.
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "list", allocations: [], ports: [] } }, 25565), false);
|
||||
// Degenerate/missing shapes fail closed as well.
|
||||
assert.equal(isPortInNodeAllocation({}, 8080), false);
|
||||
assert.equal(isPortInNodeAllocation({ ports: {} }, 8080), false);
|
||||
assert.equal(isPortInNodeAllocation({ ports: null }, 8080), false);
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "list", ports: [] } }, 8080), false);
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "range", start: 0, count: 10 } }, 8080), false);
|
||||
// Non-empty pools keep enforcing the allocation as the allowlist.
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "list", allocations: [], ports: [25565] } }, 25565), true);
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "list", allocations: [], ports: [25565] } }, 25566), false);
|
||||
// Range boundaries: inclusive start, exclusive end.
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "range", start: 25565, count: 10 } }, 25565), true);
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "range", start: 25565, count: 10 } }, 25574), true);
|
||||
assert.equal(isPortInNodeAllocation({ ports: { mode: "range", start: 25565, count: 10 } }, 25575), false);
|
||||
});
|
||||
Reference in new issue
Block a user