<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Nette Framework forum : Development of Nette Framework : RFC – Request for Comments : nette/application: restoreRequest and redirects</title>
<link>https://forum.nette.org/en</link>
<description>Nette Framework forum</description>
<language>en</language>
<docs>http://backend.userland.com/rss</docs>
<item>
	<title>nette/application: restoreRequest and redirects (RFC – Request for Comments)</title>
	<dc:creator>enumag</dc:creator>
	<link>https://forum.nette.org/en/22160#p151172</link>
	<guid isPermaLink="false">151172@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@hrach&lt;/span&gt; I don&apos;t understand your point about
forms. Can you elaborate how the data could leak? I do use nextras/secured-links
of course. ;-)&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@xificurk&lt;/span&gt; The security issue is a minor one and
usage of nextras/secured-links takes care of it as well. I just thought it was
better to mention it, that&apos;s all.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@bckp&lt;/span&gt; I disagree. When you restore a form, it
won&apos;t be sent immediately and you don&apos;t want to lose your work by pressing F5,
do you? When you restore something else like a signal you have the redirect
there anyway.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@FilipProcházka&lt;/span&gt; :-) Are you ok with the magical
_rid parameter as well? I couldn&apos;t find a way to get rid of it.&lt;/p&gt;
</description>
	<pubDate>Sun, 15 Feb 2015 14:18:56 +0100</pubDate>
</item>
<item>
	<title>nette/application: restoreRequest and redirects (RFC – Request for Comments)</title>
	<dc:creator>Filip Procházka</dc:creator>
	<link>https://forum.nette.org/en/22160#p151122</link>
	<guid isPermaLink="false">151122@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;Good work! I agree that the &lt;code&gt;restoreRequest&lt;/code&gt; should not in fact
cause a redirect. And I have no problem with keeping the &lt;code&gt;POST&lt;/code&gt; in
session a bit longer.&lt;/p&gt;
</description>
	<pubDate>Fri, 13 Feb 2015 20:13:14 +0100</pubDate>
</item>
<item>
	<title>nette/application: restoreRequest and redirects (RFC – Request for Comments)</title>
	<dc:creator>bckp</dc:creator>
	<link>https://forum.nette.org/en/22160#p151087</link>
	<guid isPermaLink="false">151087@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;I do not like idea of keeping restored request in session. By my opinion, if
request is restored, then should be deleted, F5 will not work, but if you send
let&apos;s say comment, and press F5, it will not send it again ( becouse good
practise is to do redirect after add data ). And for the request, it should be
same idea…&lt;/p&gt;
</description>
	<pubDate>Fri, 13 Feb 2015 11:01:59 +0100</pubDate>
</item>
<item>
	<title>nette/application: restoreRequest and redirects (RFC – Request for Comments)</title>
	<dc:creator>xificurk</dc:creator>
	<link>https://forum.nette.org/en/22160#p151083</link>
	<guid isPermaLink="false">151083@http://forum.nette.org/en</guid>
	<description>&lt;h4&gt;Regarding security issue&lt;/h4&gt;

&lt;p&gt;If you let potential attacker work on your computer under your account,
you&apos;re asking for trouble. There are so many attack vectors, lot of them much
more simpler than crafting special signal URL in hopes that the first thing
you&apos;ll do when you come back is cooperate and fill in your credentials :-) The
attacker can go for your stored passwords (most people have at least some stored
in their primary browser), or services you&apos;re actually signed in at that moment
(most people are permanently signed in to their email, which in turn unlocks all
other accounts).&lt;br&gt;
For these reasons I don&apos;t think there &lt;em&gt;is&lt;/em&gt; a security issue.&lt;/p&gt;
</description>
	<pubDate>Fri, 13 Feb 2015 10:19:07 +0100</pubDate>
</item>
<item>
	<title>nette/application: restoreRequest and redirects (RFC – Request for Comments)</title>
	<dc:creator>hrach</dc:creator>
	<link>https://forum.nette.org/en/22160#p151080</link>
	<guid isPermaLink="false">151080@http://forum.nette.org/en</guid>
	<description>&lt;ul&gt;
	&lt;li&gt;I like the solution 2. This can be included in &lt;a
	href=&quot;https://github.com/nette/application/pull/4&quot;&gt;the refactoring&lt;/a&gt; of flash
	messages &amp;amp; stored requests.&lt;/li&gt;

	&lt;li&gt;Security issue:
		&lt;ul&gt;
			&lt;li&gt;Forms: I see the another reason to call it partial protection – your
			private unsubmitted data could leak!!!&lt;/li&gt;

			&lt;li&gt;Signals: your signals would be save if you use &lt;a
			href=&quot;https://github.com/nextras/secured-links&quot;&gt;nextras/secured-links&lt;/a&gt;, which
			you should use anyway.&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/li&gt;
&lt;/ul&gt;
</description>
	<pubDate>Fri, 13 Feb 2015 09:44:58 +0100</pubDate>
</item>
<item>
	<title>nette/application: restoreRequest and redirects (RFC – Request for Comments)</title>
	<dc:creator>enumag</dc:creator>
	<link>https://forum.nette.org/en/22160#p151077</link>
	<guid isPermaLink="false">151077@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;First of all I&apos;d like to say that I&apos;ve written this RFC as I understand the
behaviour. In case I failed to mention something relevant or wrote something
that is untrue, it&apos;s fully unintentional. Of course I might have missed or
misinterpreted something. In such case please tell me!&lt;/p&gt;

&lt;p&gt;I&apos;m not sure if my solution is the best, some people probably won&apos;t like it.
Maybe the best solution is to preserve the behaviour as is because of
compatibility and/or because my solution might be overcomplicated. I&apos;m fine with
this RFC getting denied, in such case I will just rework it as an addon.&lt;/p&gt;

&lt;h4&gt;Analysis of the current behaviour&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;How &lt;a
href=&quot;https://api.nette.org/nette/2.2/source-Application.UI.Presenter.php.html#739-754&quot;&gt;canonicalize&lt;/a&gt;
works:&lt;/strong&gt; The purpose of canonicalize is to automatically redirect to a
canonical URL for the current request. It means that when several URLs can
trigger the same request (usually through different routers), the user gets
redirected to the URL that he was supposed to use in the first place – the
one that the router generates for the request when creating a link to it.
&lt;em&gt;Canonicalize only works when the HTTP method is GET or HEAD.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How &lt;a
href=&quot;https://api.nette.org/nette/2.2/source-Application.UI.Presenter.php.html#652-667&quot;&gt;forward&lt;/a&gt;
works:&lt;/strong&gt; When you call &lt;code&gt;Presenter::forward()&lt;/code&gt;, new request is
created and &lt;a
href=&quot;https://api.nette.org/nette/2.2/source-Application.UI.Presenter.php.html#975&quot;&gt;gets&lt;/a&gt;
&lt;code&gt;&apos;FORWARD&apos;&lt;/code&gt; as &lt;a
href=&quot;https://api.nette.org/nette/2.2/source-Application.Request.php.html#27&quot;&gt;method&lt;/a&gt;
(instead of GET, POST or anything else). Therefore canonicalization won&apos;t be
triggered for such request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How &lt;a
href=&quot;https://api.nette.org/nette/2.2/source-Application.UI.Presenter.php.html#1106-1124&quot;&gt;restoreRequest&lt;/a&gt;
works:&lt;/strong&gt; When a request is restored using
&lt;code&gt;Presenter::restoreRequest()&lt;/code&gt;, the request is loaded from the session
and gets a RESTORED flag to be distinguishable from the normal requests. Then
the presenter forwards to this request. &lt;em&gt;However the method is NOT set to
FORWARD in this case. Therefore canonicalization IS triggered if the method was
GET or HEAD. The URL won&apos;t match of course so the user gets redirected.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;How I understand it&lt;/h4&gt;

&lt;p&gt;I believe that the redirecting behaviour for restored request was not
intended and is actually a bug. Reasons:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;canonicalization is meant to redirect one-way URLs to the canonical one, not
	to deal with forwarded requests&lt;/li&gt;

	&lt;li&gt;restoreRequest uses ForwardResponse, which means that the restored request
	is meant to be treated as a forwarded request&lt;/li&gt;

	&lt;li&gt;if the redirect was intentional it would probably be done in the
	restoreRequest method itself in the first place&lt;/li&gt;

	&lt;li&gt;the URL changes for GET requests but not for POST requests which I believe
	most people were never aware of&lt;/li&gt;

	&lt;li&gt;with the redirect the RESTORED flag is lost which might even be a security
	issue for come people&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(It was a security issue for me, see the appendix of this RFC.)&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@JanTvrdík&lt;/span&gt;: You were opposing me the most when
