Skip to content

FIX: Can't validate uploaded file for tmp folder mismatching #27352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

baranada
Copy link

@baranada baranada commented Mar 19, 2020

Description (*)

when upload file, php $_FILE variable returns destination path of symlinked tmp folder.
but sys_get_temp_dir() function only return target path of symlinked folder.
this mismatch made it can't pass the validation. this commit validate on both target path and destination path if symlink found.
I couldn't find php $_FILE source reference, so I put both path on validation.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Invalid parameter given. A valid $fileId[tmp_name] is expected #25835: Invalid parameter given. A valid $fileId[tmp_name] is expected

Manual testing scenarios (*)

  1. set default magento php environment sys_temp_dir ini value to any folder you made symlink to other folder.
  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"
  6. You can try another setting with type "file"
  7. check if validation passes or below message will pop.
    "Invalid parameter given. A valid $fileId[tmp_name] is expected"

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

…r tmp folder mismatching magento#25835

when upload file, php returns destination path of symlinked tmp folder.
but sys_get_temp_dir() function only return target path of symlinked folder.
this mismatch made it can't pass the validation. this commit validate on both target path and destination path if symlink found.
I couldn't find php $_FILE source reference, so I put both pathes on validation.
@m2-assistant
Copy link

m2-assistant bot commented Mar 19, 2020

Hi @baranada. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

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

@lenaorobei
Copy link
Contributor

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

@lenaorobei lenaorobei closed this Mar 30, 2020
@m2-assistant
Copy link

m2-assistant bot commented Mar 30, 2020

Hi @baranada, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@hostep
Copy link
Contributor

hostep commented Apr 2, 2020

@baranada: changes look good at first sight, but don't work in all cases.

For example, on macOS, when executing is_link('/var/tmp/') it will not work properly, because on macOS, the symlink is on the /var/ directory and not on /var/tmp/.

Here are suggested changes to make it work in all cases I think:

$sysTmpFolder = sys_get_temp_dir();
$sysTmpFolderRealPath = realpath($sysTmpFolder);

$allowedFolders = [
    $sysTmpFolder,
    $sysTmpFolderRealPath,
    $this->directoryList->getPath(DirectoryList::MEDIA),
    $this->directoryList->getPath(DirectoryList::VAR_DIR),
    $this->directoryList->getPath(DirectoryList::TMP),
    $this->directoryList->getPath(DirectoryList::UPLOAD),
];

I'm reopening this PR based on the fact that I believe there is an actual improvement we can make here.

@baranada
Copy link
Author

baranada commented Apr 4, 2020

@baranada: changes look good at first sight, but don't work in all cases.

For example, on macOS, when executing is_link('/var/tmp/') it will not work properly, because on macOS, the symlink is on the /var/ directory and not on /var/tmp/.

Here are suggested changes to make it work in all cases I think:

$sysTmpFolder = sys_get_temp_dir();
$sysTmpFolderRealPath = realpath($sysTmpFolder);

$allowedFolders = [
    $sysTmpFolder,
    $sysTmpFolderRealPath,
    $this->directoryList->getPath(DirectoryList::MEDIA),
    $this->directoryList->getPath(DirectoryList::VAR_DIR),
    $this->directoryList->getPath(DirectoryList::TMP),
    $this->directoryList->getPath(DirectoryList::UPLOAD),
];

I'm reopening this PR based on the fact that I believe there is an actual improvement we can make here.

@hostep, thank you for your reply.
I found that if you use realpath function, we don't have to put both paths in the validation folder.
we just pass sys_get_temp_dir results to realpath, then it will pass the validation, symlink or not.
I will apply this changes and make unit test.

@hostep
Copy link
Contributor

hostep commented Apr 10, 2020

@baranada: to be safe I would check on both paths, I'm not familiar enough with how the path in the $_FILES global are being set in all kinds of different environments.

I think we are probably most safe when checking for both the symlinked and the realpath directories.

@hostep
Copy link
Contributor

hostep commented Apr 24, 2020

Hi @baranada: are you still able/willing to put some extra time into this? Thanks!

@baranada
Copy link
Author

baranada commented Apr 24, 2020

I couldn't make test code for this.
I pushed the code with your suggestion. it was wrote back on 21 days ago. so github timeline is tangled.
if it's necessary to write test code, then I hope someone to help on this.
Thank you for following up.

@hostep
Copy link
Contributor

hostep commented May 12, 2020

@lenaorobei: can you or somebody else please review this, that would be appreciated, thanks! 🙂

@sidolov sidolov added Priority: P3 May be fixed according to the position in the backlog. Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. labels Aug 17, 2020
@lenaorobei lenaorobei self-assigned this Sep 4, 2020
@lenaorobei
Copy link
Contributor

@magento run all tests

Copy link
Contributor

@lenaorobei lenaorobei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good. @baranada, could you please write simple integration test to cover this case?

@baranada
Copy link
Author

The fix looks good. @baranada, could you please write simple integration test to cover this case?

Since I am not familiar with magento2 test code, I will try and come back to you next week.

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this PR to corresponding status as requested in #27352 (review)

@ghost ghost assigned ihor-sviziev Sep 21, 2020
@ihor-sviziev ihor-sviziev removed their assignment Sep 21, 2020
@engcom-Golf
Copy link
Contributor

@magento run all tests

@engcom-Golf
Copy link
Contributor

Hi, @baranada!
Please review test failures and fix them for the further processing of PR.
Thank you!

@engcom-Golf engcom-Golf self-assigned this Sep 30, 2020
@baranada baranada closed this Sep 30, 2020
@m2-assistant
Copy link

m2-assistant bot commented Sep 30, 2020

Hi @baranada, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@baranada baranada reopened this Sep 30, 2020
@m2-assistant
Copy link

m2-assistant bot commented Sep 30, 2020

Hi @baranada. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

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

@baranada
Copy link
Author

baranada commented Nov 6, 2020

@magento run all tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: File Priority: P3 May be fixed according to the position in the backlog. Progress: pending review Release Line: 2.4 Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid parameter given. A valid $fileId[tmp_name] is expected
7 participants