Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

+153
View File
@@ -0,0 +1,153 @@
"use strict";
// Panel -> node admin request signing key derivation.
//
// Every panel-signed admin request (node updates, disk management, server
// reinstall) is HMAC-authenticated with a per-node secret derived from the
// panel's PANEL_HMAC_SECRET master value. The master itself is never written
// to a node's config.yml, so a root compromise of any single node exposes
// only that node's derived secret — it cannot forge admin signatures against
// the panel or against any other node in the fleet.
//
// Derivation must stay deterministic and stable across panel releases:
// nodes persist the derived secret in config.yml (panel.hmac_secret) and
// verify with it directly. The parameters below are therefore fixed forever;
// if they ever need to change, ship a new version constant and a rotation
// path rather than editing these in place.
const crypto = require("crypto");
const HKDF_SALT = "adpanel-node-admin-signing-v1";
const HKDF_INFO_PREFIX = "panel-admin-signing:node:";
const DERIVED_SECRET_BYTES = 32; // -> 64 hex characters
function normalizeNodeId(nodeId) {
return String(nodeId || "").trim().toLowerCase();
}
// Returns the 64-char hex signing secret for a node, or "" when either input
// is missing/blank. Callers treat "" as "signing not configured" and fail
// closed, exactly like an unset PANEL_HMAC_SECRET.
function deriveNodeHmacSecret(masterSecret, nodeId) {
const master = String(masterSecret || "").trim();
const id = normalizeNodeId(nodeId);
if (!master || !id) return "";
const okm = crypto.hkdfSync("sha256", master, HKDF_SALT, HKDF_INFO_PREFIX + id, DERIVED_SECRET_BYTES);
return Buffer.from(okm).toString("hex");
}
// True for values shaped exactly like a derived secret (64 lowercase hex
// chars). Used to avoid redundant signing attempts when an operator happens
// to set a master that collides with a derived value's format.
function isDerivedSecretFormat(value) {
return /^[0-9a-f]{64}$/.test(String(value || ""));
}
// Panel -> node probe signing (GET /v1/info, GET /api/system/stats).
//
// The probe envelope reuses the node admin envelope verbatim (action="probe",
// subject=<purpose>) so the daemon-side verifier, drift window and nonce
// replay cache are shared with existing signed admin requests. The base
// string MUST stay byte-identical to the daemon's verifyNodeAdminRequest:
// <node-uuid>|probe|<purpose>|admin|<ts>|<nonce>
// nodeId is the node's uuid exactly as written into the daemon's config.yml
// (trimmed, not case-folded) — only the *secret* derivation normalizes case.
function probeSignatureBase(nodeId, purpose, ts, nonce) {
const id = String(nodeId || "").trim();
const subj = String(purpose || "").trim();
return `${id}|probe|${subj}|admin|${ts}${nonce ? `|${nonce}` : ""}`;
}
// Signs a probe request and returns { headers, nonce } — the headers object
// carries x-panel-role/ts/sign/nonce and must be merged into the outgoing
// request headers. Returns null when signing is not possible (blank secret or
// inputs); callers then fall back to the legacy unsigned probe.
function signPanelProbeRequest(signingSecret, nodeId, purpose) {
const secret = String(signingSecret || "").trim();
if (!secret || !String(nodeId || "").trim() || !String(purpose || "").trim()) return null;
const ts = String(Date.now());
const nonce = crypto.randomBytes(16).toString("hex");
const base = probeSignatureBase(nodeId, purpose, ts, nonce);
const sig = crypto.createHmac("sha256", secret).update(base).digest("hex");
return {
headers: {
"x-panel-role": "admin",
"x-panel-ts": ts,
"x-panel-sign": sig,
"x-panel-nonce": nonce,
},
nonce,
};
}
function verifyNodeProbeResponse(signingSecret, nodeId, purpose, probe, response) {
const nonce = probe?.nonce;
const proof = response?.panel_nonce_proof;
const secret = String(signingSecret || "").trim();
if (!secret || !nonce || response?.panel_nonce !== nonce || typeof proof !== "string" || !/^[a-f0-9]{64}$/.test(proof)) return false;
const base = `${String(nodeId || "").trim()}|probe-response|${String(purpose || "").trim()}|${nonce}`;
const expected = crypto.createHmac("sha256", secret).update(base).digest();
return crypto.timingSafeEqual(Buffer.from(proof, "hex"), expected);
}
// Panel -> node server-asset apply-version signing
// (POST /v1/servers/<name>/apply-version). The daemon dispatches this path
// BEFORE bearer-token auth, so the envelope is the request's only
// authentication and the handler fails closed on a missing signature.
//
// The base string MUST stay byte-identical to the daemon's verifyPanelSignature:
// <server-name>|<providerId>|<versionId>|<url>|<ts>[|<nonce>]
// Note the applied-version endpoint, unlike reinstall/node-admin, binds no
// x-panel-role field — the daemon's verifier reads only ts/sign/nonce here.
// serverName is the technical server name exactly as placed (URL-encoded) in
// the request path: the daemon re-derives it from the decoded path segment
// with its sanitizer, which is a no-op on an already-sanitized technical name.
// providerId/versionId bind the signature to the requested store version and
// must be the exact body values — sign "" for either when the body omits it.
// url must be the exact body url (already trimmed by the caller).
function applyVersionSignatureBase(serverName, providerId, versionId, url, ts, nonce) {
const name = String(serverName || "").trim();
const pid = String(providerId ?? "");
const vid = String(versionId ?? "");
const u = String(url ?? "");
return `${name}|${pid}|${vid}|${u}|${ts}${nonce ? `|${nonce}` : ""}`;
}
// Signs one apply-version request and returns { headers, nonce } — the headers
// carry x-panel-ts/sign/nonce (no role field; see above) and must be merged
// into the outgoing request headers. Returns null when signing is not
// possible (blank secret, server name or url); callers treat null as "cannot
// send a verifiable request" and fail closed — the daemon rejects unsigned
// applies unconditionally. opts.legacy signs without a nonce for node
// binaries that predate nonce-authenticated signatures (same ladder as the
// reinstall flow).
function signPanelApplyVersionRequest(signingSecret, serverName, providerId, versionId, url, opts = {}) {
const secret = String(signingSecret || "").trim();
const name = String(serverName || "").trim();
const u = String(url || "").trim();
if (!secret || !name || !u) return null;
const ts = String(Date.now());
const nonce = opts.legacy ? "" : crypto.randomBytes(16).toString("hex");
const base = applyVersionSignatureBase(name, providerId, versionId, u, ts, nonce);
const sig = crypto.createHmac("sha256", secret).update(base).digest("hex");
const headers = {
"x-panel-ts": ts,
"x-panel-sign": sig,
};
if (nonce) {
headers["x-panel-nonce"] = nonce;
}
return { headers, nonce };
}
module.exports = {
deriveNodeHmacSecret,
isDerivedSecretFormat,
probeSignatureBase,
signPanelProbeRequest,
verifyNodeProbeResponse,
applyVersionSignatureBase,
signPanelApplyVersionRequest,
HKDF_SALT,
HKDF_INFO_PREFIX,
};