WP Manifestindependent plugin directory
manifest / content / cmb2-field-post-search-ajax

CMB2 Field Type: Post Search Ajax WP Registry grade D+. High-severity findings · 2 findingsWP RegistryHigh-severity findings · 2 findingsOpen the reportD+

CMB2 field type to attach posts to each others.

★ 39stars
14kcomposer installs
Latestlatest · 9 years ago

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/alexis-magina/cmb2-field-post-search-ajax/archive/refs/heads/master.zip

On Packagist as alexis-magina/cmb2-field-post-search-ajax:

composer require alexis-magina/cmb2-field-post-search-ajax
No built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.

CMB2 custom field "post_search_ajax"

Custom field for CMB2 to attach posts to each others.

Same approach than CMB2 Attached Posts Field with Ajax request, multiple/single option, and different UI.

Installation

You can install this field type as you would a WordPress plugin:

  • Download the plugin
  • Place the plugin folder in your /wp-content/plugins/ directory
  • Activate the plugin in the Plugin dashboard

Composer Intallation:

composer require alexis-magina/cmb2-field-post-search-ajax:dev-master

Usage - Admin

Follow the example in example-field-setup.php for a demonstration.

Options :

  • limit (int, default = 1 : single selection) : limit the number of posts that can be selected
  • sortable (bool, default = false) : Allow selected posts to be sort
  • query_args (array) : setup the ajax search query : pass a wp_query args array.

Filter : (since 1.1.2) Ajax results can be filtered to customize returned text and posts values. Use filter "mag_cmb_post_search_ajax_result", for example :

function example_callback( $arr ) {
    // $arr['data'] : contains post_id
    // $arr['guid'] : contains admin edit post url
    // $arr['value'] : contains post title
    $arr['value'] = 'Custom string '.$arr['value'];
    return $arr;
}
add_filter( 'mag_cmb_post_search_ajax_result', 'example_callback' );

Usage - FrontEnd

You can retrieve the meta data using get_post_meta( get_the_ID(), 'your_field_id', true );

If field limit > 1, this will return an array of attached post IDs. If field limit == 1, this will return only the single attached post ID.

Screenshot

example

Changelog

1.0.0

  • Initial commit

1.1.0-sebask

  • Added a function which enables usage of CMB2 Field Post Search Ajax from a location other then the Wordpress Plugins folder.

1.1.1-sebask

  • Fixed a minor bug which caused the use of an undefined constant.

1.1.2

  • Fixed issue #2 : no way to delete value for fields with limit = 1
  • Added a class exists check (issue #3)
  • Added a filter "mag_cmb_post_search_ajax_result" to allow customize ajax results
  • Fixed a minor bug of encoding chars in autocomplete results

1.1.3

  • Add Support for user queries
  • Fixed issue #11 : sorting problem
  • Fixed conflict with ui-autocomplete

1.1.4-sebask

  • Fixed issue #14: safe mode for jquery-ui-autocomplete and devbridge/jQuery-Autocomplete compatibility.

1.1.5

  • Fixed issue #17 : Undefined index if the field is empty

Releases

2 releases.

Tag
Published
Latest latest
Mar 29, 2017 9 years ago
Mar 27, 2017 9 years ago

These releases are tags only. The author does not attach a packaged zip, so there are no download counts to report.

D+ grade

Security

CMB2 Field Type: Post Search Ajax 1.1.4-sebask · audited by WP Registry

High-severity findings.

1 high 1 medium
Audited release
1.1.4-sebask
Findings
2
Worst severity
high
Content hash
011f55cc6341dab503a2d838…

Findings

  • high Contributor+ stored XSS in multi-select results via unsanitized _results meta

    xss_stored

    cmb2_sanitize_post_search_ajax copies $fid.'_results' from CMB2 data_to_save (raw POST) into the field value with no absint/is_numeric/kses. For limit>1, render() concatenates each $val into an admin HTML attribute: value="'.$val.'" plus unescaped $title/$guid. A Contributor who can save a post that includes this field (the bundled example registers it on post with limit 10) can POST fieldid_results[]=1"><script>alert(document.cookie)</script> (or an img onerror). That string is stored in post meta and echoed as real document HTML — not innerHTML — when an Editor/Administrator opens the metabox, so <script> executes in wp-admin (session/nonce theft, actions as the victim). WordPress does not kses post meta; CMB2's Types::input() escaping is bypassed on this manual <li> path. Limit==1 uses CMB2 input() (typically esc_attr) so the same dirty meta is display-escaped in admin but still stored.

    cmb-field-post-search-ajax.php:46-64,105-114

    Recommendation

    In sanitize(), map the saved value through absint (or array_map('absint')) and drop non-positive IDs. In render(), output $val with esc_attr, $guid with esc_url, $title with esc_html, and $field_name with esc_attr. Do the same in js/mag-post-search-ajax.js onSelect (it currently string-concatenates suggestion.value/guid into jQuery.append HTML).

  • medium Authenticated AJAX search runs attacker-controlled WP_Query/WP_User_Query with no capability check

    missing_capability

    wp_ajax_cmb_post_search_ajax_get_results is registered for every logged-in user. The only gate is wp_verify_nonce on POST psacheck; there is no current_user_can('edit_posts'), list_users, or read_private_posts. query_args is json_decode(stripslashes(htmlspecialchars_decode($_POST['query_args']))) and passed as the full args array to WP_Query or WP_User_Query; object=user is also client-controlled. mag-post-search-ajax.js sends the data-queryargs attribute, so any caller with a nonce can replace the field's intended query. Core WP_Query only applies author/read_private restrictions when perm=readable/editable is set; an explicit post_status of draft, private, any, or a WooCommerce wc-* status is interpolated without those checks. Observable JSON: each hit's get_the_title()/display_name, ID, and get_edit_post_link()/get_edit_user_link(). A Contributor (or any role that can load a screen that localizes psa.nonce) can enumerate other users' drafts/private posts, shop_order-style CPTs, and the user directory (search_columns=user_email confirms addresses). Not unauthenticated (no nopriv). posts_per_page=-1 is an additional cheap DoS.

    cmb-field-post-search-ajax.php:36,162-201

    Recommendation

    Require current_user_can('edit_posts') (or the capability of the CMB2 object) after the nonce check. Do not trust client query_args: keep allowed WP_Query/WP_User_Query args in a server-side field config (or a signed token) and only apply a sanitized search string. Force perm=readable, cap post_status/post_type to the field's allow-list, reject object=user unless the current user has list_users, and cap posts_per_page.

WP Registry hashes the installable build and reports on that exact bytes-for-bytes copy. Embargoed findings are withheld until they are disclosed, so a clean verdict means nothing public is outstanding. WP Manifest does not audit code itself.