3 ms·
There's a bit of code to try and stop path traversal, but it doesn't account for passing d=.. as a url param. A shortened test case using the same code as the
by tyingq 11y ago
There's a bit of code to try and stop path traversal, but it doesn't account for passing d=.. as a url param.
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.
- dmpop9mm 11y agoGood point. I'll look into it. Thanks!
- tyingq 11y agoThe typical approach is to use realpath(). That would expand out any relative portions of the path, which you could then compare directly with the expected path and know for sure it's in a subdirectory of your intended photo directory. 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.
- dmpop9mm 11y agoThank you very much for the useful pointers!