Using basename() is not locale safe

OnkelTem - July 4, 2008 - 05:50
Project:Drupal
Version:7.x-dev
Component:file system
Category:bug report
Priority:normal
Assigned:drewish
Status:needs work
Description

Hi,

I'm using D5 and got much troubles yesterday with uploading files with non-latin characters in file names: all of them had been stripping. This behaviour reproduces on both my Debian Etch servers with PHP5 (but not with PHP4).

After investigations I found, that standard PHP functions basename() is in charge for stripping. The comments on php.net conforms this, basename() is not safe:

http://php.net/manual/en/function.basename.php

Passing UTF-8 string like 'Тест.txt' to basename() results to 'txt', cutting off filename.

Looks like there are no appropriate solutions or workarounds and basename() simply should be replaced with alternative.

#1

OnkelTem - July 4, 2008 - 06:25

UPD.

basename() is locale dependent.

For example, this works, no stripping:

setlocale(LC_ALL, 'ru_RU.UTF-8');
$file->filename = trim(basename($_FILES["files"]["name"][$source]), '.');

This doesn't:

setlocale(LC_ALL, 'C');
$file->filename = trim(basename($_FILES["files"]["name"][$source]), '.');

#2

OnkelTem - July 4, 2008 - 07:20

chx ( http://drupal.org/user/9446 ) has created a replacement for basename() called drupal_basename().

Code is:

function drupal_basename($path,$prefix = '') {
$path = preg_replace('|^.+[\\/]|', '', $path);
if ($prefix) {
$path = preg_replace('|'. preg_quote($prefix) .'$|', '', $path);
}
return $path;
}

Function goes to file.inc (is it good)?

My first patch. Replaced all occurences of basename() in 'includes' and standard 'modules' dirs.

This patch is only for current Drupal 5.7 release!!!

AttachmentSize
basename_replacement_includes.patch 3.5 KB
basename_replacement_modules.patch 2.99 KB

#3

chx - July 4, 2008 - 07:37
Status:active» needs review

This is against HEAD with a little doxygen.

AttachmentSize
drupal_basename.patch 13.05 KB

#4

Damien Tournoud - July 4, 2008 - 08:00
Status:needs review» needs work

Patch does not apply (patch: **** malformed patch at line 100: Index: includes/locale.inc), but both the diagnostic and the implementation looks sane.

Also: this is a perfect usecase of unit testing. So this shouldn't go in without a good test plan.

#5

chx - July 4, 2008 - 08:23
Status:needs work» needs review

Here is a better patch. Damien, can you try writing test cases? Stuff like \0 in file names... maybe make them consisting all of 256 bytes.

AttachmentSize
drupal_basename.patch 13.09 KB

#6

drewish - July 29, 2008 - 17:53

subscribing

#7

c960657 - July 31, 2008 - 08:00

For paths like "/a/" and "/a///", the proposed drupal_basename() returns the empty string, but PHP's basename() returns "a". Is this intentional?

#8

drewish - September 11, 2008 - 20:54

really needs some documentation for the parameters of drupal_basename. we could just copy and paste from php's docs.

#9

drewish - September 11, 2008 - 21:28
Title:Using basename() is not safe» Using basename() is not local safe

Here's a re-roll that fixes the failed hunk in scripts/run-tests.sh and adds some PHPDocs.

AttachmentSize
file_278425.patch 13.71 KB

#10

Damien Tournoud - September 14, 2008 - 21:27
Title:Using basename() is not local safe» Using basename() is not locale safe

#11

drewish - September 16, 2008 - 18:16

Now that file.test landed I guess we could write some tests for this...

#12

webchick - September 16, 2008 - 23:53
Status:needs review» needs work

Yes, tests please. This looks like something relatively obscure which would not come up in the course of regular usage. Perfect for a test case.

#13

drewish - September 18, 2008 - 09:22
Assigned to:Anonymous» drewish
Status:needs work» postponed

i'll write some tests once #203204: Uploaded files have the permissions set to 600 gets committed

#14

drewish - November 22, 2008 - 16:58
Status:postponed» needs work

i think i'd put the wrong issue in the above post... i don't see what the that has to do with this issue. probably one of the other file_test.module patches.

#15

munzirtaha - December 15, 2008 - 12:40

subscribing

#16

hapydoyzer - July 22, 2009 - 04:37

subscribing

#17

OnkelTem - August 27, 2009 - 08:59

Patch for D6.13

AttachmentSize
drupal_6_basename.patch 7.76 KB
 
 

Drupal is a registered trademark of Dries Buytaert.