Security implications of using open() on untrusted strings.

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Nov 24 02:29:33 EST 2008


On Mon, 24 Nov 2008 00:44:45 -0500, r0g wrote:

> Hi there,
> 
> I'm trying to validate some user input which is for the most part simple
> regexery however I would like to check filenames and I would like this
> code to be multiplatform.
> 
> I had hoped the os module would have a function that would tell me if a
> proposed filename would be valid on the host system but it seems not. I
> have considered whitelisting but it seems a bit unfair to make the rest
> of the world suffer the naming restrictions of windows. Moreover it
> seems both inelegant and hard work to research the valid file/directory
> naming conventions of every platform that this app could conceivably run
> on and write regex's for all of them so...

That's probably why nobody has written a function for the os module to do 
the same... and just wait until you get into the murky universe of cross-
platform Unicode filenames.

Honestly, I think your best bet is to just trust the file system to 
recognize a bad file name and raise an exception. What counts as a bad 
file name is surprisingly hard to define, especially if you want to be 
cross-platform. See here for more details:


http://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-
filename-in-python


-- 
Steven



More information about the Python-list mailing list