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,257 @@
|
||||
// Standalone verification harness for node bearer token integrity gate (finding 1)
|
||||
// and node API bind host resolution (finding 2).
|
||||
//
|
||||
// Run: node scripts/verify-node-token-integrity.js
|
||||
"use strict";
|
||||
|
||||
const assert = require("assert");
|
||||
const crypto = require("crypto");
|
||||
const path = require("path");
|
||||
|
||||
process.env.SESSION_SECRET = process.env.SESSION_SECRET || crypto.randomBytes(32).toString("hex");
|
||||
|
||||
// adpanel:node-token-sealing-key-override — standalone mirror of index.js's
|
||||
// boot guard (requireStrongSecretOverride). This script seals node tokens
|
||||
// with whatever override the environment carries: a set-but-short (< 32
|
||||
// chars) NODE_TOKEN_SEALING_KEY would seal/repair tokens under a key the
|
||||
// panel refuses to boot with, so this script's sealing would disagree with
|
||||
// the panel's own classification. Fail here instead — before any seal,
|
||||
// repair, or require of the crypto module.
|
||||
const nodeTokenSealingKeyOverride = process.env.NODE_TOKEN_SEALING_KEY;
|
||||
if (nodeTokenSealingKeyOverride !== undefined && nodeTokenSealingKeyOverride !== "" && nodeTokenSealingKeyOverride.length < 32) {
|
||||
console.error("[config] NODE_TOKEN_SEALING_KEY is set but too short. Set a random 32+ chars secret in env.NODE_TOKEN_SEALING_KEY, or leave it unset to derive one from SESSION_SECRET.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const nodeTokenCrypto = require("../utils/node-token-crypto");
|
||||
const { verifyNodeTokenIntegrity } = require("../utils/node-token-integrity");
|
||||
const nodesModule = require("../nodes");
|
||||
|
||||
let passed = 0;
|
||||
function ok(cond, label) {
|
||||
assert.ok(cond, label);
|
||||
passed++;
|
||||
console.log(` ok - ${label}`);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
console.log("[1] node-token-crypto classification");
|
||||
{
|
||||
// 1a. empty
|
||||
ok(nodeTokenCrypto.classifyStoredNodeToken("").state === "empty", "empty string classified as empty");
|
||||
ok(nodeTokenCrypto.classifyStoredNodeToken(null).state === "empty", "null classified as empty");
|
||||
ok(nodeTokenCrypto.classifyStoredNodeToken(undefined).state === "empty", "undefined classified as empty");
|
||||
|
||||
// 1b. plaintext
|
||||
const ptRes = nodeTokenCrypto.classifyStoredNodeToken("legacy_plaintext_secret_123");
|
||||
ok(ptRes.state === "plaintext", "raw secret classified as plaintext");
|
||||
ok(ptRes.plaintext === "legacy_plaintext_secret_123", "plaintext value extracted");
|
||||
|
||||
// 1c. encrypted
|
||||
const enc = nodeTokenCrypto.encryptNodeToken("working_credential_456");
|
||||
ok(nodeTokenCrypto.isEncryptedNodeToken(enc), "token has enc:v1: prefix");
|
||||
const encRes = nodeTokenCrypto.classifyStoredNodeToken(enc);
|
||||
ok(encRes.state === "encrypted", "valid ciphertext classified as encrypted");
|
||||
ok(encRes.plaintext === "working_credential_456", "plaintext matches decrypted value");
|
||||
|
||||
// 1d. corrupt
|
||||
const corruptTag = enc.slice(0, -5) + "AAAAA";
|
||||
ok(nodeTokenCrypto.classifyStoredNodeToken(corruptTag).state === "corrupt", "tampered tag classified as corrupt");
|
||||
const malformed = "enc:v1:invalid-parts";
|
||||
ok(nodeTokenCrypto.classifyStoredNodeToken(malformed).state === "corrupt", "malformed parts classified as corrupt");
|
||||
}
|
||||
|
||||
console.log("[2] hardenNode token_status preservation and auto-minting safety");
|
||||
{
|
||||
// 2a. Healthy node: status stays ok
|
||||
const healthy = nodesModule.hardenNode({
|
||||
id: "n1",
|
||||
token: "valid-token",
|
||||
token_status: nodeTokenCrypto.TOKEN_STATUS_OK
|
||||
});
|
||||
ok(healthy.token_status === nodeTokenCrypto.TOKEN_STATUS_OK, "healthy node retains TOKEN_STATUS_OK");
|
||||
ok(healthy.token === "valid-token", "healthy token preserved");
|
||||
|
||||
// 2b. Legacy token (encrypted at rest, flagged rotate_required): MUST NOT be clobbered to ok!
|
||||
const legacy = nodesModule.hardenNode({
|
||||
id: "n2",
|
||||
token: "legacy-unrotated-token",
|
||||
token_status: nodeTokenCrypto.TOKEN_STATUS_ROTATE_REQUIRED
|
||||
});
|
||||
ok(legacy.token_status === nodeTokenCrypto.TOKEN_STATUS_ROTATE_REQUIRED, "unrotated legacy token PRESERVES rotate_required status");
|
||||
ok(legacy.token === "legacy-unrotated-token", "legacy token value preserved");
|
||||
|
||||
// 2c. Invalidated node (no token, flagged rotate_required): MUST NOT auto-mint a new secret
|
||||
const invalidated = nodesModule.hardenNode({
|
||||
id: "n3",
|
||||
token: "",
|
||||
token_status: nodeTokenCrypto.TOKEN_STATUS_ROTATE_REQUIRED
|
||||
});
|
||||
ok(invalidated.token_status === nodeTokenCrypto.TOKEN_STATUS_ROTATE_REQUIRED, "invalidated node retains rotate_required status");
|
||||
ok(invalidated.token === "", "invalidated node is NOT given an auto-minted token (prevents silent desync)");
|
||||
|
||||
// 2d. Brand new node (no token, no status): auto-mints secret and sets status ok
|
||||
const brandNew = nodesModule.hardenNode({
|
||||
id: "n4"
|
||||
});
|
||||
ok(brandNew.token_status === nodeTokenCrypto.TOKEN_STATUS_OK, "brand new node gets TOKEN_STATUS_OK");
|
||||
ok(typeof brandNew.token === "string" && brandNew.token.length > 10, "brand new node gets minted token");
|
||||
}
|
||||
|
||||
console.log("[3] rotateNodeToken lifecycle");
|
||||
{
|
||||
const db = require("../db");
|
||||
const origQuery = db.query;
|
||||
let savedNode = null;
|
||||
db.query = async (sql, params) => {
|
||||
if (sql.includes("FROM nodes")) {
|
||||
return [{
|
||||
id: "node-test-1",
|
||||
uuid: "node-test-1",
|
||||
name: "test-node",
|
||||
address: "127.0.0.1",
|
||||
token: nodeTokenCrypto.encryptNodeToken("old-token"),
|
||||
token_status: nodeTokenCrypto.TOKEN_STATUS_ROTATE_REQUIRED,
|
||||
build_config: "{}"
|
||||
}];
|
||||
}
|
||||
if (sql.includes("UPDATE nodes")) {
|
||||
savedNode = { token_status: params[8] };
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
const rotated = await nodesModule.rotateNodeToken("node-test-1", "adpnt_fresh_token_xyz");
|
||||
ok(rotated.token === "adpnt_fresh_token_xyz", "rotated token stored");
|
||||
ok(rotated.token_status === nodeTokenCrypto.TOKEN_STATUS_OK, "rotating clears rotate_required to TOKEN_STATUS_OK");
|
||||
ok(savedNode && savedNode.token_status === nodeTokenCrypto.TOKEN_STATUS_OK, "database UPDATE received TOKEN_STATUS_OK");
|
||||
|
||||
db.query = origQuery;
|
||||
}
|
||||
|
||||
console.log("[4] resolveNodeApiBindHost precedence and IPv6 formatting");
|
||||
{
|
||||
// 4a. Explicit override takes precedence
|
||||
const explicit = nodesModule.resolveNodeApiBindHost({
|
||||
address: "198.51.100.2",
|
||||
buildConfig: { api_host: "10.0.0.1" }
|
||||
});
|
||||
ok(explicit === "10.0.0.1", "explicit buildConfig.api_host honored");
|
||||
|
||||
// 4b. Invalid explicit override ignored
|
||||
const invalidExplicit = nodesModule.resolveNodeApiBindHost({
|
||||
address: "198.51.100.2",
|
||||
buildConfig: { api_host: "bad host with spaces\n" }
|
||||
});
|
||||
ok(invalidExplicit === "198.51.100.2", "invalid api_host ignored and falls back to address");
|
||||
|
||||
// 4c. IPv4 address
|
||||
const ipv4 = nodesModule.resolveNodeApiBindHost({
|
||||
address: "198.51.100.2:8080"
|
||||
});
|
||||
ok(ipv4 === "198.51.100.2", "IPv4 address without port extracted");
|
||||
|
||||
// 4d. Bracketed IPv6 address
|
||||
const ipv6 = nodesModule.resolveNodeApiBindHost({
|
||||
address: "[2001:db8::1]:8080"
|
||||
});
|
||||
ok(ipv6 === "2001:db8::1", "bracketed IPv6 unbracketed and recognized as IP");
|
||||
|
||||
// 4e. Hostname falls back to 0.0.0.0
|
||||
const host = nodesModule.resolveNodeApiBindHost({
|
||||
address: "node.example.com:8080"
|
||||
});
|
||||
ok(host === "0.0.0.0", "hostname falls back to 0.0.0.0");
|
||||
|
||||
// 4f. Cloudflare-proxied falls back to 0.0.0.0
|
||||
const cf = nodesModule.resolveNodeApiBindHost({
|
||||
address: "198.51.100.2",
|
||||
buildConfig: { ssl_enabled: true, under_cloudflare: true }
|
||||
});
|
||||
ok(cf === "0.0.0.0", "Cloudflare-proxied node falls back to 0.0.0.0");
|
||||
|
||||
// 4g. FLEET_WIDE wildcard escape hatch
|
||||
process.env.NODE_API_BIND_MODE = "wildcard";
|
||||
const wildcard = nodesModule.resolveNodeApiBindHost({
|
||||
address: "198.51.100.2"
|
||||
});
|
||||
ok(wildcard === "0.0.0.0", "NODE_API_BIND_MODE=wildcard forces 0.0.0.0");
|
||||
delete process.env.NODE_API_BIND_MODE;
|
||||
}
|
||||
|
||||
console.log("[5] verifyNodeTokenIntegrity gate");
|
||||
{
|
||||
const testDb = {
|
||||
rows: [
|
||||
{ id: "1", name: "healthy", token: nodeTokenCrypto.encryptNodeToken("healthy-token"), token_status: "ok" },
|
||||
{ id: "2", name: "legacy-pt", token: "plaintext-token-old", token_status: null },
|
||||
{ id: "3", name: "corrupted", token: "enc:v1:corrupt:corrupt:corrupt", token_status: "ok" },
|
||||
{ id: "4", name: "empty-tok", token: "", token_status: "ok" }
|
||||
],
|
||||
queries: [],
|
||||
async query(sql, params) {
|
||||
this.queries.push({ sql, params });
|
||||
if (sql.startsWith("SELECT")) return this.rows;
|
||||
if (sql.startsWith("UPDATE nodes SET token = ?, token_status = ? WHERE id = ?")) {
|
||||
const [tok, stat, id] = params;
|
||||
const row = this.rows.find(r => r.id === id);
|
||||
if (row) { row.token = tok; row.token_status = stat; }
|
||||
} else if (sql.startsWith("UPDATE nodes SET token = NULL, token_status = ? WHERE id = ?")) {
|
||||
const [stat, id] = params;
|
||||
const row = this.rows.find(r => r.id === id);
|
||||
if (row) { row.token = null; row.token_status = stat; }
|
||||
} else if (sql.startsWith("UPDATE nodes SET token_status = ? WHERE id = ?")) {
|
||||
const [stat, id] = params;
|
||||
const row = this.rows.find(r => r.id === id);
|
||||
if (row) { row.token_status = stat; }
|
||||
}
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const pushedRotations = [];
|
||||
const summary = await verifyNodeTokenIntegrity({
|
||||
db: testDb,
|
||||
getNodeByIdOrName: async (id) => {
|
||||
const row = testDb.rows.find(r => r.id === id);
|
||||
if (!row) return null;
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
token: row.token ? (nodeTokenCrypto.decryptNodeToken(row.token) || row.token) : null
|
||||
};
|
||||
},
|
||||
rotateNodeCredential: async (node, newToken) => {
|
||||
pushedRotations.push({ id: node.id, newToken });
|
||||
return true; // simulated successful push to daemon
|
||||
},
|
||||
logger: { log() {}, warn() {}, error() {} }
|
||||
});
|
||||
|
||||
ok(summary.rotated === 1, "plaintext row was re-encrypted and successfully rotated");
|
||||
ok(summary.invalidated === 1, "corrupt ciphertext row was invalidated (token set to NULL)");
|
||||
ok(summary.flagged >= 2, "corrupt and empty rows flagged rotate_required");
|
||||
|
||||
const rowPt = testDb.rows.find(r => r.id === "2");
|
||||
ok(rowPt.token_status === "ok", "rotated row normalized to ok");
|
||||
ok(nodeTokenCrypto.isEncryptedNodeToken(rowPt.token), "rotated row stored as GCM ciphertext");
|
||||
|
||||
const rowCorrupt = testDb.rows.find(r => r.id === "3");
|
||||
ok(rowCorrupt.token === null, "corrupt token invalidated to NULL");
|
||||
ok(rowCorrupt.token_status === "rotate_required", "corrupt token row flagged rotate_required");
|
||||
|
||||
const rowEmpty = testDb.rows.find(r => r.id === "4");
|
||||
ok(rowEmpty.token_status === "rotate_required", "empty token row flagged rotate_required");
|
||||
}
|
||||
|
||||
console.log(`\nALL ${passed} CHECKS PASSED`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error("Test failure:", err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user