Skip to content

Stats: avoid Fatal errors when trying to fetch malformed data#35746

Merged
jeherve merged 1 commit intotrunkfrom
fix/stats-fetch-fatal
Feb 19, 2024
Merged

Stats: avoid Fatal errors when trying to fetch malformed data#35746
jeherve merged 1 commit intotrunkfrom
fix/stats-fetch-fatal

Conversation

@jeherve
Copy link
Copy Markdown
Member

@jeherve jeherve commented Feb 19, 2024

Proposed changes:

This is a follow-up to #34557. It was discussed a bit here:
https://github.com/Automattic/jetpack/pull/34557/files#r1475310509

I ran into the following issue in the post editor today:

PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #2 must be of type array, WP_Error given in wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-stats/src/class-wpcom-stats.php:453
Stack trace:
wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-stats/src/class-wpcom-stats.php(453): array_merge()
wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-stats/src/class-wpcom-stats.php(215): Automattic\Jetpack\Stats\WPCOM_Stats->fetch_post_stats()

The added safety checks in this PR fix the issue, by not assuming anything about the format of the returned data.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

  • N/A

Does this pull request change what data or activity we track or use?

  • No

Testing instructions:

  • Enable beta blocks on your site: add_filter( 'jetpack_blocks_variation', function () { return 'beta'; } );
  • Go to Posts > Add New
  • Insert a Blog Stats block
  • You should see no errors in the editor and in your logs.

This is a follow-up to #34557. It was discussed a bit here:
https://github.com/Automattic/jetpack/pull/34557/files#r1475310509

I ran into the following issue in the post editor today:

```
PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #2 must be of type array, WP_Error given in wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-stats/src/class-wpcom-stats.php:453
Stack trace:
wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-stats/src/class-wpcom-stats.php(453): array_merge()
wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-stats/src/class-wpcom-stats.php(215): Automattic\Jetpack\Stats\WPCOM_Stats->fetch_post_stats()
```

This added security fixes the issue.
@jeherve jeherve added Bug When a feature is broken and / or not performing as intended [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. [Status] Needs Review This PR is ready for review. [Pri] Normal [Status] Needs Team Review Obsolete. Use Needs Review instead. labels Feb 19, 2024
@jeherve jeherve requested review from a team and kangzj February 19, 2024 09:47
@jeherve jeherve self-assigned this Feb 19, 2024
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 19, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the fix/stats-fetch-fatal branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack fix/stats-fetch-fatal
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Copy Markdown
Contributor

@darssen darssen left a comment

Choose a reason for hiding this comment

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

Changes look sensible to me. I was able to test the flow after adding a Blog Stats showing the data for the individual post and things went smooth.

I added a small comment on the initial check but it is not a blocker.

if (
! is_array( $data )
|| empty( $data )
|| is_wp_error( $data )
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.

Do we need is_wp_error here? Feels like ! is_array( $data ) should have taken care of it.

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.

I wasn't sure, but it was the only check that was there before my change so I figured it may cover a use-case I haven't thought about.

I think I'll leave it here for now. Better be safe than sorry. :)

@jeherve jeherve merged commit 6b65230 into trunk Feb 19, 2024
@jeherve jeherve deleted the fix/stats-fetch-fatal branch February 19, 2024 14:51
@github-actions github-actions bot removed [Status] Needs Review This PR is ready for review. [Status] Needs Team Review Obsolete. Use Needs Review instead. labels Feb 19, 2024
@kangzj
Copy link
Copy Markdown
Contributor

kangzj commented Feb 19, 2024

Thanks for fixing this @jeherve 👍

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

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. [Package] Stats Data [Pri] Normal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants