Re: Check valid filename before copy

From: Ben Morrow (usenet_at_morrow.me.uk)
Date: 02/25/04


Date: Wed, 25 Feb 2004 15:49:10 +0000 (UTC)


Gary Mayor <gary@abertron.co.uk> wrote:
> I'm getting a filename from a user then copying a file to a new file
> with the new filename specified by the user. So the script is like this
>
> $file = param("file");
>
> system("cp $from $file");

Don't use system, use File::Copy.

> I need to check the $file string for any invalid characters such as ../
> @ |. I can do a regex expression to check them but I was hopeing someone
> had already done the expressions what would be all the characters I
> would need to check for security reasons?
>
> Sample Regex
>
> $file=~/\.|\..|\/|\@/)
>
> Is there a better way
>
> Ideas Please?

Don't look for invalid characters, look for valid ones.

die unless $file =~ /^[\w.-+]+$/;

Ben

-- 
               We do not stop playing because we grow old; 
                  we grow old because we stop playing.
                            ben@morrow.me.uk


Relevant Pages

  • Re: BOGUS SOFTWARE ON EBAY BEWARE !!!!!!!!!!!!!!!
    ... Riccardo Rubini wrote: ... copying and pasting pieces of what I wrote you and your ... Men looking for compassion and playing the victim ...
    (comp.sys.cbm)
  • Re: Check valid filename before copy
    ... 2916 East Upper Hayden Lake Road ... "Gary Mayor" wrote in message ... > I need to check the $file string for any invalid characters such as ../ ... I can do a regex expression to check them but I was hopeing someone ...
    (comp.lang.perl.misc)
  • Re: notWigan...
    ... I've been copying more than watching and I think it's damn fine second ... As I wasn't playing, I was in the fortunate ( ... Like you, I've not seen all of it yet, but I though Incommunicado was ...
    (uk.music.guitar)
  • Copying from CD to Media Library
    ... I'm a basic user of WM9 and have encountered the following problem: ... playing as well as copying. ... copying it (creating a distorted track), ...
    (microsoft.public.windowsmedia)
  • Re: Copying from CD to Media Library
    ... > I'm a basic user of WM9 and have encountered the following problem: ... > playing as well as copying. ... > copying it (creating a distorted track), ...
    (microsoft.public.windowsmedia)