Menu

1 / 2

The __proto__ Key in an API Response Is a Merge Bug Waiting to Happen

DEV Community: javascript·zhihu wu·about 12 hours ago
#UkE9u9l1
#devtojavascript#devto#dev#object#merge
Reading 0:00
15s threshold

Every JSON formatter shows it plainly: a key named __proto__ sitting in the middle of an otherwise normal API response. It looks harmless because nothing crashes. That is exactly the problem — the bug shows up two files later, in the code that copies the object.

I tested this in Node 22 with one payload:

{ "__proto__" : { "polluted" : "yes" } , "ok" : 1 }

Enter fullscreen mode

Exit fullscreen mode

JSON.parse() itself is safe. The key becomes an ordinary own property:

const obj = JSON . parse ( raw ) ; Object . keys ( obj ) ; // [ '__proto__', 'ok' ] obj . polluted ; // undefined obj . hasOwnProperty ( '__proto__' ) ; // true

Enter fullscreen mode

Exit fullscreen mode

So parsing is not the vulnerability. Copying is.

const merged = Object . assign ( { } , obj ) ; merged . polluted ; // 'yes' — inherited from the attacker's object ( { } ) .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.