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

Fix: LegacyProxy::get_instance_of throws when trying to get an instance of a non-Woo namespaced class #45178

Merged
merged 2 commits into from Feb 28, 2024

Conversation

Konamiman
Copy link
Contributor

@Konamiman Konamiman commented Feb 28, 2024

Changes proposed in this Pull Request:

The get_instance_of method of the LegacyProxy class isn't intended to retrieve instances of classes that live in the src directory (the dependency injection container should be used for these instead), and will throw an exception if that is attempted. However, in order to check if it should throw it just checks if the class name has a namespace; this will cause a false positive for namespaced classes defined outside of WooCommerce.

This pull request fixes this: the method will throw only if the full class name starts with Automattic\WooCommerce\.

How to test the changes in this Pull Request:

The bug was discovered after merging #44833: there are plugins that register namespaced REST API controllers and cause the exception. Thus for testing the fix we'll use the following code:

<?php

namespace Foo\Bar;

class Fizz {
	public function register_routes() {
		register_rest_route( 'fizz', '/buzz', [[
			'methods' => \WP_REST_Server::READABLE,
			'callback'  => fn($request) => 'Buzz!',
			'permission_callback' => fn($request) => true,
		]]);
	}
}

add_filter('woocommerce_rest_api_get_rest_namespaces', function($classes) {
	$classes['fizz'] = [\Foo\Bar\Fizz::class];
	return $classes;
});

To setup this code:

  • If you are testing locally: put the code in a new fizz.php file in the root directory of the plugin, and add the following at the end of the woocommerce.php file: include __DIR__ . '/fizz.php';
  • If you are testing using a remote server: install the Code Snippets plugin and add the code in a new snippet.

Once the code is prepared, run the following in the command line (or perform the equivalent request with Postman or a similar tool):

curl <site root>/wp-json/fizz/buzz
  • Without the fix this will result in an error, and the server logs will show the following error: GET /wp-json/fizz/buzz - Uncaught Exception: The LegacyProxy class is not intended for getting instances of classes in the src directory, please use init method injection or the instance of Automattic\WooCommerce\Vendor\Psr\Container\ContainerInterface for that.
  • With the fix the response will be the string "Buzz!".

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Fix LegacyProxy::get_instance_of throwing an error when trying to get an instance of a non-Woo namespaced class.

Comment

of a class with a namespace that doesn't start with 'Automattic\WooCommerce'.
@Konamiman Konamiman self-assigned this Feb 28, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Feb 28, 2024
Copy link
Contributor

Hi @lsinger,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

github-actions bot commented Feb 28, 2024

Test Results Summary

Commit SHA: 9426278

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 37s
E2E Tests275007203476m 11s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@Konamiman Konamiman closed this Feb 28, 2024
@Konamiman Konamiman reopened this Feb 28, 2024
Copy link
Contributor

@lsinger lsinger left a comment

Choose a reason for hiding this comment

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

This looks good and tests as intended. The exception is now covered by unit tests in both positive and negative cases: a test fails if it's thrown when it shouldn't be, and a test fails if it's not thrown when it should be. LGTM! 🚢

@lsinger
Copy link
Contributor

lsinger commented Feb 28, 2024

@Konamiman I edited your PR description to a) generate a changelog file and b) address an omission in the fizz.php test code that was unrelated to the problem that was solved but generated an error on the site frontend. The latter I did just to make sure that if we look at this again in the future, the test code doesn't throw any unexpected errors. Because I made those edits I'm going to let you check them and merge the PR yourself instead of merging it directly.

@Konamiman Konamiman merged commit bfa3ffb into trunk Feb 28, 2024
42 checks passed
@Konamiman Konamiman deleted the fix_legacy_proxy_get_instance_of branch February 28, 2024 15:31
@github-actions github-actions bot added this to the 8.8.0 milestone Feb 28, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Feb 28, 2024
@alopezari alopezari added needs: internal testing Indicates if the PR requires further testing conducted by Solaris status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Feb 29, 2024
@Stojdza Stojdza modified the milestones: 8.8.0, 8.7.0 Feb 29, 2024
github-actions bot pushed a commit that referenced this pull request Feb 29, 2024
Stojdza pushed a commit that referenced this pull request Feb 29, 2024
* Fix: LegacyProxy::get_instance_of throws when trying to get an instance of a non-Woo namespaced class (#45178)

* Prep for cherry pick 45178

---------

Co-authored-by: Néstor Soriano <konamiman@konamiman.com>
Co-authored-by: WooCommerce Bot <no-reply@woo.com>
Konamiman added a commit that referenced this pull request Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants