Skip to content

Improve error messages when validation requests fail#3793

Merged
westonruter merged 4 commits intodevelopfrom
improve/validation-request-error-messages
Nov 21, 2019
Merged

Improve error messages when validation requests fail#3793
westonruter merged 4 commits intodevelopfrom
improve/validation-request-error-messages

Conversation

@westonruter
Copy link
Copy Markdown
Member

@westonruter westonruter commented Nov 19, 2019

Summary

This PR addresses something I noticed when troubleshooting a support topic, namely that when a validation request fails the error message is too generic and not provide enough context for why the problem is happening and how it can be resolved. This includes direction to check Site Health and how to find the support forum and submit topics.

For testing, there is a plugin which allows you to (re-)validate URLs with particular query vars to trigger various error scenarios, for example:

  • ?amp_simulate_validate_request_error=response_comment_absent
  • ?amp_simulate_validate_request_error=wsod
  • ?amp_simulate_validate_request_error=bad_host
  • ?amp_simulate_validate_request_error=timeout

This PR also addresses an issue discovered where query params added to the original URLs are normalized-away in the resulting URL that is used for the amp_validated_url post type. It also fixes encoding query params in URLs that are added to links in the admin bar.

Before

Screen Shot 2019-11-18 at 21 51 25

After

Screen Shot 2019-11-18 at 21 50 33

Relates to #3789 and #1371 and #2199.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added this to the v1.4.2 milestone Nov 19, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Nov 19, 2019
$review_messages[] = esc_html(
sprintf(
/* translators: 1: error message. 2: error code. */
__( 'However, there was an error when checking the AMP validity for your site.', 'amp' ),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ha, love the sprintf() here... 😄

*/
public static function serialize_validation_error_messages( $messages ) {
$encoded_messages = base64_encode( wp_json_encode( array_unique( $messages ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
return wp_hash( $encoded_messages ) . ':' . $encoded_messages;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just want to note that the wp_hash() function uses md5, which is usually to be avoided for security-related logic.

Using the PHP hash_hmac() directly would allow the use of a stronger algo.

The context does not seem too critical to me right now, but I am not a security expert. (IANASE?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TIL: IANASE

Note that \WP_Customize_Widgets::get_instance_hash_key() uses wp_hash() as opposed to hash_hmac(), so I think it's fine. And you're right, it's not critical here, as the strings being output are being passed through wp_kses() in any case.

Co-Authored-By: Alain Schlesser <[email protected]>
@kienstra
Copy link
Copy Markdown
Contributor

Approved

Hi @westonruter,
This looks good.

When I intentionally caused an error in the validation request:

add_action(
	'init',
	static function() {
		if ( ! empty( $_GET['amp_validate'] ) ) {
			throw new Exception();
		}
	}
);

...the notice looked good:

validation-notice

wp_kses(
$error_message,
[
'a' => array_fill_keys( [ 'href', 'target' ], true ),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice use of array_fill_keys().

@kienstra
Copy link
Copy Markdown
Contributor

kienstra commented Nov 21, 2019

Ah, you created a plugin to test the validation notices. I should have used that instead of creating my own function.

@westonruter westonruter merged commit be67818 into develop Nov 21, 2019
@westonruter westonruter deleted the improve/validation-request-error-messages branch November 21, 2019 05:01
westonruter added a commit that referenced this pull request Nov 21, 2019
* Remove broken removal of query args during URL normalization

* Add missing encoding of validate URL query args in admin bar

* Improve error messages shown after failing to perform validation requests

* Fix PHP comments

Co-Authored-By: Alain Schlesser <[email protected]>
@kienstra
Copy link
Copy Markdown
Contributor

Question about moving to 'Done'

Hi @westonruter,
Hope your day's off to a great start.

What do you think about moving this straight to 'Done'? I'm not sure how much traditional QA can be done, without using PHP to simulate a failed request.

@westonruter
Copy link
Copy Markdown
Member Author

Yes, you tested it so that is good enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Signed the Google CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants