Make WordPress Core

Opened 6 months ago

Closed 5 weeks ago

Last modified 5 weeks ago

#61970 closed defect (bug) (fixed)

Fatal error: Attempt to read property "ID" on null in /src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

Reported by: antonvlasenko's profile antonvlasenko Owned by: swissspidy's profile swissspidy
Milestone: 6.8 Priority: normal
Severity: normal Version: 6.7
Component: REST API Keywords: has-patch has-unit-tests has-testing-info
Focuses: template, rest-api Cc:

Description

This error occurs when a user tries to fetch autosave posts or revisions for a template (template part) that is file-based and does not have an associated entity in the posts table.
The returned HTTP code is 403, which is also incorrect.
Both the fatal error and the HTTP code need to be fixed.

Attachments (4)

Snímek obrazovky 2024-10-14 v 23.25.46.png (138.2 KB) - added by antonvlasenko 4 months ago.
Snímek obrazovky 2024-10-14 v 23.26.01.png (134.8 KB) - added by antonvlasenko 4 months ago.
Snímek obrazovky 2024-10-14 v 23.26.51.png (139.1 KB) - added by antonvlasenko 4 months ago.
Snímek obrazovky 2024-10-14 v 23.27.09.png (135.4 KB) - added by antonvlasenko 4 months ago.

Download all attachments as: .zip

Change History (13)

#1 @antonvlasenko
6 months ago

Props to @Mamaduka for discovering this issue.

This ticket was mentioned in PR #7272 on WordPress/wordpress-develop by @antonvlasenko.


6 months ago
#2

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

#3 @antonvlasenko
4 months ago

  • Keywords has-testing-info added

Steps to test https://github.com/WordPress/wordpress-develop/pull/7272:

  1. On a fresh installation of WordPress, activate the Twenty Twenty-Four theme.
  2. Send GET requests to the following endpoints:
  • /wp-json/wp/v2/templates/twentytwentyfour//search/revisions/1
  • /wp-json/wp/v2/templates/twentytwentyfour//search/autosaves
  • /wp-json/wp/v2/template-parts/twentytwentyfour//header/revisions/1
  • /wp-json/wp/v2/template-parts/twentytwentyfour//header/autosaves
  1. Observe that there are no REST API-related fatal PHP errors in the logs and that you receive the following REST API responses:
       
       {
         "code": "rest_invalid_template",
         "message": "Templates based on theme files can't have revisions.",
         "data": {
           "status": 400
         }
       }
    

P.S. While manual testing is important, the aforementioned PR contains unit tests that verify the endpoint returns the correct response.

Last edited 4 months ago by antonvlasenko (previous) (diff)

@antonvlasenko commented on PR #7272:


4 months ago
#4

This is looking good so for. Needs testing.

Thank you for the review, @spacedmonkey.
I’ve added testing instructions and fixed the failing PHPUnit tests.

#5 @antonvlasenko
4 months ago

Test Report

This report validates that the indicated patch addresses the issue.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/7272

Environment

  • WordPress: 6.7-beta2-59195-src
  • PHP: 8.3.3
  • Server: Apache/2.4.57 (Unix) PHP/8.3.3
  • Database: mysqli (Server: 5.7.43 / Client: mysqlnd 8.3.3)
  • Browser: Safari 18.0 (macOS)
  • Theme: Twenty Twenty-Four 1.2
  • MU-Plugins: None activated
  • Plugins:
    • Datetime
    • Gutenberg 19.1.0
    • WordPress Beta Tester 3.5.3

Actual Results

  • ✅ Template revisisions endpoint returns correct HTTP response code (400) for files based templates and template parts.
  • ✅ Template autosaves endpoint returns correct HTTP response code (400) for files based templates and template parts.

Supplemental Artifacts

https://core.trac.wordpress.org/raw-attachment/ticket/61970/Snímek%20obrazovky%202024-10-14%20v%C2%A023.25.46.png
https://core.trac.wordpress.org/raw-attachment/ticket/61970/Snímek%20obrazovky%202024-10-14%20v%C2%A023.26.01.png
https://core.trac.wordpress.org/raw-attachment/ticket/61970/Snímek%20obrazovky%202024-10-14%20v%C2%A023.26.51.png
https://core.trac.wordpress.org/raw-attachment/ticket/61970/Snímek%20obrazovky%202024-10-14%20v%C2%A023.27.09.png

P.S. Apologies for the visual noise caused by the attached files below.

Last edited 4 months ago by antonvlasenko (previous) (diff)

#6 @kadamwhite
4 months ago

  • Milestone changed from Awaiting Review to 6.8

Thanks for the patch, and the testing. Added 6.8 milestone to get this in early next cycle once we're out of 6.7 RC!

#7 @swissspidy
5 weeks ago

  • Owner set to swissspidy
  • Resolution set to fixed
  • Status changed from new to closed

In 59605:

REST API: Improve autosave and revision endpoints for templates and template parts.

Fixes those endpoints for file-based templates and template parts, as templates based on theme files can't be revisioned or autosaved.

Props antonvlasenko, swissspidy, spacedmonkey, kadamwhite.
Fixes #61970.

#9 @swissspidy
5 weeks ago

In 59607:

Undo accidental svn:mergeinfo property change in [59605].

See #61970.

Note: See TracTickets for help on using tickets.