Skip to content

Keep stored objects unchanged when updating URLs after an attachment rename - #1237

Merged
vytisbulkevicius merged 9 commits into
developmentfrom
fix/rename-serialized-values
Nov 12, 2026
Merged

vytisbulkevicius merged 9 commits into
developmentfrom
fix/rename-serialized-values

Conversation

@vytisbulkevicius

@vytisbulkevicius vytisbulkevicius commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

When an attachment is renamed, the plugin updates its old URL in every database column that holds it, unserializing stored values to do so. Stored values are now unserialized with only stdClass allowed, and a value that holds any other object is left exactly as stored.

What changed

  • Serialized values (Optml_Attachment_Db_Renamer::replace_urls_in_value()): are read with allowed_classes => [ 'stdClass' ]. No other class is created while the rename runs.

  • Values with other objects (new holds_other_objects()): a value that holds an object of another class, or an enum, is left unchanged. Before, such a value was rewritten with the classes it named, or the rename stopped with an error when the class was not loaded.

  • Values the renamer cannot read safely: a value with references (r: or R:), or one that does not unserialize (for example an enum whose class is not loaded), is left unchanged. Before, a reference cycle exhausted memory, and an unreadable value got a raw text replacement that broke its declared lengths.

  • Custom serialized objects (C:): is_serialized() and the column check now accept C: and E:, so these values reach the checks above and stay as stored.

  • Arrays and stdClass values: keep getting the new URL, written back as before.

  • Serialized values inside strings (new replace_in_string()): a string in serialized data that holds a serialized value, as some plugins store options, is rewritten as data, with the same checks. Before, it got a raw text replacement that broke its declared lengths. This applies when the string reads back to the same text, also with floats from PHP 7.0, and to three nested levels. Deeper values, and values the checks refuse, stay as stored.

  • Serialized strings (s:): the column check now finds them. A string with text after its declared length gets the plain replacement, so that text is kept.

Note

URLs inside objects of plugin classes (other than stdClass) are no longer rewritten after a rename. Rewriting them needs the object read without its class, which can lose data, for example the payload of a Serializable object.

The reference check reads the raw value. A value whose string text holds ;R:1;, or one with acyclic shared references, also stays as stored. The value stays valid, but keeps the old URL.

The depth cap bounds memory, because each nested level reads a copy of the string.

QA

  1. Go to WP Admin → Media → Library, open an image, and click Replace or Rename. Enter a new name under Rename attached file, and click Rename.

    Expect: the rename succeeds, and a post that shows the image still displays it with the new file name.

  2. Before the rename in step 1, store an option that holds the image URL inside an array. Use WP-CLI, with the image URL from step 1:

    wp option update optml_qa_gallery '{"image":"<image URL>"}' --format=json

    Rename the image as in step 1, then run wp option get optml_qa_gallery --format=json.

    Expect: the option holds the new image URL.

  3. Store a comment that holds a serialized object of a class that is not loaded, with the image URL inside. Use WP-CLI, with the image URL and the post ID:

    wp eval '$u = "<image URL>"; echo wp_insert_comment( [ "comment_post_ID" => <post ID>, "comment_content" => "O:9:\"Not_Here1\":1:{s:3:\"url\";s:" . strlen( $u ) . ":\"" . $u . "\";}" ] );'

    Rename the image as in step 1, then run wp comment get <comment ID> --field=comment_content.

    Expect: the rename succeeds, and the comment content is unchanged.

Review responses

  • Whitespace after a top-level serialized string: kept as is. It is dropped when the value is written back with serialize(). unserialize() ignores it on read, so the stored data reads the same before and after.

  • Old URL left in comments and term descriptions: covered by the existing test_replace_updates_comment_content and test_replace_updates_term_description, which pass at HEAD. This PR does not change how tables and primary keys are found.

Issue: Codeinwp/optimole-service#1947

…URLs

Serialized values are read with only stdClass allowed, and values that hold
any other object are left as stored.
@pirate-bot

pirate-bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Plugin build for fa309fb is ready 🛎️!

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

Two stored-value shapes are still rewritten and corrupted, and the cycle test accepts a wrong write.

