The SSRF vulnerability in axios (CVE-2024-39338) stemmed primarily from how URLs were parsed and resolved in server-side environments, specifically within the httpAdapter function. This function, in vulnerable versions, used new URL(fullPath, 'http://localhost'). This behavior, introduced in version 1.3.2, meant that if fullPath could be controlled to be a protocol-relative URL (e.g., //attacker.com), it would resolve to http://attacker.com, bypassing the intended baseURL and leading to SSRF.
The isAbsoluteURL helper function also played a crucial role. Its pre-fix behavior incorrectly identified protocol-relative URLs as non-absolute. This caused the buildFullPath function (which uses isAbsoluteURL) to combine the baseURL with the protocol-relative URL, forming a string like https://legit.com//attacker.com. Even this combined string, when passed to the vulnerable httpAdapter's new URL(..., 'http://localhost') call, would still resolve to https://attacker.com due to how the URL constructor handles paths starting with // after a host.
The patch commit 6b6b605eaf73852fb2dae033f1e786155959de3a addressed both issues:
- It modified
httpAdapter to pass undefined as the base to new URL() in server-side environments, causing protocol-relative URLs to correctly throw an 'Invalid URL' error.
- It corrected
isAbsoluteURL to properly recognize protocol-relative URLs as absolute, ensuring buildFullPath handles them correctly by not prepending a baseURL if the input URL is already protocol-relative.
Both functions were involved in the vulnerable logic: httpAdapter contained the direct SSRF mechanism via the new URL call with a fixed 'http://localhost' base, and the pre-patch isAbsoluteURL facilitated the construction of a fullPath that could trigger this mechanism.