Show HN: Mejiro – single-file photo hosting PHP app
dmpop.github.io
dmpop.github.io
A shortened test case using the same code as the app to try and thwart traversal:
<?php
$_GET['d']='..';
$photo_dir='photos/';
$sub_photo_dir = basename($_GET['d']).'/';
$photo_dir = str_replace("//",
"/",$photo_dir.$sub_photo_dir);
print "photo_dir: [$photo_dir]";
?>
That would allow you to upload a file one directory above the intended $photo_dir.If you go that way, though, you'll also want to stop using the literal '/' and use the DIRECTORY_SEPARATOR constant so that your script runs on windows. realpath(), aside from expanding relative paths, would convert / to \ on a windows platform.
A suggestion -- you mention "Automatic thumbnail generation" in the feature list but the thumbnails in the demo have massive file sizes (~80-100KB each at 800px width).
Thumbnails should generally be <20KB and <320px width. You could re-use the `createTim()` function you have to generate an additional, proper thumbnail instead of the 800px preview image squashed down via HTML as you do now.
To get started, I would suggest:
1. Generating your own file names w/extension instead of relying on $_FILES['filetoupload']['name']
2. After move_uploaded_file(), change file permissions to 644 to help mitigate possibility of file execution
3. Use getimagesize() to determine if file is indeed an image. It is still possible to embed code into a validate image to bypass getimagesize(), but #1 will help prevent Apache/etc from interpreting the file as PHP.
4. Ideally you would also strip metadata from the image file and only keep resized images and delete the originals.
Also would suggest that you do not use the same password on your demo site as you have posted on Github.
I know for example that ownCloud has great problems to efficiently display images and something like what you created looks a lot better than what they have so far. Keep it up :)!
It's a neat little script, but I see at least one severe exploit in there ($filename - leave the rest to you).
Esp. after he clearly told that he would really like constructive feedback and stated, that he has no real coding experience.
Why would you play elite and kick someone lower in the ranks his way? It only shows your true colors. And imho they are not really nice in any way.
There is absolutely nothing wrong with being proud of it, regardless of the language or inevitable vulnerabilities that come from being inexperienced with web development.
It's a very small leap to go from this, to understanding separation of concerns.
Go easy on newcomers. We're not that elite.