<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Session &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/session/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Secure Forms with Zend Framework</title>
		<link>/2010/04/09/secure-forms-with-zend-framework/</link>
		<comments>/2010/04/09/secure-forms-with-zend-framework/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 07:16:17 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Cryptographic hash function]]></category>
		<category><![CDATA[Hash function]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[MD5]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[web server]]></category>

		<guid isPermaLink="false">/?p=1421</guid>
		<description><![CDATA[Maybe the correct title is not &#8220;with Zend Framework&#8221;, but &#8220;with PHP&#8221;, because the general approach I used is purely PHP and no Zend Framework dependency is used. However let me mention that ZF allows you to build forms with Zend_Form, which gives you an abstraction over the HTML forms with many goodies like validation, &#8230; <a href="/2010/04/09/secure-forms-with-zend-framework/" class="more-link">Continue reading <span class="screen-reader-text">Secure Forms with Zend Framework</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
<li><a href="/2010/04/28/zend_datesetoptions-and-format_type-in-zend-framework-1-10-3/" rel="bookmark" title="Zend_Date::setOptions and format_type in Zend Framework 1.10.3">Zend_Date::setOptions and format_type in Zend Framework 1.10.3 </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Maybe the correct title is not &#8220;with Zend Framework&#8221;, but &#8220;with PHP&#8221;, because the general approach I used is purely PHP and no <a title="Zend Framework" href="http://framework.zend.com/" target="_blank">Zend Framework</a> dependency is used. However let me mention that ZF allows you to build forms with Zend_Form, which gives you an abstraction over the HTML forms with many goodies like validation, filtering and protection.</p>
<h2>Zend_Form and Zend_Form_Element_Hash</h2>
<p>Although the technique I&#8217;m using is doing the same thing, note that in ZF there&#8217;s a <a title="Zend_Form_Element_Hash" href="http://framework.zend.com/apidoc/core/Zend_Form/Element/Zend_Form_Element_Hash.html" target="_blank">Zend_Form_Element_Hash</a> which generates and validates the form, thus protecting you from CSRF attacks. The thing is that I didn&#8217;t use it because the form I&#8217;m protecting is not generated with <a title="Zend_Form" href="http://framework.zend.com/manual/en/zend.form.html" target="_blank">Zend_Form</a>, and I cannot benefit from everything ZF is giving to me. However you can easily reproduce the basic strategy with every form and every framework till it&#8217;s written in PHP.</p>
<h2>What&#8217;s the solution?</h2>
<p>It&#8217;s pretty simple and it&#8217;s described many many times around the web, simply generate a random hash, a possible solution is to use uniqid in combination with mt_rand and md5, thus you&#8217;d get quite strong hash.</p>
<p>Step two is to pass this generated hash, also stored in the session in a hidden value of the form. Of course now the most asked question is: but that&#8217;s visible to the source and thus everybody will have a valid hash.</p>
<p>There&#8217;s the trick. OK everybody will have a valid hash, but on submit the hash is validated against the SESSION variable, and as you know the session is specified between the browser (client) and the web server. Although the attacker may have a valid hash he must execute the attacking script from the same domain, possibly with the same browser, which makes the task rather difficult.</p>
<h2>An Example</h2>
<p>Let me show a breve example, it may help make things clearer.</p>
<p>1. First step &#8211; start the session</p>
<pre lang="php" escaped="true">
&lt;?php
session_start();
?&gt;
</pre>
<p>2. Second step &#8211; validate the form against the $_SESSION and generate a valid token</p>
<pre lang="php" escaped="true">
&lt;?php
if (isset($_POST['name']) &amp;&amp; $_POST['token'] == $_SESSION['token'])
    echo $_POST['name'];
else
    echo 'dont hack';

$_SESSION['token'] = md5(uniqid('test', true));
?&gt;
</pre>
<p>3. Third step &#8211; make a form</p>
<pre lang="html4strict" escaped="true">
&lt;form method="POST" action=""&gt;
&lt;input type="hidden" value="&lt;?php echo $_SESSION['token'] ?&gt;" name="token" /&gt;
&lt;input type="text" name="name" value="stoimen" /&gt;
&lt;input type="submit" name="submit" /&gt;
&lt;/form&gt;
</pre>
<p><a href="http://www.stoimen.com/projects/php.secure.forms/">Demo here</a>.</p>
<p>For more to test this you may try to make the same form somewhere else on the web and to point the action to http://www.stoimen.com/projects/php.secure.forms/! Without the session validation it&#8217;s absolutely sure you can post on the attacked server. </p>
<p>P.S. Now I&#8217;ve to admit that this have nothing to do with Zend Framework, however it&#8217;s good practice and thus may be used with every framework.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
<li><a href="/2010/04/28/zend_datesetoptions-and-format_type-in-zend-framework-1-10-3/" rel="bookmark" title="Zend_Date::setOptions and format_type in Zend Framework 1.10.3">Zend_Date::setOptions and format_type in Zend Framework 1.10.3 </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/04/09/secure-forms-with-zend-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
