diff --git a/README.md b/README.md
index 0bf9d69401b..a9df78ad94b 100644
--- a/README.md
+++ b/README.md
@@ -245,6 +245,7 @@ If you see SQL errors after upgrading please remember to check for this specific
- `admin/emails/admin_notification_email_template`
- `catalog/product_image/progressive_threshold`
- `catalog/search/search_separator`
+- `contacts/security/validate_formkey`
- `dev/log/max_level`
- `newsletter/security/enable_form_key`
- `sitemap/category/lastmod`
diff --git a/app/code/core/Mage/Contacts/controllers/IndexController.php b/app/code/core/Mage/Contacts/controllers/IndexController.php
index 55c392a652b..1dcedb9e1f1 100644
--- a/app/code/core/Mage/Contacts/controllers/IndexController.php
+++ b/app/code/core/Mage/Contacts/controllers/IndexController.php
@@ -28,10 +28,11 @@
*/
class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
{
- public const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
- public const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
- public const XML_PATH_EMAIL_TEMPLATE = 'contacts/email/email_template';
- public const XML_PATH_ENABLED = 'contacts/contacts/enabled';
+ public const XML_CSRF_USE_FLAG_CONFIG_PATH = 'contacts/security/validate_formkey';
+ public const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
+ public const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
+ public const XML_PATH_EMAIL_TEMPLATE = 'contacts/email/email_template';
+ public const XML_PATH_ENABLED = 'contacts/contacts/enabled';
/**
* @return $this
@@ -65,6 +66,10 @@ public function postAction()
/** @var Mage_Core_Model_Translate $translate */
$translate->setTranslateInline(false);
try {
+ if (!$this->_validateFormKey()) {
+ Mage::throwException($this->__('Invalid Form Key. Please submit your request again.'));
+ }
+
$postObject = new Varien_Object();
$postObject->setData($post);
@@ -83,7 +88,7 @@ public function postAction()
}
if ($error) {
- throw new Exception();
+ Mage::throwException($this->__('Unable to submit your request. Please, try again later'));
}
$mailTemplate = Mage::getModel('core/email_template');
/** @var Mage_Core_Model_Email_Template $mailTemplate */
@@ -98,24 +103,36 @@ public function postAction()
);
if (!$mailTemplate->getSentSuccess()) {
- throw new Exception();
+ Mage::throwException($this->__('Unable to submit your request. Please, try again later'));
}
$translate->setTranslateInline(true);
- Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
- $this->_redirect('*/*/');
+ Mage::getSingleton('customer/session')->addSuccess($this->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
+ } catch (Mage_Core_Exception $e) {
+ $translate->setTranslateInline(true);
- return;
- } catch (Exception $e) {
+ Mage::logException($e);
+ Mage::getSingleton('customer/session')->addError($e->getMessage());
+ } catch (Throwable $e) {
$translate->setTranslateInline(true);
- Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
- $this->_redirect('*/*/');
- return;
+ Mage::logException($e);
+ Mage::getSingleton('customer/session')->addError($this->__('Unable to submit your request. Please, try again later'));
}
+ $this->_redirect('*/*/');
} else {
$this->_redirect('*/*/');
}
}
+
+ /**
+ * Check if form key validation is enabled in contacts config.
+ *
+ * @return bool
+ */
+ protected function _isFormKeyEnabled()
+ {
+ return Mage::getStoreConfigFlag(self::XML_CSRF_USE_FLAG_CONFIG_PATH);
+ }
}
diff --git a/app/code/core/Mage/Contacts/etc/config.xml b/app/code/core/Mage/Contacts/etc/config.xml
index 62df870a413..74e8c38b317 100644
--- a/app/code/core/Mage/Contacts/etc/config.xml
+++ b/app/code/core/Mage/Contacts/etc/config.xml
@@ -94,6 +94,9 @@
form_key
block output. Otherwise contact form will not work.]]>