-
Notifications
You must be signed in to change notification settings - Fork 270
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
Incorrect user capabilities result in unexpected 401 responses from the REST API #1611
Comments
I'm curious whether this has anything to do with #1539 and will grab this to test after the other PR is merged. |
@brandonpayton thank you for the support! When I investigated briefly, the permissions check unexpectedly returned |
@dcalhoun, it looks like the auth is not working for some reason. I'm getting the same message, and if I instrument the permissions check to error_log Hoping to look into this a bit more later. |
@dcalhoun After some debugging, it looks like Playground is auto-sending cookies and interfering with application password auth. Here's how: Playground is auto-sending login cookies here: wordpress-playground/packages/php-wasm/universal/src/lib/php-request-handler.ts Lines 485 to 496 in 2b1f0b6
When Playground auto-adds cookies to these requests that contain add_filter( 'determine_current_user', 'wp_validate_auth_cookie' );
add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 );
add_filter( 'determine_current_user', 'wp_validate_application_password', 20 ); Then when if ( null === $nonce ) {
// No nonce at all, so act as if it's an unauthenticated request.
wp_set_current_user( 0 );
return true;
} This is an interesting one. Maybe it would make sense for the PHPRequestHandler to omit cookies if a request appears to contain another auth-related header. But cookies aren't just for auth. So it seems presumptuous to remove cookies when we don't know the user's intent. We could filter out specific WP cookies, but it still might be solving a problem caused by a bit of magic using a bit more magic. Read and understand our cookie store code better would probably help generate other ideas. |
I wonder if it's time to get rid of the internal cookie store and scope all cookies to the specific scoped WordPress site path |
@adamziel, this is a great idea. I think there is a rare possibility of the following:
Maybe we should add site ID to the scope to avoid that rare possibility. |
I think this may turn out to be tricky because of restrictions on using It would be nice if set cookies could be treated as HttpOnly, but it seems like we may have to set cookies with JS. |
If we want to get rid of the cookie store, it may be necessary to do something like: This is not great because the cookies would not be set at the exact time as they would be if they were actually included as Alternately, to solve this GH issue, we could keep the cookie store and do the following:
cc @adamziel |
Because we cannot directly relay I think a good solution may be to support filter callbacks for requests and responses within PHPRequestHandler so the bulk of the logic can be maintained in Then, as mentioned above, we can address this bug by always setting a cookie in the web client and letting its presence or absence tell the worker whether to relay cookies from the cookie store. |
On second thought, we should just be able to handle this in the service worker with a per-scope cookie store. AFAICT, this would require no API changes. I am working on this. |
As I work through this, one awkwardness with handling this in the service worker is that there is no access to |
It looks like The two things I still want to figure out are: |
Scratch that. We should just respect the So I plan to:
That should keep Playground running as expected in the browser and allow Playground CLI to behave more like a normal web server (which does not apply the same cookies for all clients). |
When querying the REST API with successful authentication, endpoints return unexpected 401 response statuses. It appears the user's capabilities are not accurately portrayed in the context of WordPress Playground.
I discovered this issue while using
curl
to query a site running with the Studio app, but I also reproduced the issue usingwp-now
. Contrastingly, performing the following steps with a site running withwp-env
result in the expected outcome.Steps to Reproduce
npx @wp-now/wp-now start
~/.wp-now/wordpress-versions/latest/wp-config.php
to set the environment tolocal
:define('WP_ENVIRONMENT_TYPE', 'local');
admin
user).echo -n 'admin:<application_password>' | base64 | pbcopy
curl
request for the block types endpoint:curl --header "Authorization: Basic <base64_string>" -L http://localhost:<port>/?rest_route=/wp/v2/block-types
Expected Outcome
A 200 response containing the site's block types is returned.
Actual Outcome
A 401 response is returned:
The text was updated successfully, but these errors were encountered: