--etag-save - truncates append-redirected stdout
curl's ETag-saving code truncates the output file whenever the underlying file descriptor is a regular file, without checking whether the stream is a curl-owned file or stdout redirected by the shell. A malicious server returning an ETag header can therefore wipe pre-existing content in a file the user opened for append.
Originally disclosed on HackerOne
- Disclosed after 2 days
Summary
save_etag() (line 267–273) calls fileno(), fstat(), ftruncate(fd, 0) whenever the underlying fd is a regular file. It does not check OutStruct.regular_file, which is FALSE for stdout even when shell-redirected to a file. When --etag-save - is used with append redirection (>>), an HTTP 2xx/3xx response containing an ETag header causes ftruncate to destroy all pre-existing content in the append target before writing the ETag value. The peer controls the trigger via the response status code and ETag header.
Reproduction
python3 poc_etag_truncate.py <sha>
- Write
PRESERVE\nto a log file - Run
curl --etag-save - http://server/ >> logfilewhere server returnsETag: "remote" - Log file now contains only
"remote"\n—PRESERVEis gone
Control: Same setup but server returns no ETag header → PRESERVE retained.
Root cause
// src/tool_cb_hdr.c:267-273
int fd = fileno(etag_save->stream);
if((fd != -1) &&
!curlx_fstat(fd, &file) &&
(S_ISREG(file.st_mode) && // ← only checks physical file type
toolx_ftruncate(fd, 0))) // ← truncates regardless of ownership
S_ISREG alone cannot distinguish a curl-owned ETag output file from stdout redirected to a regular file.
Remediation
Guard ftruncate with etag_save->regular_file (the semantic stream flag) in addition to S_ISREG.
Impact
A remote server can destroy arbitrary pre-existing content in any regular file the user's shell has opened for append on stdout. The attacker controls the trigger (any 2xx/3xx response with an ETag header) and the replacement content (the ETag value). The user must have chosen --etag-save - and append redirection, but the target file, its prior contents, and the decision to append are entirely the user's — curl silently violates the append contract. In scripting contexts where multiple curl invocations append ETags to a shared log, a single malicious server response wipes the entire log history.v
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
The code checks only whether the file descriptor points to a regular file (S_ISREG) and truncates it, without also checking the semantic flag that marks the stream as a curl-owned ETag output file rather than redirected stdout.
Impact
A remote server can destroy arbitrary pre-existing content in any regular file the user's shell has opened for append on stdout, replacing it with the ETag value, though the user must have chosen --etag-save - and append redirection.
What the attacker needed
User runs curl with --etag-save -; stdout redirected with >> to a regular file; server returns 2xx/3xx with an ETag header
The fix
The truncation should be guarded by the semantic stream flag (etag_save->regular_file) in addition to the S_ISREG check.
How it was found
- Write some content to a log file.
- Run curl with --etag-save - against a server that returns an ETag header, appending stdout to the log file.
- Observe that the log file now contains only the ETag value and the original content is gone.
- As a control, repeat with a server that returns no ETag header and see the original content retained.
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.
When you run a program and send its output to a file with '>>', the shell opens the file in append mode so new text is added at the end. curl has a feature that saves an ETag (a small identifier a server sends with a response) to a file, and when told to save it to standard output it mistakenly treats that redirected file as its own and empties it first. A malicious server can trigger this by simply including an ETag header, erasing whatever was already in the file.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.