From 03fbee0a224f678b7988b02b3421c055d7ec1144 Mon Sep 17 00:00:00 2001 From: ightevenmckane187 <214069160+ightevenmckane187@users.noreply.github.com> Date: Sun, 16 Aug 2026 22:03:11 +0000 Subject: [PATCH] fix(security): prevent heap memory leak and syntax errors in PersistenceLayer - Replace Buffer.allocUnsafe with Buffer.alloc to zero-initialize serialization output buffer and prevent potential uninitialized heap memory leaks. - Clean up syntax errors and unused outBuf variable in save(). --- src/os/persistence/PersistenceLayer.ts | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/os/persistence/PersistenceLayer.ts b/src/os/persistence/PersistenceLayer.ts index b60a349..ae6db36 100644 --- a/src/os/persistence/PersistenceLayer.ts +++ b/src/os/persistence/PersistenceLayer.ts @@ -13,23 +13,14 @@ export class PersistenceLayer { */ save(data: any, key: string): Buffer { const payload = JSON.stringify(data); - const payloadBuf = Buffer.from(payload, 'utf8'); - - // Bolt Optimization: Allocate unsafe buffer for exact combined size to avoid Buffer.concat and intermediate payload allocations - const outBuf = Buffer.allocUnsafe(38 + payloadBuf.length); - - // Copy pre-allocated header and payload - outBuf.set(HEADER_BUF, 0); - outBuf.set(payloadBuf, 38); + const payloadByteLength = Buffer.byteLength(payload, 'utf8'); - // Compute and write HMAC signature directly - const hmac = crypto.createHmac('sha256',. key); - hmac.update(payloadBuf); + const hmac = crypto.createHmac('sha256', key); + hmac.update(payload, 'utf8'); const signature = hmac.digest(); - outBuf.set(signature, 6); - const payloadByteLength = Buffer.byteLength(payload, 'utf8'); - const out = Buffer.allocUnsafe(HEADER_LENGTH + SIGNATURE_LENGTH + payloadByteLength); + // Security Hardening: Use Buffer.alloc to zero-initialize buffer memory and prevent uninitialized heap memory leaks + const out = Buffer.alloc(HEADER_LENGTH + SIGNATURE_LENGTH + payloadByteLength); // Zero-copy set of pre-allocated header out.set(HEADER_MAGIC, 0);