Repository navigation
fix(security): block protected post meta in dynamic content, conditions and Atomic Wind #3195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
51bd6ce
a0cb17e
cb09d21
02d81e6
034bf18
d5fdf56
697b6ae
0a4ffcc
1a43333
cf11b5e
ee8616d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| // 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; | ||
| } | ||
|
pirate-bot marked this conversation as resolved.
Resolved
|
||
|
|
||
| /** | ||
|
|
@@ -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' ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 0a4ffcc.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still open in part at cf11b5e. The |
||
| $meta = get_the_author_meta( esc_html( $data['metaKey'] ), intval( get_post_field( 'post_author', $data['context'] ) ) ); | ||
|
pirate-bot marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
|
|
@@ -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 ) ) { | ||
|
pirate-bot marked this conversation as resolved.
Resolved
pirate-bot marked this conversation as resolved.
Resolved
|
||
| $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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.