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

validate postId in the site-editor.php #7188

Open
wants to merge 9 commits into
base: trunk
Choose a base branch
from

Conversation

mi5t4n
Copy link

@mi5t4n mi5t4n commented Aug 13, 2024

Added validation for the postId parameter in site-editor.php to ensure that the post exists before proceeding. If the post ID is invalid or the post does not exist, the script will now terminate with an error message, preventing potential errors or unintended behavior.

Trac ticket: https://core.trac.wordpress.org/ticket/61796


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Aug 13, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mi5t4n, hellofromtonya, peterwilsoncc, davidbaumwald, benniledl.

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

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mi5t4n mi5t4n force-pushed the fix/61796 branch 2 times, most recently from 01c46f2 to d01e1e6 Compare August 13, 2024 17:08
Added validation for the `postId` parameter in `site-editor.php` to ensure that the post exists before proceeding. If the post ID is invalid or the post does not exist, the script will now terminate with an error message, preventing potential errors or unintended behavior.
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

I've added a couple of notes inline.

While testing I noticed that going to wp-admin/site-editor.php?postType=page&canvas=edit ignores the postType parameter and loads the Blog Home template.

It suggests that further validation of the user input via the URL is required

Comment on lines 118 to 120
}


Copy link
Contributor

@peterwilsoncc peterwilsoncc Aug 14, 2024

Choose a reason for hiding this comment

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

CS: Remove double line break.

Edit: You'll need to do this manually as the inline suggestion doesn't appear to like white space changes.

src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
@dream-encode
Copy link
Contributor

dream-encode commented Aug 14, 2024

Though it's probably beyond the scope of the original ticket, I see a lot of duplication of isset( $_GET['postType'] ) and sanitize_key( $_GET['postType'] ).

Wondering if we could have something like a default $current_post_type = false|null then check and sanitize it once for reuse in the rest of the file. Same could pro be done for $_GET['postId'] and $_GET['path']?

Could probably move the early bails a bit higher in the file as well, to save some unnecessary code running when it's not needed? Just based off a brief look, unless I am missing something, I don't see anything between lines 76 and 118 that is in any way dependent on any of the code running above.

@benniledl
Copy link

would be nice to have an error message for various post statuses as in #7215

@mi5t4n
Copy link
Author

mi5t4n commented Aug 23, 2024

@dream-encode @peterwilsoncc Thank you for the suggestions. I have made the changes.

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

I've added a few notes inline for additional reuse of $post_type_param and to avoid a potential PHP error if the value is not set.

src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
src/wp-admin/site-editor.php Outdated Show resolved Hide resolved
Additional reuse of `$post_type_param` and to avoid a potential PHP error if the value is not set.

Co-authored-by: Peter Wilson <[email protected]>
@hellofromtonya
Copy link
Contributor

Before 6.6.0, it redirected to site-editor.php?path=%2Fpage&canvas=view. Granted that did not indicate the why.

Why wp_die()?

My concern is two-fold:

  • Before this bug: It redirected, but stayed in the Site Editor.
  • Will the user understand how to get back to the editor, i.e. get out of this error page?

I'm wondering:

Could a lighter approach might be a better user experience? "lighter" meaning -> retain the previous behavior to keep the user in the Site Editor, while also displaying a message to inform the user why their requested page did not show.

@dream-encode @peterwilsoncc What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants