Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/pixelines-email-protector/pixeline-email-protector.php
+++ b/pixelines-email-protector/pixeline-email-protector.php
@@ -3,7 +3,7 @@
Plugin Name: Email protector
Plugin URI: https://pixeline.be
Description: Obfuscate any email address published on the public face of a wordpress website, protecting them against spambots and email harvesters.
-Version: 1.5.0
+Version: 1.5.1
Author: pixeline
Author URI: https://pixeline.be
License: GPLv2 or later
@@ -11,7 +11,7 @@
*/
if (!defined('EMAIL_PROTECTOR_VERSION')) {
- define('EMAIL_PROTECTOR_VERSION', '1.5.0');
+ define('EMAIL_PROTECTOR_VERSION', '1.5.1');
}
if (!class_exists('WP_Email_Protector')) {
@@ -138,25 +138,46 @@
return $content;
}
- $anchor_pattern = '~<ab([^>]*?)bhrefs*=s*(?:"mailto:([^"]*)"|'mailto:([^']*)'|mailto:([^s>]+))([^>]*)>(.*?)</a>~is';
+ /* The opening tag ends at its first ">", as kses (which cleaned this content
+ when it was saved) reads it. Its attributes are then parsed properly rather
+ than searched: an "href=mailto:" that sits inside another attribute's value
+ is text, not the link's target. */
+ $anchor_pattern = '~<ab([^>]*)>(.*?)</a>~is';
$plugin = $this;
return preg_replace_callback(
$anchor_pattern,
function ($matches) use ($plugin) {
- $mailto_value = '';
- for ($i = 2; $i <= 4; $i++) {
- if (isset($matches[$i]) && $matches[$i] !== '') {
- $mailto_value = $matches[$i];
+ $attributes = $plugin->pep_parse_attributes($matches[1]);
+
+ /* Markup that cannot be read with certainty is left exactly as it was
+ written: rewriting it could change what a browser makes of it. */
+ if ($attributes === false) {
+ return $matches[0];
+ }
+
+ // Like a browser, only the first href counts.
+ $href = null;
+ foreach ($attributes as $attribute) {
+ if (strtolower($attribute['name']) === 'href') {
+ $href = $attribute['value'];
break;
}
}
- if ($mailto_value === '') {
+ if ($href === null) {
return $matches[0];
}
- $mailto_value = wp_specialchars_decode($mailto_value, ENT_QUOTES);
+ $href = trim(wp_specialchars_decode((string) $href, ENT_QUOTES));
+ if (stripos($href, 'mailto:') !== 0) {
+ return $matches[0];
+ }
+
+ $mailto_value = substr($href, strlen('mailto:'));
+ if ($mailto_value === '') {
+ return $matches[0];
+ }
/* Only the part before ?subject=…&body=… is the address; the query is
carried over untouched so the restored link still opens with it. */
@@ -164,7 +185,7 @@
$email = $query_at === false ? $mailto_value : substr($mailto_value, 0, $query_at);
$query = $query_at === false ? '' : substr($mailto_value, $query_at);
- $label = trim(strip_tags($matches[6]));
+ $label = trim(strip_tags($matches[2]));
if (
$email === ''
@@ -173,20 +194,82 @@
|| strcasecmp($label, $mailto_value) === 0
|| preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,63}/i', $label)
) {
- return $email;
+ /* The address was decoded from an attribute and now becomes page
+ text: it must be escaped, or "<script>" turns into a script. */
+ return esc_html($email);
+ }
+
+ // Rebuild the opening tag from the parsed attributes, each one re-escaped.
+ $tag = '<a';
+ foreach ($attributes as $attribute) {
+ $name = strtolower($attribute['name']);
+ if ($name === 'href' || $name === 'data-pep-email') {
+ continue;
+ }
+ $tag .= ' ' . $attribute['name'];
+ if ($attribute['value'] !== null) {
+ $tag .= '="' . esc_attr($attribute['value']) . '"';
+ }
}
- $attributes = rtrim($matches[1]) . $matches[5];
- $parked = $plugin->pep_obfuscate_address($email) . $query;
+ $parked = $plugin->pep_obfuscate_address($email) . $query;
- return '<a' . $attributes . ' data-pep-email="' . esc_attr($parked) . '">'
- . $matches[6] . '</a>';
+ return $tag . ' data-pep-email="' . esc_attr($parked) . '">'
+ . $matches[2] . '</a>';
},
$content
);
}
+ /**
+ * Read the attributes of an opening tag.
+ *
+ * Values are returned as written (entities still encoded), null for an attribute
+ * without a value. Anything the parser cannot account for character by character —
+ * a stray or unbalanced quote, an odd attribute name — makes it give up and return
+ * false, so that the caller leaves the tag alone instead of guessing.
+ *
+ * @param string $attribute_string Everything between "<a" and ">".
+ * @return array|false List of array('name' => string, 'value' => string|null).
+ */
+ function pep_parse_attributes($attribute_string)
+ {
+ $attribute_pattern = '~Gs+([A-Za-z_:][A-Za-z0-9_:.-]*)(?:s*=s*(?:"([^"]*)"|'([^']*)'|([^s"'=<>`]+)))?~';
+
+ $attributes = array();
+ $offset = 0;
+ $length = strlen($attribute_string);
+
+ while ($offset < $length) {
+ if (!preg_match($attribute_pattern, $attribute_string, $match, 0, $offset)) {
+ // Only trailing whitespace or a self-closing slash may be left over.
+ if (preg_match('~^s*/?s*$~', substr($attribute_string, $offset))) {
+ break;
+ }
+ return false;
+ }
+
+ $value = null;
+ for ($i = 2; $i <= 4; $i++) {
+ if (isset($match[$i]) && $match[$i] !== '') {
+ $value = $match[$i];
+ break;
+ }
+ }
+ // An explicitly empty value (title="") is still a value.
+ if ($value === null && strpos($match[0], '=') !== false) {
+ $value = '';
+ }
+
+ $attributes[] = array('name' => $match[1], 'value' => $value);
+ $offset += strlen($match[0]);
+ }
+
+ return $attributes;
+ }
+
+
/**
* Replace emails only in text nodes, never inside HTML tags/attributes.
*
--- a/pixelines-email-protector/tests/regression.php
+++ b/pixelines-email-protector/tests/regression.php
@@ -1,136 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-// Lightweight regression tests for plugin behavior without bootstrapping WordPress.
-
-$GLOBALS['pxln_test_options'] = array();
-
-function add_action($tag, $callback, $priority = 10, $accepted_args = 1) {}
-function add_filter($tag, $callback, $priority = 10, $accepted_args = 1) {}
-function is_admin()
-{
- return false;
-}
-function plugins_url($path = '', $plugin = '')
-{
- return 'https://example.test/wp-content/plugins/pixeline-email-protector';
-}
-function plugin_dir_path($file)
-{
- return dirname($file) . '/';
-}
-function plugin_basename($file)
-{
- return basename($file);
-}
-function load_plugin_textdomain($domain, $deprecated = false, $plugin_rel_path = false) {}
-function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $in_footer = false) {}
-function wp_specialchars_decode($string, $quote_style = ENT_COMPAT)
-{
- return html_entity_decode($string, $quote_style, 'UTF-8');
-}
-function esc_attr($text)
-{
- return htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8');
-}
-function esc_html($text)
-{
- return htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8');
-}
-function __($text, $domain = 'default')
-{
- return $text;
-}
-function register_setting($option_group, $option_name, $args = array()) {}
-function add_settings_section($id, $title, $callback, $page) {}
-function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {}
-function sanitize_text_field($value)
-{
- return trim(strip_tags((string) $value));
-}
-function wp_unslash($value)
-{
- return $value;
-}
-
-function get_option($option, $default = false)
-{
- return array_key_exists($option, $GLOBALS['pxln_test_options'])
- ? $GLOBALS['pxln_test_options'][$option]
- : $default;
-}
-
-function update_option($option, $value)
-{
- $GLOBALS['pxln_test_options'][$option] = $value;
- return true;
-}
-
-if (!function_exists('wp_html_split')) {
- function wp_html_split($input)
- {
- return preg_split('/(<[^>]+>)/', (string) $input, -1, PREG_SPLIT_DELIM_CAPTURE);
- }
-}
-
-require_once dirname(__DIR__) . '/pixeline-email-protector.php';
-
-function assert_true($condition, $message)
-{
- if (!$condition) {
- fwrite(STDERR, "Assertion failed: {$message}n");
- exit(1);
- }
-}
-
-$plugin = new WP_Email_Protector();
-
-$input = '<p>Contact: john@doe.com</p><script>var e="john@doe.com";</script><style>.x{content:"john@doe.com";}</style><pre>john@doe.com</pre>';
-$output = $plugin->email_protect($input);
-
-assert_true(strpos($output, '<span class="pep-email">john(') !== false, 'Visible content email should be replaced.');
-assert_true(strpos($output, '<script>var e="john@doe.com";</script>') !== false, 'Script block should remain unchanged.');
-assert_true(strpos($output, '<style>.x{content:"john@doe.com";}</style>') !== false, 'Style block should remain unchanged.');
-assert_true(strpos($output, '<pre>john@doe.com</pre>') !== false, 'Preformatted block should remain unchanged.');
-
-$label_output = $plugin->email_protect('john@doe.com(John Doe)');
-assert_true(strpos($label_output, 'title="John Doe"') !== false, 'Label in parenthesis should map to title attribute.');
-
-$safe_output = safe_email('john@doe.com');
-assert_true(strpos($safe_output, 'class="pep-email"') !== false, 'safe_email() should return plugin markup.');
-
-// mailto anchors: an autolink is still discarded, a labelled link is now kept.
-
-$autolink = $plugin->email_protect('<p>Write to <a href="mailto:john@doe.com">john@doe.com</a> today.</p>');
-assert_true(strpos($autolink, '<span class="pep-email">john(') !== false, 'Autolinked address should become a protected span.');
-assert_true(strpos($autolink, '<a ') === false, 'Autolink anchor should be discarded.');
-
-$shouting = $plugin->email_protect('<a href="mailto:john@doe.com">JOHN@DOE.COM</a>');
-assert_true(strpos($shouting, '<a ') === false, 'Autolink should be recognised whatever its case.');
-
-$button = $plugin->email_protect('<a class="btn-primary" target="_blank" href="mailto:john@doe.com">Get in touch</a>');
-assert_true(strpos($button, 'class="btn-primary"') !== false, 'Labelled anchor should keep its classes.');
-assert_true(strpos($button, 'target="_blank"') !== false, 'Labelled anchor should keep its other attributes.');
-assert_true(strpos($button, '>Get in touch</a>') !== false, 'Labelled anchor should keep its visible text.');
-assert_true(strpos($button, 'data-pep-email="john(') !== false, 'Labelled anchor should carry the obfuscated address.');
-assert_true(strpos($button, 'mailto:john@doe.com') === false, 'Labelled anchor should not leak the address.');
-assert_true(strpos($button, 'john@doe.com') === false, 'No readable address should survive anywhere in the markup.');
-
-$with_subject = $plugin->email_protect('<a class="btn" href="mailto:john@doe.com?subject=Hello there">Mail us</a>');
-assert_true(strpos($with_subject, '?subject=Hello there"') !== false, 'A mailto query should be carried over untouched.');
-
-$nested = $plugin->email_protect('<a class="btn" href="mailto:john@doe.com"><span>Get <b>in</b> touch</span></a>');
-assert_true(strpos($nested, '<span>Get <b>in</b> touch</span></a>') !== false, 'Markup inside the label should survive.');
-
-$single_quoted = $plugin->email_protect("<a class='btn' href='mailto:john@doe.com'>Get in touch</a>");
-assert_true(strpos($single_quoted, 'data-pep-email="john(') !== false, 'Single-quoted href should be handled too.');
-
-$address_in_label = $plugin->email_protect('<a class="btn" href="mailto:john@doe.com">Write to john@doe.com</a>');
-assert_true(strpos($address_in_label, 'data-pep-email') === false, 'A label holding an address must not produce a nested anchor.');
-assert_true(strpos($address_in_label, '<span class="pep-email">john(') !== false, 'That case falls back to a protected span.');
-
-$untouched = $plugin->email_protect('<p><a href="/pricing">Pricing</a> and john@doe.com</p>');
-assert_true(strpos($untouched, '<a href="/pricing">Pricing</a>') !== false, 'Anchors without a mailto should be left alone.');
-
-echo "All regression tests passed.n";