From c60e73a43444b1b0f6c57a6777e8e132d68ed2c2 Mon Sep 17 00:00:00 2001
From: Mohamed ELIDRISSI <67818913+elidrissidev@users.noreply.github.com>
Date: Sat, 30 Jul 2022 17:06:18 +0100
Subject: [PATCH 1/4] Add form key validation to Contacts form
---
.../Contacts/controllers/IndexController.php | 39 ++++++++++++++-----
app/code/core/Mage/Contacts/etc/config.xml | 3 ++
app/code/core/Mage/Contacts/etc/system.xml | 17 ++++++++
.../base/default/template/contacts/form.phtml | 1 +
.../rwd/default/template/contacts/form.phtml | 1 +
app/locale/en_US/Mage_Contacts.csv | 4 ++
6 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/app/code/core/Mage/Contacts/controllers/IndexController.php b/app/code/core/Mage/Contacts/controllers/IndexController.php
index e01a69ac747..e9f6adab7e2 100644
--- a/app/code/core/Mage/Contacts/controllers/IndexController.php
+++ b/app/code/core/Mage/Contacts/controllers/IndexController.php
@@ -28,6 +28,11 @@
*/
class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
{
+ /**
+ * Use CSRF validation flag from contacts config
+ */
+ const XML_CSRF_USE_FLAG_CONFIG_PATH = 'contacts/security/validate_formkey';
+
const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE = 'contacts/email/email_template';
@@ -64,6 +69,10 @@ public function postAction()
/** @var Mage_Core_Model_Translate $translate */
$translate->setTranslateInline(false);
try {
+ if (!$this->_validateFormKey()) {
+ Mage::throwException($this->__('Invalid Form Key. Please resubmit your request again'));
+ }
+
$postObject = new Varien_Object();
$postObject->setData($post);
@@ -82,7 +91,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 */
@@ -97,24 +106,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 69b88ce001b..8bb40a9db38 100644
--- a/app/code/core/Mage/Contacts/etc/config.xml
+++ b/app/code/core/Mage/Contacts/etc/config.xml
@@ -93,6 +93,9 @@
form_key
block output. Otherwise contact form will not work.]]>