@@ -96,6 +96,9 @@ class Form extends Container implements Nette\HtmlStringable
9696 /** @internal @var Nette\Http\IRequest used only by standalone form */
9797 public $ httpRequest ;
9898
99+ /** @var bool */
100+ protected $ crossOrigin = false ;
101+
99102 /** @var mixed or null meaning: not detected yet */
100103 private $ submittedBy ;
101104
@@ -214,6 +217,15 @@ public function setHtmlAttribute(string $name, $value = true)
214217 }
215218
216219
220+ /**
221+ * Disables CSRF protection using a SameSite cookie.
222+ */
223+ public function allowCrossOrigin (): void
224+ {
225+ $ this ->crossOrigin = true ;
226+ }
227+
228+
217229 /**
218230 * Cross-Site Request Forgery (CSRF) form protection.
219231 */
@@ -474,6 +486,9 @@ protected function receiveHttpData(): ?array
474486 }
475487
476488 if ($ httpRequest ->isMethod ('post ' )) {
489+ if (!$ this ->crossOrigin && !$ httpRequest ->isSameSite ()) {
490+ return null ;
491+ }
477492 $ data = Nette \Utils \Arrays::mergeTree ($ httpRequest ->getPost (), $ httpRequest ->getFiles ());
478493 } else {
479494 $ data = $ httpRequest ->getQuery ();
@@ -658,6 +673,7 @@ private function getHttpRequest(): Nette\Http\IRequest
658673 if (!$ this ->httpRequest ) {
659674 $ factory = new Nette \Http \RequestFactory ;
660675 $ this ->httpRequest = $ factory ->createHttpRequest ();
676+ Nette \Http \Helpers::initCookie ($ this ->httpRequest , new Nette \Http \Response );
661677 }
662678 return $ this ->httpRequest ;
663679 }
0 commit comments