From a5aee910ffa2bd98b12b38206e04457cc63178ac Mon Sep 17 00:00:00 2001 From: liikane <226742748+liikane@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:06:45 +0000 Subject: [PATCH 1/2] Initial plan From 2b6ebd385b87c5b7a4c44ddefafa5b26e9593a46 Mon Sep 17 00:00:00 2001 From: liikane <226742748+liikane@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:10:03 +0000 Subject: [PATCH 2/2] Fix thumbnail boundary and resize failures Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- src/Frontend.php | 7 +- tests/RegressionTest.php | 173 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 tests/RegressionTest.php diff --git a/src/Frontend.php b/src/Frontend.php index 503ddfa..c68e898 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -165,7 +165,8 @@ private function generate_content_image_thumbnail( $src, $width = 150, $height = // Prevent path traversal attacks $file_path = realpath( $file_path ); $basedir_real = realpath( $basedir ); - if ( ! $file_path || ! $basedir_real || 0 !== strpos( $file_path, $basedir_real ) ) { + $basedir_prefix = $basedir_real ? rtrim( $basedir_real, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR : ''; + if ( ! $file_path || ! $basedir_real || 0 !== strpos( $file_path, $basedir_prefix ) ) { return esc_url( $src ); } if ( file_exists( $file_path ) ) { @@ -191,6 +192,10 @@ private function generate_content_image_thumbnail( $src, $width = 150, $height = } $resized = $editor->resize( $width, $height, true ); + if ( is_wp_error( $resized ) || ! $resized ) { + return esc_url( $src ); + } + $saved = $editor->save( $thumb_path ); if ( ! is_wp_error( $saved ) && $saved ) { diff --git a/tests/RegressionTest.php b/tests/RegressionTest.php new file mode 100644 index 0000000..85098e2 --- /dev/null +++ b/tests/RegressionTest.php @@ -0,0 +1,173 @@ +resize_result = $resize_result; + } + + public function resize( $width, $height, $crop = false ) { + return $this->resize_result; + } + + public function save( $path ) { + $this->save_calls[] = $path; + return array( 'path' => $path ); + } + } + + class RegressionTest extends TestCase { + private $root; + private $basedir; + private $baseurl = 'https://example.test/uploads'; + + protected function setUp(): void { + parent::setUp(); + $this->root = sys_get_temp_dir() . '/doneproject-regression-' . bin2hex( random_bytes( 8 ) ); + $this->basedir = $this->root . '/uploads'; + mkdir( $this->basedir, 0700, true ); + State::$uploads = array( + 'basedir' => $this->basedir, + 'baseurl' => $this->baseurl, + ); + State::$image_editor = null; + State::$image_editor_calls = 0; + State::$route = null; + State::$current_user_can = false; + State::$last_capability = null; + } + + protected function tearDown(): void { + $this->removeDir( $this->root ); + parent::tearDown(); + } + + public function testThumbnailRejectsFilesInSiblingDirectories(): void { + $sibling = $this->root . '/uploads-neighbor'; + mkdir( $sibling, 0700, true ); + file_put_contents( $sibling . '/image.jpg', 'image' ); + State::$image_editor = new RegressionImageEditor( true ); + $src = $this->baseurl . '-neighbor/image.jpg'; + + $result = $this->callThumbnailGenerator( $src ); + + $this->assertSame( $src, $result ); + $this->assertSame( 0, State::$image_editor_calls ); + } + + public function testThumbnailFallsBackWhenResizeFails(): void { + file_put_contents( $this->basedir . '/image.jpg', 'image' ); + $editor = new RegressionImageEditor( new WP_Error() ); + State::$image_editor = $editor; + $src = $this->baseurl . '/image.jpg'; + + $result = $this->callThumbnailGenerator( $src ); + + $this->assertSame( $src, $result ); + $this->assertSame( array(), $editor->save_calls ); + } + + public function testRestRouteRequiresReadCapability(): void { + new REST( (object) array( 'slug' => 'doneproject-plugin' ) ); + $permission_callback = State::$route['permission_callback']; + + $this->assertFalse( call_user_func( $permission_callback ) ); + $this->assertSame( 'read', State::$last_capability ); + + State::$current_user_can = true; + $this->assertTrue( call_user_func( $permission_callback ) ); + } + + private function callThumbnailGenerator( $src ) { + $frontend = new Frontend( (object) array( 'version' => '1.0' ) ); + $method = new \ReflectionMethod( $frontend, 'generate_content_image_thumbnail' ); + $method->setAccessible( true ); + return $method->invoke( $frontend, $src ); + } + + private function removeDir( $dir ) { + if ( ! is_dir( $dir ) ) { + return; + } + + foreach ( scandir( $dir ) as $entry ) { + if ( '.' === $entry || '..' === $entry ) { + continue; + } + + $path = $dir . '/' . $entry; + if ( is_dir( $path ) ) { + $this->removeDir( $path ); + } else { + unlink( $path ); + } + } + + rmdir( $dir ); + } + } +}