Skip to content
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

[HttpFoundation] UploadedFile needs Locale parameter to pass #2577

Closed
helios-ag opened this issue Nov 8, 2011 · 15 comments
Closed

[HttpFoundation] UploadedFile needs Locale parameter to pass #2577

helios-ag opened this issue Nov 8, 2011 · 15 comments
Labels
Bug Good first issue Ideal for your first contribution! (some Symfony experience may be required) HttpFoundation

Comments

@helios-ag
Copy link
Contributor

Because function basename() is locale aware, the constructor need to pass a locale parameter and set it in constructor. Without setting a locale non-latin first characters cut off from beginning until a latin letter or a non-letter character found in the name.

from HttpFoundation\File\UploadedFile.php

    $this->originalName = basename($originalName);
    $this->mimeType = $mimeType ?: 'application/octet-stream';
    $this->size = $size;
    $this->error = $error ?: UPLOAD_ERR_OK;
    $this->test = (Boolean) $test;

    parent::__construct($path, UPLOAD_ERR_OK === $this->error);
}

Should be like this (an example):
setlocale(LC_ALL, 'ru_RU.UTF8');
$this->originalName = basename($originalName);
$this->mimeType = $mimeType ?: 'application/octet-stream';
$this->size = $size;
$this->error = $error ?: UPLOAD_ERR_OK;
$this->test = (Boolean) $test;

    parent::__construct($path, UPLOAD_ERR_OK === $this->error);
}
@stealth35
Copy link
Contributor

Maybe just :

<?php
$this->originalName = substr(strrchr(str_replace('\\', '/', $originalName), '/'), 1);

BTW why use basename ?

@stof
Copy link
Member

stof commented Apr 4, 2012

@fabpot ping

3 similar comments
@helios-ag
Copy link
Contributor Author

@fabpot ping

@dinamic
Copy link

dinamic commented Apr 19, 2012

@fabpot ping

@helios-ag
Copy link
Contributor Author

@fabpot ping

@dinamic
Copy link

dinamic commented May 2, 2012

Imho, this should not modify the value that comes from the browser - one might need it as raw value.

This check should matter only if you are trying to directly save the uploaded file without specifying a filename to be used.

@dinamic
Copy link

dinamic commented May 8, 2012

@fabpot ping

@vicb
Copy link
Contributor

vicb commented May 8, 2012

@dinamic this value could be forged so sanitization is required.

Could anyone submit a PR with the fix suggested by @stealth35 + unit tests.

Thanks.

@dinamic
Copy link

dinamic commented May 9, 2012

@vicb I meant to say that the sanitization which is performed is going to be needed only if you rely on the $originalName to return you a value, which would be safe to use on the current system. Doing so however should fall beyond the scope of this class.

Let me explain more promptly my point of view.

I will give you an example case:

  • I want to upload a file named "aux"
  • The server is powered by Windows NT

While this is perfectly valid case and you will have no troubles running it under *NIX, you will fall into trouble if your hosting service is running on Windows-based OS, as the "AUX" is a reserved filename there.

More information: http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words

Imho, the value of originalName should not be filtered and it has to be up for the developer to choose a filename for the uploaded file. If some filtration has to be applied, it should be minimal and the developer should have access to the unmodified value.

Note: if we accept that the value would not be filtered, it has to be reflected into the docs and stress how important is to not rely on the safety of the value.

@vicb
Copy link
Contributor

vicb commented May 9, 2012

The purpose of the sanitization is not the make the name bullet proof but to prevent security issues (there is no way to prevent a developer from using it even if he must not) - the $originalName can still contain reserved characters or be a reserved name.

getClientOriginalName() already has a warning in its phpDoc: Then is should not be considered as a safe value.

What would be the advantage of having access to the unfiltered name ?

@dinamic
Copy link

dinamic commented May 10, 2012

Well, it would prevent us from getting one value from the client of our website and display a different one.

We could filter it, off course, but it has to be up for the developer to decide it and the framework should not force filtration. Otherwise, some developers might just go backward and use the $_FILES array to get the job done.

@vicb
Copy link
Contributor

vicb commented May 10, 2012

The sanitization removes the path segment and leaves only the basename. I have just re-tested (w/ chrome & ff) and only the basename is sent by the browser, meaning that the sanitization is transparent and will only have an effect on forged values.

There must be something I don't get ?

@dinamic
Copy link

dinamic commented May 18, 2012

Dunno, I just hate it when there's a restriction which might cause a bug in your application and you can't get the raw value and process it by yourself. Right now, we have multiple opened issues because of this and I cannot implement a workaround without actually modifying the sf2 code or completely handle the upload process, validation and entity creation by myself.

I just want a getter method which would return the unaltered value, so if the new sanitization blocks me or anyone else - he could handle it gracefully until fixed.

@vicb
Copy link
Contributor

vicb commented May 18, 2012

Dunno, I just hate it when there's a restriction which might cause a bug in your application and you can't get the raw value and process it by yourself. Right now, we have multiple opened issues because of this and I cannot implement a workaround without actually modifying the sf2 code or completely handle the upload process, validation and entity creation by myself.

As stated in my last comment, you'll get the raw value unless someone forges a request, how could that cause issues ?

@dinamic
Copy link

dinamic commented May 18, 2012

Well, I believe the same intention applied while setting basename() in first place :)

@vicb vicb closed this as completed in 8223632 May 21, 2012
fabpot added a commit that referenced this issue May 21, 2012
Commits
-------

8223632 [HttpFoundation] Fix the UploadedFilename name sanitization (fix #2577)

Discussion
----------

[HttpFoundation] Fix the UploadedFilename name sanitization (fix #2577)

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/vicb/symfony.png?branch=uploadedfile2.0)](http://travis-ci.org/vicb/symfony)
Fixes the following tickets: #2577

---------------------------------------------------------------------------

by travisbot at 2012-05-21T14:00:22Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1389203) (merged 8223632 into 87bb366).
homer6 pushed a commit to homer6/symfony that referenced this issue May 21, 2012
* 2.0:
  fixed CS
  [HttpFoundation] Fix the UploadedFilename name sanitization (fix symfony#2577)
fabpot added a commit that referenced this issue Oct 27, 2012
This PR was squashed before being merged into the 2.0 branch (closes #5496).

Commits
-------

9872d26 [HttpFoundation] Fix name sanitization after perfoming move

Discussion
----------

[HttpFoundation] Fix name sanitization after perfoming move

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #2577
License of the code: MIT

Further work on #2577, fixes name sanitization, after moving file name with new name with non latin characters in the beginning.

---------------------------------------------------------------------------

by stloyd at 2012-09-12T09:52:05Z

You must revert chmod changes.

---------------------------------------------------------------------------

by helios-ag at 2012-09-12T14:30:36Z

@stloyd fixed

---------------------------------------------------------------------------

by stof at 2012-10-13T21:12:43Z

@fabpot what is the status of this PR ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Good first issue Ideal for your first contribution! (some Symfony experience may be required) HttpFoundation
Projects
None yet
Development

No branches or pull requests

5 participants