Path Traversal Bypass in file:// URLs Due to Incomplete URL-Encoded Path Normalization
The dedotdotify() function in curl's URL parser only recognises literal forward slashes when removing path traversal sequences, so URL-encoded slashes and dots pass through unchanged. After the path is decoded in file.c, the resulting ../ sequences are used directly to open a file, allowing an attacker to read arbitrary files via a crafted file:// URL.
- Disclosed after 1 hour
Summary:
The dedotdotify() function in lib/urlapi.c is responsible for removing path traversal sequences (../ and ./) from URLs according to RFC 3986. However, the function only recognizes literal forward slashes (/) when identifying path segments and does not handle URL-encoded slashes (%2f or %2F). This allows an attacker to bypass path traversal protection in file:// URLs by using URL-encoded path traversal sequences.
Vulnerability Flow:
- Attacker provides a file:// URL with URL-encoded path traversal:
file:///%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd - The path
%2f%2e%2e%2f%2e%2e%2fetc%2fpasswdis processed bydedotdotify()while still URL-encoded dedotdotify()usesISSLASH(x)macro defined as((x) == '/')which only checks for literal/, not%2f- The URL-encoded traversal sequence
%2f%2e%2e%2fpasses throughdedotdotify()unchanged - In
file.c:192-193,Curl_urldecode()decodes the path, converting%2f%2e%2e%2fto/../ - The decoded path
/../../etc/passwdis used directly incurlx_open()at line 263 with no additional validation - Path traversal succeeds, allowing access to arbitrary files
Proof of No Mitigation:
-
dedotdotify()limitation: The function atlib/urlapi.c:836checksif(ISSLASH(*input))whereISSLASHis defined at line 777 as((x) == '/'). This only matches literal forward slashes, not URL-encoded%2fsequences. -
No post-decoding validation: After URL decoding in
file.c:192-193, the decoded path is used directly incurlx_open()at line 263. There are no checks for../sequences after decoding (verified by grep search showing nostrstr,strchr, or validation functions checking for../infile.c). -
Test case evidence: The unit test
tests/unit/unit1395.cline 44 demonstrates this behavior:C { "%2f%2e%2e%2f", "%2f%2e%2e%2f" }This confirms that
%2f%2e%2e%2f(encoded/../) is NOT normalized and passes through unchanged. -
Code flow verification:
lib/urlapi.c:1233:dedotdotify(path, pathlen, &dedot)is called on URL-encoded pathlib/urlapi.c:836: Only checks for literal/viaISSLASH(*input)lib/file.c:192:Curl_urldecode()decodes the pathlib/file.c:263: Decoded path used incurlx_open()with no validation
AI Disclosure: This vulnerability was identified through manual code review and analysis. AI assistance was used to search and analyze the codebase, but the vulnerability discovery and analysis were performed through systematic code examination.
Affected version
This vulnerability affects the current curl/libcurl codebase. To determine the exact version:
curl -V
The vulnerability exists in the source code at:
lib/urlapi.c(lines 777, 836, 1230-1242)lib/file.c(lines 192-193, 263)
Platform: All platforms that support file:// URLs (Unix-like systems, Windows, etc.)
Steps To Reproduce:
-
Create a test file to read:
Bash echo "sensitive data" > /tmp/test_file.txt -
Attempt to access the file using URL-encoded path traversal:
Bash curl "file:///%2f%2e%2e%2f%2e%2e%2f%2e%2e%2ftmp%2ftest_file.txt" -
Verify the attack succeeds by observing that curl reads the file despite the path traversal sequences:
Bash # Expected: Should be blocked or normalized # Actual: File is successfully read, proving path traversal bypass curl -v "file:///%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd" 2>&1 | head -20
Alternative reproduction using libcurl API:
-
Compile and run the following C program:
C #include <curl/curl.h> #include <stdio.h> int main(void) { CURL *curl = curl_easy_init(); if(curl) { CURLcode res; curl_easy_setopt(curl, CURLOPT_URL, "file:///%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd"); res = curl_easy_perform(curl); if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } return 0; } -
Compile:
gcc -o test_curl test_curl.c -lcurl -
Run:
./test_curl- The program will successfully read/etc/passwd(or equivalent sensitive file) despite the path traversal sequences.
Supporting Material/References:
Code Evidence
Vulnerable Code Location 1 - ISSLASH definition:
// lib/urlapi.c:777
#define ISSLASH(x) ((x) == '/') // Only checks literal '/', not '%2f'
Vulnerable Code Location 2 - dedotdotify() function:
// lib/urlapi.c:836
while(clen && !result) {
if(ISSLASH(*input)) { // <-- Only matches literal '/', bypassed by '%2f'
// ... path normalization logic
}
// URL-encoded sequences like %2f%2e%2e%2f pass through unchanged
}
Vulnerable Code Location 3 - No post-decoding validation:
// lib/file.c:192-193
result = Curl_urldecode(data->state.up.path, 0, &real_path,
&real_path_len, REJECT_ZERO);
// ... no validation here ...
// lib/file.c:263
fd = curlx_open(real_path, O_RDONLY); // <-- Decoded path used directly
Test Case Evidence
The unit test file tests/unit/unit1395.c demonstrates that %2f%2e%2e%2f is not normalized:
// Line 44
{ "%2f%2e%2e%2f", "%2f%2e%2e%2f" }, // Input == Output (not normalized)
This test case proves that URL-encoded path traversal sequences bypass the normalization function.
Proof of No Mitigation
Search Results Confirming No Additional Validation:
-
No path validation in file.c after decoding:
Bash $ grep -n "strstr\|strchr\|check.*\.\.\|validate.*path\|sanitize.*path" lib/file.c # No matches found -
dedotdotify() only processes literal slashes:
ISSLASH(x)macro definition:((x) == '/')(line 777)- Function checks:
if(ISSLASH(*input))(line 836) - URL-encoded
%2fdoes not match this condition
-
Direct file access after decoding:
- Line 263:
fd = curlx_open(real_path, O_RDONLY); - No intermediate validation or sanitization
- Decoded path with
../sequences is used directly
- Line 263:
Impact Demonstration
Successful Path Traversal:
- Input URL:
file:///%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd - After dedotdotify (unchanged):
%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd - After URL decoding:
/../../etc/passwd - File opened:
/etc/passwd(or equivalent sensitive system file)
Affected Use Cases
This vulnerability affects any application or system that:
- Processes user-controlled URLs: Web applications that accept URLs from users and use curl/libcurl to fetch them
- File management systems: Applications that use file:// URLs for local file operations
- Backup/restore tools: Systems that use curl/libcurl with file:// protocol for file operations
- URL processing services: Services that proxy or process URLs, including file:// URLs
- Automated systems: Scripts and automation tools using curl/libcurl with file:// URLs
Real-World Attack Scenarios
Scenario 1: Web Application URL Processor
- A web application allows users to provide URLs for processing
- Attacker provides:
file:///%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd - Application uses libcurl to fetch the URL
- Attacker gains access to system password file
Scenario 2: File Management Application
- Application uses file:// URLs to access local files based on user input
- Attacker manipulates the URL to traverse outside intended directory
- Sensitive application configuration or user data is exposed
Scenario 3: Backup System
- Backup system uses curl with file:// URLs
- Attacker can read backup files, configuration, or other sensitive data
- May lead to further system compromise
Impact
Summary:
-
Arbitrary File Read: Attackers can bypass path normalization using URL-encoded sequences (
%2f%2e%2e%2f) to read any file accessible to the curl process, including/etc/passwd,/etc/shadow, configuration files, API keys, database credentials, and SSH private keys. -
Remote Exploitation via libcurl Integration: When libcurl is used by web applications or services that process user-controlled URLs, this local vulnerability escalates to remote arbitrary file read, enabling attackers to access sensitive files on the server.
-
Credential Theft and Privilege Escalation: Exposed credentials from configuration files, environment files, and key stores enable attackers to pivot to other systems, escalate privileges, or compromise external services.
-
Application Source Code Disclosure: Attackers can read application source code to identify additional vulnerabilities, proprietary algorithms, hardcoded secrets, and business logic flaws for further exploitation.
-
Inconsistent Security Behavior: The normalization bypass creates inconsistent behavior where literal
../is blocked but%2f%2e%2e%2fsucceeds, breaking security assumptions in applications that rely on curl's path normalization. -
Bypass of Input Validation: Applications that sanitize URLs before passing to curl may fail to detect URL-encoded traversal sequences, as validation typically checks for literal
../patterns but misses encoded equivalents.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the path normalisation step runs before URL decoding and only matches literal slashes, so encoded traversal sequences survive normalisation and are decoded afterwards without re-validation.
Impact
An attacker could read arbitrary files accessible to the curl process, and when libcurl is used by a service that processes user-supplied URLs, this could become a remote arbitrary file read.
What the attacker needed
Ability to supply a file:// URL to curl or libcurl
How it was found
- Create a test file to read.
- Request a file:// URL containing URL-encoded path traversal sequences.
- Observe that curl reads the target file despite the traversal sequences.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A path traversal is when a program is tricked into opening a file outside the folder it should be limited to, by using '..' to go up directories. Here, curl tries to clean up such sequences but only spots the plain '/' character, not its encoded form '%2f'. So an attacker can hide the '..' behind encoding, and after curl decodes it, the file is opened without further checks.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.