Skip to content

Commit ddec006

Browse files
authored
[Backport] Added form key validation to Contacts form (#4610)
* added form_key to templates * added config option * added translation * added validation * added ddev cypress commands * added simple cypress test * updated .gitignore * added ddev cypress commands * doc * removed cypress for now
1 parent c48ccee commit ddec006

File tree

9 files changed

+63
-9
lines changed

9 files changed

+63
-9
lines changed

.cypress.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
e2e: {
3+
setupNodeEvents(on, config) {
4+
// implement node event listeners here
5+
},
6+
baseUrl: 'https://magento-lts.ddev.site'
7+
},
8+
};

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ phpunit.xml
9797

9898
# DDEV
9999
.ddev/.sampleData
100+
.ddev/addon-metadata
100101
.ddev/config.yaml
101102
.ddev/config.*.yaml
102103
.ddev/xhgui
103104
.ddev/xhprof
104-
.ddev/docker-compose.xhgui.yaml
105-
.ddev/docker-compose.xhgui_norouter.yaml
105+
.ddev/docker-compose.*.yaml
106106
app/etc/includes/ddev.xhgui.php
107+
108+
# local development
109+
.localdev

app/code/core/Mage/Contacts/controllers/IndexController.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
*/
2323
class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
2424
{
25+
/**
26+
* Use CSRF validation flag from contacts config
27+
*/
28+
public const XML_CSRF_USE_FLAG_CONFIG_PATH = 'contacts/security/enable_form_key';
2529
public const XML_PATH_ENABLED = 'contacts/contacts/enabled';
2630
public const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
2731
public const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
@@ -61,6 +65,10 @@ public function postAction()
6165
/** @var Mage_Core_Model_Translate $translate */
6266
$translate->setTranslateInline(false);
6367
try {
68+
if (!$this->_validateFormKey()) {
69+
Mage::throwException($this->__('Invalid Form Key. Please submit your request again.'));
70+
}
71+
6472
$postObject = new Varien_Object();
6573
$postObject->setData($post);
6674

@@ -112,16 +120,26 @@ public function postAction()
112120

113121
$translate->setTranslateInline(true);
114122
Mage::getSingleton('customer/session')->addSuccess($this->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
115-
} catch (Mage_Core_Exception $e) {
123+
} catch (Mage_Core_Exception $exception) {
116124
$translate->setTranslateInline(true);
117-
Mage::logException($e);
118-
Mage::getSingleton('customer/session')->addError($e->getMessage());
119-
} catch (Exception $e) {
120-
Mage::logException($e);
125+
Mage::logException($exception);
126+
Mage::getSingleton('customer/session')->addError($exception->getMessage());
127+
} catch (Throwable $throwable) {
128+
Mage::logException($throwable);
121129
Mage::getSingleton('customer/session')->addError($this->__('Unable to submit your request. Please, try again later'));
122130
}
123131
}
124132

125133
$this->_redirect('*/*/');
126134
}
135+
136+
/**
137+
* Check if form key validation is enabled in contacts config.
138+
*
139+
* @return bool
140+
*/
141+
protected function _isFormKeyEnabled()
142+
{
143+
return Mage::getStoreConfigFlag(self::XML_CSRF_USE_FLAG_CONFIG_PATH);
144+
}
127145
}

app/code/core/Mage/Contacts/etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
<sender_email_identity>custom2</sender_email_identity>
9292
<email_template>contacts_email_email_template</email_template>
9393
</email>
94+
<security>
95+
<enable_form_key>0</enable_form_key>
96+
</security>
9497
</contacts>
9598
</default>
9699
</config>

