Blog / A04:2021 Insecure Design
A04:2021 · Insecure Design

Your key generator hands back the caller's keys

A key pair function that spreads caller options over its own output lets anyone pin the private key to a value they already know.

caller passes anoptions objectgenerator setspublic…options spreadoverwrites…app trusts apredictable keyATTACKER INPUTIMPACTtrust boundary crossed
Attack path · caller passes an options object → generator sets public and private keys → options spread overwrites both fields → app trusts a predictable key

A key pair function that spreads caller options over its own output lets anyone pin the private key to a value they already know.

What goes wrong

You asked for a fresh key pair. Because the function spreads the caller's options object after the fields it just generated, whatever the caller passed for the public or private key wins. The bytes it derived get computed and then quietly thrown away.

Most callers pass a harmless options object and never notice. The one who passes a private key they picked themselves gets a key pair the application treats as freshly generated and unguessable. From there they can forge signatures, read anything sealed to that key, or register as a party you now consider trusted.

The bug is not in the crypto. It is in the order of two lines inside an object literal.

The pattern

Vulnerable
function generateKeyPair(options) {
  const pair = deriveEd25519()
  return {
    publicKey: pair.public,
    privateKey: pair.private,
    ...options            // caller fields land last and win
  }
}
Hardened
function generateKeyPair(options) {
  const pair = deriveEd25519()
  return {
    ...options,           // caller metadata first
    publicKey: pair.public,
    privateKey: pair.private   // generated values always win
  }
}
How the pipeline adjudicated itHELD
ABCDEFGH

Grounding and reachability were confirmed. The exploit-path gate would not conclude without the calling code, so the finding was held and an evidence request was raised instead of being reported as fact.

Ed25519 · per-tenant signing key · gate ledger A–H · verify offline against the published payload hash

Why it matters to you

Wallets, signing services, and identity systems all assume that a generate call returns something the caller cannot predict. Break that assumption and every trust decision built on the key is worthless. An attacker does not need to break Ed25519 when they can supply the private half and have your own code bless it as random.

Spread and merge helpers make this easy to get wrong in any language. Anywhere you build a result by folding untrusted input into an object you are returning, check which side of the merge wins.

Check your own code