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,113 @@
|
||||
"use strict";
|
||||
|
||||
// MongoDB admin credential sealing (audit finding H2).
|
||||
//
|
||||
// The panel could only store a bcrypt hash of the install-time MongoDB admin
|
||||
// password, so every later user-management operation had to strip
|
||||
// `authorization: enabled` from /etc/mongod.conf, restart mongod, run
|
||||
// unauthenticated, and restore — leaving the live server wide open to any
|
||||
// local process during the window, and permanently broken if the panel died
|
||||
// mid-operation. Storing a sealable copy of the admin credential lets the
|
||||
// panel authenticate as the admin instead.
|
||||
//
|
||||
// Envelope format (identical to webhook secrets / node tokens):
|
||||
//
|
||||
// enc:v1:<iv_b64>:<tag_b64>:<ciphertext_b64>
|
||||
//
|
||||
// AES-256-GCM with a key derived from the caller-supplied key material via
|
||||
// scrypt and a mongo-admin-specific salt (domain separation: one master
|
||||
// secret must never yield the same AES key for two subsystems).
|
||||
// adpanel:mongo-admin-sealing-key-override — the key material is chosen by
|
||||
// the ONE wiring site (index.js getMongoAdminCredCipherKey): the
|
||||
// MONGO_ADMIN_SEALING_KEY override when set, else SESSION_SECRET. The
|
||||
// override changes the KDF INPUT only (same scrypt here, same salt, same
|
||||
// 32-byte output), so rotating SESSION_SECRET no longer re-keys the sealed
|
||||
// admin credential; this module stays pure (no environment access) by
|
||||
// contract, which is why the input selection lives at the wiring site.
|
||||
//
|
||||
// This module is pure: no I/O, no caching, no environment access. Fail
|
||||
// closed — seal/open return null on ANY failure (missing/short key material,
|
||||
// malformed envelope, GCM verification failure) rather than throwing, so a
|
||||
// caller can never accidentally persist or act on an unverified value.
|
||||
// Plaintext is never logged and never echoed in error paths.
|
||||
|
||||
const crypto = require("crypto");
|
||||
|
||||
const MONGO_ADMIN_ENC_PREFIX = "enc:v1:";
|
||||
const MONGO_ADMIN_KDF_SALT = "adpanel-mongo-admin-cred-v1";
|
||||
|
||||
// Derives the 32-byte AES key from key material. Accepts either the raw
|
||||
// secret (string — scrypt-derived with MONGO_ADMIN_KDF_SALT) or a
|
||||
// pre-derived 32-byte Buffer (so callers can cache the scrypt result).
|
||||
// Returns null when no usable key material is supplied.
|
||||
function deriveMongoAdminCipherKey(keyMaterial) {
|
||||
try {
|
||||
if (Buffer.isBuffer(keyMaterial)) {
|
||||
return keyMaterial.length === 32 ? keyMaterial : null;
|
||||
}
|
||||
const secret = typeof keyMaterial === "string" ? keyMaterial : "";
|
||||
if (!secret) return null;
|
||||
return crypto.scryptSync(secret, MONGO_ADMIN_KDF_SALT, 32);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Envelope-format check only (no cryptographic verification). Splits must be
|
||||
// present, non-empty and valid base64; GCM still has the final say on
|
||||
// integrity during open.
|
||||
function isMongoSecretEnvelope(value) {
|
||||
if (typeof value !== "string" || !value.startsWith(MONGO_ADMIN_ENC_PREFIX)) return false;
|
||||
const parts = value.slice(MONGO_ADMIN_ENC_PREFIX.length).split(":");
|
||||
if (parts.length !== 3) return false;
|
||||
return parts.every((p) => p.length > 0 && Buffer.from(p, "base64").length > 0);
|
||||
}
|
||||
|
||||
// Seals plaintext into the envelope format. Returns null on any failure —
|
||||
// callers must treat null as "do not persist".
|
||||
function sealMongoSecret(plaintext, keyMaterial) {
|
||||
try {
|
||||
const value = typeof plaintext === "string" ? plaintext : String(plaintext || "");
|
||||
if (!value) return null;
|
||||
const key = deriveMongoAdminCipherKey(keyMaterial);
|
||||
if (!key) return null;
|
||||
const iv = crypto.randomBytes(12);
|
||||
// authTagLength: 16 on both sides rejects truncated auth tags at
|
||||
// setAuthTag time instead of decrypting under a partial MAC.
|
||||
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv, { authTagLength: 16 });
|
||||
const ciphertext = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
|
||||
const authTag = cipher.getAuthTag();
|
||||
return `${MONGO_ADMIN_ENC_PREFIX}${iv.toString("base64")}:${authTag.toString("base64")}:${ciphertext.toString("base64")}`;
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Opens a sealed envelope. Returns the plaintext on success, null on any
|
||||
// failure (wrong key, rotated key material since storage, tampering,
|
||||
// truncation). Never returns a partially decrypted value.
|
||||
function openMongoSecret(envelope, keyMaterial) {
|
||||
try {
|
||||
if (!isMongoSecretEnvelope(envelope)) return null;
|
||||
const key = deriveMongoAdminCipherKey(keyMaterial);
|
||||
if (!key) return null;
|
||||
const parts = envelope.slice(MONGO_ADMIN_ENC_PREFIX.length).split(":");
|
||||
const iv = Buffer.from(parts[0], "base64");
|
||||
const authTag = Buffer.from(parts[1], "base64");
|
||||
const ciphertext = Buffer.from(parts[2], "base64");
|
||||
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv, { authTagLength: 16 });
|
||||
decipher.setAuthTag(authTag);
|
||||
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString("utf8");
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MONGO_ADMIN_ENC_PREFIX,
|
||||
MONGO_ADMIN_KDF_SALT,
|
||||
deriveMongoAdminCipherKey,
|
||||
isMongoSecretEnvelope,
|
||||
sealMongoSecret,
|
||||
openMongoSecret,
|
||||
};
|
||||
Reference in new issue
Block a user