Skip to content

Commit d055554

Browse files
elidrissidevkiatng
andauthored
Add formkey validation to Newsletter subscribe action (theme BC-break) (#1866)
* Validate formkey when subscribing to newsletter * Add a flag to control CSRF validation * Add note to flag * Fix typos and translate note Co-authored-by: Ng Kiat Siong <[email protected]> * Add new config to README.md Co-authored-by: Ng Kiat Siong <[email protected]>
1 parent 6b705e4 commit d055554

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Most important changes will be listed here, all other changes since `19.4.0` can
8282
- `admin/emails/admin_notification_email_template`
8383
- `catalog/product_image/progressive_threshold`
8484
- `catalog/search/search_separator`
85+
- `newsletter/security/enable_form_key`
8586

8687
### New Events
8788
- `adminhtml_block_widget_form_init_form_values_after`

app/code/core/Mage/Newsletter/controllers/SubscriberController.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@
3333
*/
3434
class Mage_Newsletter_SubscriberController extends Mage_Core_Controller_Front_Action
3535
{
36+
/**
37+
* Use CSRF validation flag from newsletter config
38+
*/
39+
const XML_CSRF_USE_FLAG_CONFIG_PATH = 'newsletter/security/enable_form_key';
40+
3641
/**
3742
* New subscription action
3843
*/
3944
public function newAction()
4045
{
46+
if (!$this->_validateFormKey()) {
47+
$this->_redirectReferer();
48+
return;
49+
}
50+
4151
if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
4252
$session = Mage::getSingleton('core/session');
4353
$customerSession = Mage::getSingleton('customer/session');
@@ -125,4 +135,14 @@ public function unsubscribeAction()
125135
}
126136
$this->_redirectReferer();
127137
}
138+
139+
/**
140+
* Check if form key validation is enabled in newsletter config.
141+
*
142+
* @return bool
143+
*/
144+
protected function _isFormKeyEnabled()
145+
{
146+
return Mage::getStoreConfigFlag(self::XML_CSRF_USE_FLAG_CONFIG_PATH);
147+
}
128148
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@
196196
<sending>
197197
<set_return_path>0</set_return_path>
198198
</sending>
199+
<security>
200+
<enable_form_key>0</enable_form_key>
201+
</security>
199202
</newsletter>
200203
</default>
201204
<crontab>

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@
118118
</un_email_template>
119119
</fields>
120120
</subscription>
121+
<security translate="label">
122+
<label>Security</label>
123+
<frontend_type>text</frontend_type>
124+
<sort_order>1</sort_order>
125+
<show_in_default>1</show_in_default>
126+
<show_in_website>1</show_in_website>
127+
<show_in_store>1</show_in_store>
128+
<fields>
129+
<enable_form_key translate="label comment">
130+
<label>Enable Form Key Validation</label>
131+
<frontend_type>select</frontend_type>
132+
<source_model>adminhtml/system_config_source_yesno</source_model>
133+
<sort_order>1</sort_order>
134+
<show_in_default>1</show_in_default>
135+
<show_in_website>1</show_in_website>
136+
<show_in_store>1</show_in_store>
137+
<comment><![CDATA[<strong style="color:red">Important!</strong> Enabling this option means that your custom templates used for newsletter subscription must contain <code>form_key</code> block output. Otherwise newsletter subscription will not work.]]></comment>
138+
</enable_form_key>
139+
</fields>
140+
</security>
121141
</groups>
122142
</newsletter>
123143
</sections>

app/design/frontend/base/default/template/newsletter/subscribe.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<strong><span><?php echo $this->__('Newsletter') ?></span></strong>
3030
</div>
3131
<form action="<?php echo $this->getFormActionUrl() ?>" method="post" id="newsletter-validate-detail">
32+
<?php echo $this->getBlockHtml('formkey') ?>
3233
<div class="block-content">
3334
<div class="form-subscribe-header">
3435
<label for="newsletter"><?php echo $this->__('Sign Up for Our Newsletter:') ?></label>

app/design/frontend/rwd/default/template/newsletter/subscribe.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<strong><span><?php echo $this->__('Newsletter') ?></span></strong>
3030
</div>
3131
<form action="<?php echo $this->getFormActionUrl() ?>" method="post" id="newsletter-validate-detail">
32+
<?php echo $this->getBlockHtml('formkey') ?>
3233
<div class="block-content">
3334
<div class="form-subscribe-header">
3435
<label for="newsletter"><?php echo $this->__('Sign Up for Our Newsletter:') ?></label>

app/locale/en_US/Mage_Newsletter.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
" Copy"," Copy"
2+
"<strong style=""color:red"">Important!</strong> Enabling this option means that your custom templates used for newsletter subscription must contain <code>form_key</code> block output. Otherwise newsletter subscription will not work.","<strong style=""color:red"">Important!</strong> Enabling this option means that your custom templates used for newsletter subscription must contain <code>form_key</code> block output. Otherwise newsletter subscription will not work."
23
"Action","Action"
34
"Add New Template","Add New Template"
45
"Add to Queue","Add to Queue"
@@ -32,6 +33,7 @@
3233
"Edit Queue","Edit Queue"
3334
"Edit Template","Edit Template"
3435
"Email","Email"
36+
"Enable Form Key Validation","Enable Form Key Validation"
3537
"Enter your email address","Enter your email address"
3638
"Error Code","Error Code"
3739
"Error Text","Error Text"
@@ -88,6 +90,7 @@
8890
"Save Newsletter","Save Newsletter"
8991
"Save Template","Save Template"
9092
"Save and Resume","Save and Resume"
93+
"Security","Security"
9194
"Selected problem subscribers have been unsubscribed.","Selected problem subscribers have been unsubscribed."
9295
"Selected problems have been deleted.","Selected problems have been deleted."
9396
"Sender","Sender"

0 commit comments

Comments
 (0)