Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php is altering original filename

Tags:

php

escaping

I have a file upload, and try to take care of strange filenames by removing unwanted characters. But I have the situation where I just played around with unwanted (chars in) filenames and got the following result:

My Bad Filename is: 1\;ping\ -c1\ 1.1.1.1
Firefox is sending:
Content-Disposition: form-data; name="send_file"; filename="1\;ping\ -c1\ 1.1.1.1"

But PHP's $_FILES['send_file']['name'] object is just giving me 1.1.1.1 (with a leading space) as name.

In general that would be ignorable but I'd like to understand what is happening here

What is happening here? Please enlighten me

Software used:

  • Ubuntu 18.04 LTS
  • Apache 2.4.29
  • PHP-FPM 7.2
  • Firefox 70
like image 910
Daywalker Avatar asked Oct 31 '19 08:10

Daywalker


2 Answers

PHP's $_FILES['send_file']['name'] does not equal Content-Disposition's filename="..." value.

PHP is doing some sanitizing: removes path and stores only name of the file, like it said in documentation:

$_FILES['userfile']['name']

The original name of the file on the client machine.

Removing path means that everything before last slash/backslash (including them) is removed, that's what you got.

like image 187
Styx Avatar answered Nov 15 '22 00:11

Styx


It relate to very old issue since 2005, and basically the character \ is not valid character for the file name in Window

You can found more detail here, and old bug with status Won't fix

https://pear.php.net/bugs/bug.php?id=5681

like image 33
Vo Kim Nguyen Avatar answered Nov 15 '22 00:11

Vo Kim Nguyen