Repository navigation
Closes #971: Enforce WordPress nonce verification sniffs - #1341
Conversation
Remove the WordPress.Security.NonceVerification.Missing and .Recommended excludes from phpcs.xml and resolve every reported case. - Verify the settings form nonce before the "Save & Go to Bulk Optimizer" redirect is set. - Read AJAX request parameters through a single helper; a value sent with the other HTTP method now yields 'noop' instead of an undefined index. - Share one helper for the admin page slug in Imagify_Views. - Keep a justified phpcs:ignore only for read-only screen detection, list filters, values read before the caller's own nonce check, and NextGEN Gallery's own import request; drop legacy "WPCS: CSRF ok" comments. - Add integration tests and update the AGENTS.md trap. Refs #971 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
🟢 Coverage 72.00% diff coverage
Metric Results Coverage variation Report missing for af6a7711 Diff coverage ✅ 72.00% diff coverage (50.00%) Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (af6a771) Report Missing Report Missing Report Missing Head commit (f973f60) 20590 2909 14.13% Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#1341) 25 18 72.00% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%1 Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Restructure the tests added for #971 to the project standard: one test class per tested method, a single testShouldReturnExpected() method, and cases loaded from Tests/Fixtures through configTestData. Add unit tests next to the integration tests so the changed lines count toward Codacy diff coverage, which only measures the unit suite: - Imagify_Admin_Ajax_Post get_context / get_folder_type / get_imagify_action - Bulk::get_context - Imagify_Views is_settings_page / is_bulk_page / is_files_page - Imagify_Settings::maybe_set_redirection Unit and integration tests for the same method share one fixture file. Refs #971 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fication Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
develop now reports integration test coverage to Codacy (#1340), so the unit tests added only to satisfy the diff coverage gate are redundant with the integration tests sharing their fixtures. Remove them, and move Bulk::get_context coverage to an integration test on the same fixture. Refs #971 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Note
This PR was created by an AI agent (Claude Code) and reviewed by a human before merge.
Description
Closes #971
Removes the
WordPress.Security.NonceVerification.Missingand.Recommendedexcludes fromphpcs.xmland resolves all 27 reported cases. Where a fix in code made sense, it is in code. Aphpcs:ignoreremains only where the read changes nothing, and each one has its reason written next to it.User impact:
'noop'instead of reading a key that doesn't exist. Before, that raised an undefined-index warning and returned''. The request is still rejected either way.Type of change
Detailed scenario
What was tested
Automated:
composer phpcswith the two sniffs enabled: no errors on the whole codebase.composer run-stan: no errors.composer test-unit: 604 tests pass. The 2 "risky" results are aversion_compare()deprecation inupdate_site_option_on_networkthat was already there before this change.--exclude-group AbilitiesCatalog): 285 tests pass, 37 skipped because they need multisite.testShouldReturnExpected()and its cases in aTests/Fixturesfile (36 cases):ImagifySettings/maybeSetRedirection: redirects with a valid nonce; no redirect when the nonce is missing, belongs to another action, or the bulk button wasn't clicked.ImagifyAdminAjaxPost/getContext,getFolderType,getImagifyActionandBulk/getContext: read from the right method, defaults (noop/optimize), method mismatch, array values, sanitization.ImagifyViews/isSettingsPage,isBulkPage,isFilesPage: page detection with and without?page=, on the right and wrong screens.No manual browser testing was done.
How to test
composer install, thencomposer phpcs. Expect noNonceVerificationerrors.vendor/bin/phpunit --configuration Tests/Integration/phpunit.xml.dist --group Bulk,ImagifySettings,ImagifyAdminAjaxPost,ImagifyViews/wp-admin/options-general.php?page=imagify, click Save & Go to Bulk Optimizer. It should save and land on the Bulk Optimizer. Save Changes should stay on the settings page.Affected Features & Quality Assurance Scope
get_context): bulk optimize, folder type data, bulk info seen.get_folder_type/get_imagify_action.Imagify_Views::is_settings_page/is_bulk_page/is_files_page), which decides which assets and notices load.Technical description
Documentation
Imagify_Settings::maybe_set_redirection()hooks intopre_update_option_imagify_settings, which can fire outside the settings form. It now checks theimagify-optionsnonce (the actionsettings_fields()uses) withwp_verify_nonce()before it sets$_REQUEST['_wp_http_referer'].Imagify_Admin_Ajax_Post::get_request_param()(private) is now the only place that reads$_GET/$_POSTforget_context(),get_folder_type()andget_imagify_action(). The nonce is checked by the calling AJAX callback, not inside the helper: several callbacks build the nonce action from the very values being read (media ID, context).Bulk::get_context()uses the same pattern inline.Imagify_Views::get_requested_page()(private) is now the only read of$_GET['page']for page detection.phpcs:ignorecomments are all on read-only code: screen detection, list filters, and NextGEN Gallery's own import request, whose nonce NextGEN checks itself. Legacy// WPCS: CSRF ok.comments, which PHPCS 3 no longer honors, were replaced.AGENTS.md: the trap that said these sniffs are deliberately suppressed now says when an ignore is acceptable.New dependencies
None.
Risks
options.php) and the network form (admin-post.php) post_wpnonceforimagify-optionsthroughsettings_fields(), so real form submissions are unaffected.get_context()now returns'noop'instead of''. Both values make the callers refuse the request (Noop context or "Invalid request").Mandatory Checklist
Code validation
Code style
Unticked items justification
The settings, AJAX and page-detection changes are covered by the new integration tests. The NextGEN Gallery lines, the Media Library dropdown and the
upload.phpfilter were not run in a browser. Their logic is unchanged apart from reading the value once, so QA should cover them with the manual steps above.Additional Checks
🤖 Generated with Claude Code