6

I have a plugin for my app that users can implement in their website that contains a form. The problem is that I get TokenMismatchExceptionwhen the form is submitted. From all research I've done I can see is a protection for cross-origin from Laravel.

I know I can disable it, but I don't know how I will protect the form after that?

Have any of you came across this issue? What is the best practice?

Thank you

Note: I've noticed that if the user has previously visited the original website and then goes to the website where the iframe is included, the Exception is not throwing anymore.

lesandru
  • 839
  • 2
  • 11
  • 27

4 Answers4

8

you need to change the same_site value in config/session.php .

like this

'same_site' => 'none',
'secure' => env('SESSION_SECURE_COOKIE'),

and set SESSION_SECURE_COOKIE=true in the .env file.

more info check this youtube video

Mohamed SLimani
  • 351
  • 2
  • 9
2

Laravel doesn't allow forms to be submitted from other domains, but you can set an exception.

You can add the exception in App/Http/Middleware/VerifyCsrfToken.php.

protected $except = [
    'post/something'
];

Source: EasyLaravel

Alex
  • 4,674
  • 5
  • 38
  • 59
  • I just answered to @Houssain Amrani that I've included it already – lesandru Nov 26 '15 at 22:52
  • Are you sure the CSRF field gets rendered? Does the data gets sent to your server correctly? You can check that in FireBug or Chrome's Debug tool. – Alex Nov 26 '15 at 22:55
  • yeah man. The CSRF gets rendered, everything works fine in Firefox and Chrome, but in Safari I get the TokenMismatchException – lesandru Nov 26 '15 at 22:56
  • check my note in question, that's something weird, if I visit the original website the iframe works fine after. I'm thinking it might be related to cookies – lesandru Nov 26 '15 at 22:57
  • I might have an idea. Add the URL the iframe gets submitted to the `$except` array in `Middleware/VerifyCsrfToken.php`. – Alex Nov 26 '15 at 23:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96298/discussion-between-lesandru-and-alex). – lesandru Nov 26 '15 at 23:04
0

if I understood the problem. probably the submit form must have a token input that contains token session.

<form mothod = 'post' action = 'YourAction'>

<input type = 'hidden' name = '_token' value = '{{Session:token()}}'>
.
.
.
</form>
Houssain Amrani
  • 329
  • 2
  • 6
0

You can disable CSRF token check for the specific URL by adding the URL in app/Http/Middleware/VerifyCsrfToken.php file in except array

Akshay Khale
  • 8,151
  • 8
  • 50
  • 58