DiskCache: more strict checking for input filenames, getUrl() is no longer static

This commit is contained in:
Andrew Dolgov 2019-08-14 09:49:18 +03:00
parent 65450f8a2b
commit 3c075bfd21
5 changed files with 16 additions and 16 deletions

View File

@ -673,10 +673,12 @@ class Article extends Handler_Protected {
$rv = array(); $rv = array();
$cache = new DiskCache("images");
while ($line = $sth->fetch()) { while ($line = $sth->fetch()) {
if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) { if ($cache->exists(sha1($line["content_url"]))) {
$line["content_url"] = DiskCache::getUrl(sha1($line["content_url"])); $line["content_url"] = $cache->getUrl(sha1($line["content_url"]));
} }
array_push($rv, $line); array_push($rv, $line);

View File

@ -3,7 +3,7 @@ class DiskCache {
private $dir; private $dir;
public function __construct($dir) { public function __construct($dir) {
$this->dir = CACHE_DIR . "/" . basename($dir); $this->dir = CACHE_DIR . "/" . clean_filename($dir);
} }
public function getDir() { public function getDir() {
@ -39,7 +39,7 @@ class DiskCache {
} }
public function getFullPath($filename) { public function getFullPath($filename) {
$filename = basename($filename); $filename = clean_filename($filename);
return $this->dir . "/" . $filename; return $this->dir . "/" . $filename;
} }
@ -72,8 +72,8 @@ class DiskCache {
return send_local_file($this->getFullPath($filename)); return send_local_file($this->getFullPath($filename));
} }
static public function getUrl($filename) { public function getUrl($filename) {
return get_self_url_prefix() . "/public.php?op=cached_url&file=" . $filename; return get_self_url_prefix() . "/public.php?op=cached_url&file=" . basename($this->dir) . "/" . $filename;
} }
// check for locally cached (media) URLs and rewrite to local versions // check for locally cached (media) URLs and rewrite to local versions
@ -103,7 +103,7 @@ class DiskCache {
if ($cache->getSize($cached_filename) > 0) { if ($cache->getSize($cached_filename) > 0) {
$src = DiskCache::getUrl(sha1($src)); $src = $cache->getUrl(sha1($src));
if ($entry->hasAttribute('poster')) if ($entry->hasAttribute('poster'))
$entry->setAttribute('poster', $src); $entry->setAttribute('poster', $src);

View File

@ -1202,13 +1202,7 @@ class Handler_Public extends Handler {
} }
function cached_url() { function cached_url() {
$filename = $_GET['file']; list ($cache_dir, $filename) = explode("/", $_GET["file"], 2);
if (strpos($filename, "/") !== FALSE) {
list ($cache_dir, $filename) = explode("/", $filename, 2);
} else {
$cache_dir = "images";
}
$cache = new DiskCache($cache_dir); $cache = new DiskCache($cache_dir);

View File

@ -594,6 +594,10 @@
} }
} }
function clean_filename($filename) {
return basename(preg_replace("/\.\.|[\/\\\]/", "", $filename));
}
function make_password($length = 12) { function make_password($length = 12) {
$password = ""; $password = "";
$possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ*%+^"; $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ*%+^";

View File

@ -101,7 +101,7 @@ class Cache_Starred_Images extends Plugin {
$local_filename = $article_id . "-" . sha1($enc["content_url"]); $local_filename = $article_id . "-" . sha1($enc["content_url"]);
if ($this->cache->exists($local_filename)) { if ($this->cache->exists($local_filename)) {
$enc["content_url"] = DiskCache::getUrl("starred-images/" . $local_filename); $enc["content_url"] = $this->cache->getUrl($local_filename);
} }
return $enc; return $enc;
@ -123,7 +123,7 @@ class Cache_Starred_Images extends Plugin {
$local_filename = $article_id . "-" . sha1($src); $local_filename = $article_id . "-" . sha1($src);
if ($this->cache->exists($local_filename)) { if ($this->cache->exists($local_filename)) {
$entry->setAttribute("src", DiskCache::getUrl("starred-images/" . $local_filename)); $entry->setAttribute("src", $this->cache->getUrl($local_filename));
$entry->removeAttribute("srcset"); $entry->removeAttribute("srcset");
} }
} }