Skip to content

Security (Low): keep generated secrets in Rust behind a handle so they are not copied onto the V8 heap #178

Backlog

Description

@WingZer0o

Severity: Low
Not in 2.0.0. #127 records this as an Info note, and #154 documents it. This issue is the follow-up that would change the API in a later release. 2.0 still returns secret bytes to JavaScript.

Finding

ZeroAlloc zeros memory when the Rust allocator frees it. A secret that has been copied into JavaScript is not in that allocator.

Every secret the SDK returns or accepts crosses the FFI boundary as Array<number> (or, for passwords and PEM keys, as string). napi copies a Rust Vec<u8> into a JavaScript array. V8 stores that array on its heap, and it may copy the array again when it resizes it, when it optimizes a function, or when it moves objects during garbage collection. Garbage collection does not zero the old copy. Filling the array with zeros afterward does not clear the copies V8 already made.

So a key the caller has finished with can still be recovered from the process heap. This is a local memory-disclosure issue. It is not a remote attack on the algorithm. It matters for a process dump, a snapshot, swap, or anything else that can read the Node process.

Buffer and Uint8Array do not fix it. They are still V8 heap objects. The copy into JavaScript has already happened.

How a caller uses the library if the key never comes back

The caller does not need the key bytes. They need operations on the key. That is how a non-extractable WebCrypto CryptoKey works, and it is the same shape here.

A handle is a JavaScript object that names a key kept in Rust. Creating a key returns the handle. Encryption, decryption, signing, verification, encapsulation, and derivation take the handle and return the thing the caller actually stores or reads:

  • Ciphertext, a sealed blob, a signature, a public key, a salt, and a password hash still come back as bytes. Those are meant to be saved or sent.
  • Plaintext still comes back as bytes. The application has to read the message. The finding is about keys, shared secrets, and other values the caller does not need to look at.
  • Dropping the handle is what zeros the key, on the Rust heap, where ZeroAlloc runs.

A typical call is: generate a handle, seal a message with it, hold the handle for the life of the request or the process, and drop it when finished. The application stores the ciphertext, not the key.

Two cases still put secret bytes in JavaScript, and the API should say so:

  • A password the user typed. It already exists as a JavaScript string before it reaches this library. A handle cannot pull it back off the V8 heap. The library can avoid making extra copies and can zero the Rust copy. It cannot erase the string the application holds.
  • A key that must leave the process. Saving a key, or sending one to another service, needs an explicit export. That export is opt-in. The returned bytes are the V8 copy this finding describes. Wrapping the key with AES-KW and returning the wrapped bytes is the alternative when the raw key should not be exported. The default handle does not export.

Public keys can be exported freely. They are not secret.

Proposed API

Add handle types beside the current byte API. Do not remove the byte functions in the release that introduces handles. Callers who already hold raw keys keep working. The byte-returning generators stay, and their docs point at the handle types.

  • One handle type per secret the library creates: AES keys, ChaCha20-Poly1305 keys, Ascon keys, X25519 secrets, RSA and Ed25519 private keys, ML-KEM and ML-DSA secrets, and Argon2-derived AES keys.
  • Constructors and generators return a handle. seal / open, encrypt / decrypt, sign / verify, encapsulate / decapsulate, and the X25519 derivation helpers accept a handle anywhere they accept key bytes today.
  • The handle is not enumerable and does not expose the bytes. toString and inspection redact it.
  • An explicit export returns the bytes for the persistence case, and the docs say those bytes land on the V8 heap.
  • Releasing the handle zeros the Rust buffer before it is freed. A finalizer does the same if the caller never releases it. A finalizer runs later, so release is the path that zeros the key at a known time.

What stays as bytes: plaintext, ciphertext, signatures, public keys, salts, nonces, password-hash strings, and any value export returns.

What this does not fix

  • Plaintext and passwords that start in JavaScript.
  • Secrets already returned by the 1.x and 2.0 byte APIs. Those stay on the heap until the caller drops them and V8 reuses the memory.
  • A handle the caller immediately exports. That is the byte API again.

Release

Ship this after 2.0.0, as an additive API. Removing the byte-returning key generators would be a later major version, once callers have moved to handles.

Acceptance

  • Generating an AES, X25519, RSA, Ed25519, or post-quantum key returns a handle, and the raw secret is not readable from JavaScript.
  • seal / open and sign / verify accept the handle and return ciphertext, plaintext, or a signature as bytes.
  • Dropping or releasing the handle zeros the Rust copy.
  • export is explicit, and its documentation states that the returned bytes are not zeroized.
  • The byte API still works, and the migration note says which calls should move to handles.

Activity

  1. added
    security
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions