diff --git a/inc/plugins/class-atomic-wind-blocks.php b/inc/plugins/class-atomic-wind-blocks.php index e0924c025..a6f67617b 100644 --- a/inc/plugins/class-atomic-wind-blocks.php +++ b/inc/plugins/class-atomic-wind-blocks.php @@ -1399,17 +1399,29 @@ public function render_post_fields( $block_content, $block ) { case 'custom_field': $meta_key = isset( $block['attrs']['customFieldKey'] ) ? sanitize_text_field( $block['attrs']['customFieldKey'] ) : ''; if ( $meta_key ) { - $value = ''; - if ( function_exists( 'get_field' ) ) { - $acf = get_field( $meta_key, get_the_ID() ); - if ( is_string( $acf ) ) { - $value = $acf; + // An ACF field key (e.g. `field_abc123`) resolves to a meta name that may be protected, + // so check the resolved name as well, not only the selector the block stores. + $resolved_key = $meta_key; + if ( function_exists( 'get_field_object' ) ) { + $field_object = get_field_object( $meta_key, get_the_ID(), false ); + if ( is_array( $field_object ) && ! empty( $field_object['name'] ) ) { + $resolved_key = (string) $field_object['name']; } } - if ( ! $value ) { - $raw = get_post_meta( get_the_ID(), $meta_key, true ); - if ( is_string( $raw ) ) { - $value = $raw; + // Never print a protected meta key (e.g. a `_`-prefixed field holding a form submitter's data). + if ( ! is_protected_meta( $meta_key, 'post' ) && ! is_protected_meta( $resolved_key, 'post' ) ) { + $value = ''; + if ( function_exists( 'get_field' ) ) { + $acf = get_field( $meta_key, get_the_ID() ); + if ( is_string( $acf ) ) { + $value = $acf; + } + } + if ( ! $value ) { + $raw = get_post_meta( get_the_ID(), $meta_key, true ); + if ( is_string( $raw ) ) { + $value = $raw; + } } } } diff --git a/plugins/otter-pro/inc/plugins/class-block-conditions.php b/plugins/otter-pro/inc/plugins/class-block-conditions.php index 89f2baf58..c36ae2a82 100644 --- a/plugins/otter-pro/inc/plugins/class-block-conditions.php +++ b/plugins/otter-pro/inc/plugins/class-block-conditions.php @@ -133,6 +133,11 @@ public function has_meta( $condition, $type = 'post' ) { $id = ''; $meta = ''; + // A protected key is never read, so its value cannot leak through the comparison result. + if ( Dynamic_Content::is_protected_meta_key( $condition['meta_key'], $type ) ) { + return false; + } + if ( 'post' === $type ) { $id = get_the_ID(); $meta = get_post_meta( $id, $condition['meta_key'], true ); diff --git a/plugins/otter-pro/inc/plugins/class-dynamic-content.php b/plugins/otter-pro/inc/plugins/class-dynamic-content.php index 111e594df..8d031d172 100644 --- a/plugins/otter-pro/inc/plugins/class-dynamic-content.php +++ b/plugins/otter-pro/inc/plugins/class-dynamic-content.php @@ -192,20 +192,103 @@ public function get_terms( $data ) { * Blocks WordPress protected (underscore-prefixed) meta and sensitive user fields, * since these getters can be reached for any published post without authentication. * - * @param string $key Meta key. + * @param string $key Meta key. + * @param string $meta_type Meta type the key is read from ('post' or 'user'), for the is_protected_meta filter. * * @return bool */ - public static function is_protected_meta_key( $key ) { + public static function is_protected_meta_key( $key, $meta_type = 'post' ) { $key = (string) $key; + if ( self::is_protected_meta_name( $key, $meta_type ) ) { + return true; + } + + // get_the_author_meta() -> WP_User::__get resolves field names to real user-meta keys, so check the + // resolved key too, while still checking the raw key above for get_user_meta reads. + if ( 'user' === $meta_type ) { + $lower = strtolower( $key ); + + // Legacy capability level; user-only, so a post field named user_level stays allowed. + if ( 'user_level' === $lower ) { + return true; + } + + // Forward aliases, e.g. pass -> user_pass. + $aliases = array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ); + if ( in_array( $lower, $aliases, true ) ) { + return self::is_protected_meta_name( 'user_' . $lower, $meta_type ); + } + + // Back-compat fields, e.g. user_firstname -> first_name. + $back_compat = array( + 'user_firstname' => 'first_name', + 'user_lastname' => 'last_name', + 'user_description' => 'description', + ); + if ( isset( $back_compat[ $lower ] ) ) { + return self::is_protected_meta_name( $back_compat[ $lower ], $meta_type ); + } + } + + return false; + } + + /** + * Whether a single meta key (checked verbatim) is protected. + * + * @param string $key Meta key. + * @param string $meta_type Meta type the key is read from ('post' or 'user'). + * + * @return bool + */ + private static function is_protected_meta_name( $key, $meta_type ) { if ( '' === $key || '_' === $key[0] ) { return true; } $protected = array( 'user_pass', 'user_activation_key', 'session_tokens' ); - return in_array( strtolower( $key ), $protected, true ); + if ( in_array( strtolower( $key ), $protected, true ) ) { + return true; + } + + // Honor the `is_protected_meta` filter, so a key another plugin protects without a `_` prefix is caught too. + return is_protected_meta( $key, $meta_type ); + } + + /** + * Whether an ACF selector resolves to a protected meta key. + * + * An ACF field key (e.g. `field_abc123`) is harmless on its own, but ACF resolves it to the field's + * meta name, which can be a protected `_`-prefixed key. + * + * @param string $selector ACF field key or name. + * @return bool + */ + public static function is_protected_acf_field( $selector ) { + if ( ! function_exists( 'acf_get_field' ) ) { + return false; + } + + $seen = array(); + $field = acf_get_field( $selector ); + + // Walk the parent chain: a sub-field under a protected repeater is reachable through the repeater fallback. + while ( is_array( $field ) ) { + if ( isset( $field['name'] ) && self::is_protected_meta_key( $field['name'] ) ) { + return true; + } + + $parent = isset( $field['parent'] ) ? $field['parent'] : ''; + if ( empty( $parent ) || isset( $seen[ $parent ] ) ) { + break; + } + $seen[ $parent ] = true; + $field = acf_get_field( $parent ); + } + + return false; } /** @@ -246,7 +329,10 @@ public function get_acf( $data ) { } $field_key = sanitize_text_field( $data['metaKey'] ); - $meta = get_field( $field_key, $data['context'] ); + if ( self::is_protected_meta_key( $field_key ) || self::is_protected_acf_field( $field_key ) ) { + return $default; + } + $meta = get_field( $field_key, $data['context'] ); // Get the ACF repeater's sub-field value. if ( ( null === $meta || false === $meta || '' === $meta ) && function_exists( 'acf_get_field' ) ) { @@ -485,7 +571,7 @@ public function get_author_meta( $data ) { $default = isset( $data['default'] ) ? esc_html( $data['default'] ) : ''; $meta = ''; - if ( isset( $data['metaKey'] ) && ! self::is_protected_meta_key( $data['metaKey'] ) ) { + if ( isset( $data['metaKey'] ) && ! self::is_protected_meta_key( $data['metaKey'], 'user' ) ) { $meta = get_the_author_meta( esc_html( $data['metaKey'] ), intval( get_post_field( 'post_author', $data['context'] ) ) ); } @@ -508,7 +594,7 @@ public function get_loggedin_meta( $data ) { $default = isset( $data['default'] ) ? esc_html( $data['default'] ) : ''; $meta = ''; - if ( isset( $data['metaKey'] ) && ! self::is_protected_meta_key( $data['metaKey'] ) ) { + if ( isset( $data['metaKey'] ) && ! self::is_protected_meta_key( $data['metaKey'], 'user' ) ) { $meta = get_user_meta( $user_id, esc_html( $data['metaKey'] ), true ); } @@ -683,7 +769,7 @@ public function evaluate_content_media_server( $path, $request ) { $id = $request->get_param( 'id' ); $meta = $request->get_param( 'meta' ); - if ( 'postMeta' === $type && ! empty( $meta ) ) { + if ( 'postMeta' === $type && ! empty( $meta ) && ! self::is_protected_meta_key( $meta ) ) { $value = get_post_meta( $context, $meta, true ); if ( ! empty( $value ) ) { @@ -706,7 +792,7 @@ public function evaluate_content_media_server( $path, $request ) { } } - if ( 'acf' === $type && ! empty( $meta ) && class_exists( 'ACF' ) ) { + if ( 'acf' === $type && ! empty( $meta ) && class_exists( 'ACF' ) && ! self::is_protected_meta_key( $meta ) && ! self::is_protected_acf_field( $meta ) ) { $field = get_field( $meta, $context ); // Fall back to repeater sub-field traversal when the field is a @@ -752,7 +838,7 @@ public function evaluate_content_media_server( $path, $request ) { * @return string|string[] */ public function evaluate_content_media_content( $value, $data ) { - if ( 'postMeta' === $data['type'] && isset( $data['meta'] ) && ! empty( $data['meta'] ) ) { + if ( 'postMeta' === $data['type'] && isset( $data['meta'] ) && ! empty( $data['meta'] ) && ! self::is_protected_meta_key( $data['meta'] ) ) { $meta = get_post_meta( $data['context'], $data['meta'], true ); if ( ! empty( $meta ) ) { @@ -775,7 +861,7 @@ public function evaluate_content_media_content( $value, $data ) { } } - if ( 'acf' === $data['type'] && ! empty( $data['meta'] ) && class_exists( 'ACF' ) ) { + if ( 'acf' === $data['type'] && ! empty( $data['meta'] ) && class_exists( 'ACF' ) && ! self::is_protected_meta_key( $data['meta'] ) && ! self::is_protected_acf_field( $data['meta'] ) ) { $field = get_field( $data['meta'], $data['context'] ); // Fall back to repeater sub-field traversal when the field is a diff --git a/plugins/otter-pro/inc/plugins/class-posts-acf-integration.php b/plugins/otter-pro/inc/plugins/class-posts-acf-integration.php index aca2283bf..801084820 100644 --- a/plugins/otter-pro/inc/plugins/class-posts-acf-integration.php +++ b/plugins/otter-pro/inc/plugins/class-posts-acf-integration.php @@ -55,7 +55,8 @@ public function render_acf_fields( $data, $attributes, $id, $element ) { } } - if ( ( ! isset( $custom_meta_field['display'] ) || true === $custom_meta_field['display'] ) && isset( $custom_meta_field['field'] ) && function_exists( 'get_field_object' ) ) { + if ( ( ! isset( $custom_meta_field['display'] ) || true === $custom_meta_field['display'] ) && isset( $custom_meta_field['field'] ) && function_exists( 'get_field_object' ) + && ! Dynamic_Content::is_protected_meta_key( $custom_meta_field['field'] ) && ! Dynamic_Content::is_protected_acf_field( $custom_meta_field['field'] ) ) { $field = get_field_object( $custom_meta_field['field'], $id ); if ( false !== $field ) { $data .= '
Placeholder
'; + + $result = $this->instance->render_post_fields( $content, $block ); + + $this->assertStringNotContainsString( 'Protected Meta Value', $result ); + $this->assertStringContainsString( 'Placeholder', $result ); + + wp_reset_postdata(); + } + + public function test_custom_field_skips_acf_field_with_protected_name() { + $GLOBALS['otter_test_acf_fields'] = array( + 'field_secret' => array( 'name' => '_acf_protected', 'value' => 'ACF Secret Value' ), + ); + + $this->reset_in_query( true ); + global $post; + $post = get_post( $this->post_id ); + setup_postdata( $post ); + + $block = $this->make_block( 'atomic-wind/text', array( 'postField' => 'custom_field', 'customFieldKey' => 'field_secret' ) ); + $content = 'Placeholder
'; + + $result = $this->instance->render_post_fields( $content, $block ); + + $this->assertStringNotContainsString( 'ACF Secret Value', $result ); + $this->assertStringContainsString( 'Placeholder', $result ); + + wp_reset_postdata(); + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + public function test_custom_field_renders_a_public_meta_key() { + update_post_meta( $this->post_id, 'otter_public_field', 'Public Meta Value' ); + + $this->reset_in_query( true ); + global $post; + $post = get_post( $this->post_id ); + setup_postdata( $post ); + + $block = $this->make_block( 'atomic-wind/text', array( 'postField' => 'custom_field', 'customFieldKey' => 'otter_public_field' ) ); + $content = 'Placeholder
'; + + $result = $this->instance->render_post_fields( $content, $block ); + + $this->assertStringContainsString( 'Public Meta Value', $result ); + + wp_reset_postdata(); + } + // ------------------------------------------------------- // render_query_loop — post type visibility (otter-internals#319) // ------------------------------------------------------- diff --git a/tests/test-block-conditions.php b/tests/test-block-conditions.php index 6f2beb2bb..04ddf2317 100644 --- a/tests/test-block-conditions.php +++ b/tests/test-block-conditions.php @@ -659,4 +659,80 @@ public function test_screen_size_condition_enqueues_hide_styles_for_head(): void $this->assertMatchesRegularExpression( '/id=["\']o-condition-hide-inline-css["\']/', $head_styles ); $this->assertStringContainsString( '.o-hide-on-mobile:is(.o-hide-on-mobile,#_){display:none!important}', $head_styles ); } + + /** + * A protected meta key cannot be used as a value oracle through a block condition (otter-internals#320). + */ + public function test_post_meta_condition_ignores_protected_key() { + $post_id = $this->factory()->post->create(); + update_post_meta( $post_id, '_secret', 'leaked-value' ); + + $this->go_to( get_permalink( $post_id ) ); + global $post; + $post = get_post( $post_id ); + setup_postdata( $post ); + + $condition = array( + 'type' => 'postMeta', + 'meta_key' => '_secret', + 'meta_compare' => 'if_contains', + 'meta_value' => 'leaked', + ); + + $result = $this->block_conditions->evaluate_condition( $condition ); + + $this->assertFalse( $result ); + + wp_reset_postdata(); + } + + /** + * A logged-in-user-meta condition honors a filter that protects a key only for user meta (otter-internals#320). + */ + public function test_logged_in_user_meta_ignores_user_only_filter_protected_key() { + wp_set_current_user( $this->user_id ); + update_user_meta( $this->user_id, 'nick_x', '1' ); + + $filter = static function ( $protected, $meta_key, $meta_type ) { + return ( 'nick_x' === $meta_key && 'user' === $meta_type ) ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 3 ); + + $condition = array( + 'type' => 'loggedInUserMeta', + 'meta_key' => 'nick_x', + 'meta_compare' => 'is_true', + ); + + $result = $this->block_conditions->evaluate_condition( $condition ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertFalse( $result ); + } + + /** + * A user meta key named like a core alias is still checked verbatim against the filter. + */ + public function test_logged_in_user_meta_checks_an_alias_named_key_verbatim() { + wp_set_current_user( $this->user_id ); + update_user_meta( $this->user_id, 'email', '1' ); + + $filter = static function ( $protected, $meta_key, $meta_type ) { + return ( 'email' === $meta_key && 'user' === $meta_type ) ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 3 ); + + $condition = array( + 'type' => 'loggedInUserMeta', + 'meta_key' => 'email', + 'meta_compare' => 'is_true', + ); + + $result = $this->block_conditions->evaluate_condition( $condition ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertFalse( $result ); + } } diff --git a/tests/test-dynamic-content-server.php b/tests/test-dynamic-content-server.php index a595d69ab..ddb2b71b2 100644 --- a/tests/test-dynamic-content-server.php +++ b/tests/test-dynamic-content-server.php @@ -5,6 +5,10 @@ * @package gutenberg-blocks */ +if ( ! function_exists( 'acf_get_field' ) ) { + require_once __DIR__ . '/php/acf-parent-cycle.php'; +} + /** * Access rules of the otter/v1/dynamic routes for visitors and editors. */ @@ -147,4 +151,39 @@ private function can_request( string $route, array $params ): bool { return true === call_user_func( $handler['permission_callback'], $request ); } + + /** + * The dynamic image does not put a protected post meta value into the path (otter-internals#320). + */ + public function test_media_server_blocks_protected_post_meta(): void { + $post_id = self::factory()->post->create(); + update_post_meta( $post_id, '_img', 'https://evil.example/x.png' ); + + $request = new WP_REST_Request( 'GET', '/otter/v1/dynamic' ); + $request->set_param( 'type', 'postMeta' ); + $request->set_param( 'meta', '_img' ); + $request->set_param( 'context', $post_id ); + + $path = ( new \ThemeIsle\OtterPro\Plugins\Dynamic_Content() )->evaluate_content_media_server( 'fallback.jpg', $request ); + + $this->assertSame( 'fallback.jpg', $path ); + } + + /** + * The dynamic image ACF branch does not put a protected field value into the path (otter-internals#320). + */ + public function test_media_server_blocks_protected_acf_field(): void { + $GLOBALS['otter_test_acf_fields'] = array( 'field_secret' => array( 'name' => '_secret', 'value' => 'https://evil.example/x.png' ) ); + + $request = new WP_REST_Request( 'GET', '/otter/v1/dynamic' ); + $request->set_param( 'type', 'acf' ); + $request->set_param( 'meta', 'field_secret' ); + $request->set_param( 'context', self::factory()->post->create() ); + + $path = ( new \ThemeIsle\OtterPro\Plugins\Dynamic_Content() )->evaluate_content_media_server( 'fallback.jpg', $request ); + + $this->assertStringNotContainsString( 'evil.example', $path ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } } diff --git a/tests/test-dynamic-content.php b/tests/test-dynamic-content.php index d3c2bddcc..fefcf64e4 100644 --- a/tests/test-dynamic-content.php +++ b/tests/test-dynamic-content.php @@ -2710,4 +2710,302 @@ function ( int $errno, string $errstr ) use ( &$captured ): bool { $this->assertSame( 'Example
', $result, $label ); } } + + /** + * The ACF dynamic tag does not read a protected meta key (otter-internals#320). + */ + public function test_get_acf_blocks_a_protected_meta_key() { + $GLOBALS['otter_test_acf_fields'] = array( '_secret' => array( 'value' => 'ACF Secret' ) ); + + $result = $this->dynamic_content_pro->get_acf( array( + 'metaKey' => '_secret', + 'context' => $this->post_id, + 'default' => 'KEPT', + ) ); + + $this->assertSame( 'KEPT', $result ); + $this->assertStringNotContainsString( 'ACF Secret', $result ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + /** + * An ACF field key that resolves to a protected name is blocked too. + */ + public function test_get_acf_blocks_an_acf_field_with_a_protected_name() { + $GLOBALS['otter_test_acf_fields'] = array( 'field_x' => array( 'name' => '_secret', 'value' => 'ACF Secret' ) ); + + $result = $this->dynamic_content_pro->get_acf( array( + 'metaKey' => 'field_x', + 'context' => $this->post_id, + 'default' => 'KEPT', + ) ); + + $this->assertSame( 'KEPT', $result ); + $this->assertStringNotContainsString( 'ACF Secret', $result ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + /** + * The dynamic image does not put a protected meta value into the src. + */ + public function test_media_content_blocks_protected_post_meta() { + update_post_meta( $this->post_id, '_img', 'https://evil.example/x.png' ); + + $result = $this->dynamic_content_pro->evaluate_content_media_content( 'ORIGINAL', array( + 'type' => 'postMeta', + 'meta' => '_img', + 'context' => $this->post_id, + ) ); + + $this->assertSame( 'ORIGINAL', $result ); + } + + /** + * A sub-field under a protected ACF repeater is blocked through the parent chain. + */ + public function test_get_acf_blocks_a_sub_field_under_a_protected_repeater() { + $GLOBALS['otter_test_acf_fields'] = array( + 'field_leaf' => array( 'key' => 'field_leaf', 'name' => 'leaf', 'parent' => 'field_repeater', 'value' => 'ACF Secret' ), + 'field_repeater' => array( 'key' => 'field_repeater', 'name' => '_secret', 'parent' => 0 ), + ); + + $result = $this->dynamic_content_pro->get_acf( array( + 'metaKey' => 'field_leaf', + 'context' => $this->post_id, + 'default' => 'KEPT', + ) ); + + $this->assertSame( 'KEPT', $result ); + $this->assertStringNotContainsString( 'ACF Secret', $result ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + /** + * The Posts Grid ACF custom field does not print a protected field (otter-internals#320). + */ + public function test_posts_acf_integration_blocks_a_protected_field() { + $GLOBALS['otter_test_acf_fields'] = array( + 'field_secret' => array( 'name' => '_secret', 'value' => 'ACF Secret Value' ), + ); + + $integration = new \ThemeIsle\OtterPro\Plugins\Posts_ACF_Integration(); + $attributes = array( + 'customMetas' => array( + array( 'id' => 'custom_1', 'field' => 'field_secret', 'display' => true ), + ), + ); + + $result = $integration->render_acf_fields( 'BASE', $attributes, $this->post_id, 'custom_1' ); + + $this->assertSame( 'BASE', $result ); + $this->assertStringNotContainsString( 'ACF Secret Value', $result ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + /** + * The dynamic image ACF branch does not surface a protected field value. + */ + public function test_media_content_blocks_protected_acf_field() { + $GLOBALS['otter_test_acf_fields'] = array( 'field_secret' => array( 'name' => '_secret', 'value' => 'https://evil.example/x.png' ) ); + + $result = $this->dynamic_content_pro->evaluate_content_media_content( 'ORIGINAL', array( + 'type' => 'acf', + 'meta' => 'field_secret', + 'context' => $this->post_id, + ) ); + + $flat = is_array( $result ) ? implode( ' ', $result ) : (string) $result; + $this->assertStringNotContainsString( 'evil.example', $flat ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + /** + * is_protected_acf_field() terminates on a cyclic parent chain. + */ + public function test_is_protected_acf_field_handles_a_cyclic_parent_chain() { + $GLOBALS['otter_test_acf_fields'] = $this->cyclic_acf_fields(); + + $this->assertFalse( \ThemeIsle\OtterPro\Plugins\Dynamic_Content::is_protected_acf_field( 10 ) ); + + unset( $GLOBALS['otter_test_acf_fields'] ); + } + + /** + * A key another plugin protects via the is_protected_meta filter (without a `_` prefix) is blocked too. + */ + public function test_blocks_a_filter_protected_meta_key() { + update_post_meta( $this->post_id, 'snap_x', 'https://evil.example/x.png' ); + + $filter = static function ( $protected, $meta_key ) { + return 'snap_x' === $meta_key ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 2 ); + + $result = $this->dynamic_content_pro->evaluate_content_media_content( 'ORIGINAL', array( + 'type' => 'postMeta', + 'meta' => 'snap_x', + 'context' => $this->post_id, + ) ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertSame( 'ORIGINAL', $result ); + } + + /** + * Author meta honors a filter that protects a key only for user meta. + */ + public function test_author_meta_blocks_a_user_only_filter_protected_key() { + $author_id = self::factory()->user->create(); + $post_id = self::factory()->post->create( array( 'post_author' => $author_id ) ); + update_user_meta( $author_id, 'nick_x', 'secret-nick' ); + + $filter = static function ( $protected, $meta_key, $meta_type ) { + return ( 'nick_x' === $meta_key && 'user' === $meta_type ) ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 3 ); + + $result = $this->dynamic_content_pro->get_author_meta( array( + 'metaKey' => 'nick_x', + 'context' => $post_id, + 'default' => 'DEF', + ) ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertSame( 'DEF', $result ); + $this->assertStringNotContainsString( 'secret-nick', $result ); + } + + /** + * Author meta blocks the core user-field aliases, so `pass` cannot return the password hash. + */ + public function test_author_meta_blocks_core_user_aliases() { + global $wpdb; + + $author_id = self::factory()->user->create(); + $post_id = self::factory()->post->create( array( 'post_author' => $author_id ) ); + + // Seed the reset key so the activation_key alias has a value to leak without the fix. + $wpdb->update( $wpdb->users, array( 'user_activation_key' => 'secret-key' ), array( 'ID' => $author_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery + clean_user_cache( $author_id ); + + foreach ( array( 'pass', 'activation_key' ) as $alias ) { + $result = $this->dynamic_content_pro->get_author_meta( array( + 'metaKey' => $alias, + 'context' => $post_id, + 'default' => 'DEF', + ) ); + + $this->assertSame( 'DEF', $result, "The '$alias' alias must be blocked." ); + } + } + + /** + * Author meta blocks the legacy user_level key, so a visitor cannot read the author's capability level. + */ + public function test_author_meta_blocks_user_level() { + $author_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $post_id = self::factory()->post->create( array( 'post_author' => $author_id ) ); + + $result = $this->dynamic_content_pro->get_author_meta( array( + 'metaKey' => 'user_level', + 'context' => $post_id, + 'default' => 'DEF', + ) ); + + $this->assertSame( 'DEF', $result ); + } + + /** + * A post field literally named user_level is not user meta, so it stays readable. + */ + public function test_post_meta_allows_a_field_named_user_level() { + $post_id = self::factory()->post->create(); + update_post_meta( $post_id, 'user_level', 'Beginner' ); + + $result = $this->dynamic_content_pro->get_post_meta( array( + 'metaKey' => 'user_level', + 'context' => $post_id, + 'default' => 'DEF', + ) ); + + $this->assertSame( 'Beginner', $result ); + } + + /** + * Logged-in user meta honors a filter that protects a key only for user meta. + */ + public function test_loggedin_meta_blocks_a_user_only_filter_protected_key() { + $uid = self::factory()->user->create(); + wp_set_current_user( $uid ); + update_user_meta( $uid, 'nick_x', 'secret-nick' ); + + $filter = static function ( $protected, $meta_key, $meta_type ) { + return ( 'nick_x' === $meta_key && 'user' === $meta_type ) ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 3 ); + + $result = $this->dynamic_content_pro->get_loggedin_meta( array( + 'metaKey' => 'nick_x', + 'default' => 'DEF', + ) ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertSame( 'DEF', $result ); + } + + /** + * Author meta resolves a back-compat field to its real meta key before the filter check. + */ + public function test_author_meta_blocks_a_filter_protected_back_compat_field() { + $author_id = self::factory()->user->create(); + $post_id = self::factory()->post->create( array( 'post_author' => $author_id ) ); + update_user_meta( $author_id, 'first_name', 'secret-first' ); + + $filter = static function ( $protected, $meta_key, $meta_type ) { + return ( 'first_name' === $meta_key && 'user' === $meta_type ) ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 3 ); + + $result = $this->dynamic_content_pro->get_author_meta( array( + 'metaKey' => 'user_firstname', + 'context' => $post_id, + 'default' => 'DEF', + ) ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertSame( 'DEF', $result ); + $this->assertStringNotContainsString( 'secret-first', $result ); + } + + /** + * A user meta key named like a core alias is still checked verbatim against the filter. + */ + public function test_loggedin_meta_checks_an_alias_named_key_verbatim() { + $uid = self::factory()->user->create(); + wp_set_current_user( $uid ); + update_user_meta( $uid, 'email', 'secret-mail' ); + + $filter = static function ( $protected, $meta_key, $meta_type ) { + return ( 'email' === $meta_key && 'user' === $meta_type ) ? true : $protected; + }; + add_filter( 'is_protected_meta', $filter, 10, 3 ); + + $result = $this->dynamic_content_pro->get_loggedin_meta( array( + 'metaKey' => 'email', + 'default' => 'DEF', + ) ); + + remove_filter( 'is_protected_meta', $filter, 10 ); + + $this->assertSame( 'DEF', $result ); + } }