Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[HttpFoundation] Generate safe fallback filename for wrongly encoded filename#23658
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
[HttpFoundation] Generate safe fallback filename for wrongly encoded filename#23658
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| } | ||
| if ('' ===$filenameFallback && (!preg_match('/^[\x20-\x7e]*$/',$filename) ||false !==strpos($filename,'%'))) { | ||
| $encoding =mb_detect_encoding($filename,null,true); |
nicolas-grekasJul 25, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
since this is a cleaning code, what about doing something like that instead?$encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This would indeed help with generating the filenameFallback no matter how weird the $filename is. But consideringResponseHeaderbag::makeDisposition the$filename parameter is described and expected to beA unicode string and placed in the header field parameterfilename* defined as utf-8 encoded without further processing. If passing e.g. an ISO-8859-1 encoded $filename with german umlauts and not throwing this exception would lead to a bad or invalid Content-Disposition response header. MaybesetContentDisposition should actually ensure the $filename to be utf-8 encoded in any case. But this could be a BC break, although I think everything but utf-8 seems to be a misusage. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think we have no need to be stricter now than we were already. If people put non-utf8 string, that's going to be sent and displayed in some way by browsers, so there is already a "reporting" effect.
throwing an exception would just break code that work "fine" now. So, still on the "8bit" side :)
| $encoding =mb_detect_encoding($filename,null,true); | ||
| if (false ===$encoding) { | ||
| thrownew \InvalidArgumentException('The filename encoding cannot be detected.'); |
javiereguiluzJul 26, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should we use this error message instead?
thrownew \InvalidArgumentException(sprintf('The "%s" filename encoding cannot be detected.',$filename));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
When$filename is not a "clean" utf-8 string, but have a broken encoding, this could break the output wherever the exception message will be used. Actually we had an exception from the mongodb related code not showing up in the first place, cause its message included the broken non utf-8 encoded filename which in turn caused an Exception in the ExceptionListener, while trying to generate a JSON response with an invalid utf-8 string. So I suggest not to include the filename here.
9762238 to8fd5569Comparexelaris commentedJul 28, 2017
The PR was updated as suggested by@nicolas-grekas. |
nicolas-grekas commentedAug 5, 2017
Thank you@xelaris. |
…ly encoded filename (xelaris)This PR was merged into the 2.7 branch.Discussion----------[HttpFoundation] Generate safe fallback filename for wrongly encoded filename| Q | A| ------------- | ---| Branch? | 2.7| Bug fix? | no| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets || License | MIT| Doc PR |This handles the case where the encoding of a random string cannot be detected. Until now this causes a PHP Warning `mb_strlen(): Unknown encoding ""`.Commits-------8fd5569 [HttpFoundation] Generate safe fallback filename for wrongly encoded filename
This handles the case where the encoding of a random string cannot be detected. Until now this causes a PHP Warning
mb_strlen(): Unknown encoding "".