The vulnerability is a prototype pollution issue within the js-yaml library, affecting versions up to 4.1.0. The root cause lies in the unsafe handling of keys named __proto__ during the parsing of YAML documents. When a YAML file containing such a key is parsed, the library would directly assign properties to a JavaScript object, allowing an attacker to modify the prototype of all objects in the application.
The patch addresses this by introducing a new setProperty function in lib/loader.js. This function specifically checks for the __proto__ key and, if found, uses Object.defineProperty to set the value. This method of property assignment prevents prototype pollution. For all other keys, it uses a standard, faster assignment.
Two functions were identified as vulnerable because they were modified to use this new setProperty function:
-
storeMappingPair: This function is responsible for creating key-value pairs from the YAML input. The original code performed a direct assignment, _result[keyNode] = valueNode, which was the source of the vulnerability for standard mappings.
-
mergeMappings: This function implements the YAML merge (<<) feature. It was also vulnerable due to a direct assignment, destination[key] = source[key], when merging mappings.
By patching these two functions, the library now safely handles __proto__ keys, preventing the prototype pollution vulnerability. During an exploit, these two functions would be present in the call stack as they process the malicious YAML input.