Repository navigation
fix(http): neutralize client-controlled upload tmp_name in Swoole adapter - #14897
Conversation
…pter Swoole's multipart parser honors an internal `Swoole-Upload-File` part header and pins part metadata to a client-chosen `tmp_name` with `error: 0` and the real size of the target file (swoole/swoole-src#6309). Because `is_uploaded_file()` trusts Swoole metadata, a remote unauthenticated request can make upload-handling code read an arbitrary server-local file (e.g. /etc/passwd) as if it were a standard multipart upload. No released Swoole version contains the upstream fix yet (`upload_preprocessed` lands with 6.3/master). The Swoole adapter is the single choke point: genuine uploads are always written to `<upload_tmp_dir>/swoole.upfile.XXXXXX`, so getFiles() now rejects any `tmp_name` outside those directories/that naming scheme, neutralizing spoofed entries into UPLOAD_ERR_CANT_WRITE with no size, while real uploads (flat or bracketed list form) pass through untouched. Parser-level regression tests cover the spoofed marker through SwooleRequest::parse(), genuine uploads, mixed lists, wrong-dir and nonexistent paths.
🟢 Tier S · Ready to merge
The PR hardens the Swoole HTTP adapter against client-supplied Latest changes: The newest commit adds a note to
📂 Walkthrough · 4
Reviewed the commits since |
Sanitizing only the getFiles() copy left the spoofed path in Swoole's tmpfiles list, which Swoole unlinks when the request is freed, so the marker still deleted arbitrary files. Scrub tmpfiles and files once in the constructor, walk nested bracketed entries, keep the errors of ordinary failed uploads, and require the exact upload directory.
The v4 tag moved to v4.7.0, so zizmor flagged the pinned v4.6.0 hash as carrying a mismatched version comment.
A marker naming another request's in-flight swoole.upfile passed the name and directory checks. The raw body carries every marker, so match those values exactly and drop the path heuristics and their tests.
The findings that requested changes are resolved. See the summary comment for what is still open.
It only covers Swoole builds without the swoole/swoole-src#6309 fix.
…file-marker fix(http): neutralize client-controlled upload tmp_name in Swoole adapter (cherry picked from commit 1d15ef8)
…lues (backport appwrite#14897, appwrite#14792) Swoole accepts a Swoole-Upload-File part header as a ready temp file, so a client could name any server path as its upload and have it unlinked. The framework also calls any callable argument, so a request value such as ["Class", "method"] ran as code. The 1.9 dependency set caps utopia-php/http below the releases that fix both, so the request wrapper applies the guards.
What does this PR do?
Neutralizes client-controlled Swoole upload paths in the HTTP adapter. This closes an arbitrary file read and an arbitrary file deletion against Swoole-based consumers such as Appwrite.
The bug. Swoole's multipart parser trusts an internal part header,
Swoole-Upload-File: <path>(swoole/swoole-src#6309, originally #6161/#6162). Any remote client can send it, and Swoole then:tmp_nameis the client-chosen path (error: 0, real size of the target);is_uploaded_file(), soUtopia\Storage\Validator\Uploadaccepts it and the file's contents are stored and served back to the attacker (e.g./etc/passwd);$request->tmpfiles, which Swooleunlink()s when the request object is freed. In the API container (uid 0), one unauthenticated multipart request to any route deletes the target file.Upstream status. Fixed on Swoole master (#6309, commit
0306f5360, requires the newupload_preprocessedcontext), but not in any released version: 6.2.x (we run 6.2.3) is unguarded.The fix. The adapter constructor reads every
Swoole-Upload-Fileheader value from the raw multipart body (Swoole matches the name case-insensitively at the start of a header line, and so does the scan). Any entry in Swoole'stmpfileslist equal to one of those values is:tmpfiles, so Swoole no longer unlinks it;files(tmp_name: '',error: UPLOAD_ERR_CANT_WRITE,size: 0), walking nestedname[]/name[0][part]entries with their matchingerror/sizetrees.Matching is exact, so a marker that names another request's in-flight
swoole.upfile.*is caught too. Genuine uploads, ordinary failed uploads (e.g.UPLOAD_ERR_NO_FILE), and files whose content merely contains the marker text are left untouched. The spoofed path stays in the worker'srfc1867_uploaded_filestable (it can't be cleared from PHP), but no consumer receives it anymore.Also fixes the
Checks / Workflowslane (failing onmaintoo): thedocker/login-actionv4tag moved to v4.7.0, so zizmor flagged the pinned v4.6.0 hash's# v4comment.Test plan
Regression tests run the real Swoole parser (
SwooleRequest::parse()) against a fixture file, never system files:tmpfiles, and the target survives the request being freed;file[0][part]is dropped while the genuine sibling stays an upload and an empty file part keepsUPLOAD_ERR_NO_FILE;The first two fail with the constructor hook disabled. The package unit suite (161 tests) is green under Swoole 6.2.3, and
bin/monorepo check http(Pint, PHPStan max, Rector) passes. A realSwoole\Http\Serverprobe confirmed markers naming/tmp/victimand an existing/tmp/swoole.upfile.abc123are blanked and both files survive, while normal uploads still work and are cleaned up.Example request reproducing the bug
The response carries the server's
/etc/passwdas the uploaded file's content, and the file is then deleted from the container.Related: appwrite-labs/cloud#6674