Skip to content

Commit

Permalink
Working on Foundation middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 19, 2016
1 parent b62983e commit 893a044
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Console/Optimize/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
$basePath.'/vendor/laravel/framework/src/Illuminate/Http/Middleware/FrameGuard.php',
$basePath.'/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php',
$basePath.'/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php',
$basePath.'/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyPostSize.php',

This comment has been minimized.

Copy link
@fernandobandeira

fernandobandeira Dec 19, 2016

Contributor

Shouldn't this be ValidatePostSize ? It was renamed after all, this file doesn't exist anymore...
Sent a PR #16871 so we don't forget about it 😄

$basePath.'/vendor/symfony/http-foundation/Request.php',
$basePath.'/vendor/symfony/http-foundation/ParameterBag.php',
$basePath.'/vendor/symfony/http-foundation/FileBag.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Illuminate\Http\Exception\PostTooLargeException;

class VerifyPostSize
class ValidatePostSize
{
/**
* Handle an incoming request.
Expand Down
61 changes: 35 additions & 26 deletions src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function handle($request, Closure $next)
if (
$this->isReading($request) ||
$this->runningUnitTests() ||
$this->shouldPassThrough($request) ||
$this->inExceptArray($request) ||
$this->tokensMatch($request)
) {
return $this->addCookieToResponse($request, $next($request));
Expand All @@ -68,13 +68,34 @@ public function handle($request, Closure $next)
throw new TokenMismatchException;
}

/**

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Dec 19, 2016

Member

Would be better to use the method on the request class it ask if it was a read verb used?

* Determine if the HTTP request uses a ‘read’ verb.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function isReading($request)
{
return in_array($request->method(), ['HEAD', 'GET', 'OPTIONS']);
}

/**
* Determine if the application is running unit tests.
*
* @return bool
*/
protected function runningUnitTests()
{
return $this->app->runningInConsole() && $this->app->runningUnitTests();
}

/**
* Determine if the request has a URI that should pass through CSRF verification.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function shouldPassThrough($request)
protected function inExceptArray($request)
{
foreach ($this->except as $except) {
if ($except !== '/') {
Expand All @@ -90,36 +111,35 @@ protected function shouldPassThrough($request)
}

/**
* Determine if the application is running unit tests.
* Determine if the session and input CSRF tokens match.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function runningUnitTests()
protected function tokensMatch($request)
{
return $this->app->runningInConsole() && $this->app->runningUnitTests();
$token = $this->getTokenFromRequest($request);

return is_string($request->session()->token()) &&
is_string($token) &&
hash_equals($request->session()->token(), $token);
}

/**
* Determine if the session and input CSRF tokens match.
* Get the CSRF token from the request.
*
* @param \Illuminate\Http\Request $request
* @return bool
* @return string
*/
protected function tokensMatch($request)
protected function getTokenFromRequest($request)
{
$sessionToken = $request->session()->token();

$token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');

if (! $token && $header = $request->header('X-XSRF-TOKEN')) {
$token = $this->encrypter->decrypt($header);
}

if (! is_string($sessionToken) || ! is_string($token)) {
return false;
}

return hash_equals($sessionToken, $token);
return $token;
}

/**
Expand All @@ -142,15 +162,4 @@ protected function addCookieToResponse($request, $response)

return $response;
}

/**
* Determine if the HTTP request uses a ‘read’ verb.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function isReading($request)
{
return in_array($request->method(), ['HEAD', 'GET', 'OPTIONS']);
}
}

0 comments on commit 893a044

Please sign in to comment.