Why Plaintext Logs Are a Critical Liability
Application logs frequently contain sensitive customer information: session tokens, authorization headers, database connection strings, and user details captured in uncaught exception stack traces. If a database snapshot leaks or an unauthorized actor gains access to storage disks or database replicas, plaintext logs immediately create severe data breaches.
Implementing Authenticated AES-256-GCM
AES-GCM (Galois/Counter Mode) provides both confidentiality and tamper-proof message authenticity. Unlike legacy CBC mode, GCM produces a 128-bit authentication tag that immediately detects bit-flipping and chosen-ciphertext attacks. Here is a standalone, tested Node.js implementation using native node:crypto:
const crypto = require('node:crypto');
// Generate or load 256-bit (32 bytes) master encryption key
function generateMasterKey() {
return crypto.randomBytes(32).toString('hex');
}
// Encrypt payload using AES-256-GCM
function encryptPayload(plaintext, masterKeyHex) {
const masterKey = Buffer.from(masterKeyHex, 'hex');
// NIST SP 800-38D recommended 96-bit (12-byte) initialization vector
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv('aes-256-gcm', masterKey, iv);
const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
const authTag = cipher.getAuthTag(); // 128-bit authentication tag
return {
cipherText: encrypted.toString('base64'),
iv: iv.toString('hex'),
authTag: authTag.toString('hex')
};
}
// Decrypt and authenticate payload (handles both JSON and plain-text strings)
function decryptPayload(vaultEntry, masterKeyHex) {
const masterKey = Buffer.from(masterKeyHex, 'hex');
const decipher = crypto.createDecipheriv(
'aes-256-gcm',
masterKey,
Buffer.from(vaultEntry.iv, 'hex')
);
decipher.setAuthTag(Buffer.from(vaultEntry.authTag, 'hex'));
const decrypted = Buffer.concat([
decipher.update(Buffer.from(vaultEntry.cipherText, 'base64')),
decipher.final()
]);
const plaintext = decrypted.toString('utf8');
try {
return JSON.parse(plaintext);
} catch {
return plaintext;
}
}
Cryptographic Guarantees & Tamper Detection
Decryption validates both the ciphertext and the authentication tag. If even a single byte of the encrypted payload or header was modified on disk, Node's native crypto engine will reject the record with an authentication tag mismatch exception: Error: Unsupported state or unable to authenticate data.
Key Vault Management in Production
TracerNest incorporates this cryptographic pipeline by default. Plaintext logs never touch physical disk or database storageโonly the local .master.key can decrypt the log stream. Best practices for key security include:
- Strict File Permissions: Restrict
.master.keypermissions to POSIX mode0600(read/write for owner only). - Explicit Constructor Key Injection: In Docker or Kubernetes clusters, pass the master key explicitly into the constructor options via
new TracerNestLogger({ masterKey: process.env.TRACERNEST_MASTER_KEY })or the CLI flag--key <masterKey>. TracerNest maintains a strict zero-env-access policy where configuration must be explicitly injected, avoiding silent fallback to an ephemeral.master.keythat could be lost on container restart. - Exclude from Git: Ensure
.master.keyis listed in your project's.gitignore.