954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Checking for going up a Directory

I have been working on flatfile and directory system in PHP and not that I have got the script working I want to make it more secure. What I have is one directory with sub-directorys insided them and insided the subdirectorys are about 20 files each.

Forexample:

directory/
  	sub1/
  	sub2/


When the script is working out what directory to open it works like this:

directory/$subNum/$filename

What I want to be able to do is stop people from going down directorys (../../filename) because this could be a big security risk. What I want to know is if there is a better way than !preg_match('../',$filename).

Ragnarok
Junior Poster in Training
94 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

I would think that would suffice. Does anyone here think that it would still be a security risk? The only thing I could see still being a problem is that someone could still manually enter the directory URL into their browser for access to the files.

cscgal
The Queen of DaniWeb
Administrator
19,433 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

Don't do that. Put the flat file above the web root. I'm not sure of your hosting environment, but most linux set ups allow you to access files above the web root with your scripts.

If there is no way to do that in your situation, then yes, block them with your script since it is already written. But also, change the file perms so only your scripts can access it, and use .htaccess to block access to those files from everybody except your scripts and/or the owner of the files.

Arizona Web
Junior Poster
118 posts since Jun 2004
Reputation Points: 16
Solved Threads: 2
 

there is a slight problem with that fact that I am making it an open source script

Ragnarok
Junior Poster in Training
94 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

HI
I have benn working with a similiar script and i have solved this security issue doing a 'string replace' to the url:

str_replace('..','',$requested_url);


It works like a charm.

;)

ciberwing
Newbie Poster
1 post since Jan 2009
Reputation Points: 9
Solved Threads: 0
 
Yayo_SK
Newbie Poster
11 posts since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You