diff --git a/autoload/Domain/Article/ArticleRepository.php b/autoload/Domain/Article/ArticleRepository.php index d432f95..bd1a360 100644 --- a/autoload/Domain/Article/ArticleRepository.php +++ b/autoload/Domain/Article/ArticleRepository.php @@ -318,9 +318,7 @@ class ArticleRepository if (is_array($results)) { foreach ($results as $row) { - if (file_exists('../' . $row['src'])) { - unlink('../' . $row['src']); - } + $this->safeUnlink($row['src']); } } @@ -337,9 +335,7 @@ class ArticleRepository if (is_array($results)) { foreach ($results as $row) { - if (file_exists('../' . $row['src'])) { - unlink('../' . $row['src']); - } + $this->safeUnlink($row['src']); } } @@ -819,9 +815,7 @@ class ArticleRepository $results = $this->db->select('pp_articles_files', '*', ['article_id' => null]); if (is_array($results)) { foreach ($results as $row) { - if (file_exists('../' . $row['src'])) { - unlink('../' . $row['src']); - } + $this->safeUnlink($row['src']); } } @@ -836,15 +830,29 @@ class ArticleRepository $results = $this->db->select('pp_articles_images', '*', ['article_id' => null]); if (is_array($results)) { foreach ($results as $row) { - if (file_exists('../' . $row['src'])) { - unlink('../' . $row['src']); - } + $this->safeUnlink($row['src']); } } $this->db->delete('pp_articles_images', ['article_id' => null]); } + /** + * Usuwa plik z dysku tylko jeśli ścieżka pozostaje wewnątrz katalogu upload/. + * Zapobiega path traversal przy danych z bazy. + */ + private function safeUnlink(string $src): void + { + $base = realpath('../upload'); + if (!$base) { + return; + } + $full = realpath('../' . ltrim($src, '/')); + if ($full && strpos($full, $base . DIRECTORY_SEPARATOR) === 0 && is_file($full)) { + unlink($full); + } + } + /** * Pobiera artykuly opublikowane w podanym zakresie dat. */ diff --git a/autoload/Domain/Product/ProductRepository.php b/autoload/Domain/Product/ProductRepository.php index a538629..c546bd0 100644 --- a/autoload/Domain/Product/ProductRepository.php +++ b/autoload/Domain/Product/ProductRepository.php @@ -1601,9 +1601,7 @@ class ProductRepository $results = $this->db->select( 'pp_shop_products_files', '*', [ 'AND' => [ 'product_id' => $productId, 'to_delete' => 1 ] ] ); if ( is_array( $results ) ) { foreach ( $results as $row ) { - if ( file_exists( '../' . $row['src'] ) ) { - unlink( '../' . $row['src'] ); - } + $this->safeUnlink( $row['src'] ); } } $this->db->delete( 'pp_shop_products_files', [ 'AND' => [ 'product_id' => $productId, 'to_delete' => 1 ] ] ); @@ -1614,9 +1612,7 @@ class ProductRepository $results = $this->db->select( 'pp_shop_products_images', '*', [ 'AND' => [ 'product_id' => $productId, 'to_delete' => 1 ] ] ); if ( is_array( $results ) ) { foreach ( $results as $row ) { - if ( file_exists( '../' . $row['src'] ) ) { - unlink( '../' . $row['src'] ); - } + $this->safeUnlink( $row['src'] ); } } $this->db->delete( 'pp_shop_products_images', [ 'AND' => [ 'product_id' => $productId, 'to_delete' => 1 ] ] ); @@ -2125,14 +2121,28 @@ class ProductRepository $results = $this->db->select( 'pp_shop_products_images', '*', [ 'product_id' => null ] ); if ( is_array( $results ) ) { foreach ( $results as $row ) { - if ( file_exists( '../' . $row['src'] ) ) { - unlink( '../' . $row['src'] ); - } + $this->safeUnlink( $row['src'] ); } } $this->db->delete( 'pp_shop_products_images', [ 'product_id' => null ] ); } + /** + * Usuwa plik z dysku tylko jeśli ścieżka pozostaje wewnątrz katalogu upload/. + * Zapobiega path traversal przy danych z bazy. + */ + private function safeUnlink(string $src): void + { + $base = realpath('../upload'); + if (!$base) { + return; + } + $full = realpath('../' . ltrim($src, '/')); + if ($full && strpos($full, $base . DIRECTORY_SEPARATOR) === 0 && is_file($full)) { + unlink($full); + } + } + /** * Oznacza plik do usunięcia. */ diff --git a/templates/articles/article-entry.php b/templates/articles/article-entry.php index a10b878..8e3c760 100644 --- a/templates/articles/article-entry.php +++ b/templates/articles/article-entry.php @@ -2,11 +2,12 @@