I sent a commit to fix it so what do you think about these reasons? I&apos;d like to
hear your opinion.&lt;/p&gt;

&lt;h4&gt;Consequences&lt;/h4&gt;

&lt;p&gt;Many people relied on this behaviour of restoreRequest. The common use-case
is that when the user got logged out automatically the request got stored. When
he loggs in again the application restores the request so that the user could
continue from whereever he was and wouldn&apos;t lose his unsaved form data for
example. Of course after he logged in again it&apos;s better to change the URL as
well to where he was when he got logged out. The restoreRequest was good for
this (at least for GET requests) so many people used it.&lt;/p&gt;

&lt;p&gt;When I tried to fix the behaviour with &lt;a
href=&quot;https://github.com/nette/application/commit/d1189771205a48b6bdda469fadee878cc874802b&quot;&gt;this
commit&lt;/a&gt; it was a BC break which is why it was reverted shortly after. (And it
was the correct decision.)&lt;/p&gt;

&lt;h4&gt;Solution&lt;/h4&gt;

&lt;p&gt;If you have a better solution or a suggestion to improve this one, please
tell me.&lt;/p&gt;

&lt;h4&gt;Step 1: New method redirectToRequest&lt;/h4&gt;

&lt;p&gt;In my opinion the restoreRequest method should not trigger a redirect. Not by
itself and not via canonicalization either. Consider the &lt;a
href=&quot;https://github.com/nette/application/commit/d1189771205a48b6bdda469fadee878cc874802b&quot;&gt;original
commit&lt;/a&gt; to also be a part of the proposed solution.&lt;/p&gt;

&lt;p&gt;But I do understand that the behaviour with redirect is useful so I
implemented a new method for it.&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;	public function redirectToRequest($key)
	{
		$session = $this-&amp;gt;getSession(&apos;Nette.Application/requests&apos;);
		if (!isset($session[$key]) || ($session[$key][0] !== NULL &amp;amp;&amp;amp; $session[$key][0] !== $this-&amp;gt;getUser()-&amp;gt;getId())) {
			return;
		}
		$request = clone $session[$key][1];
		unset($session[$key]);
		// it&apos;s useless to set the RESTORED flag, it will be lost anyway
		// $request-&amp;gt;setFlag(\Nette\Application\Request::RESTORED, TRUE);
		$parameters = $request-&amp;gt;getParameters();
		$parameters[self::FLASH_KEY] = $this-&amp;gt;getParameter(self::FLASH_KEY);
		$request-&amp;gt;setParameters($parameters);
		// $refUrl copied from Presenter::createRequest()
		$refUrl = new \Nette\Http\Url($this-&amp;gt;httpRequest-&amp;gt;getUrl());
		$refUrl-&amp;gt;setPath($this-&amp;gt;httpRequest-&amp;gt;getUrl()-&amp;gt;getScriptPath());
		$url = $this-&amp;gt;router-&amp;gt;constructUrl($request, $refUrl);
		$this-&amp;gt;sendResponse(new RedirectResponse($url));
	}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course this will throw away POST data so it is not ideal. Also after a
redirect the RESTORED flag is lost. Both of these issues are solved by the
solution below.&lt;/p&gt;

&lt;h4&gt;Step 2: Emulation for POST requests&lt;/h4&gt;

