Skip to content

Commit c42ffa8

Browse files
authored
docs: warn against untrusted paths in UploadedFile move()/store() (#10446)
1 parent 4e13c9e commit c42ffa8

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

system/HTTP/Files/UploadedFile.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public function __construct(string $path, string $originalName, ?string $mimeTyp
122122
* @see http://php.net/is_uploaded_file
123123
* @see http://php.net/move_uploaded_file
124124
*
125-
* @param string $targetPath Path to which to move the uploaded file.
125+
* @param string $targetPath Path to which to move the uploaded file. This is NOT sanitized. Never build it from
126+
* untrusted input: a value containing "../" can move the file outside the intended
127+
* directory. Use a path controlled by your application.
126128
* @param string|null $name The name to rename the file to. When null, the client-provided name is used and sanitized.
127129
* A caller-supplied name is NOT sanitized.
128130
* @param bool $overwrite State for indicating whether to overwrite the previously generated file with the same
@@ -347,8 +349,10 @@ public function isValid(): bool
347349
* By default, upload files are saved in writable/uploads directory. The YYYYMMDD folder
348350
* and random file name will be created.
349351
*
350-
* @param string|null $folderName the folder name to writable/uploads directory.
351-
* @param string|null $fileName the name to rename the file to.
352+
* @param string|null $folderName the folder name to writable/uploads directory. This is NOT sanitized. Never build
353+
* it from untrusted input: a value containing "../" can move the file outside the
354+
* uploads directory.
355+
* @param string|null $fileName the name to rename the file to. A caller-supplied name is NOT sanitized.
352356
*
353357
* @return string file full path
354358
*/

user_guide_src/source/libraries/uploaded_files.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ In PHP versions below 8.1, this returns ``null``
331331
Moving Files
332332
============
333333

334+
.. warning::
335+
Do not let user input directly decide the destination directory. The ``$targetPath`` argument of ``move()`` and the
336+
``$folderName`` argument of ``store()`` are never sanitized, so an attacker-controlled value containing ``../`` can
337+
move the uploaded file outside the intended directory. Use directories controlled by your application.
338+
339+
Filenames are only sanitized by default: ``move()`` sanitizes the client-provided name when the ``$name`` argument is
340+
omitted, and ``store()`` generates a random name when ``$fileName`` is omitted. A caller-supplied ``$name`` or
341+
``$fileName`` is NOT sanitized, so if you pass one, generate it yourself or sanitize it with
342+
:php:func:`sanitize_filename`.
343+
334344
with Original Filename
335345
----------------------
336346

0 commit comments

Comments
 (0)