Guard set_post_categories against empty-list category wipe#29
Merged
Conversation
WordPress.com treats POST {categories_by_id: []} as a wipe: it removes
every category and reassigns the post to the site default. agents/apply.md
already documents that an empty cats list from analysis is a bug, not a
pass-through, but set_post_categories had no guard. A single empty entry
would silently strip a post's categories, and the drift check (gated on
'if sent_ids') was skipped for the empty case, so the wipe went undetected.
Refuse an empty category_ids list with a ValueError unless the caller
passes the new allow_clear=True flag (reserved for intentional clears such
as snapshot restore). Document the enforcement in apply.md and add
regression tests for both the refusal and the explicit-clear path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WordPress.com treats
POST {categories_by_id: []}as a wipe: it strips every category off the post, and WordPress then drops the post into the site's default category.apply.mdalready says an emptycatslist from analysis is a bug, not something to pass through. ButWpcomAdapter.set_post_categories()had no guard, so a single stray empty entry would silently clear a post's categories; and since the drift check was gated onif sent_ids, the empty case skipped it, so the wipe went undetected.Now an empty
category_idsraises aValueErrorunless the caller passes the newallow_clear=True(reserved for intentional clears, like restore). I documented the behavior inapply.mdand added regression tests for both the refusal and the explicit-clear path.While testing against a live Jetpack site I confirmed the guard fires: an empty list is rejected before any request goes out, so a stray empty entry can't reach WordPress.com and trigger the wipe.
Testing
python3 -m unittest discover tests— greenruff check lib/ tests/— clean