Magento2: Invalid parameter given. A valid $fileId[tmp_name] is expected

Created on 29 Nov 2019  ·  19Comments  ·  Source: magento/magento2

Preconditions (*)

  1. Magento 2.3.3

Steps to reproduce (*)

  1. Login into the admin panel
  2. Go to Stores - Settings - Configuration.
  3. Open tab Catalog - Catalog. Fieldset "Product Image Placeholders".
  4. Select the placeholder image for field "Base"
  5. Click to button "Save"
    You can try another setting with type "file"

Expected result (*)


Screen Shot 2019-11-29 at 10 14 52 AM
Error message: Invalid parameter given. A valid $fileId[tmp_name] is expected.

Actual result (*)

  1. File uploaded and the configuration saved without errors

Issue reason

The reason for this issue is additional validation - function validateFileId
In this function we have check is uploaded file located in "correnct" tmp dicartory.
sys_get_temp_dir() get folder /tmp
$_FILES["file"]["tmp_name"] located in folder /chroot/home/tmp/xxxxxxx
Nexcess support said: "This actually makes sense, because /tmp is a symbolic link to /chroot/home/tmp on our servers."

Possible fixes

  1. [Correct way] Configure upload_tmp_dir to an allowed folder:
  • "var/tmp" (or another based on sys_get_temp_dir() value)
  • "/pub/media"
  • "/var"
  • "/var/tmp"
  • "/pub/media/upload"

But it can be not so easy (impossible). For example, we have issue on Nexcess server. Server support responce:

sys_get_temp_dir: /tmp
$_FILES["file"]["tmp_name"]: /chroot/home/tmp/phpPEOxFn
This actually makes sense, because /tmp is a symbolic link to /chroot/home/tmp on our servers.

  1. [Hotfix] Add preference for class Magento/Framework/File/Uploader.php and remove validateFileId logic. You can not add a preference for this file.

  2. [Hotfix v2] Add custom patch. How to apply here. Patch here

Filesystem Clear Description Format is valid P3 PR Created S3 Dev.Experience

Most helpful comment

I can confirm that setting the ini setting sys_temp_dir to the actual temp dir for the system fixes the issue.
For me it is

sys_temp_dir = '/private/tmp'

On a Nexcess system, the value should be set to /chroot/home/tmp

All 19 comments

Hi @OleksiyOleksiyovych. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@OleksiyOleksiyovych do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

@magento give me 2.3-develop instance

Hi @omaxmo. Thank you for your request. I'm working on Magento 2.3-develop instance for you

Hi @omaxmo, here is your Magento instance.
Admin access: https://i-25835-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.

I can not reproduce it on vanilla Magento instance which Magneto provides to test, but it's reproducible on vanilla Magento on other servers (for example, Nexcess).
Can someone provide steps for server configuration?

Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @engcom-Charlie
Thank you for verifying the issue. Based on the provided information internal tickets MC-29272 were created

Issue Available: @engcom-Charlie, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

Hi @AndreyChorniy. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


Hi @magento-engcom-team , Any updates on this?

Seems like 2763ce536d05 added the file id validation method isValidFileId that is now causing this issue on some systems, where /private/tmp is used instead of /tmp. Maybe this can be fixed by setting the sys_temp_dir PHP ini setting I haven't tried.

I can confirm that setting the ini setting sys_temp_dir to the actual temp dir for the system fixes the issue.
For me it is

sys_temp_dir = '/private/tmp'

On a Nexcess system, the value should be set to /chroot/home/tmp

I can reproduce this issue on Magento 2.3.4 using \Magento\MediaStorage\Model\File\Uploader

Closing this issue since it can be resolved by configuring sys_temp_dir PHP setting.

@lenaorobei: I don't agree with you closing this issue.

It worked perfectly in Magento 2.3.2 without any changes to some PHP setting, and it got broken in Magento 2.3.3 and 2.3.4

If this was broken due to some security issue being fixed, then fine, but it should at least be mentioned on the release notes in a big warning.

Anyway, I still believe there is an actual bug here.
We should figure out why uploading a product image placeholder (on a macOS system for example), uses the path /private/var/tmp/, which doesn't match with what sys_get_temp_dir() returns (being: /var/tmp/).
On macOS, these are symlinked btw, so that might be a part of the explanation:

$ ls -ld /var
lrwxr-xr-x@ 1 root  wheel    11B Feb 23  2019 /var -> private/var

If we want to fix this, we would need to make the tmp location which the file upload uses for product image placeholders (and possibly other things as well), also uses sys_get_temp_dir(), that's probably a better way to go then just closing and ignoring this issue.

Will do some further digging while I'm at it and try to update new findings here.

Hmm, after researching a bit, it looks like when getting the tmp_name of the $_FILES global returns /private/var/tmp/ on macOS, while sys_get_temp_dir() returns /var/tmp/.

So it would be a bad idea to try to change the path in the $_FILES global.

The next best option would probably be to detect if sys_get_temp_dir() is a symlink and in that case, resolve it to its real path and use that to check as well in the allowed paths.
It looks like somebody already made a PR doing exactly this: https://github.com/magento/magento2/pull/27352 (even though it is not fully working in my case)

@lenaorobei: any objections to proceeding with accepting https://github.com/magento/magento2/pull/27352 if some modifications are added? Or do you consider that some sort of security issue?

Why does not include upload_tmp_dir in the allowed directories ?
It think it's required unless sys_get_temp_dir() == get_cfg_var('upload_tmp_dir')

This is (wrongly?) stored in SYS_TMP and it seems there no DirectoryList const key for it...

Maybe just add $this->directoryList->getPath(DirectoryList::SYS_TMP) is $allowedFolders

I agree with @xxorax. The upload directory may be different than the directory called by sys_get_temp_dir(). This is the case with my local install of MAMP and they can get set differently on other servers as well. The upload_tmp_dir should definitely be added to the core as an allowed temp directory.

I am using valet plus and I am unable to get past this error

When I set

sys_temp_dir = '/private/var/tmp'

Within Magento 2 inside the class src/vendor/magento/framework/File/Uploader.php
the value of sys_get_temp_dir() is /var/tmp

Inside of a simple php file, I get the right value as set inside my ini file /private/var/tmp
I even tried setting TMPDIR in my env

❯❯❯ env |grep -i tmpdir
TMPDIR=/private/var/tmp

For me this is a big issue, as I cannot upload files. I'm on a mac, and I'm not sure how to fix this. Any ideas?

@WinstonN Strange - I am using php 7.3 with valet+ currently, and when I edit /usr/local/etc/valet-php/7.3/php.ini and set sys_temp_dir = '/private/tmp' orsys_temp_dir = '/private/var/tmp' and then run

$ php -a
Interactive shell

php > echo sys_get_temp_dir() . PHP_EOL;
/private/tmp

It outputs the value I set in the .ini file. Sorry to ask the obvious, but this is what I would check in your shoes...

  • did I restart php-fpm after changing the file?
  • did I change the right .ini file?
  • is there another .ini file that is loaded after the one I changed which resets the value?
Was this page helpful?
0 / 5 - 0 ratings