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

Enhance wording of "Invalid nonce" messaging #3098

Closed
aaemnnosttv opened this issue Apr 6, 2021 · 15 comments
Closed

Enhance wording of "Invalid nonce" messaging #3098

aaemnnosttv opened this issue Apr 6, 2021 · 15 comments
Labels
P2 Low priority QA: Eng Requires specialized QA by an engineer Rollover Issues which role over to the next sprint Type: Enhancement Improvement of an existing feature
Milestone

Comments

@aaemnnosttv
Copy link
Collaborator

aaemnnosttv commented Apr 6, 2021

Feature Description

There are a few cases in the plugin where we currently wp_die with "Invalid nonce". This is not the most useful message for a case that is likely uncommon but still fairly possible for a user to encounter. As such, we should re-evaluate the messaging used in each case and potentially add a retry mechanism for better UX such as that discussed in #2935 (comment).


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • A new private method Authentication::invalid_nonce_error( $action ) should be introduced:
    • It should be inspired by the wp_nonce_ays function of WordPress core, in fact its else case can be used as is for the "default" case, so the new method should probably actually call wp_nonce_ays in certain situations.
    • Instead of the log-out condition that wp_nonce_ays has, we should have special behavior for proxy-related invalid nonces: If the nonce $action starts with googlesitekit_proxy_, instead of using the referer like WordPress core does by default, our "link back" URL should be the googlesitekit-splash screen. The messaging should remain the same as in WordPress core.
  • Everywhere Site Kit currently dies with an "Invalid nonce." error message, Site Kit should instead rely on the new method introduced above.

Implementation Brief

Add new invalid nonce error method

Add a new private method, Authentication::invalid_nonce_error( $action ).

  • The $action parameter here will be a string describing the nonce action.
  • The idea is that the function will test $action in order to deal with certain edge cases, but will fall through to the wp_nonce_ays function in WP core as the default behavior if none apply.

Here we need to add a check to determine whether the invalid nonce is proxy-related.

  • Test if the $action parameter starts with the substring googlesitekit_proxy_.
  • If so, return the same message and linked text as wp_nonce_ays, i.e. 'The link you followed has expired. Please try again' (the linked text being 'Please try again').
  • However, instead of using the referrer for the redirect link, redirect to the googlesitekit-splash screen instead.
  • If the $action is not proxy-related, fall through to calling wp_nonce_ays as the default behavior.

Add test coverage for this new method.

Refactor instances of wp_die to use the new method

Refactor all instances where Site Kit currently dies with an "Invalid nonce." error message to instead rely on the new Authentication::invalid_nonce_error method, for example:

includes/Core/Authentication/Authentication.php:

654:	wp_die( esc_html__( 'Invalid nonce.', 'google-site-kit' ), 400 );
683:	wp_die( esc_html__( 'Invalid nonce.', 'google-site-kit' ), 400 );
1066:	wp_die( esc_html__( 'Invalid nonce.', 'google-site-kit' ), 400 );
1253:	wp_die( esc_html__( 'Invalid nonce.', 'google-site-kit' ) );

includes/Core/Util/Reset.php:

259:	wp_die( esc_html__( 'Invalid nonce.', 'google-site-kit' ), 400 );

You will likely need to refactor the test coverage for these files as well to reflect the changes.

Test Coverage

  • Test coverage should be added for the new method, and some existing tests might need to be refactored as mentioned above.

Visual Regression Changes

  • None anticipated, unless there are images that handle invalid nonce cases.

QA Brief

  • Add a call to invalid nonce error and ensure the hyperlink on the resulting error screen is pointing to wp-admin/admin.php?page=googlesitekit-splash
<?php
add_action( 'admin_init', function() {
    Google\Site_Kit\Core\Authentication\Authentication::invalid_nonce_error('googlesitekit_proxy_foo');
});

Changelog entry

  • Improve wording of "Invalid nonce" errors.
@aaemnnosttv aaemnnosttv added P2 Low priority Type: Enhancement Improvement of an existing feature labels Apr 6, 2021
@aaemnnosttv
Copy link
Collaborator Author

@felixarntz assigning to you to determine the specific wording to be used.

@aaemnnosttv aaemnnosttv removed their assignment Apr 15, 2021
@felixarntz
Copy link
Member

@aaemnnosttv Can you clarify what the intention of the improved messaging here is? I'm not sure I understand your point in the linked comment.

I agree it makes sense to come up with more helpful messages here, but realistically what is the solution in those scenarios? WordPress in some places is telling the user to refresh and try again, but I doubt that would help here. In other places it's also just saying something that the nonce is invalid.

