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

[Blueprints] Reload after autologin to set login cookies during boot #1914

Merged
merged 13 commits into from
Oct 22, 2024

Conversation

bgrgicak
Copy link
Collaborator

@bgrgicak bgrgicak commented Oct 16, 2024

This PR follows up on #1856 and forces a reload after autologin to ensure the user is logged in correctly during boot.

Autologin will now only run during HTTP requests. If the code was executed using the php.run() method it's a CLI call, so we can't login because there are no cookies which are required for logins to work.

Before this PR, WordPress auth cookies weren't set in the $_COOKIES global after boot. Adding them required a refresh.

Not having a correct $_COOKIES global meant that some features like nonce generated on boot was incorrect.
If that nonce was used to make a request, it would fail because of the nonce.

Testing Instructions (or ideally a Blueprint)

  • CI

@bgrgicak bgrgicak self-assigned this Oct 16, 2024
@bgrgicak bgrgicak requested a review from akirk October 16, 2024 18:23
@bgrgicak bgrgicak added [Type] Bug An existing feature does not function as intended [Package][@wp-playground] Blueprints labels Oct 16, 2024
Copy link
Member

@akirk akirk left a comment

Choose a reason for hiding this comment

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

There is a syntax error that makes it fatal.

packages/playground/wordpress/src/index.ts Outdated Show resolved Hide resolved
Co-authored-by: Alex Kirk <akirk@users.noreply.github.com>
@bgrgicak bgrgicak marked this pull request as draft October 16, 2024 21:23
@bgrgicak bgrgicak marked this pull request as ready for review October 17, 2024 06:56
* The redirect should only run if the current PHP request is
* a HTTP request. If it's a PHP run, there is nothing to reload.
*/
if (!empty($_SERVER['REQUEST_URI'])) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@adamziel I would love to get your thoughts on this.

If the current script execution is a PHP run and not an HTTP request, there is no way for us to redirect.
But, there is also no way for us to set the cookies correctly. Should we just skip autologin during PHP runs?
Is there a better way of detecting this then checking if the $_SERVER global is populated?

@bgrgicak bgrgicak force-pushed the fix/set-cookies-after-autologin branch from 200eb6e to 11d4345 Compare October 18, 2024 15:20
@bgrgicak bgrgicak marked this pull request as draft October 18, 2024 15:21
@bgrgicak
Copy link
Collaborator Author

Safari bug recap

In Safari the WordPress iframe stopped loading the WordPress page in this commit (and older). The network tab didn't show any scoped requests like /scope:123/wp-admin/.
Chrome worked as expected, and two requests were recorded during login: the first one a redirect (to add cookies), and the second a 200 that returned the page.

After some research, it turns out the issue was in the URL used in wp_redirect. The URL was relative and this broke Playground in Safari, after switching to a full URL it started working again.

@bgrgicak bgrgicak marked this pull request as ready for review October 22, 2024 13:25
@bgrgicak bgrgicak merged commit 97f5811 into trunk Oct 22, 2024
9 checks passed
@bgrgicak bgrgicak deleted the fix/set-cookies-after-autologin branch October 22, 2024 13:50
bgrgicak added a commit that referenced this pull request Oct 22, 2024
Comment on lines +121 to +133
/**
* The redirect should only run if the current PHP request is
* a HTTP request. If it's a PHP CLI run, we can't login the user
* because logins require cookies which aren't available in the CLI.
*
* Currently all Playground requests use the "cli" SAPI name
* to ensure support for WP-CLI, so the best way to distinguish
* between a CLI run and an HTTP request is by checking if the
* $_SERVER['REQUEST_URI'] global is set.
*
* If $_SERVER['REQUEST_URI'] is not set, we assume it's a CLI run.
*/
if (empty($_SERVER['REQUEST_URI'])) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice comment!

@@ -131,27 +147,42 @@ export async function setupPlatformLevelMuPlugins(php: UniversalPHP) {
if (!$user) {
return;
}
/**
* This approach is described in a comment on
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️

Comment on lines +164 to +167
* Both WordPress home url and REQUEST_URI may include the site scope
* subdirectory.
* To prevent the redirect from including two scopes, we need to
* remove the scope from the REQUEST_URI if it's present.
Copy link
Collaborator

Choose a reason for hiding this comment

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

How can we remove this special case? WordPress sites installed in a subdirectory seem to support redirects just fine, e.g. https://w.org/playground. What are they doing differently than this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The home_url will always include the scope subdirectory.
REQUEST_URI might sometimes include it and other times not. I need to check why it's not consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's possible to make unscoped requests to PHP using the request handler.

For example this unit test will make an unscoped request to PHP.

We could update the test to include a scope, but it's also something a user might do, so I think it's better to cover that case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

$redirect_url = '/' . implode('/', array_slice($parts, 2));
}
wp_redirect(
home_url($redirect_url),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the home_url() call necessary here?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is about providing an absolute URL to Safari as @bgrgicak mentioned above. Can you add this info as a comment in a follow-up PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point @akirk, I can explain this in a comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package][@wp-playground] Blueprints [Type] Bug An existing feature does not function as intended
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants