"use strict"; // Tests pinning the actual precedence of resolveNodeApiBindHost (nodes.js) — // the function that decides the `api.host` line written into every node's // config.yml. Getting this wrong either exposes the daemon API on every // interface of the host or bricks the panel→node connection, so the ordering // is pinned exactly as implemented: // // 1. NODE_API_BIND_MODE=wildcard (env, case/whitespace-insensitive) — // fleet-wide escape hatch, wins over EVERYTHING → "0.0.0.0" // 2. buildConfig.api_host when it is a valid bare host token // (/^[A-Za-z0-9._:-]{1,64}$/) — per-node override, wins over the // address and over the Cloudflare wildcard (operator choice is read // first); "0.0.0.0" itself restores the wildcard bind // 2b. buildConfig.api_host that is NOT a bare host token (URL, spaces) is // IGNORED with a warning — never echoed into the YAML // 3. nodeUnderCloudflare flags → "0.0.0.0" (a proxied name gives no // bindable origin IP) // 4. node.address when it is a literal IP (v4/v6), after stripping an // optional scheme, :port and IPv6 brackets → that address // 5. anything else (hostnames) → "0.0.0.0" default // // The function is executed for real: nodes.js is loaded against a stubbed db // module (same Module._load technique as server-suspension-rest-bridge — // nodes.js cannot be exercised through index.js, which boots servers on // require). const test = require("node:test"); const assert = require("node:assert"); const path = require("node:path"); const Module = require("node:module"); process.env.ADPANEL_ALLOW_PRIVATE_NODES = "1"; const ROOT = path.join(__dirname, ".."); const NODES_JS = path.join(ROOT, "nodes.js"); const dbStub = { async query() { return []; }, pool: { async execute() { return [[]]; }, async query() { return [[]]; }, }, }; const nodesApp = (() => { const originalLoad = Module._load; Module._load = function (request, parent, isMain) { if (request === "./db" && parent && parent.filename === NODES_JS) return dbStub; return originalLoad.apply(this, arguments); }; try { return require(NODES_JS); } finally { Module._load = originalLoad; } })(); const resolveNodeApiBindHost = nodesApp.resolveNodeApiBindHost; assert.equal(typeof resolveNodeApiBindHost, "function", "resolveNodeApiBindHost must stay exported"); function bindHost(node, bindMode) { const previous = process.env.NODE_API_BIND_MODE; const hadPrevious = Object.prototype.hasOwnProperty.call(process.env, "NODE_API_BIND_MODE"); if (bindMode === undefined) delete process.env.NODE_API_BIND_MODE; else process.env.NODE_API_BIND_MODE = bindMode; try { return resolveNodeApiBindHost(node); } finally { if (hadPrevious) process.env.NODE_API_BIND_MODE = previous; else delete process.env.NODE_API_BIND_MODE; } } test("unset env, no buildConfig, no address -> default wildcard bind", () => { assert.equal(bindHost({}), "0.0.0.0"); assert.equal(bindHost(null), "0.0.0.0"); }); test("NODE_API_BIND_MODE=wildcard wins over api_host and a literal IP (fleet-wide escape hatch)", () => { const node = { address: "203.0.113.5", buildConfig: { api_host: "192.0.2.9" } }; assert.equal(bindHost(node, "wildcard"), "0.0.0.0"); // The mode match is trimmed + lowercased. assert.equal(bindHost(node, " WILDCARD "), "0.0.0.0"); // Any other value is not a mode: normal precedence applies (api_host wins). assert.equal(bindHost(node, "literal"), "192.0.2.9"); }); test("buildConfig.api_host override is honored over a literal IP address", () => { assert.equal(bindHost({ address: "203.0.113.5", buildConfig: { api_host: "panel.example.com" } }), "panel.example.com"); assert.equal(bindHost({ address: "203.0.113.5", buildConfig: { api_host: "192.0.2.9" } }), "192.0.2.9"); // Setting the override to the wildcard itself restores the 0.0.0.0 bind. assert.equal(bindHost({ address: "203.0.113.5", buildConfig: { api_host: "0.0.0.0" } }), "0.0.0.0"); }); test("an invalid buildConfig.api_host is ignored with a warning, never echoed into the config", () => { const warnings = []; const originalWarn = console.warn; console.warn = (...args) => warnings.push(args.join(" ")); try { // Spaces and a URL both fail the bare-host-token shape. assert.equal(bindHost({ address: "203.0.113.5", buildConfig: { api_host: "not a host" } }), "203.0.113.5"); assert.equal(bindHost({ address: "203.0.113.5", buildConfig: { api_host: "http://192.0.2.9" } }), "203.0.113.5"); // Falls all the way through when nothing else binds either. assert.equal(bindHost({ buildConfig: { api_host: "bad host" } }), "0.0.0.0"); } finally { console.warn = originalWarn; } assert.ok( warnings.some((w) => w.includes("Ignoring invalid buildConfig.api_host")), "an ignored override must be surfaced with the [nodes] warning" ); }); test("Cloudflare-proxied nodes wildcard-bind even with a literal IP — but a valid api_host still wins", () => { const proxied = { address: "203.0.113.5", under_cloudflare: 1 }; assert.equal(bindHost(proxied), "0.0.0.0"); assert.equal(bindHost({ address: "203.0.113.5", cloudflare_proxy: true }), "0.0.0.0"); assert.equal(bindHost({ address: "203.0.113.5", buildConfig: { cloudflare_proxy: "1" } }), "0.0.0.0"); // Precedence detail: the operator override is read BEFORE the Cloudflare check. assert.equal(bindHost({ address: "203.0.113.5", under_cloudflare: 1, buildConfig: { api_host: "origin.example.com" } }), "origin.example.com"); }); test("a literal IP address (v4/v6, with optional scheme/port/brackets) is used as the bind host", () => { assert.equal(bindHost({ address: "203.0.113.5" }), "203.0.113.5"); assert.equal(bindHost({ address: "203.0.113.5:8080" }), "203.0.113.5"); assert.equal(bindHost({ address: "https://203.0.113.5:8080/" }), "203.0.113.5"); assert.equal(bindHost({ address: "[2001:db8::1]" }), "2001:db8::1"); assert.equal(bindHost({ address: "http://[2001:db8::1]:8443/api" }), "2001:db8::1"); assert.equal(bindHost({ address: "2001:db8::1" }), "2001:db8::1"); }); test("a hostname address gives no bindable interface -> default wildcard bind", () => { assert.equal(bindHost({ address: "node.example.com" }), "0.0.0.0"); assert.equal(bindHost({ address: "https://node.example.com/" }), "0.0.0.0"); });