Skip to content
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

Prevent empty query var from causing false positive endpoint detection #5804

Merged
merged 2 commits into from
Jan 25, 2021
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
7 changes: 5 additions & 2 deletions src/PairedRouting.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,16 @@ public function detect_endpoint_in_environment() {
continue;
}

$paired_url_structure = $this->get_paired_url_structure();

// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$old_path = wp_unslash( $_SERVER[ $var_name ] ); // Because of wp_magic_quotes().
$new_path = $this->get_paired_url_structure()->remove_endpoint( $old_path );
if ( $old_path === $new_path ) {
if ( ! $paired_url_structure->has_endpoint( $old_path ) ) {
continue;
}

$new_path = $paired_url_structure->remove_endpoint( $old_path );

$this->suspended_environment_variables[ $var_name ] = [ $old_path, $new_path ];

$this->did_request_endpoint = true;
Expand Down
10 changes: 10 additions & 0 deletions tests/php/src/PairedRoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ public function get_data_for_test_paired_requests() {
'/',
false,
],
'url_with_empty_query_var' => [
AMP_Theme_Support::READER_MODE_SLUG,
Option::PAIRED_URL_STRUCTURE_QUERY_VAR,
'/?foo=',
false,
],
'path_suffix_transitional_mode_amp' => [
AMP_Theme_Support::TRANSITIONAL_MODE_SLUG,
Option::PAIRED_URL_STRUCTURE_PATH_SUFFIX,
Expand Down Expand Up @@ -370,6 +376,10 @@ public function test_detect_endpoint_in_environment() {
$_SERVER['REQUEST_URI'] = $this->instance->add_endpoint( '/' );
$this->instance->detect_endpoint_in_environment();
$this->assertTrue( $this->get_private_property( $this->instance, 'did_request_endpoint' ) );

$_SERVER['REQUEST_URI'] = '/?foo=';
$this->instance->detect_endpoint_in_environment();
$this->assertFalse( $this->get_private_property( $this->instance, 'did_request_endpoint' ) );
}

/** @return array */
Expand Down