r/programming • • Nov 27 '12

Minty Wiki, A One File PHP Wiki that uses Markdown Syntax.

https://github.com/am2064/Minty-Wiki
0 Upvotes

4 comments sorted by

4

u/JW_00000 Nov 27 '12

Security-related question: looking at the code, I see the contents of the page you're viewing are printed using the following function call:

readDirFile('.');

which is implemented as follows:

function readDirFile($dir){
	$file = "$dir"."/".$_GET['entry'];
	$mark = fopen($file,"r");
	$markContents = fread($mark,filesize("$file"));
	echo Markdown($markContents);
	fclose($mark);
}

However, imagine I request the page with ?entry=../../password.ini (or similar), would that mean that file is opened and its contents are shown?

Also, why use "$dir" instead of $dir?

2

u/xxNIRVANAxx Nov 27 '12 edited Nov 27 '12

Excellent find! This is my attempt to solve this security problem:

function readDirFile($dir){
    $validEntry = $_GET['entry'];
    if (strpos($validEntry, '..') !== false) {
    // entry is invalid, let's show them the main page
	    readDirectory('.');
    } else {
    // entry is valid, carry on
	    $file = $dir."/".$validEntry;
	    $mark = fopen($file,"r");
	    $markContents = fread($mark,filesize("$file"));
	    echo Markdown($markContents);
	    fclose($mark);
    }
}

I'm currently learning PHP so please respond with any tips/tricks you may have. Here's a live demo of the script, since there isn't one linked to by the git page.

Edit: Found another place we should check whether $entry is valid: when editting! Here's my solution:

Find:

if ($_GET['edit']){
?>
	<form action="." method="post">
		<input type="hidden" name="article" value="<?php echo $_GET['entry']; ?>" >
		<textarea name="update" rows="25" cols="100"<?php if(!$editting) echo "disabled";?>><?php readDirFileRAW('.');?></textarea>
 		<br/>
    	<?php if($editting){?>
    	<input type="submit" value="Submit">
  	<?php } ?>
     </form>
<?php
}
?>

Replace:

if ($_GET['edit']){
    $validEntry = $_GET['entry'];
    if (strpos($validEntry, '..') !== false) {
    // entry is invalid, let's show them the main page
	    readDirectory('.');
    } else {
?>
    <form action="." method="post">
	    <input type="hidden" name="article" value="<?php echo $_GET['entry']; ?>" >
	    <textarea name="update" rows="25" cols="100"<?php if(!$editting) echo "disabled";?>><?php readDirFileRAW('.');?></textarea>
	    <br/>
	    <?php if($editting){?>
	    <input type="submit" value="Submit">
	    <?php } ?>
    </form>
<?php
    }
}
?>

edit 2: Maybe we should protect index.php as well.

3

u/thrashr888 Nov 28 '12

i would use a whitelist instead of a blacklist. and at least save the files with a .md extension, then only read files with that extension.

here's a decent php doc page with a few good suggestions to get you started: http://php.net/manual/en/security.filesystem.php

1

u/JW_00000 Nov 29 '12

thrashr888's suggestion is exactly what I wanted to reply: instead of attempting to blacklist all non-allowed values, whitelist those that are allowed. Put the markdown files in a data directory, with extension .md, and write a function that retrieves all *.md files in that directory. Then see if $_GET['entry'] matches any of these.

In general, you should never trust user input, so treat all $_GET and $_POST variables with care! You never know what will be in them.