We all know that ".." can be used to access lower levels of the file structure when used in $_GET["filename"] with
require("/folderwithtrustedfiles/" . $_GET["filename"]);
My questions is:
If all files within folderwithtrustedfiles are trusted and ok for require, can this be considered as secure or is there any other trick other than .. to include files from other folders?
if(strpos($_GET["filename"], "..") === false)
require("/folderwithtrustedfiles/" . $_GET["filename"]);
You can type the following url:
http://yourserver.com/index.php?file=../config/db
I would enforce the base path using realpath(). Like this:
$filename = __DIR__ . '/content/' . $_GET['file'];
if(strpos(realpath($filename), __DIR__ . '/content/') !== 0) {
die('bad path');
}
Related
I am working on a website of a client for which I didn't write the code. I have troubles making files downloadable.
It is about a subdomain where users can download course files.
The website files are contained in the folder "courses" (on the root level).
The file for displaying the downloadable course files is contained in
"courses/displayfiles.php".
The downloadable files are contained in a folder in "courses/downloadfolder". Inside this folder, each user has his own
files folder which as its name has the user id.
displayfiles.php: The following code successfully displays all files that can be downloaded by the logged-in user:
$path = "downloadfolder/" . $_SESSION['userId'] . "/";
$files = array();
$output = #opendir($path) or die("$path could not be found");
while ($file = readdir($output)) {
if (($file != "..") and ($file != ".")) {
array_push($files, $file);
}
}
closedir($output);
sort($files);
foreach ($files as $file) {
echo '<a class="imtext" href="downloadfolder/' . $_SESSION['userId'] . '/' . $file . '/">' . $file . '</a><br/>';
}
So what does not work about this code: When a user clicks on a file, I get a "404 Not Found" message that the file was not found. How can this be?
Why does displaying the files totally works fine, but at the same time I get a 404 error when clicking a file? The files path ($path) must be correct, or not? What further investigations do I need to take in order to solve this problem?
* UPDATE *
I decided to modify the files loop as followed (changing the href):
foreach ($files as $file) {
echo '<a class="imtext" href="http://'.$_SERVER['HTTP_HOST']. '/downloadfolder/' . $_SESSION['courseId'] . '/' . $file . '/">' . $file . '</a><br/>';
}
Still, when I click on a file, I get a 404 Not Found error. How can this be?
You have to look where the webroot of your page is, where the php file generating the list is located and wherer the files are.
Your generated link is relative to the php file generating the link, which might not be corresponding to the URL in the browser. I'd try to make this link relative to the webroot (note the leading slash!)
echo '<a class="imtext" href="/courses/downloadfolder/' . $_SESSION['userId'] . '/' . $file . '/">' . $file . '</a><br/>';
If that guessed solution doesn't work please provide the current URL of the page where this links are generated and one generated link, so we can help you better.
I'm trying to recursively list every file that is in my bucket. It's not too many files but I'd like to list them to test a few things. This code works on a normal file system but it's not working on Google Cloud Storage.
Anyone have any suggestions?
function recurse_look($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_look($src . '/' . $file);
}
else {
echo $src . '/' . $file;
echo "<br />";
}
}
}
closedir($dir);
}
recurse_look("gs://<BUCKET>");
Personally, I would recommend not using a filesystem-impersonation abstraction layer on top of Google Cloud Storage, for a task such as listing everything inside a bucket -- rather, just reach out for the underlying functionality.
In particular, see https://cloud.google.com/storage/docs/json_api/v1/json-api-php-samples for everything about authentication etc, and, once, that's taken care of, focus on just one line in the example:
$objects = $storageService->objects->listObjects(DEFAULT_BUCKET);
This is all you need to list all objects in a bucket (which is not the same thing as "files in a directory", and the "filesystem simulations" on top of buckets and objects, I offer as being just my personal opinion, end up hurting rather than helping despite their excellent intentions:-).
Now if the objects' names contain e.g slashes and you want to take that into account as symbolically signifying something or other, go right ahead, but at least this way you're sure you're getting all the objects actually existing in the bucket, and, nothing but those!-)
Now that glob is working, you can try something like this
function lstree($dir) {
foreach (glob($dir . '/*') as $path) {
if (is_dir($path)) {
echo $path;
lstree($path);
} else {
echo $path;
}
}
lstree('gs://{bucket}/');
I'm trying to turn the files in my 'objects' directory into an array, then use them to load the objects. But, for some reason, I continue to get this error
Warning: opendir(C:\xampp\htdocs/objects,C:\xampp\htdocs/objects): The system cannot find the file specified. (code: 2)
here is the code:
public function loadObjects(){
$files = array();
if ($handle = opendir(APP_PATH . 'objects'))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
$files[] = $entry;
}
}
}
closedir($handle);
if(is_array($files) && count($files) > 0)
{
foreach($files as $value)
{
require_once(APP_PATH . 'objects/' . $value);
$value = stristr($value, '.', true);
self::$objects[$value] = new $object(self::$instance);
}
}
}
I know this is an old question but for any future viewers I will post an anwser just in case.
This type of error usually comes from a simple oversight. When developing most aplication the developer usualy uses a path like
http://localhost/myAppHome
or
http://96.82.102.233/myAppHome(if on remote server)
In this perticular case the APP_PATH is probably defined somethig like that:
define('APP_PATH',$_SERVER['DOCUMENT_ROOT']);
This will be wrong in every case when the app is being developed outside of a domain name.
$_SERVER['DOCUMENT_ROOT'] will resolve to the root of domain which in this case will be
http://localhost or http://96.82.102.233
The main directory for localhost or the IP address is going to be the diretory root of the server itself => drive:/xampp/htdocs (for example)
Basically to avoid this issue you should always mind not to ask for 'DOCUMENT_ROOT' when developing without a domain pointing to you app.
If you dont require reqular deploys you can just add the missing folder to the definition like so :
define('APP_PATH',$_SERVER['DOCUMENT_ROOT'].'/myAppHome');
In case you deploy on reqular basis and you are afraid you will forget to rever this change before depoying you can always insert an IF when defing APP_PATH like:
if($_SERVER['SERVER_NAME']=='localhost'){
define('APP_PATH', $_SERVER['DOCUMENT_ROOT'].'/myAppHome');
}else{
define('APP_PATH', $_SERVER['DOCUMENT_ROOT']);
}
You are trying to open that directory with a "/".
Try to replace:
C:\xampp\htdocs/objects
to
C:\xampp\htdocs\objects
Please be sure APP_PATH variable is not null and correct values. There is no scandir function usage on your codes.
After that, i suggest you to use DirectoryIterator.
http://www.php.net/manual/en/class.directoryiterator.php
Complete example:
http://fabien.potencier.org/article/43/find-your-files
APP_HOST = DIR folder;
APP_PATH = APP_PATH + DIR folder;
Example = "C:/xampp/htdocs" + "/parent/child/index.php"
if ($_SERVER['SERVER_NAME'] == "localhost") {
define('APP_HOST', pathinfo($_SERVER['PHP_SELF'], PATHINFO_DIRNAME));
define('APP_PATH', $_SERVER['DOCUMENT_ROOT'] . APP_HOST);
} else {
define('APP_PATH', $_SERVER['DOCUMENT_ROOT']);
}
I have a two part question...
For my file check I need to look to see if the file is present in $filechk or $dirchk.
How can I use a wildcard on the file extension $filename.* when doing a file check?
I'm using is_file because I read that it's twice as fast when checking if a file exists.
code
$filechk1 = "/temp/files/" . $data[0] . ".doc";
$filechk2 = "/temp/files/" . $data[1] . ".doc";
$dirchk1 = "/temp/files/" . $IDname . $data[0] . ".doc";
$dirchk2 = "/temp/files/" . $IDname . $data[1] . ".doc";
if(is_file($filechk1) && ($filechk2)){
...
}
else { ... }
you should get a list of all of the files in the directory and then check the file extensions - is_file is for a single file only.
To check a number of files, just do a separate is_file() or file_exists() - the speed difference between the two is hardly relevant if you're doing this on one or two files.
For a wildcard search, do a glob().
$files = glob("/path/to/directory/*.doc");
print_r($files);
We have a script, /scripts/ourscript.php and a file, /texts/elvis.txt.
How can we change contents of this file, when we run ourscript.php?
Use file_put_contents() method to set the contents of a file.
If you need just to save new data, you can do:
$elvis = 'Contents here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
If, instead of saving data, you need to change existing data, do:
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
$elvis = file_get_contents($fileName);
// Do changes to $elvis here.
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
Finally, if you need to append something new to existing contents, use:
$elvis = PHP_EOL . 'Contents to append to existing stuff here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
// Noticed FILE_APPEND as third argument?
if (file_put_contents($fileName, $elvis, FILE_APPEND) === false)
{
// Handle error here.
}
While MainMa has given you a direct answer, I'll point you to:
http://php.net/manual/en/function.file.php
Since it seems that you might have more of these questions, which could have been easily answered by looking at the documentation.
Also by figuring things out with the help of the documentation you'll learn how to solve such problems on your own, you know independence is a nice thing to have :)