Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/optima-express/adminPage/iHomefinderAdminCommunityPages.php
+++ b/optima-express/adminPage/iHomefinderAdminCommunityPages.php
@@ -18,14 +18,17 @@
protected function getContent()
{
- if ($this->isUpdated()) {
- $title = iHomefinderUtility::getInstance()->getRequestVar("title");
- $cityZip = iHomefinderUtility::getInstance()->getRequestVar("cityZip");
- $propertyType = iHomefinderUtility::getInstance()->getRequestVar("propertyType");
- $bed = iHomefinderUtility::getInstance()->getRequestVar("bed");
- $bath = iHomefinderUtility::getInstance()->getRequestVar("bath");
- $minPrice = iHomefinderUtility::getInstance()->getRequestVar("minPrice");
- $maxPrice = iHomefinderUtility::getInstance()->getRequestVar("maxPrice");
+ // Only the real form creates a page: a POST carrying the nonce that settings_fields() prints.
+ // A GET, or a POST without a valid nonce, never creates a page (CSRF).
+ if ($this->isUpdated() && isset($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] === "POST") {
+ check_admin_referer(iHomefinderConstants::OPTION_GROUP_COMMUNITY_PAGES . "-options");
+ $title = $this->getPostValue("title");
+ $cityZip = $this->getPostValue("cityZip");
+ $propertyType = $this->getPostValue("propertyType");
+ $bed = $this->getPostValue("bed");
+ $bath = $this->getPostValue("bath");
+ $minPrice = $this->getPostValue("minPrice");
+ $maxPrice = $this->getPostValue("maxPrice");
$this->updateCommunityPages($title, $cityZip, $propertyType, $bed, $bath, $minPrice, $maxPrice);
}
?>
@@ -123,6 +126,19 @@
<?php
}
+ /**
+ * A form value from $_POST only, left slashed as WordPress delivers it, because
+ * wp_insert_post() unslashes the post it is given.
+ */
+ private function getPostValue($name)
+ {
+ $result = null;
+ if (isset($_POST[$name]) && is_scalar($_POST[$name])) {
+ $result = (string) $_POST[$name];
+ }
+ return $result;
+ }
+
private function updateCommunityPages($title, $cityZip, $propertyType, $bed, $bath, $minPrice, $maxPrice)
{
$shortCode = iHomefinderShortcodeDispatcher::getInstance()->buildSearchResultsShortCode($cityZip, $propertyType, $bed, $bath, $minPrice, $maxPrice);
--- a/optima-express/iHomefinder.php
+++ b/optima-express/iHomefinder.php
@@ -10,7 +10,7 @@
* Includes search and listing pages, widgets and shortcodes.
* Requires an IDX account from iHomefinder.
* Get a paid account with data from your MLS.
- * Version: 9.0.2
+ * Version: 9.0.3
* Author: ihomefinder
* Author URI: http://www.ihomefinder.com
* License: GPLv2 or later
--- a/optima-express/iHomefinderCacheUtility.php
+++ b/optima-express/iHomefinderCacheUtility.php
@@ -37,12 +37,12 @@
$result = null;
if (self::CACHE_ENABLED) {
$cacheKey = $this->getKey($key);
- $this->logger->debug("get cached version cacheKey " . $cacheKey);
$result = get_transient($cacheKey);
if ($result === false) {
$result = null;
}
- $this->logger->debug($result);
+ // Never log the cached value: it is a whole upstream response, including the request URL.
+ $this->logger->debug("cache " . ($result === null ? "miss" : "hit") . " cacheKey " . $cacheKey);
}
return $result;
}
--- a/optima-express/iHomefinderConstants.php
+++ b/optima-express/iHomefinderConstants.php
@@ -11,7 +11,7 @@
interface iHomefinderConstants
{
- const VERSION = "9.0.2";
+ const VERSION = "9.0.3";
const VERSION_NAME = "Optima Express";
const EXTERNAL_URL = "www.idxhome.com/service/wordpress";
//Control Panel sign-in pages (MAX-4708, D23); the test host is used only in a non-production environment
--- a/optima-express/iHomefinderLogger.php
+++ b/optima-express/iHomefinderLogger.php
@@ -22,18 +22,27 @@
return self::$instance;
}
+ /**
+ * Debug logging needs WP_DEBUG, plus either the DEBUG constant or a site administrator who
+ * sent debug=true. A visitor's request flag alone never turns it on.
+ */
private function isDebug()
{
- $debugValue = iHomefinderUtility::getInstance()->getRequestVar("debug");
- $debug = false;
- if ($debugValue === "true" || iHomefinderConstants::DEBUG) {
- $debug = true;
+ if (!defined("WP_DEBUG") || !WP_DEBUG) {
+ return false;
+ }
+ if (iHomefinderConstants::DEBUG) {
+ return true;
}
- return $debug;
+ if (!function_exists("current_user_can") || !current_user_can("manage_options")) {
+ return false;
+ }
+ return iHomefinderUtility::getInstance()->getRequestVar("debug") === "true";
}
-
+
/**
- * Dumps messages to the screen if debugging on
+ * Writes messages to the PHP error log if debugging is on. Debug output never goes to the
+ * page, not even for an administrator.
*
* @param mixed $message The message to be logged
*
@@ -42,8 +51,10 @@
public function debug($message)
{
if ($this->isDebug()) {
- echo esc_html(microtime(true)) . ": ";
- var_dump($message);
+ if (!is_string($message)) {
+ $message = print_r($message, true);
+ }
+ error_log("[Optima Express debug] " . microtime(true) . ": " . $message);
}
}
}
--- a/optima-express/iHomefinderRequestor.php
+++ b/optima-express/iHomefinderRequestor.php
@@ -96,7 +96,8 @@
$this->addParameter("leadCaptureSupport", true);
$this->addParameter("phpStyle", true);
-
+
+ $this->removeDebugParameter();
$requestUrl = $this->utility->buildUrl($requestUrl, $this->getParameters());
$ihfid = iHomefinderUrlFactory::getInstance()->getBaseUrl() . ";" . "WordpressPlugin";
@@ -244,7 +245,8 @@
$this->addParameter("method", "handleRequest");
$this->addParameter("viewType", "json");
$this->addParameter("phpStyle", true);
-
+ $this->removeDebugParameter();
+
$requestArgs = array(
"timeout" => "200",
"body" => $this->getParameters(),
@@ -439,6 +441,22 @@
return $this;
}
+ /**
+ * Never forward a visitor's debug flag to the iHomefinder service, which returns internal
+ * error detail when it is set. Matched case-insensitively.
+ */
+ private function removeDebugParameter()
+ {
+ if (is_array($this->parameters)) {
+ foreach (array_keys($this->parameters) as $name) {
+ if (strtolower((string) $name) === "debug") {
+ unset($this->parameters[$name]);
+ }
+ }
+ }
+ return $this;
+ }
+
public function addParameters($parameters)
{
if (is_array($parameters)) {
--- a/optima-express/iHomefinderShortcodeDispatcher.php
+++ b/optima-express/iHomefinderShortcodeDispatcher.php
@@ -890,6 +890,9 @@
$result .= $slug;
if (is_array($attributes)) {
foreach ($attributes as $name => $value) {
+ // Strip the characters that could end the attribute, the shortcode or open a tag. Not esc_attr():
+ // WordPress never entity-decodes shortcode attributes, so a name like Coeur d'Alene would break.
+ $value = str_replace(array(""", "[", "]", "<", ">", "\"), "", (string) $value);
$result .= " " . $name . "="" . $value . """;
}
}
--- a/optima-express/iHomefinderStateManager.php
+++ b/optima-express/iHomefinderStateManager.php
@@ -149,9 +149,21 @@
public function setSubscriberId($value)
{
- if(!empty($value)) {
- $this->setCookie(self::SUBSCRIBER_ID, $value);
+ if(self::isValidSubscriberId($value)) {
+ $this->setCookie(self::SUBSCRIBER_ID, (string) $value);
+ }
+ }
+
+ /**
+ * A subscriber id is the iHomefinder subscriber's numeric id. Anything else is rejected,
+ * not coerced, because the request value is visitor-controlled.
+ */
+ public static function isValidSubscriberId($value)
+ {
+ if(is_int($value)) {
+ return $value > 0;
}
+ return is_string($value) && preg_match('/^[1-9][0-9]{0,18}$/', $value) === 1;
}
public function hasSubscriberId()
@@ -230,13 +242,15 @@
public function getCurrentUrl()
{
- $scheme = "http";
- if(is_ssl()) {
- $scheme = "https";
- }
- $host = $_SERVER["HTTP_HOST"];
- $requestUri = $_SERVER["REQUEST_URI"];
- $result = $scheme . "://" . $host . $requestUri;
+ //The origin comes from the site's own home URL, never the visitor-supplied Host header.
+ //REQUEST_URI already includes any subdirectory, so only the scheme, host and port are taken.
+ $home = wp_parse_url(home_url());
+ $origin = $home["scheme"] . "://" . $home["host"];
+ if(!empty($home["port"])) {
+ $origin .= ":" . $home["port"];
+ }
+ $requestUri = isset($_SERVER["REQUEST_URI"]) ? wp_unslash($_SERVER["REQUEST_URI"]) : "/";
+ $result = esc_url_raw($origin . $requestUri);
return $result;
}
@@ -332,12 +346,18 @@
$expireTime = time() + self::COOKIE_TIMEOUT;
if(headers_sent()) {
//WordPress does not buffer the response so we use JS to set cookies on shortcode requests because headers have already been sent
+ //Name and value are URL-encoded, as setcookie() does, and then JSON-encoded, so no request or remote data can
+ //close the script block. PHP URL-decodes $_COOKIE on read, so readers see the original value.
+ $cookie = wp_json_encode(
+ rawurlencode((string) $name) . "=" . rawurlencode((string) $value),
+ JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
+ );
$value = '
<script type="text/javascript">
(function() {
var expire = new Date();
expire.setSeconds(expire.getSeconds() + ' . self::COOKIE_TIMEOUT . ');
- document.cookie = "' . $name . '=' . $value . '; expires=" + expire.toUTCString() + "; path=/";
+ document.cookie = ' . $cookie . ' + "; expires=" + expire.toUTCString() + "; path=/";
})();
</script>
';
--- a/optima-express/iHomefinderUtility.php
+++ b/optima-express/iHomefinderUtility.php
@@ -76,7 +76,8 @@
if (substr($url, -1) !== "?" && substr($url, -1) !== "&") {
$url .= "&";
}
- $url .= $key . "=" . urlencode(trim($value));
+ // Parameter names come from visitor requests too, so encode them like the values.
+ $url .= rawurlencode((string) $key) . "=" . urlencode(trim($value));
}
}
return $url;
--- a/optima-express/virtualPage/iHomefinderAbstractVirtualPage.php
+++ b/optima-express/virtualPage/iHomefinderAbstractVirtualPage.php
@@ -128,6 +128,10 @@
protected function getPreviousSearchLink()
{
$previousUrl = $this->stateManager->getLastSearchUrl();
+ if (!empty($previousUrl)) {
+ // The cookie is visitor-controlled: keep it only if it is an http(s) URL on this site.
+ $previousUrl = wp_validate_redirect(esc_url_raw(wp_unslash((string) $previousUrl)), "");
+ }
$text = null;
if (empty($previousUrl)) {
$previousUrl = iHomefinderUrlFactory::getInstance()->getListingsSearchFormUrl(true);
@@ -139,7 +143,7 @@
}
$result = null;
if (!empty($text) and !empty($previousUrl)) {
- $result = "<a href="" . $previousUrl . "">< " . $text . "</a>";
+ $result = "<a href="" . esc_url($previousUrl) . "">< " . $text . "</a>";
}
return $result;
}