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

Add new Offloading_Files_Check check #566

Conversation

davidperezgar
Copy link
Member

@davidperezgar davidperezgar commented Aug 10, 2024

Related #486

This adds a new PHPCS Offloading_Files_Check check for detecting asset offloading (styles, scripts, images, etc.)

It does so by creating 2 custom PHPCS sniffs that are inspired by ones from WPCS:

  • EnqueuedResourceOffloadingSniff - Detect arbitrary URLs in wp_enqueue_* calls
  • OffloadingSniff - Detect arbitrary URLs in HTML strings

These new sniffs are developed according to PHP_CodeSniffer requirements in a new Composer package that is symlinked from the phpcs-sniffs folder. We should consider moving those to a new repository or contribute them to WPCS.

The sniffs need to be available in the final Plugin Check plugin, but I don't think we need to include the phpcs-sniffs folder in there. It should be included as part of vendor. Needs testing though!

To-do:

  • Add PHPCS-style tests for these sniffs
  • Figure out vendor situation for final plugin ZIP
  • Manual testing
  • Polishing
  • Consider proper location for these new sniffs
  • Run tests for these sniffs in CI

@davidperezgar davidperezgar changed the title Check: Calling remote files (js, css, images, etc). Offloading / External Prevent Calling remote files (js, css, images, etc). Offloading / External Aug 10, 2024
@davidperezgar
Copy link
Member Author

I'd need some help. I don't find functions that will help to do:

  1. Detect functions and their arguments even if they are multiline. Our internal scanner has a function from the parser that detects all functions from the plugin.
  2. Detect if an URL is external (where to do it?)

@swissspidy swissspidy changed the title Prevent Calling remote files (js, css, images, etc). Offloading / External Add new Offloading_Files_Check check Aug 22, 2024
@swissspidy
Copy link
Member

This is the first time we're writing custom sniffs for Plugin Check so there's lots to figure out still.

@davidperezgar
Copy link
Member Author

Yes, for sure we have to figure out deeply how it fits with the checks. This is a good start.

@davidperezgar davidperezgar marked this pull request as ready for review September 1, 2024 15:42
Copy link

github-actions bot commented Sep 1, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: davidperezgar <[email protected]>
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: ernilambar <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@swissspidy
Copy link
Member

Run tests for these sniffs in CI

FYI, this is not done yet.

What I meant with that to-do is that there ideally should be dedicated PHPCS-style unit tests under phpcs-sniffs/PluginCheck

We probably also need to add phpcs-sniffs to .distignore (needs testing)

@swissspidy swissspidy added the [Type] Enhancement A suggestion for improvement of an existing feature label Sep 10, 2024
@swissspidy swissspidy added this to the 1.2.0 milestone Sep 10, 2024
@swissspidy
Copy link
Member

This is getting closer. Unit tests now run and even code coverage works.

What's left is just manual testing. Ideally by generating a production-ready ZIP file and then testing that on a new site to see whether the sniffs are really included.

@ernilambar
Copy link
Member

Amazing. I did not even know we could use separate composer package list this. 💗

@davidperezgar
Copy link
Member Author

I've made a zip for plugin-check and run in a clean site, and I couldn't manage to work.

For test purposes, I use this plugin, and I'm afraid it does not detect offloaded.

@swissspidy
Copy link
Member

I've made a zip for plugin-check and run in a clean site

How did you do it? Trying to reproduce.

Which means it would be an issue with the build process.

I am trying the following steps:

  1. composer install --no-dev
  2. wp dist-archive . /path/to/write/the/plugin-check.zip
  3. Upload break-guidelines-test plugin to new site
  4. Upload generated plugin-check.zip to new site
  5. Run Plugin Check against break-guidelines-test in the admin

Result:

Screenshot 2024-09-13 at 10 39 19

@davidperezgar
Copy link
Member Author

That's how I do it! Let me test it again.

@davidperezgar
Copy link
Member Author

Anyway, if you manage to work, it's ok.

@ernilambar
Copy link
Member

I tested it in my local setup. PluginCheck.CodeAnalysis.EnqueuedResourceOffloading.OffloadedContent errors are there with that test plugin.

@ernilambar ernilambar merged commit f43dd53 into trunk Sep 23, 2024
28 checks passed
@ernilambar ernilambar deleted the 486-check-calling-remote-files-js-css-images-etc-offloading-external branch September 23, 2024 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check: Calling remote files (js, css, images, etc). Offloading / External
3 participants