Opened 7 months ago
Closed 3 months ago
#65123 closed defect (bug) (duplicate)
Having a malformed post type query in $_GET or $_POST yields a critical error
| Reported by: | hheuzebrutc | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Permalinks | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
Steps to reproduce:
- Go to a fresh install's homepage
- Add ?attachment[foo]=bar to the url
- press enter
With the "attachment" post type, only the front page will crash, other pages will yield a 404. However, doing this with any custom post types the site might have can crash any page (as long as the post type is public).
What's especially annoying for me is it also works with $_POST. If I make a form with an input name like public_post_type[sub_array], the site crashes upon submission and my form is never processed.
Change History (21)
This ticket was mentioned in PR #11650 on WordPress/wordpress-develop by @sukhendu2002.
7 months ago
#2
- Keywords has-patch added
Trac ticket: https://core.trac.wordpress.org/ticket/65123
This ticket was mentioned in PR #11651 on WordPress/wordpress-develop by @dhrupo.
7 months ago
#4
## Summary
This ignores non-scalar values when sanitizing post_type query vars in WP::parse_request(), preventing malformed array input from surfacing PHP warnings during front-end requests.
## Testing Instructions
- Request
/?post_type[][]=page&post_type[]=poston a local site withWP_DEBUG_DISPLAYenabled. - Confirm no warning is emitted before the page output.
- Run
npm run test:php -- --filter Tests_WP_ParseRequest.
#5
in reply to: ↑ description
@
7 months ago
Replying to hheuzebrutc:
- Add ?attachment[foo]=bar to the url
How did you come to add this to the URL?
#6
@
7 months ago
Well at first I wasn't touching the URL at all, as mentioned above I was making a custom form as a shortcode, with several input fields grouped together in an array that happened to have the same name as one of my custom post types, something like post_type[input_name]. And no matter what I did, WordPress Core always crashed before my form had a chance to be processed. I figured it was something to do with query vars and tried it with the URL as well. I chose attachments in my example because as far as I can tell, it's the only public post type that's guaranteed to do this on every WordPress site.
#7
@
7 months ago
- Milestone Awaiting Review → 7.1
- Version 6.9.4
It is worth hardening what is passed from input vars.
@westonruter commented on PR #11651:
7 months ago
#8
What does this PR add over the existing one https://github.com/WordPress/wordpress-develop/pull/11650? I think efforts should be consolidated there, and this PR be closed.
@dhrupo commented on PR #11651:
7 months ago
#9
@dhrupo commented on PR #11651:
7 months ago
#10
@westonruter Agreed. #11650 already covers the same ticket and broader query-var handling, so it makes more sense to consolidate review there instead of splitting effort across two PRs. I am closing this PR in favor of #11650.
#11
@
7 months ago
Tested PR https://github.com/WordPress/wordpress-develop/pull/11650
Before the patch, passing a non-scalar attachment value to WP_Query::parse_query() set is_attachment and is_single, and constructing a custom post type query with an array query var triggered a fatal error in sanitize_title_for_query().
After the patch, attachment is normalized to an empty string, both flags remain false, and the custom post type query completes without a fatal error.
Patch tests well for me.
#12
@
4 months ago
I think this is a duplicate of #60745 (@westonruter, are you able to do a logic check?)
The open PR attached to this ticket is making changes to WP_Query whereas I think the bug is in WP.
WP_Query is a developer API whereas the bug of failing to process $_GET parameters correctly is in the user facing API, WP, which processes the URL provided by the site visitor.
#13
@
4 months ago
Test Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/12431
Environment
- WordPress: 7.1-beta3
- Subdirectory: No
- PHP: 8.3.32
- Server: PHP.wasm
- Database: WP_SQLite_Driver (Server: 8.0.38 / Client: 3.51.0)
- Browser: Chrome 150.0.0.0
- OS: Windows 10/11
- Theme: Twenty Twenty-Five 1.5
- MU Plugins: None activated
- Plugins:
- Test Reports 1.3.0
- WordPress Beta Tester 4.0.0
Steps taken
- Opened the front page of the WordPress site using a web browser.
- Added
?attachment[foo]=barto the end of the site URL and pressed enter. - Used a terminal to send a form submission (POST request) carrying
attachment[foo]=bar. - Verified that the home page loaded normally instead of throwing a critical error.
- Checked server logs to ensure no error messages were generated during both requests.
- Patch is solving the problem
Expected result
- Passing array data in attachment query parameters should not crash the site or produce critical errors.
Actual Results
- The patch works as expected. The front page loaded without any site crash or critical error screen when tested with modified attachment queries.
Additional Notes
- Tested on WordPress Playground (PHP.wasm environment).
Screenshots/Screencast with results
- N/A
Support Content
nazmulasif commented on PR #11650:
4 months ago
#14
Tested this PR locally and via Playground.
Passing non-scalar array values to attachment and custom post type query vars no longer sets invalid state flags or triggers fatal errors in sanitize_title_for_query(). The values normalize as expected and queries execute cleanly.
Works fine for me!
This ticket was mentioned in Slack in #core by adrianduffell. View the logs.
4 months ago
#16
@
3 months ago
Environment:
- Trunk: a379d09c5b
- WP 7.1-beta4-62899-src (Docker/nginx, PHP 8.3.32)
- CPT
sg_bookregistered.parse_requestquery vars logged.
Test results (tested both GET and POST):
| State | attachment[foo]=bar | sg_book[foo]=bar (CPT) |
| Trunk | Fatal | Fatal |
| PR 12738 (unmodified) | Fatal | Fatal |
| PR 12738 (with attachment added) | 400 Bad Request | Fatal |
| PR 11650 | 200 OK | 200 OK |
Note: PR 12738 handles feed[]=admin with 400. I added attachment to list for the test above.
PHPUnit (Tests_Query_ParseQuery):
- Trunk: 17 tests
- PR 11650: 20 tests, 24 assertions (added tests pass)
Findings:
- Duplicate? Not fully. Root cause is same. #60745 has attachment. But #60745 does not cover POST or CPT.
- Where fix belongs? WP.
WP::parse_request()sees POST. WP copies bad array tonameat class-wp.php:362. This happens before WP_Query exists. - Unify with 12738? PR 12738 check is after line 362. Array is already copied. Also, 12738 uses static list. CPTs are registered with
register_post_type()and WP reads them withget_post_types(). Static list cannot catch CPT. It will still fatal. - 400 vs 200: PR 12738 gives 400. PR 11650 gives 200. Reporter says form does not process. With 400, form still does not process.
- Comment 13: Test report in comment 13 is for PR 12431. Not this ticket.
I can take patch if we know which way to go.
This ticket was mentioned in Slack in #core by softglaze. View the logs.
3 months ago
#18
@
3 months ago
Thanks for the detailed report, @softglaze.
Duplicate? Not fully. Root cause is same. #60745 has attachment. But #60745 does not cover POST or CPT.
I think it would be best to update the other ticket to include any items that are missing in order to cover validation fully in a single commit. Does that work for you?
#19
@
3 months ago
@peterwilsoncc
Yes, that works.
I will post the consolidated findings and the list of missing items over on #60745 now.
Hi @hheuzebrutc, welcome to Trac and thanks for the report!
I am able to reproduce the fatal error.
I'm not sure how common this is, but
attachmentis supposed to be a string, as it's used to fetch an attachment by slug/post_name. It looks like type validation is handled mostly inWP_Query::parse_query(), butattachmentisn't checked properly. We probably need anis_scalarcheck, like what is done withsubpost(which, if provided, gets assigned toattachment).