From f102c9da9603b92e2bcc1f192057277c9816fa84 Mon Sep 17 00:00:00 2001 From: selul Date: Thu, 19 Nov 2026 17:27:47 +0200 Subject: [PATCH 1/3] fix: improve handling of requests, markup and local files Co-Authored-By: Claude Mythos 5.2 --- .../web-components/__tests__/player.test.js | 44 +++++++++++ .../src/video-player/web-components/player.js | 7 +- inc/api.php | 3 - inc/lazyload_replacer.php | 50 +++++++++--- inc/logger.php | 58 ++++++++++++-- inc/manager.php | 2 +- inc/media_offload.php | 37 +++++++++ inc/rest.php | 33 +++++++- inc/v2/PageProfiler/Profile.php | 16 ++++ inc/video_player.php | 1 + phpstan-baseline.neon | 6 -- tests/test-api-requests.php | 67 ++++++++++++++++ tests/test-lazyload-viewport.php | 2 +- tests/test-lazyload.php | 38 +++++++++ tests/test-logger.php | 77 ++++++++++++++++++- tests/test-media.php | 36 +++++++++ tests/test-rest-optimizations.php | 56 +++++++++++++- 17 files changed, 497 insertions(+), 36 deletions(-) create mode 100644 assets/src/video-player/web-components/__tests__/player.test.js create mode 100644 tests/test-api-requests.php diff --git a/assets/src/video-player/web-components/__tests__/player.test.js b/assets/src/video-player/web-components/__tests__/player.test.js new file mode 100644 index 000000000..b56fb4b97 --- /dev/null +++ b/assets/src/video-player/web-components/__tests__/player.test.js @@ -0,0 +1,44 @@ +/** + * Tests for the video player web component. + */ + +describe( 'optimole-video-player', () => { + beforeAll( () => { + global.OMVideoPlayerBlock = { + play: 'Play', + pause: 'Pause', + mute: 'Mute', + unmute: 'Unmute', + fullscreen: 'Fullscreen', + exitFullscreen: 'Exit fullscreen' + }; + + require( '../player' ); + }); + + afterEach( () => { + document.body.innerHTML = ''; + }); + + const mount = ( src ) => { + const player = document.createElement( 'optimole-video-player' ); + player.setAttribute( 'video-src', src ); + document.body.appendChild( player ); + + return player.querySelector( 'video' ); + }; + + test( 'uses the whole video-src attribute as the video source', () => { + const sources = [ + 'https://example.org/clip.mp4', + 'https://example.org/clip.mp4?" data-extra="1' + ]; + + sources.forEach( ( src ) => { + const video = mount( src ); + + expect( video.getAttribute( 'src' ) ).toBe( src ); + expect( video.getAttributeNames().sort() ).toEqual([ 'class', 'src' ]); + }); + }); +}); diff --git a/assets/src/video-player/web-components/player.js b/assets/src/video-player/web-components/player.js index ed75ab9e3..a18dbf4ff 100644 --- a/assets/src/video-player/web-components/player.js +++ b/assets/src/video-player/web-components/player.js @@ -71,7 +71,7 @@ class OptimoleVideoPlayer extends HTMLElement { render() { this.innerHTML = `
- +
${this.getIcon( this.icons.spinner )}
@@ -90,6 +90,11 @@ class OptimoleVideoPlayer extends HTMLElement {
`; + + // The source is set on the element, so the URL is never read as markup. + if ( this.videoSrc ) { + this.querySelector( '.optml-video' ).setAttribute( 'src', this.videoSrc ); + } } setupEventListeners() { diff --git a/inc/api.php b/inc/api.php index da8e38758..abe79bd07 100644 --- a/inc/api.php +++ b/inc/api.php @@ -246,7 +246,6 @@ private function build_args( $method, $url, $headers, $params ) { 'method' => $method, 'timeout' => 45, 'user-agent' => 'Optimle WP (v' . OPTML_VERSION . ') ', - 'sslverify' => false, 'headers' => $headers, ]; if ( $method !== 'GET' ) { @@ -297,7 +296,6 @@ public function call_onboard_api( $images = [] ) { ], 'timeout' => 60, 'blocking' => true, - 'sslverify' => false, 'data_format' => 'body', ]; return wp_remote_post( $this->onboard_api_root, $options ); @@ -405,7 +403,6 @@ public function log_offload_error( $error_response ) { 'headers' => [ 'Content-Type' => 'application/json' ], 'timeout' => 15, 'blocking' => true, - 'sslverify' => false, 'data_format' => 'body', 'body' => [ 'error_body' => wp_json_encode( $body ), diff --git a/inc/lazyload_replacer.php b/inc/lazyload_replacer.php index 86db4b793..7653f97e6 100644 --- a/inc/lazyload_replacer.php +++ b/inc/lazyload_replacer.php @@ -41,6 +41,10 @@ final class Optml_Lazyload_Replacer extends Optml_App_Replacer { * attribute value starts with a backslash is not matched, so JSON-escaped iframe markup is left as written. */ private const TAG_ATTRIBUTE = self::TAG_ATTRIBUTE_SEPARATOR . '[^\s"\'>\/=]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|[^\s"\'>\\\\][^\s>]*))?'; + /** + * One attribute of a JSON-escaped start tag with its separator, where quotes are written as \" or \'. + */ + private const TAG_ATTRIBUTE_SLASHED = self::TAG_ATTRIBUTE_SEPARATOR . '[^\s"\'>\/=\\\\]+(?:\s*=\s*(?:\\\\"[^"]*?\\\\"|\\\\\'[^\']*?\\\\\'|[^\s"\'>\\\\][^\s>]*))?'; /** * If frame lazyload is present on page. * @@ -282,22 +286,24 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_ ); } + $loading_pattern = $this->image_attribute_pattern( 'loading', $is_slashed ); + if ( self::instance()->settings->get( 'native_lazyload' ) === 'enabled' ) { - // First check if loading attribute already exists - if ( strpos( $new_tag, 'loading=' ) !== false ) { - // Replace existing loading attribute with lazy - $loading_attr = $is_slashed ? 'loading=\"lazy\"' : 'loading="lazy"'; - $new_tag = preg_replace( '/loading=(["\'])[^"\']*\1/i', $loading_attr, $new_tag ); - } else { + if ( preg_match( $loading_pattern, $new_tag ) !== 1 ) { // No loading attribute exists, add it after is_valid_mimetype_from_url( $original_url, [ 'gif' => true, 'svg' => true ] ); @@ -362,8 +368,11 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_ ); // We remove srcset and sizes attributes since our lazyload does not need them. - $pattern = '/\s+(?:srcset|sizes)=("[^"]*"|\'[^\']*\'|\\\\"[^\\\\"]*\\\\")/i'; - $new_tag = preg_replace( $pattern, '', $new_tag ); + foreach ( [ 'srcset', 'sizes' ] as $attribute ) { + foreach ( [ false, true ] as $slashed ) { + $new_tag = preg_replace( $this->image_attribute_pattern( $attribute, $slashed ), '$1', $new_tag, 1 ) ?? $new_tag; + } + } // We keep this for srcset lazyload compatibility that might break our mechanism. $new_tag = str_replace( 'srcset=', 'old-srcset=', $new_tag ); @@ -374,6 +383,23 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_ return $new_tag . ''; } + /** + * Build the pattern that finds a quoted attribute of an image tag. + * + * Every attribute before it is matched whole, so the same text inside another attribute's value is not taken + * for the attribute. Group 1 is the tag up to the attribute's separator and group 2 the separator. + * + * @param string $name Attribute name. + * @param bool $is_slashed Whether the tag's quotes are JSON-escaped. + * + * @return string The pattern. + */ + private function image_attribute_pattern( string $name, bool $is_slashed ): string { + $attribute = $is_slashed ? self::TAG_ATTRIBUTE_SLASHED : self::TAG_ATTRIBUTE; + $value = $is_slashed ? '(?:\\\\"[^"]*?\\\\"|\\\\\'[^\']*?\\\\\')' : '(?:"[^"]*"|\'[^\']*\')'; + + return '#^((?:\s*)?(?:(?!\s*' . $name . '\s*=)' . $attribute . ')*))(' . self::TAG_ATTRIBUTE_SEPARATOR . ')' . $name . '\s*=\s*' . $value . '#i'; + } /** * Replaces video embeds with lazyload embeds. * diff --git a/inc/logger.php b/inc/logger.php index a020e64aa..f5c20ffaa 100644 --- a/inc/logger.php +++ b/inc/logger.php @@ -18,9 +18,9 @@ class Optml_Logger { private $upload_dir; /** - * Log filenames. + * The log types, with the fixed file name each was stored under before file names carried a hash. * - * @var array + * @var array */ private $log_filenames = [ self::LOG_TYPE_OFFLOAD => 'offload.log', @@ -64,6 +64,50 @@ private function __construct() { } add_action( 'wp_ajax_optml_fetch_logs', [ $this, 'fetch_logs_ajax_handler' ] ); + add_action( 'admin_init', [ $this, 'protect_log_files' ] ); + } + + /** + * Get the path of a log file. + * + * The file name carries a hash only this site can compute, so the log cannot be requested by guessing its URL. + * A log written under the earlier fixed name is moved to the new one. + * + * @param string $type Type of log (offload/rollback). + * + * @return string Absolute path of the log file. + */ + private function get_file_path( $type ) { + $legacy_path = $this->upload_dir . $this->log_filenames[ $type ]; + $file_path = $this->upload_dir . $type . '-' . wp_hash( $type ) . '.log'; + + // A log that cannot be moved is deleted, so nothing stays under the old name. + if ( file_exists( $legacy_path ) && ( file_exists( $file_path ) || ! @rename( $legacy_path, $file_path ) ) ) { + wp_delete_file( $legacy_path ); + } + + return $file_path; + } + + /** + * Keep the log directory from being listed or served, and move the logs written under the earlier fixed names. + * + * The .htaccess file is written last, so its presence means the directory needs no further work. + * + * @return void + */ + public function protect_log_files() { + $htaccess = $this->upload_dir . '.htaccess'; + if ( file_exists( $htaccess ) || ! $this->has_permission() ) { + return; + } + + foreach ( array_keys( $this->log_filenames ) as $type ) { + $this->get_file_path( $type ); + } + + file_put_contents( $this->upload_dir . 'index.html', '' ); + file_put_contents( $htaccess, "\n\tRequire all denied\n\n\n\tDeny from all\n\n" ); } /** @@ -84,7 +128,7 @@ public function add_log( $type, $message ) { return false; } - $file_path = $this->upload_dir . $this->log_filenames[ $type ]; + $file_path = $this->get_file_path( $type ); $handle = @fopen( $file_path, 'a' ); if ( $handle === false ) { @@ -110,7 +154,7 @@ public function get_log( $type, $lines = 500, $separator = '' ) { return false; } - $file_path = $this->upload_dir . $this->log_filenames[ $type ]; + $file_path = $this->get_file_path( $type ); if ( $this->has_logs( $type ) ) { $data = $this->tailCustom( $file_path, $lines, true, $separator ); @@ -131,7 +175,7 @@ public function has_logs( $type ) { return false; } - $file_path = $this->upload_dir . $this->log_filenames[ $type ]; + $file_path = $this->get_file_path( $type ); return file_exists( $file_path ); } @@ -215,8 +259,8 @@ private function tailCustom( $filepath, $lines = 1, $adaptive = true, $separator */ public function fetch_logs_ajax_handler() { // Check for nonce security - $nonce = $_REQUEST['nonce']; - if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { + $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ) : ''; + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) || ! current_user_can( 'manage_options' ) ) { wp_die( __( 'Security check failed', 'optimole-wp' ) ); } diff --git a/inc/manager.php b/inc/manager.php index b55c2de33..5fbba585c 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -524,7 +524,7 @@ public function replace_content( $html, $partial = false ) { $missing = $this->page_profiler->missing_devices( $profile_id ); $time = time(); $url = esc_url( esc_js( $this->get_current_url() ) ); - $hmac = wp_hash( $profile_id . $time . $url, 'nonce' ); + $hmac = Profile::sign( $profile_id, $time, $url ); $js_optimizer = str_replace( [ Profile::PLACEHOLDER, Profile::PLACEHOLDER_MISSING, Profile::PLACEHOLDER_TIME, Profile::PLACEHOLDER_HMAC, Profile::PLACEHOLDER_URL ], [ $profile_id, implode( ',', $missing ), strval( $time ), $hmac, $url ], diff --git a/inc/media_offload.php b/inc/media_offload.php index 785c2a2b1..8c2b04548 100644 --- a/inc/media_offload.php +++ b/inc/media_offload.php @@ -109,6 +109,12 @@ class Optml_Media_Offload extends Optml_App_Replacer { * @var bool|string Used to check if the current processed image was deduplicated. */ private static $last_deduplicated = false; + /** + * The files that uploads were stored as before their attachment was given a deduplicated name, keyed by attachment ID. + * + * @var array + */ + private static $uploaded_files = []; /** * Checks if the plugin was installed before adding POST_OFFLOADED_FLAG. * @@ -185,6 +191,7 @@ public static function instance() { add_filter( 'image_downsize', [ self::$instance, 'generate_filter_downsize_urls' ], 10, 3 ); if ( $offload_new_uploads ) { add_filter( 'wp_generate_attachment_metadata', [ self::$instance, 'generate_image_meta' ], 10, 2 ); + add_filter( 'wp_generate_attachment_metadata', [ self::$instance, 'keep_uploaded_file_name' ], 11, 2 ); } add_filter( 'wp_get_attachment_url', [ self::$instance, 'get_image_attachment_url' ], - 999, 2 ); add_filter( 'wp_insert_post_data', [ self::$instance, 'filter_uploaded_images' ] ); @@ -275,6 +282,7 @@ public function wp_update_attached_file_filter( $file, $attachment_id ) { // if we have current deduplication set and it contains the filename that is updated // we replace the updated filename with the deduplicated filename if ( ! empty( self::$current_file_deduplication ) && stripos( self::$current_file_deduplication, $no_ext_file_name ) !== false ) { + self::$uploaded_files[ $attachment_id ] = $file; $file = str_replace( $file_name, self::$current_file_deduplication, $file ); // we need to store the filename we replaced to check when uploading the image if it was deduplicated self::$last_deduplicated = $file_name; @@ -289,6 +297,35 @@ public function wp_update_attached_file_filter( $file, $attachment_id ) { return $file; } + /** + * Point an attachment that stayed local back at the file it was uploaded as. + * + * The deduplicated name belongs to the copy stored in the cloud. When the upload is not offloaded, no local + * file was saved under that name, so the attachment keeps the name of its own file. + * + * @param array $meta Attachment metadata. + * @param int $attachment_id Attachment ID. + * + * @return array The metadata, unchanged. + * @uses filter:wp_generate_attachment_metadata + */ + public function keep_uploaded_file_name( $meta, $attachment_id ) { + if ( ! isset( self::$uploaded_files[ $attachment_id ] ) ) { + return $meta; + } + + $uploaded_file = self::$uploaded_files[ $attachment_id ]; + unset( self::$uploaded_files[ $attachment_id ] ); + + if ( isset( $meta['file'] ) && self::is_uploaded_image( $meta['file'] ) ) { + return $meta; + } + + self::$last_deduplicated = false; + update_attached_file( $attachment_id, $uploaded_file ); + + return $meta; + } /** * Function for `wp_insert_attachment_data` filter-hook. * Because we remove the images when new images are added the wp deduplication using the files will not work diff --git a/inc/rest.php b/inc/rest.php index 50e086529..01b2d534c 100644 --- a/inc/rest.php +++ b/inc/rest.php @@ -155,6 +155,16 @@ class Optml_Rest { 'type' => 'string', 'required' => true, ], + 't' => [ + 'type' => 'integer', + ], + 'h' => [ + 'type' => 'string', + ], + 'pu' => [ + 'type' => 'string', + 'default' => '', + ], 'b' => [ 'type' => 'object', 'default' => [], @@ -997,9 +1007,13 @@ public function optimizations( WP_REST_Request $request ) { if ( $time < time() - 300 ) { return $this->response( 'Invalid Signature.', 'error' ); } - if ( wp_hash( $url . $time . $current_url, 'nonce' ) !== $hmac ) { + if ( ! hash_equals( Profile::sign( $url, $time, $current_url ), $hmac ) ) { return $this->response( 'Invalid Signature.', 'error' ); } + // A stored profile is kept until it expires, so a token cannot be used again to replace it. + if ( ( new Profile() )->exists( $url, $device_type ) ) { + return $this->response( 'Profile already stored', 'error' ); + } $bg_selectors = $request->get_param( 'b' ); $lcp_data = $request->get_param( 'l' ); $crop_status = $request->get_param( 'c' ); @@ -1040,7 +1054,7 @@ public function optimizations( WP_REST_Request $request ) { array_map( 'sanitize_url', array_values( $bg_urls ) ), function ( $url ) { // we ignore urls that are not from our service - return strpos( $url, Optml_Config::$service_url ) === 0; + return self::is_service_url( $url ); } ); } @@ -1081,7 +1095,7 @@ function ( $url ) { array_map( 'sanitize_url', array_values( $lcp_data['u'] ?? [] ) ), function ( $url ) { // we ignore urls that are not from our service - return strpos( $url, Optml_Config::$service_url ) === 0; + return self::is_service_url( $url ); } ); $sanitized_lcp_data['type'] = empty( $sanitized_lcp_data['imageId'] ) ? 'bg' : 'img'; @@ -1104,6 +1118,19 @@ function ( $url ) { return $this->response( 'Optimization data stored successfully' ); } + /** + * Check that a URL points at this site's Optimole host. + * + * The trailing slash ends the host, so a longer host that only starts with ours is not accepted. + * + * @param string $url The URL to check. + * + * @return bool + */ + private static function is_service_url( string $url ): bool { + return strpos( $url, trailingslashit( Optml_Config::$service_url ) ) === 0; + } + /** * Method to register above fold data routes. * diff --git a/inc/v2/PageProfiler/Profile.php b/inc/v2/PageProfiler/Profile.php index 7ac84831b..e1a5043a5 100644 --- a/inc/v2/PageProfiler/Profile.php +++ b/inc/v2/PageProfiler/Profile.php @@ -105,6 +105,22 @@ public function __construct() { $this->storage = new $storage_class(); } + /** + * Sign the values a profile submission is bound to. + * + * The values are JSON-encoded as a list, so the same characters cannot be split differently between + * the fields and still produce the same signature. + * + * @param string $id The profile ID. + * @param int $time When the token was issued, as a Unix timestamp. + * @param string $url The URL of the profiled page. + * + * @return string The signature. + */ + public static function sign( string $id, int $time, string $url ): string { + return wp_hash( (string) wp_json_encode( [ $id, $time, $url ] ), 'nonce' ); + } + /** * Generate a unique ID for the page profile * diff --git a/inc/video_player.php b/inc/video_player.php index e12e968ce..0a20be6f5 100644 --- a/inc/video_player.php +++ b/inc/video_player.php @@ -143,6 +143,7 @@ private function enqueue_video_player_component_script() { 'optimole-video-player-component', OPTML_URL . 'assets/build/video-player/frontend/frontend.js', $asset_file['dependencies'], + $asset_file['version'] ); wp_localize_script( 'optimole-video-player-component', self::LOCALIZATION_VAR, $this->get_localization() ); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index a0f7ec18c..96a623298 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1806,12 +1806,6 @@ parameters: count: 1 path: inc/lazyload_replacer.php - - - message: '#^Property Optml_Logger\:\:\$log_filenames type has no value type specified in iterable type array\.$#' - identifier: missingType.iterableValue - count: 1 - path: inc/logger.php - - message: '#^Method Optml_Main\:\:activate\(\) has no return type specified\.$#' identifier: missingType.return diff --git a/tests/test-api-requests.php b/tests/test-api-requests.php new file mode 100644 index 000000000..46500603f --- /dev/null +++ b/tests/test-api-requests.php @@ -0,0 +1,67 @@ +> + */ + private $requests = []; + + /** + * Set up the test environment before each test. + */ + public function setUp(): void { + parent::setUp(); + $this->requests = []; + add_filter( 'pre_http_request', [ $this, 'capture_request' ], 10, 2 ); + } + + /** + * Record the request and answer it without sending anything. + * + * @param false|array|WP_Error $response The short-circuit value. + * @param array $args The request arguments. + * + * @return array + */ + public function capture_request( $response, $args ) { + $this->requests[] = $args; + + return [ + 'headers' => [], + 'body' => '{}', + 'response' => [ + 'code' => 200, + 'message' => 'OK', + ], + 'cookies' => [], + 'filename' => null, + ]; + } + + /** + * Every request to the Optimole API checks the server certificate. + */ + public function test_requests_verify_the_server_certificate(): void { + $api = new Optml_Api(); + + $api->get_user_data( 'test-key' ); + $api->call_onboard_api( [ 'https://example.org/a.jpg' ] ); + $api->log_offload_error( [ 'response' => [ 'code' => 500 ] ] ); + + $this->assertCount( 3, $this->requests ); + foreach ( $this->requests as $args ) { + $this->assertTrue( $args['sslverify'] ); + } + } +} diff --git a/tests/test-lazyload-viewport.php b/tests/test-lazyload-viewport.php index 6008af84d..34ed65324 100644 --- a/tests/test-lazyload-viewport.php +++ b/tests/test-lazyload-viewport.php @@ -165,7 +165,7 @@ private function buildOptimizationsRequest(int $device, int $image_id): WP_REST_ 'b' => [], 'u' => $url, 't' => $time, - 'h' => wp_hash($url . $time . $page, 'nonce'), + 'h' => Profile::sign($url, $time, $page), 'pu' => $page, 'l' => ['i' => $image_id, 's' => '', 'u' => []], ] diff --git a/tests/test-lazyload.php b/tests/test-lazyload.php index df06e0b05..90cfa2ef7 100644 --- a/tests/test-lazyload.php +++ b/tests/test-lazyload.php @@ -427,6 +427,44 @@ public function test_json_should_add_loading () { $this->assertEquals(3, substr_count( $replaced_content, 'loading=\"lazy\"' )); } + public function test_native_lazyload_reads_loading_as_an_attribute_only() { + // As when the images that are never lazy-loaded were already found on the page. + Optml_Tag_Replacer::$lazyload_skipped_images = Optml_Lazyload_Replacer::get_skip_lazyload_limit(); + $settings = new Optml_Settings(); + $settings->update( 'native_lazyload', 'enabled' ); + Optml_Manager::instance()->init(); + + $content = 'y data-note=z'; + $replaced_content = Optml_Manager::instance()->replace_content( $content ); + + $this->assertStringContainsString( ' loading="lazy" src=', $replaced_content ); + $this->assertStringContainsString( 'title="x loading=" alt="y data-note=z">', $replaced_content ); + } + + public function test_lazyload_removes_loading_as_an_attribute_only() { + // As when the images that are never lazy-loaded were already found on the page. + Optml_Tag_Replacer::$lazyload_skipped_images = Optml_Lazyload_Replacer::get_skip_lazyload_limit(); + $content = 'y data-note=z'; + $replaced_content = Optml_Manager::instance()->replace_content( $content ); + + $this->assertStringContainsString( 'data-opt-src', $replaced_content ); + $this->assertStringNotContainsString( 'loading="eager"', $replaced_content ); + $this->assertStringContainsString( 'title="x loading=" alt="y data-note=z">', $replaced_content ); + } + + public function test_lazyload_removes_srcset_and_sizes_as_attributes_only() { + // As when the images that are never lazy-loaded were already found on the page. + Optml_Tag_Replacer::$lazyload_skipped_images = Optml_Lazyload_Replacer::get_skip_lazyload_limit(); + $content = ''; + $replaced_content = Optml_Manager::instance()->replace_content( $content ); + + // The noscript copy keeps the tag as written. + $lazy_tag = strstr( $replaced_content, '