Skip to content
Open
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
29 changes: 29 additions & 0 deletions src/wp-includes/class-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class WP {
*/
public $private_query_vars = array( 'offset', 'posts_per_page', 'posts_per_archive_page', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'tag_id', 'post_mime_type', 'perm', 'comments_per_page', 'post__in', 'post__not_in', 'post_parent', 'post_parent__in', 'post_parent__not_in', 'title', 'fields' );

/**
* Public query variables that only ever hold a single value.
*
* These are query variables that will hit a fatal error if they are given an
* array instead of a single value.
*
* @since 7.1.0
* @var string[]
*/
private $single_value_query_vars = array( 'feed' );

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.

I'd make this public to allow theme and plugin authors to do either of two things:

  • Add to the list, eg an SEO plugin may wish to add their sitemap query var to the list
  • Remove from the list, WP_Query is pretty flexible and a plugin might make use of the various filters to modified valid query vars and modify the SQL query accordingly


/**
* Extra query variables set by the user.
*
Expand Down Expand Up @@ -353,6 +364,24 @@ public function parse_request( $extra_query_vars = '' ) {
}
}

/*
* Check against the list of variables that will fatal if given an array instead
* of a single value. If we hit that condition, we end processing the request
* immediately.
*/
foreach ( $this->single_value_query_vars as $wpvar ) {
if (
isset( $this->query_vars[ $wpvar ] )
&& ! is_scalar( $this->query_vars[ $wpvar ] )
) {
wp_die(
__( 'Invalid value for a query variable.' ),
__( 'Error, this request could not be processed.' ),
400
);
Comment on lines +377 to +381
Comment on lines +377 to +381

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.

What do you think of triggering a 404 if the URL has been messed around with?

Arguably /?feed[]=rss&feed[]=atom doesn't exist and this would trigger the website's theme appropriate 404 page rather than the generically designed wp_die() error messaging.

Suggested change
wp_die(
__( 'Invalid value for a query variable.' ),
__( 'Error, this request could not be processed.' ),
400
);
$this->query_vars['error'] = 404;
unset( $this->query_vars[ $wpvar ] );

}
}

// Convert urldecoded spaces back into '+'.
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) {
if ( $t->query_var && isset( $this->query_vars[ $t->query_var ] ) ) {
Expand Down
Loading