ResearchVulnCrest analysis

When a URL Rewrite Becomes an Object Injection: Inside Optimole's Unserialize Sink

Optimole's media rename feature sweeps every text column for an attachment URL and hands serialized-looking values to a raw @unserialize() with no class restriction. Here's how that maintenance path becomes an object injection surface, and what 4.2.20 changes.

A maintenance routine that touches everything#

The Optimole media rename feature exists to solve a mundane problem: when you rename an attachment, every copy of that URL across the database needs to keep pointing at the right file. The plugin's answer is aggressive but straightforward — Optml_Attachment_Db_Renamer::replace() pulls the table list from $wpdb->tables(), iterates every text column, and selects any row whose value contains the attachment URL.

That breadth is the point. Post content, post meta, options, comments, and third-party plugin tables all get swept in the same pass. The trouble is that WordPress stores a lot of structured PHP data as serialized strings, so the renamer has to look inside those strings to rewrite URLs nested within them.

Somewhere in that "look inside" step, a database maintenance task crosses into code execution territory.

The sink: @unserialize() with no guest list#

The relevant method in 4.2.19 is Optml_Attachment_Db_Renamer::replace_urls_in_value(). When the plugin's is_serialized() heuristic says a value looks serialized, the value is passed straight to @unserialize() — no allowed_classes argument, no post-decode inspection of what came back.

Before the fix, the flow was effectively: if it looks serialized, decode it, rewrite the URL inside the decoded structure, then serialize() it and write it back. Every class name appearing in the payload is instantiated, and its magic methods run in the administrator's request context.

Two conditions gate this. The row must pass the plugin's own is_serialized() check, and it must contain the attachment URL so the LIKE '%<base_url>%' query selects it. Neither is hard to satisfy for anyone who can persist a string into a scanned table — a post, a comment, a wp_options row.

The same class is reached from the "Replace file" path via Optml_Attachment_Replace::replace(), so rename and replace both terminate at the same sink.

Why this is a design problem, not a typo#

The root cause isn't a missing check in one function — it's a trust boundary that was never drawn. The renamer treats "serialized string found in the database" as equivalent to "serialized string the plugin wrote." That equivalence does not hold on a WordPress site, where plugin and core data share tables with user-supplied content.

Object injection surfaces like this tend to stay quiet because the trigger is an admin action. A rename looks like housekeeping, which is exactly why nobody audits it. But the unserialization happens under an administrator's session, so any gadget chain reachable through loaded plugin and core classes gets that privilege level for free.

This is textbook CWE-502. The interesting part isn't the classification — it's how innocuous the surrounding feature looks.

What 4.2.20 actually changed#

The patch replaces the raw call with a guarded read_serialized() reader. The important parts:

  • allowed_classes => [ 'stdClass' ] — only plain stdClass is constructed. Anything else becomes __PHP_Incomplete_Class and is rejected downstream.
  • holds_other_objects() walks the decoded structure and returns true for any non-stdClass object, enum, or incomplete object, so the value is never re-serialized.
  • A regex check for r: and R: references refuses values that can point back at themselves, which would make the recursive walk non-terminating.
  • reads_back() verifies decoded data serializes back to the stored text before rewriting it, so values that can't round-trip safely are left byte-for-byte as stored.
  • replace_in_string() caps nesting at MAX_NESTED_DEPTH = 3 and only rewrites nested values that read back identically.

The same release adds OptimoleWP\Media\UploadsPath, which validates that paths built from attachment metadata stay inside the uploads directory before files are moved or deleted — closing a separate traversal-shaped concern in remove_all_image_sizes().

Detection and prevention for your own code#

If you maintain a plugin that reads serialized data out of the database, the check list is short and the discipline is non-negotiable:

  1. Never call unserialize() on a value that a low-privileged user could have written. Ask who can reach that column and assume the worst.
  2. Always pass allowed_classes. If you only need arrays and scalars, pass ['allowed_classes' => false]. If you need objects, enumerate them explicitly.
  3. Treat is_serialized() as a hint, not a security control — it is a shape check, not an authenticity check.
  4. Prefer json_decode() for new storage formats. Serialized PHP is a code-execution format; JSON is a data format.
  5. When you must rewrite nested serialized values, verify a round-trip before writing anything back, and leave values you can't safely decode untouched.
  6. Reach for wpdb->prepare() and column allowlists rather than scanning every table when a surgical postmeta or options lookup will do.

For detection at scale, grep your plugin code for unserialize( and check each call site for the second argument. Static analysis rules for CWE-502 will flag these, but they won't tell you whether the column is attacker-reachable — that requires reading the surrounding query.

The bug class here is not "bad PHP function"; it is "we assumed data we found in the database was data we put there." Any code that deserializes shared storage must assume the storage is untrusted.

The full version matrix and fix reference live in the Radar entry.

Search VulnCrest Radar

Are you sure?