Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions inc/plugins/class-atomic-wind-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/otter-pro/inc/plugins/class-block-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
return false;
}

if ( 'post' === $type ) {
$id = get_the_ID();
$meta = get_post_meta( $id, $condition['meta_key'], true );
Expand Down
106 changes: 96 additions & 10 deletions plugins/otter-pro/inc/plugins/class-dynamic-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +212 to +215

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correctness ✓ Verified in the sandbox The new user_level block also hides raw user meta named user_level. This assumes a site runs a plugin that stores that raw key, such as VIPer (viper.php:111-125). get_loggedin_meta() (line 598) and the loggedInUserMeta condition (class-block-conditions.php:137) read get_user_meta() with the raw key. WordPress does not map that key to {prefix}user_level there. So a subscriber's own user_level = gold now renders the default, and the condition is false. Before this commit it rendered gold and matched. Block user_level only in get_author_meta(), which reads through WP_User::__get(). Add tests for get_loggedin_meta() and the condition with raw user_level meta.


// 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;
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

/**
Expand Down Expand Up @@ -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' ) ) {
Expand Down Expand Up @@ -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' ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security The author getter still misses the WordPress back-compat user keys. This assumes a plugin protects a user meta key such as first_name with an is_protected_meta filter. get_the_author_meta() reads $authordata->user_firstname, and WP_User::__get() maps it to first_name (class-wp-user.php:137-144,298-302). The same holds for user_lastname, user_description and user_level. Line 539 checks only user_firstname, so the protected value is returned. Resolve these keys to their real meta keys before the check. Treat the key as protected when the raw or the resolved key is protected. Add an author-meta test with a filter on first_name that expects the default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still open at 697b6ae, and now reproduced. It assumes a site protects populated first_name user meta through is_protected_meta. With such a filter, get_author_meta() with metaKey user_firstname returned 'secret-first'. The filter saw only user_firstname|user (R4). Map user_firstname, user_lastname, user_description and user_level to their real meta keys before the check. Treat the key as protected when the raw or mapped key is protected. Add the first_name filter test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0a4ffcc. is_protected_meta_key() now resolves the WP_User back-compat fields (user_firstname→first_name, user_lastname→last_name, user_description→description) to their real meta keys before the check, so a filter on first_name is honored for metaKey=user_firstname. Added test_author_meta_blocks_a_filter_protected_back_compat_field.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user_firstname case is fixed. With the 697b6ae helper, the new test fails with +'secret-first' (R4). user_level is still open, though the original comment named it. This assumes a site protects that populated user meta target through is_protected_meta. WordPress maps user_level to $wpdb->prefix . 'user_level' (class-wp-user.php:141). It also maps {prefix}usersettings and {prefix}usersettingstime (:142-143). A filter protected wptests_user_level. get_author_meta() with user_level still returned '10' (R7). Add these three keys to the map, built with $wpdb->prefix. Add a user_level filter test that expects the default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still open in part at cf11b5e. The user_level case now returns the default. The new test fails with the 0a4ffcc helper (-'DEF' +'10', R3). The two settings aliases from my last reply are still missing. A filter protected wptests_user-settings. get_author_meta() with wptests_usersettings still returned 'secret-settings' (R4). Resolve {prefix}usersettings and {prefix}usersettingstime to their real keys with $wpdb->prefix before the check. Add a filter test for one of them.

$meta = get_the_author_meta( esc_html( $data['metaKey'] ), intval( get_post_field( 'post_author', $data['context'] ) ) );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}

Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 ) ) {
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
$value = get_post_meta( $context, $meta, true );

if ( ! empty( $value ) ) {
Expand All @@ -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
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<div class="o-posts-custom-field">';
Expand Down
33 changes: 33 additions & 0 deletions tests/php/acf-parent-cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,37 @@ function acf_get_field( $selector ) {
return isset( $GLOBALS['otter_test_acf_fields'][ $selector ] ) ? $GLOBALS['otter_test_acf_fields'][ $selector ] : false;
}

if ( ! function_exists( 'get_field_object' ) ) {
/**
* Return a field object from the test-controlled ACF map.
*
* @param int|string $selector Field selector.
* @param mixed $post_id Unused.
* @param bool $format Unused.
* @return array|false
*/
function get_field_object( $selector, $post_id = false, $format = true ) {
return isset( $GLOBALS['otter_test_acf_fields'][ $selector ] ) ? $GLOBALS['otter_test_acf_fields'][ $selector ] : false;
}
}

if ( ! function_exists( 'get_field' ) ) {
/**
* Return a field value from the test-controlled ACF map.
*
* @param int|string $selector Field selector.
* @param mixed $post_id Unused.
* @param bool $format Unused.
* @return mixed
*/
function get_field( $selector, $post_id = false, $format = true ) {
return isset( $GLOBALS['otter_test_acf_fields'][ $selector ]['value'] ) ? $GLOBALS['otter_test_acf_fields'][ $selector ]['value'] : null;
}
}

// Minimal marker so `class_exists( 'ACF' )`-gated branches run under tests; no behavior of its own.
if ( ! class_exists( 'ACF' ) ) {
class ACF {} // phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound
}

define( 'OTTER_TEST_ACF_GET_FIELD_STUB', true );
67 changes: 67 additions & 0 deletions tests/test-atomic-wind-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

require_once dirname( __FILE__ ) . '/php/migration-test-helper.php';

if ( ! function_exists( 'get_field' ) ) {
require_once __DIR__ . '/php/acf-parent-cycle.php';
}

/**
* Atomic Wind Blocks test case.
*/
Expand Down Expand Up @@ -2598,6 +2602,69 @@ public function test_enqueue_base_css_registers_frontend_style_without_enqueuing
);
}

// -------------------------------------------------------
// render_post_fields — protected custom field (otter-internals#320)
// -------------------------------------------------------

public function test_custom_field_skips_protected_meta() {
update_post_meta( $this->post_id, '_protected', 'Protected 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' => '_protected' ) );
$content = '<p class="wp-block">Placeholder</p>';

$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 = '<p class="wp-block">Placeholder</p>';

$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 = '<p class="wp-block">Placeholder</p>';

$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)
// -------------------------------------------------------
Expand Down
Loading
Loading