&lt;p&gt;It would be better to redirect to the correct URL even in case of POST
requests and preserve the POST data of course. Well it&apos;s AFAIK impossible to
actually redirect to a POST request but we can emulate it by keeping the stored
request in session a bit longer, adding a new parameter to the URL and
retrieving the POST data in &lt;code&gt;Presenter::run()&lt;/code&gt; right after the
redirect.&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;	const REQUEST_KEY = &apos;_rid&apos;;

	public function redirectToRequest($key)
	{
		$session = $this-&amp;gt;getSession(&apos;Nette.Application/requests&apos;);
		if (!isset($session[$key]) || ($session[$key][0] !== NULL &amp;amp;&amp;amp; $session[$key][0] !== $this-&amp;gt;getUser()-&amp;gt;getId())) {
			return;
		}
		$request = clone $session[$key][1]; // the unset was removed
		$parameters = $request-&amp;gt;getParameters();
		$parameters[self::FLASH_KEY] = $this-&amp;gt;getParameter(self::FLASH_KEY);
		$parameters[self::REQUEST_KEY] = $key; // added magic parameter
		$request-&amp;gt;setParameters($parameters);
		// $refUrl copied from Presenter::createRequest()
		$refUrl = new \Nette\Http\Url($this-&amp;gt;httpRequest-&amp;gt;getUrl());
		$refUrl-&amp;gt;setPath($this-&amp;gt;httpRequest-&amp;gt;getUrl()-&amp;gt;getScriptPath());
		$url = $this-&amp;gt;router-&amp;gt;constructUrl($request, $refUrl);
		$this-&amp;gt;sendResponse(new \Nette\Application\RedirectResponse($url));
	}

	public function run(\Nette\Application\Request $request)
	{
		if (isset($request-&amp;gt;getParameters()[self::REQUEST_KEY])) {
			$session = $this-&amp;gt;getSession(&apos;Nette.Application/requests&apos;);
			$key = $request-&amp;gt;getParameters()[self::REQUEST_KEY];
			if (isset($session[$key]) &amp;amp;&amp;amp; ($session[$key][0] === NULL || $session[$key][0] === $this-&amp;gt;getUser()-&amp;gt;getId())) {
				$stored = clone $session[$key][1]; // no unset beacuse F5 would break, this however pollutes session
				/** @var \Nette\Application\Request $stored */
				if ($stored-&amp;gt;getPresenterName() === $request-&amp;gt;getPresenterName()) { // TODO: check action as well?
					// set the RESTORED flag
					$stored-&amp;gt;setFlag(\Nette\Application\Request::RESTORED, TRUE);
					// preserve flash messages from the current request
					$parameters = $stored-&amp;gt;getParameters();
					if (isset($request-&amp;gt;getParameters()[self::FLASH_KEY])) {
						$parameters[self::FLASH_KEY] = $request-&amp;gt;getParameters()[self::FLASH_KEY];
					} else {
						unset($parameters[self::FLASH_KEY]);
					}
					$stored-&amp;gt;setParameters($parameters);
					// replace the actual request by the restored one
					$request = $stored;
				}
			}
		}
		// Continue with normal code of Presenter::run()
	}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The main issues of this solution that I expect people to dislike are the
magical &lt;code&gt;_rid&lt;/code&gt; parameter in URL when redirecting and the fact that
the request is not removed from session to keep F5 working.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@DavidGrudl&lt;/span&gt; If you want feel free to use any
part of this when creating a PR with RequestStorage.&lt;/p&gt;

&lt;h4&gt;Appendix&lt;/h4&gt;

&lt;p&gt;This is meant more as a warning for other people than as a suggestion for a
change. When a request is stored and later restored, it&apos;s not safe to
immediately do things like updating your database. The original request that was
stored might have been triggered by someone else from the same computer when the
real user was absent. &lt;code&gt;Nette\Application\UI\Form&lt;/code&gt; &lt;a
href=&quot;https://api.nette.org/nette/2.2/source-Application.UI.Form.php.html#136&quot;&gt;is
partially protected&lt;/a&gt; from this because even if the form recieves a submit
signal, it won&apos;t fire its events if the request is a restored one. I said
partially because this protection is lost when GET method is used. In that case
redirect happens and the RESTORED flag is lost.&lt;/p&gt;

&lt;p&gt;However other things than forms (like signals for deletion) are most probably
not safe from this in your application. I believe that database modifications
should almost always be done in signals. In fact I even consider this a
best-practice. Therefore I went as far as not calling
&lt;code&gt;PresenterComponent::signalReceived()&lt;/code&gt; at all for restored requests.
Here is what I use in my Presenter.&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;	public function processSignal()
	{
		if (!$this-&amp;gt;request-&amp;gt;hasFlag(Request::RESTORED)) {
			parent::processSignal();
		}
	}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&apos;m not sure if such change should be in Nette itself though. I&apos;d like to
hear your opinion about this issue as well.&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Finally I&apos;d like to thank &lt;span class=&quot;mention&quot;&gt;@castamir&lt;/span&gt; for being
the first to read this RFC and helping me greatly improve the text. :-)&lt;/p&gt;
</description>
	<pubDate>Fri, 13 Feb 2015 01:40:45 +0100</pubDate>
</item>
</channel>
</rss>