app/code/core/Mage/Contacts/etc/system.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@
107107
</email_template>
108108
</fields>
109109
</auto_reply>
110+
<security translate="label">
111+
<label>Security</label>
112+
<sort_order>100</sort_order>
113+
<show_in_default>1</show_in_default>
114+
<show_in_website>1</show_in_website>
115+
<show_in_store>1</show_in_store>
116+
<fields>
117+
<enable_form_key translate="label comment">
118+
<label>Enable Form Key Validation</label>
119+
<frontend_type>select</frontend_type>
120+
<source_model>adminhtml/system_config_source_yesno</source_model>
121+
<sort_order>1</sort_order>
122+
<show_in_default>1</show_in_default>
123+
<show_in_website>1</show_in_website>
124+
<show_in_store>1</show_in_store>
125+
<comment><![CDATA[<strong style="color:red">Important!</strong> Enabling this option means that your custom templates used for contacts form must contain <code>form_key</code> block output. Otherwise contacts form will not work.]]></comment>
126+
</enable_form_key>
127+
</fields>
128+
</security>
110129
</groups>
111130
</contacts>
112131
</sections>

app/design/frontend/base/default/template/contacts/form.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
2121
</div>
2222
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
23+
<?php echo $this->getBlockHtml('formkey') ?>
2324
<div class="fieldset">
2425
<h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
2526
<ul class="form-list">

app/design/frontend/rwd/default/template/contacts/form.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
2121
</div>
2222
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" class="scaffold-form">
23+
<?php echo $this->getBlockHtml('formkey') ?>
2324
<div class="fieldset">
2425
<h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
2526
<p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>

app/locale/en_US/Mage_Contacts.csv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"* Required Fields","* Required Fields"
2+
"<strong style=""color:red"">Important!</strong> Enabling this option means that your custom templates used for contacts form must contain <code>form_key</code> block output. Otherwise contacts form will not work.","<strong style=""color:red"">Important!</strong> Enabling this option means that your custom templates used for contacts form must contain <code>form_key</code> block output. Otherwise contacts form will not work."
23
"Auto Reply","Auto Reply"
34
"Warning! Enabling this feature may cause unwanted messages to be sent to people whose email addresses are being used abusively. Please make sure that you have implemented security measures before enabling (e.g. CAPTCHA, HoneySpam).","Warning! Enabling this feature may cause unwanted messages to be sent to people whose email addresses are being used abusively. Please make sure that you have implemented security measures before enabling (e.g. CAPTCHA, HoneySpam)."
45
"Comment","Comment"
@@ -14,9 +15,10 @@
1415
"Email Template","Email Template"
1516
"Enable Contact Us","Enable Contact Us"
1617
"Enable Auto Reply","Enable Auto Reply"
18+
"Invalid Form Key. Please submit your request again.","Invalid Form Key. Please submit your request again."
1719
"Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.","Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us."
1820
"Name","Name"
1921
"Send Emails To","Send Emails To"
2022
"Submit","Submit"
2123
"Telephone","Telephone"
22-
"Unable to submit your request. Please, try again later","Unable to submit your request. Please, try again later"
24+
"Unable to submit your request. Please, try again later","Unable to submit your request. Please try again later."

docs/content/developers/changelog/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ UPS shut down their old CGI APIs, so we removed the support for it from the `Mag
110110
- PHP 8.1 as minimum required version
111111
- Removed `scriptaculous/dragdrop.js` ([#3215](https://github.com/OpenMage/magento-lts/pull/3215))
112112
- RWD theme: updated jQuery to 3.7.0 ([#3204](https://github.com/OpenMage/magento-lts/pull/3204))
113-
- Unified CSRF configuration ([#3147](https://github.com/OpenMage/magento-lts/pull/3147)) and added form key validation to Contacts form ([#3146](https://github.com/OpenMage/magento-lts/pull/3146))
114113
- Removed double span element from HTML buttons ([#3123](https://github.com/OpenMage/magento-lts/pull/3123))
115114
- Removed all deprecated `Mysql4_` classes ([#2730](https://github.com/OpenMage/magento-lts/pull/2730)). If there are any old modules/extensions in your installation that use such classes, you must run `shell/rename-mysql4-class-to-resource.php` in the command line in order to convert them. Backup all files before running the script
116115
- Removed "admin routing compatibility mode" ([#1551](https://github.com/OpenMage/magento-lts/pull/1551))

0 commit comments

Comments
 (0)