Monday, February 21, 2011

Regular expression for file path which doesn't allow parent directories

I'm looking to write a regex for a file path that must start with some prefix. In this case it should start with '/tank/home/'. I also want to make sure that it contains no '/..' -- no jumping up to parent directories.

I spent a while fiddling around without coming up with anything quite right. I settled on using two regexes, the first which must match and the second which must not match:

'^/tank/home/'

'/\.\.(/.*)?$'

Does this do what I think it does? Is there an easier way?

This is in a bash script, for what it's worth.

From stackoverflow
  • You could use a negative lookahead to make sure that there aren't any /.. in the string:

    ^(?!.*/\.\..*)/tank/home.*$
    
    alberge : Very close, but not quite right. That doesn't match "/tank/home/..foo".
    Amber : If it's really necessary that you be able to match ..foo, then something like this should probably work: `^(?!.*/\.\.(?:/.*|$))/tank/home.*$`
    Amber : (Basically make the look-ahead only match if it's /../ or /.. at the end of the string.)
    alberge : Thanks -- I hadn't used negative lookahead before.
  • You could use negative lookbehind too:

    \/tank\/home\/([^\/]|?(<!\/..)\/)+$

  • You can expand Dav's regex to include an extra trailing slash:

    ^(?!.*/\.\./.*)/tank/home.*$
    

    But... a better option might be to make sure that the result of the path is something that starts under /tank/home:

    FILEPATH=$(readlink -f $YOURFILE)
    [[ $FILEPATH =~ ^/tank/home/ ]] && echo "starts with /tank/home/"
    
    alberge : Aha. I was wondering if there was something that did that.
  • '^/tank/home(?!.*/\.\.(/|$))/'
    

    matches /tank/home/foo..bar but not /tank/home/.. or /tank/home/foo/../bar

0 comments:

Post a Comment