You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Jiří Eichler <ej...@seznam.cz> on 2009/07/01 19:19:11 UTC

Re: [users@httpd] Wrong charset convert SOLVED

Man you are incredible. Thank you. And I'm idiot because I searched for 
mistake where it wasn't :) I had just to use:

$target_path = utf8_decode($target_path);

Maybe it will be also possible change somehow php.ini, I'm going to look 
at it. I didn't think that it is problem with php, it ran on Linux well. 
And add one line to MediaWiki won't be problem.

Thank you so much for your time.
Jiri Eichler

André Warnier wrote:
> Jiří Eichler wrote:
> ..
> I just checked your on-line example.
> I used Firefox 3.1, with the "HttpFox" add-on (recommended).
> This shows exactly what the browser is sending to the server.
> In this case, the form does a POST, in the "multipart/form-data" 
> encoding.
> I sent a small test file, which I created on my disk under Windows XP 
> (German, so basically latin-1, not latin-2 like yours).
> I used cut-and-paste from your email, to copy the filename.
> The file name is thus the same as your example, but with a .txt 
> extension.
> In the Windows Explorer (not IE), the file name on my disk looks like :
> složka.txt
> (I used cut-and-paste again in the Explorer to copy this into this 
> email).
>
> I used your (nice) example form to send this file to your server, and 
> traced it with HttpFox.
> This is actually what the browser is sending, as part of that 
> multipart POST :
>
> -----------------------------20037128598723
> Content-Disposition: form-data; name="uploadedfile"; 
> filename="složka.txt"
> Content-Type: text/plain
>
> test složka
>
> -----------------------------20037128598723--
>
> Important : the browser is NOT sending this filename as a part of the 
> URL.  It is sending it in the BODY of the POST request.
> It is also not sending it encoded as "slo%C5%BEka". It /is/ sending 
> the filename encoded as UTF-8.
> That means, that if there is a translation going on here, it is NOT at 
> the level of the upload URL.
>
> Now the question is to know how your PHP script really interprets this 
> filename.  As UTF-8 ?  How do you know for sure ?
> (I am not a PHP specialist, at all)
> I mean, precisely :
> the PHP script "uploader.php", somehow "gets" the value of the 
> parameter "uploadedfile" as a string, representing the filename that 
> the browser uploaded.
> In which encoding (in PHP) /is/ that string ? does PHP know that this 
> is Unicode/UTF-8 ?
>
> Or does /PHP/ (which runs under Apache, which runs under Windows, on a 
> Windows system where the default charset is cp-1250) think that this 
> string is encoded in cp-1250 ?
>
> And then, when PHP writes this file to the disk, it encodes the 
> filename /again/ into Unicode, and thus this time the "ž" (which is 
> originally 2 bytes representing 1 Unicode character), now becomes 4 
> bytes representing the UTF-8 encoding of "Å" and "¾" ....
>
> ... and then, PHP generates the index listing.  And, in this index 
> page, it generates the "href" as
> <a href="slo%c4%b9%c4%beka.txt">
> which looks very much like it could be the Unicode/UTF-8 encoding of 
> "složka.txt", but not like the Unicode/UTF-8 encoding of "složka.txt".
>
>
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server 
> Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Wrong charset convert SOLVED

Posted by André Warnier <aw...@ice-sa.com>.
Jiří Eichler wrote:
> I didn't program MediaWiki, but on Wikipedia it seems to be working 
> well. I just realize that we haven't solved that problem with charset, I 
> have just changed charset sent by php ... you're right with "double 
> encoding" to utf-8, Apache/php think that it is something else and 
> encode it once more. But how can we tell php that it is in utf-8? I 
> don't know. :-D    Can it be called bug when there is no way to detect 
> charset of uploaded filename?
> 
Well...
One basic problem is that there are "holes" in the HTTP 1.x 
specification, at least when considering the multi-lingual, increasingly 
Unicode-centric world in which we are living.
The next problem is that browsers do not always respect even the HTTP 
1.x specification.
The third problem is that not all browsers fail to respect it in the 
same way (but they are getting better at this).
The next issue is that, the WWW being like it is, with clients that the 
server does not control, you can never be sure of anything.
The next issue is that programming languages like PHP, do not 
necessarily offer very good tools to "mark" a string as being in any 
particular encoding.
Another issue is that it is relatively easy to check if a received text 
is valid UTF-8; but it is very hard to check if a received text is valid 
iso-8859-1 or iso-8859-2 or cp-1250, or any of the 8-bit character sets; 
and it is even harder to find out which one of them it is.

