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
function generateKeyPair(options) {
const pair = deriveEd25519()
return {
publicKey: pair.public,
privateKey: pair.private,
...options // caller fields land last and win
}
}function generateKeyPair(options) {
const pair = deriveEd25519()
return {
...options, // caller metadata first
publicKey: pair.public,
privateKey: pair.private // generated values always win
}
}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.
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
- Find every object literal or merge that places caller input after values you generate.
- Confirm generated secrets are assigned last and cannot be overridden.
- Search for a request or options object being spread into a returned credential.
- Add a test that passes a chosen private key and asserts it is ignored.