Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make sure a file path is within a given subdirectory?

I want to make sure a file path set via query string does not go outside of the desired subdirectory. Right now, I am checking that:

  1. The path does not start with "/", to prevent the user from giving an absolute path.
  2. The path does not contain "..", to prevent the user from giving a path that is outside of the desired subdirectory.
  3. The path does not contain ":", to prevent the use of a url (i.e. "http://", "ftp://", etc.). Should I ever run this script on a Windows server (not likely), this will also prevent absolute paths beginning with a drive specifier (i.e. "C:\"). Note: I'm aware that a colon is a valid character in a Unix filenames, but I will never be using it in a filename.
  4. The path does not start with "\". Just in case I change my mind about running on a Windows server, this prevents Windows network paths from being specified (i.e. "\\someserver\someshare"). Again, I'm aware that a backslash is a valid Unix filename character, but I also won't be using it in any filenames.

Are these checks sufficient?

Background

I have a PHP script that takes (via query string) the path to a sample source file to be shown to a user. So I might give them a link like "view_sample.php?path=accounting_app/report_view.php" or "view_sample.php?path=ajax_demo/get_info.js".

The script looks basically like this:

$path = $_GET['path'];
if(path_is_valid($path) && is_file("sample/$path"))
{
  header('Content-Type: text/plain');
  readfile("sample/$path");
}

My concern is that a malicious user would see the url and try to do something like "view_sample.php?path=../../database/connection_info.php" and gain access to a file which is not in the "sample" directory.

Are the four checks I defined above (which would be implemented in the path_is_valid() function) sufficient to lock out a malicious user? (Also, I think checks 1, 3, and 4 are basically irrelevant since I am prepending a relative path, but if I didn't do this would the checks be sufficient?)

like image 232
Kip Avatar asked Jan 19 '09 04:01

Kip


People also ask

How do I follow a file path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

What does \\ mean in Windows path?

Universal naming convention (UNC) paths, which are used to access network resources, have the following format: A server or host name, which is prefaced by \\ .

What does ./' mean in path?

Around 1970, Unix introduced the slash character ("/") as its directory separator. In 1981, the first version of Microsoft DOS was released. MS-DOS 1.0 did not support file directories.

How do you create a file path?

If you're using Windows 11, simply right-click on it. Then, select “Copy as path” in the contextual menu. Alternatively, in Windows 10, you can also select the item (file, folder, library) and click or tap on the “Copy as path” button from File Explorer's Home tab in Windows 10.


2 Answers

Call

$path = realpath("sample/$path");

Then check that the resulting path starts with the directory you're expecting.

like image 66
sth Avatar answered Sep 28 '22 02:09

sth


<?php
    // Current path information
    $path = $_GET['path'];
    $vroot = "sample";

    // Validate that the $path is a subfolder of $vroot
    $vroot = realpath($vroot);
    if(substr(realpath($path), 0, strlen($vroot)) != $vroot or !is_dir($path)) {lid!
        exit("Invalid path");
    } else {
       echo "Ah, everything is alright!";
    }
?>
like image 26
Evan Fosmark Avatar answered Sep 28 '22 02:09

Evan Fosmark