Validation details
  • Files reviewed: 2/2 changed files.
  • PHPUnit on PHP 8.3.36 with WP test library 7.1.4: all five new tests pass at HEAD. Against the base code, the first two tests fail and the other three pass. These runs have no run record. Logs are under the check folder.
  • Isolated PHP 8.3.36 runs: objects under private or protected keys stay unchanged timeout 30 php /workspace/checks/C1/produce.php && timeout 30 php /workspace/checks/C1/check.php | tee /workspace/checks… (run R7, exit 0). A top-level C: value is corrupted "OUTPUT_UNSERIALIZES=0 SAME=0" (not in the output of run R11).
  • Security: rename needs edit_post and a nonce. Only stdClass objects are created. No issue was found.
Untested areas
  • The Media Library rename screen and the WP-CLI QA steps were not run.
  • Enum autoload during unserialize was not executed. Enums allow no magic methods.

🤖 Automated review · run code-review-agent_6af4b3ee68d748.60636838.


🤖 Review agent — review posted ✅ on cd077d3c · changes requested · 3 findings · 15 min

Run code-review-agent_6af4b3ee68d748.60636838 · trail

Comment thread tests/media_rename/test-db-renamer.php Resolved
Values with references, values that do not unserialize, and Serializable
objects stored on their own are no longer rewritten.
…t replacement

Only the serialized forms the PHP path must protect, arrays, objects, custom
serialized objects and enums, now send a column to it.

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The three earlier findings are fixed, but a serialized string nested in stored data still gets a plain text replacement that breaks it.

Validation details
  • Files reviewed: 2/2 changed files.
  • PHPUnit on PHP 8.3.36 with WP test library 7.1.4: the whole renamer test file passes at HEAD (run R1: OK (37 tests, 83 assertions)).
  • With the pr-base renamer, the C: and enum tests fail, and the plain-text test passes (run R9: OK (1 test, 1 assertion)).
  • Isolated run of the real methods: a nested serialized string stops unserializing at HEAD and at pr-base (run R6: inner_result=false rewritten=true).
Untested areas
  • A column whose only serialized rows are top-level s: strings still gets a raw replacement, at HEAD and at pr-base. No plugin that writes such a column was found.
  • The self-reference test did not finish with the pr-base renamer, so it has no base result.
  • The Media Library rename screen and the WP-CLI QA steps were not run.

🤖 Automated review · run code-review-agent_6af4e07b6b9024.17244564.


🤖 Review agent — review posted ✅ on eb2d2625 · changes requested · 2 findings · 33 min

Run code-review-agent_6af4e07b6b9024.17244564 · trail

Comment thread tests/media_rename/test-db-renamer.php Resolved

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

A nested serialized string still breaks after a rename, as the open thread shows. Fix it or link a follow-up issue there.

Validation details
  • Files reviewed: 2/2 changed files. The only commit since the prior review deletes one test.
  • The deleted plain-text test is confirmed gone. Both C: tests fail with the pr-base renamer (prior run code-review-agent_6af4e07b6b9024.17244564, R9, not rerun).
  • The nested-string failure was recorded at HEAD and pr-base by that prior run (R6, not rerun). The renamer code is unchanged since then.
Untested areas
  • The security, variant, requirements and standards passes were not rerun. The renamer code is unchanged since the prior review, which ran them.
  • A column whose only serialized rows are top-level s: strings still gets a raw replacement. This is the same at pr-base.
  • The Media Library rename screen and the WP-CLI QA steps were not run.

🤖 Automated review · run code-review-agent_6af527e33417e3.75749451.


🤖 Review agent — review posted ✅ on 8a5a836a · changes requested · 0 findings · 5 min

Run code-review-agent_6af527e33417e3.75749451 · trail

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The nested-string fix works for values that read back unchanged, but three stored-value shapes still break after a rename.

Validation details
  • Files reviewed: 2/2 changed files.
  • PHPUnit on PHP 8.3.36: OK (40 tests, 90 assertions) [R2] for the renamer test class at HEAD.
  • With the pr-base renamer, three of the four new tests fail, and the nested-text test passes [R4].
  • An isolated run with PHP-produced inputs shows the nested C:, enum and old-precision float shapes stop unserializing [R3].
  • A 16 MiB input and invalid UTF-8 returned strings without a PCRE error [R6].
  • The security pass found no issue in the new nested unserialize().
Untested areas
  • Raw replacements in the offload and Thrive code and the Autoptimize maybe_unserialize() are unchanged code without an established producer.
  • The author's 6000-level memory measurement was not rerun.
  • The Media Library rename screen and the WP-CLI QA steps were not run.

🤖 Automated review · run code-review-agent_6af56b3a732eb1.02625472.


