Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Lumen] CSRF nie działa.
mrWodoo
post
Post #1





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Witam, mam problem, gdyż nie potrafię zabezpieczyć formularzy przed CSRF przy użyciu wbudowanego w framework Lumena systemu, gdyż funkcja csrf_token() nic nie zwraca, nie ma też błędów, wszystko odblokowane w app.php.
Proszę o pomoc (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
2 Stron V   1 2 >  
Start new topic
Odpowiedzi (1 - 19)
memory
post
Post #2





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


  1. $app->routeMiddleware([
  2. 'csrf' => 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken'
  3. ]);

Go to the top of the page
+Quote Post
mrWodoo
post
Post #3





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


mam (IMG:style_emoticons/default/smile.gif)

  1. $app->get('/xyz', [
  2. 'middleware' => 'csrf',
  3. 'uses' => 'App\Http\Controllers\ShopController@akc'
  4. ]);
  5.  
  6. $app->post('/xyz', [
  7. 'middleware' => 'csrf',
  8. 'uses' => 'App\Http\Controllers\ShopController@akc'
  9. ]);


i w app.php
  1. $app->middleware([
  2. 'Illuminate\Session\Middleware\StartSession',
  3. 'Illuminate\View\Middleware\ShareErrorsFromSession',
  4. 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken',
  5. ]);
  6.  
  7. $app->routeMiddleware([
  8. 'csrf' => 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken'
  9. ]);

Go to the top of the page
+Quote Post
memory
post
Post #4





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


może to głupie ale spróbuj zakomentować 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken', w middleware
Go to the top of the page
+Quote Post
mrWodoo
post
Post #5





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Na moje nieszczęście nic to nie dało (IMG:style_emoticons/default/sad.gif)

Oczywiście przy wysłaniu formularza dostaję wyjątek csrfTokenMismatch, więc ja tylko nie mam możliwości pobrania tego tokena do widoku :c
Go to the top of the page
+Quote Post
viking
post
Post #6





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Ostatnio przeglądałem trochę kod Laravela w tym względzie. Były tam dwa tokeny, jeden zwykły dla starszych wersji i drugi encrypted. Może przez to ci się nie zgadza?
Go to the top of the page
+Quote Post
mrWodoo
post
Post #7





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Nie zgadza się bo csrf_token() zwraca mi pusty string i tutaj jest mój problem
Go to the top of the page
+Quote Post
memory
post
Post #8





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


Cytat(mrWodoo @ 28.04.2015, 11:08:03 ) *
Na moje nieszczęście nic to nie dało (IMG:style_emoticons/default/sad.gif)

Oczywiście przy wysłaniu formularza dostaję wyjątek csrfTokenMismatch, więc ja tylko nie mam możliwości pobrania tego tokena do widoku :c


Z ciekawości sprawdziłem na świeżej kopii i śmiga
Go to the top of the page
+Quote Post
Pyton_000
post
Post #9





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


composer update i potem sprawdzaj czy nadal nie działa,
i pokaż kod widoku
Go to the top of the page
+Quote Post
mrWodoo
post
Post #10





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Kod widoku tu nie ma znaczenia, po prostu nie działało (miałem ten input), sprawdziłem i jak się okazało do value nic się nie wpisuje bo:

  1. <?php echo csrf_token(); ?>

nie wyświetla nic

update nic nie pomógł
Go to the top of the page
+Quote Post
Pyton_000
post
Post #11





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Zainstalowałem świeżego Lumena i:

app.php:
  1. $app->middleware([
  2. // 'Illuminate\Cookie\Middleware\EncryptCookies',
  3. // 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
  4. 'Illuminate\Session\Middleware\StartSession',
  5. // 'Illuminate\View\Middleware\ShareErrorsFromSession',
  6. 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken',
  7. ]);


Widok:
  1. == <?php echo csrf_token(); ?>
  2. ++
  3. {!! csrf_token() !!}

routes.php
  1. $app->get('/', function() use ($app) {
  2. return view('test');
  3. });
Go to the top of the page
+Quote Post
mrWodoo
post
Post #12





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Ok, a więc udało mi się pobrać csrf token, ale tylko, gdy route wyglądał tak jak podany przez Pytona, czyli wywołanie funkcji a nie wskazanie na akcję w kontrolerze, a zależy mi by właśnie to było w kontrolerze. Jakaś rada? (IMG:style_emoticons/default/smile.gif)

  1. $app->routeMiddleware([
  2. 'csrf' => 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken'
  3. ]);


  1. $app->get('/', ['uses' => 'App\Http\Controllers\IndexController@index', 'middleware' => 'csrf' ] );




  1. /**
  2.   * Set up
  3.   *
  4.   * @param PageHelper $page
  5.   * @param MailHelper $mail
  6.   */
  7. public function __construct(PageHelper $page, MailHelper $mail)
  8. {
  9. $this->_page = $page;
  10. $this->_mail = $mail;
  11.  
  12. $this->middleware('csrf');
  13.  
  14. echo csrf_token();
  15. }


nie wyświetla nic

Ten post edytował mrWodoo 28.04.2015, 12:09:09
Go to the top of the page
+Quote Post
Pyton_000
post
Post #13





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


no dobra..
Szukamy dalej.
routes.php
  1. $app->get('/', 'App\Http\Controllers\Test@index');


Controller:
  1. <?php namespace App\Http\Controllers;
  2.  
  3.  
  4. class Test extends Controller{
  5.  
  6. function __construct()
  7. {
  8. dd(csrf_token());
  9. }
  10.  
  11. public function index()
  12. {
  13. //dd(csrf_token());
  14. }
  15. }
Go to the top of the page
+Quote Post
mrWodoo
post
Post #14





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


wyświetla null
Go to the top of the page
+Quote Post
Pyton_000
post
Post #15





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


pokaż cały app.php
Go to the top of the page
+Quote Post
mrWodoo
post
Post #16





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. require_once __DIR__.'/../vendor/autoload.php';
  4.  
  5. Dotenv::load(__DIR__.'/../');
  6.  
  7. /*
  8. |--------------------------------------------------------------------------
  9. | Create The Application
  10. |--------------------------------------------------------------------------
  11. |
  12. | Here we will load the environment and create the application instance
  13. | that serves as the central piece of this framework. We'll use this
  14. | application as an "IoC" container and router for this framework.
  15. |
  16. */
  17.  
  18. $app = new Laravel\Lumen\Application;
  19.  
  20. // $app->withFacades();
  21.  
  22. // $app->withEloquent();
  23.  
  24. /*
  25. |--------------------------------------------------------------------------
  26. | Register Container Bindings
  27. |--------------------------------------------------------------------------
  28. |
  29. | Now we will register a few bindings in the service container. We will
  30. | register the exception handler and the console kernel. You may add
  31. | your own bindings here if you like or you can make another file.
  32. |
  33. */
  34.  
  35. $app->singleton(
  36. 'Illuminate\Contracts\Debug\ExceptionHandler',
  37. 'App\Exceptions\Handler'
  38. );
  39.  
  40. $app->singleton(
  41. 'Illuminate\Contracts\Console\Kernel',
  42. 'App\Console\Kernel'
  43. );
  44.  
  45. $app->singleton(
  46. 'PageHelper',
  47. function($app) {
  48. return new \App\Helpers\PageHelper( $app );
  49. }
  50. );
  51.  
  52. $app->singleton(
  53. 'MailHelper',
  54. function($app) {
  55. return new \App\Helpers\MailHelper( $app );
  56. }
  57. );
  58.  
  59. $app->singleton(
  60. 'PricingHelper',
  61. function($app) {
  62. return new \App\Helpers\PricingHelper( $app );
  63. }
  64. );
  65.  
  66. /*
  67. |--------------------------------------------------------------------------
  68. | Register Middleware
  69. |--------------------------------------------------------------------------
  70. |
  71. | Next, we will register the middleware with the application. These can
  72. | be global middleware that run before and after each request into a
  73. | route or middleware that'll be assigned to some specific routes.
  74. |
  75. */
  76. $app->middleware([
  77. // 'Illuminate\Cookie\Middleware\EncryptCookies',
  78. // 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
  79. 'Illuminate\Session\Middleware\StartSession',
  80. // 'Illuminate\View\Middleware\ShareErrorsFromSession',
  81. 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken',
  82. ]);
  83.  
  84. $app->routeMiddleware([
  85. 'csrf' => 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken'
  86. ]);
  87. /*
  88. |--------------------------------------------------------------------------
  89. | Register Service Providers
  90. |--------------------------------------------------------------------------
  91. |
  92. | Here we will register all of the application's service providers which
  93. | are used to bind services into the container. Service providers are
  94. | totally optional, so you are not required to uncomment this line.
  95. |
  96. */
  97.  
  98.  
  99.  
  100. /*
  101. |--------------------------------------------------------------------------
  102. | Load The Application Routes
  103. |--------------------------------------------------------------------------
  104. |
  105. | Next we will include the routes file so that they can all be added to
  106. | the application. This will provide all of the URLs the application
  107. | can respond to, as well as the controllers that may handle them.
  108. |
  109. */
  110.  
  111. require __DIR__.'/../app/Http/routes.php';
  112.  
  113.  
  114. return $app;
Go to the top of the page
+Quote Post
Pyton_000
post
Post #17





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


zmień SESSION_DRIVER na file w .env lub memcached
W logach na prawdę nic nie ma ? Mówię o logach z limena
Go to the top of the page
+Quote Post
mrWodoo
post
Post #18





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Ok, problem pojawia się gdy przekazuję jakieś parametry w konstruktorze kontrolera, w sensie csrf_token() przestaje działać gdy konstruktor kontrolera przyjmuje jakieś parametry, no ale jak teraz mam je przekazać w tym konstruktorze i by działał csrf? (IMG:style_emoticons/default/biggrin.gif)

Nawet więcej, csrf_token() działa do momentu dopóki metoda kontrolera nie przyjmie parametrów (dependency injection), bo przeniosłem te z konstruktora do akcji kontrolera i w konstruktorze csrf_token() działa, ale już w akcji i widoku wyświetlanym przez tą akcję nie.

Logi nic nie zawierają

  1. public function __construct(PageHelper $page, MailHelper $mail, PricingHelper $pricing, Request $request )
  2. {
  3. $this->_page = $page;
  4. $this->_mail = $mail;
  5. $this->_pricing = $pricing;
  6. $this->_request = $request;
  7.  
  8. echo csrf_token();
  9. }


Ten post edytował mrWodoo 28.04.2015, 13:38:37
Go to the top of the page
+Quote Post
Pyton_000
post
Post #19





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Wywal z konstruktora parametry i dodawaj na zmianę. Obstawiam którys helper.

Ew. singleton zadeklaruj tak"
  1. $app->singleton('\App\Providers\TestSingleton');
Go to the top of the page
+Quote Post
mrWodoo
post
Post #20





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Muszę wywalić wszystkie helpery...

a definicja nie zmieniła nic,
  1. $app->singleton(
  2. '\App\Helpers\PageHelper'
  3. );
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 12:31