Maybe just use core's wp_nonce_ays function for any such cases where we currently wp_die ourselves? I've added preliminary ACs based on this, but let me know your thoughts.

@felixarntz felixarntz assigned aaemnnosttv and unassigned felixarntz Apr 26, 2021
@aaemnnosttv
Copy link
Collaborator Author

@felixarntz the general intention is to offer a better experience as it's currently a rather uninformative dead-end.

I think wp_nonce_ays looks like an improvement over what we have now indeed so that would already be quite a bit better.

We could also build our own similar function which has its own cases for specific actions like wp_nonce_ays does for logout, but that might be unnecessary (just a thought).

I would be happy to proceed with it as-is unless you (or maybe @eugene-manuilov) have any other ideas?

@felixarntz
Copy link
Member

Yeah, let's go with wp_nonce_ays for now (unless you've additional input Eugene).

@eugene-manuilov
Copy link
Collaborator

@felixarntz @aaemnnosttv unfortunately, I think wp_nonce_ays won't work in our case because it uses a referer URL for the retry link in the generated markup. It means that users will be redirected to the wrong link on the proxy server instead of starting authentication from the beginning when they finish the previous authentication too late and click on the retry link.

I think we need to create our own version of the wp_nonce_ays function which will let us provide a redirect URL.

@felixarntz felixarntz self-assigned this Apr 28, 2021
@felixarntz
Copy link
Member

Good point @eugene-manuilov, I've updated the ACs to cater differently for invalid nonces for actions related to the proxy.

@felixarntz felixarntz removed their assignment Apr 28, 2021
@johnPhillips johnPhillips self-assigned this Jun 8, 2021
@johnPhillips
Copy link
Contributor

@aaemnnosttv

so the new method should probably actually call wp_nonce_ays in certain situations

Can you clarify what situations you mean here? Is there anything specific you need covered in the IB?

@johnPhillips johnPhillips removed their assignment Jun 17, 2021
@aaemnnosttv
Copy link
Collaborator Author

so the new method should probably actually call wp_nonce_ays in certain situations

Can you clarify what situations you mean here? Is there anything specific you need covered in the IB?

@johnPhillips Yes – the new method is described to be based on wp_nonce_ays which has a single conditional based on the given action. We may have multiple conditions that we match based on the actions, that's not important but my understanding of the ACs is that wp_nonce_ays would be the default/else case, whereas the current IB seems to reimplement/inline that function there. Basically, any action that we don't explicitly handle in our method would fallthrough to be handled by wp_nonce_ays.

@johnPhillips
Copy link
Contributor

Got it, thanks @aaemnnosttv
I've revised the IB accordingly. I'm not sure what other actions might need to be covered, but I've added the proxy-related one for now.

@eclarke1
Copy link
Collaborator

@johnPhillips would it be possible to add an estimate here now the IB is completed please?

@aaemnnosttv aaemnnosttv self-assigned this Jun 21, 2021
@aaemnnosttv
Copy link
Collaborator Author

IB ✅

@aaemnnosttv aaemnnosttv removed their assignment Jun 21, 2021
@eclarke1 eclarke1 added this to the Sprint 57 milestone Aug 31, 2021
@ivankruchkoff
Copy link
Contributor

Add a new private method, Authentication::invalid_nonce_error( $action ).

@johnPhillips if we make the method private, we can't call it from includes/Core/Util/Reset.php

@johnPhillips
Copy link
Contributor

@ivankruchkoff Good question. I will have added that detail because it was specific in the AC, so perhaps it's a good idea to ask @aaemnnosttv for some clarity?

@eugene-manuilov
Copy link
Collaborator

@ivankruchkoff could you please add QAB?

@ivankruchkoff ivankruchkoff added the QA: Eng Requires specialized QA by an engineer label Sep 10, 2021
@eclarke1 eclarke1 added the Rollover Issues which role over to the next sprint label Sep 13, 2021
@eclarke1 eclarke1 modified the milestones: Sprint 57, Sprint 58 Sep 13, 2021
@ivankruchkoff ivankruchkoff removed their assignment Sep 13, 2021
@johnPhillips johnPhillips self-assigned this Sep 16, 2021
@johnPhillips
Copy link
Contributor

QA ✅

Added calls to invalid_nonce_error and verified that the hyperlink pointed to wp-admin/admin.php?page=googlesitekit-splash.

@johnPhillips johnPhillips removed their assignment Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Low priority QA: Eng Requires specialized QA by an engineer Rollover Issues which role over to the next sprint Type: Enhancement Improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

6 participants