And one overall issue, is that it is not always easy to change any of 
the above, without suddenly breaking many WWW applications.

Taking all the above into account however, there are some things which 
you can do in your applications, to minimise the consequences.

One first thing is to be correct, consistent, and precise in what you 
send to the browser.
("Be strict in what you send, and tolerant in what you receive")

So if you have chosen Unicode/UTF-8 for your basic charset and encoding 
(the best choice nowadays), make sure that :
- each time your server sends some text page to the client, there is a 
proper "Content-type: xxx/yyyy; charset=utf-8" HTTP header with the 
response (see *1 below)
- each time your server sends some HTML or XML page to the client, make 
sure that it has an explicit charset declaration inside
- always verify that your pages *are* encoded in UTF-8.  Not that 
someone has been editing your pages using an old editor, which knows 
only iso-latin-2 or cp-1250.
- when you send a <form> to the client, specify the
accept-charset="utf-8" attribute in the <form> tag
- when you send a <form> to the client, which will be later submitted 
back, include some
<input name="test-encoding" type="hidden" value="xxxxxxxxxx">
where "xxxxxxxxxx" is a valid UTF-8 string containing non US-ASCII 
characters.
Then, in the script that receives the data from this form, test this 
parameter, to see if what you received is indeed UTF-8 or not.
The way to do that varies depending on the programming language.
(Maybe you can compare the length in bytes and/or the length in 
characters, or compare it with an internal identical string known to be 
UTF-8.)
- be "defensive" in your cgi-bin scripts. Everything you receive from 
the client is suspect.
- never forget that on the WWW, "the client is king". The user /can/ 
change the charset of his browser, no matter what the server tells it.
(Firefox 3.1 : View..Character encoding; IE 7 : same)



(*1) :
when I use your PHP upload page, the response page that I get from your 
server has these HTTP headers :
HTTP/1.1 200 OK
Date: Wed, 01 Jul 2009 19:44:31 GMT
Server: Apache/2.2.11 (Win32) PHP/5.2.8
X-Powered-By: PHP/5.2.8
Content-Length: 716
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=windows-1250


However, the html page itself contains :
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

That is /not/ consistent.

On the other hand, the index page received after you click on the /data 
link, has the following HTTP headers :

HTTP/1.1 200 OK
Date: Wed, 01 Jul 2009 19:54:01 GMT
Server: Apache/2.2.11 (Win32) PHP/5.2.8
Content-Length: 264
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html;charset=UTF-8



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Wrong charset convert SOLVED

Posted by Jiří Eichler <ej...@seznam.cz>.
I didn't program MediaWiki, but on Wikipedia it seems to be working 
well. I just realize that we haven't solved that problem with charset, I 
have just changed charset sent by php ... you're right with "double 
encoding" to utf-8, Apache/php think that it is something else and 
encode it once more. But how can we tell php that it is in utf-8? I 
don't know. :-D    Can it be called bug when there is no way to detect 
charset of uploaded filename?

André Warnier wrote:
> Jiří Eichler wrote:
>> Man you are incredible. 
> I had to leave that part..
> ...
> (I removed the part about the idiot however..)
> ...
> I didn't think that it is problem with php, it ran on Linux well.
>
> It runs under Linux well, probably /only/ because the locale of the 
> process under which Apache + PHP is started, is a UTF-8 locale.
> So by default, PHP is considering the filename string as UTF-8, and 
> you do not see the problem.
> But if you want to make this really portable, you should also make 
> sure it always does it right under whatever OS and whatever locale.
>
> Unfortunately, it is not easy, because the browser does not actually 
> tell you in which character set it sends the filename.  So you have to 
> "believe" that this is /your/ <form>, and that the browser does it 
> correctly.
>
> I still think that it is a bad idea to save the file under the 
> original name given by the browser, for a number of reasons.
> Let me give you a couple more reasons :
>
> 1) It is easy for a hacker, to create his own "HTTP agent" (browser).
> He does not even have to create one, there are many programs available 
> that do that.
> This client could send you a file named
> "myfile.txt > /etc/passwd"
> or "file.txt ; rm -r /*"
> Then you, on the server, use that filename in another command, like
> system("cat" . $filename . " > myotherfile");
> Got the idea ?
>
> 2) I once designed an application like that, for normal users, not 
> hackers.  And they used it for a long time, without problems.
> Then one day, years later, I had to move all these thousands of 
> uploaded files to another system.  So, I used "tar" to create an 
> archive of these files, to move them to the other system.
> Unfortunately, tar was crashing about every 50 files, because it got a 
> filename that it could not handle, like
> "My grand-mother At the <Pizza Hut>. Near the place of John & Maria".png
> (that being a simple case)
> So it took me a lot of hours to move these files.
>
>
>
>
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server 
> Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Wrong charset convert SOLVED

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Jiří Eichler wrote:
> Ok, "AddDefaultCharset off" added to httpd.conf, charset spec in header
> disappeared in both cases, It must be enough for this time. As regards
> file uploads, I really want to use utf-8 for multilingual support and I
> believe that it is technically possible even on Windows. It just needs
> to configure server to know that received filename is in utf-8. Windows
> 'reputedly' use something like Unicode. Sometimes I wonder why is so
> much problems with charsets. Maybe because it is hard to recognize in
> which charset is text written.

All filesystem URI's for Apache HTTPD on Windows are in utf-8.

But all URI's should be given spelled out in %XX form, so the encoding
itself on the page doesn't matter.

It is so much problem because user/developers think in their region, alone.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Wrong charset convert SOLVED

Posted by Jiří Eichler <ej...@seznam.cz>.
Ok, "AddDefaultCharset off" added to httpd.conf, charset spec in header 
disappeared in both cases, It must be enough for this time. As regards 
file uploads, I really want to use utf-8 for multilingual support and I 
believe that it is technically possible even on Windows. It just needs 
to configure server to know that received filename is in utf-8. Windows 
'reputedly' use something like Unicode. Sometimes I wonder why is so 
much problems with charsets. Maybe because it is hard to recognize in 
which charset is text written.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Wrong charset convert SOLVED

Posted by André Warnier <aw...@ice-sa.com>.
Jiří Eichler wrote:
> Man you are incredible. 
I had to leave that part..
...
(I removed the part about the idiot however..)
...
I didn't think that it is problem with php, it ran on Linux well.

It runs under Linux well, probably /only/ because the locale of the 
process under which Apache + PHP is started, is a UTF-8 locale.
So by default, PHP is considering the filename string as UTF-8, and you 
do not see the problem.
But if you want to make this really portable, you should also make sure 
it always does it right under whatever OS and whatever locale.

Unfortunately, it is not easy, because the browser does not actually 
tell you in which character set it sends the filename.  So you have to 
"believe" that this is /your/ <form>, and that the browser does it 
correctly.

I still think that it is a bad idea to save the file under the original 
name given by the browser, for a number of reasons.
Let me give you a couple more reasons :

1) It is easy for a hacker, to create his own "HTTP agent" (browser).
He does not even have to create one, there are many programs available 
that do that.
This client could send you a file named
"myfile.txt > /etc/passwd"
or "file.txt ; rm -r /*"
Then you, on the server, use that filename in another command, like
system("cat" . $filename . " > myotherfile");
Got the idea ?

2) I once designed an application like that, for normal users, not 
hackers.  And they used it for a long time, without problems.
Then one day, years later, I had to move all these thousands of uploaded 
files to another system.  So, I used "tar" to create an archive of these 
files, to move them to the other system.
Unfortunately, tar was crashing about every 50 files, because it got a 
filename that it could not handle, like
"My grand-mother At the <Pizza Hut>. Near the place of John & Maria".png
(that being a simple case)
So it took me a lot of hours to move these files.





---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org