🤖 Review agent — review posted ✅ on 42abe22c · changes requested · 4 findings · 26 min

Run code-review-agent_6af56b3a732eb1.02625472 · trail

Comment thread tests/media_rename/test-db-renamer.php Resolved

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The four earlier findings are fixed, but padded lengths and a runtime serialize_precision of 17 still break stored values after a rename.

Validation details
  • Files reviewed: 2/2 changed files. One history question stayed unchecked: "Renaming an image leaves the old URL in comments and term descriptions, with Unknown column 'ID' database errors".
  • PHPUnit on PHP 8.3.36 for the renamer class at HEAD: OK (44 tests, 98 assertions) [R9].
  • The five new or changed tests fail with the pr-base renamer [R10] [R11] [R12] [R14] [R15].
  • An isolated run at precision 17 shows the nested value stops unserializing [R13].
  • The security pass found no issue in either unserialize() call.
Untested areas
  • The history question "Renaming an image leaves the old URL in comments and term descriptions, with Unknown column 'ID' database errors" was not settled.
  • Trailing whitespace after a top-level serialized string is dropped. Whether the PR promise covers whitespace was not settled.
  • The padded-length findings are source-supported and were not run.
  • Raw replacements in the offload, Thrive and Autoptimize code are unchanged and have no known producer.
  • The Media Library screen and the WP-CLI QA steps were not run.

🤖 Automated review · run code-review-agent_6af58f2ca83f12.57307891.


🤖 Review agent — review posted ✅ on 73544e2b · changes requested · 4 findings · 21 min

Run code-review-agent_6af58f2ca83f12.57307891 · trail

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 Approved

The PR passes: the four earlier findings are fixed, and the note asks whether any producer stores padded lengths.

Validation details
  • Files reviewed: 2/2 changed files. One history question stayed unchecked: "Renaming an image leaves the old URL in comments and term descriptions, with Unknown column 'ID' database errors".
  • PHPUnit on PHP 8.3.36 for the renamer class at HEAD: cd /repo && grep -n '"phpunit"\|install-wp-tests' composer.json; head -30 tests/bootstrap.php | grep -n "WP_TESTS\|requi… printed OK (47 tests, 101 assertions) (run R1).
  • The three new tests fail with the 73544e2 renamer [R3] and with the pr-base renamer.
  • An isolated run of reads_back() restores serialize_precision after every call [R2].
  • The security pass found no issue in the shared unserialize() call.
Untested areas
  • The history question "Renaming an image leaves the old URL in comments and term descriptions, with Unknown column 'ID' database errors" was not settled.
  • No producer of padded lengths is known, so the note stays unconfirmed.
  • The full renamer class was not run on pr-base. It timed out at the reference-cycle test.
  • The Media Library screen and the WP-CLI QA steps were not run.
  • The variant pass did not settle these places, where the fixed cause may still reach:
    • inc/media_offload.php:2993 — unverified: Reusing the supplied prior result: raw content replacement remains, but no serialized-data producer or attachment-rename path is established.
    • inc/compatibilities/thrive.php:37-51 — unverified: Reusing the supplied prior result: raw content replacement remains, but no serialized-data producer or attachment-rename path is established.

🤖 Automated review · run code-review-agent_6af5984c2fb299.43749562.


🤖 Review agent — review posted ✅ on fa309fb7 · approved · 0 findings · 23 min

Run code-review-agent_6af5984c2fb299.43749562 · trail

* @return bool True when the data serializes to the stored text.
*/
private function reads_back( $data, string $stored ): bool {
$unpadded = preg_replace( '/(^|[;{}])([saOCE]):0+(?=\d)/', '$1$2:', $stored );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

note The unpadding regex also changes text inside string payloads. A nested padded value whose string holds text like ;s:003:"x"; then fails both comparisons. Line 619 applies the plain replacement, and the inner value stops unserializing. A sandbox run showed this with a hand-built value. Does any plugin or importer store padded lengths? If none does, this is safe. If one does, remove zeros only at token positions.

@vytisbulkevicius
vytisbulkevicius merged commit 7f612ea into development Nov 12, 2026
22 checks passed
@vytisbulkevicius
vytisbulkevicius deleted the fix/rename-serialized-values branch November 12, 2026 15:04
@pirate-bot

Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 4.2.20 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@pirate-bot pirate-bot added the released Indicate that an issue has been resolved and released in a particular version of the product. label Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Indicate that an issue has been resolved and released in a particular version of the product.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants