Skip to content

Commit

Permalink
solved host problem
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulDey12 committed Jun 7, 2020
1 parent f30df69 commit 2f8f0a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Http/Middleware/EnsureFrontendRequestsAreStateful.php
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Sanctum\Http\Middleware;

use Illuminate\Routing\Pipeline;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class EnsureFrontendRequestsAreStateful
Expand Down Expand Up @@ -58,9 +59,15 @@ public static function fromFrontend($request)

$referer = Str::replaceFirst('http://', '', $referer);

$referer = Str::endsWith('/', $referer) ? $referer : "{$referer}/";

$stateful = array_filter(config('sanctum.stateful', []));

return Str::startsWith($referer, $stateful) ||
Str::is($stateful, $referer);
$stateful = Collection::make($stateful)->map(function ($uri) {
$uri = Str::endsWith('/', $uri) ? Str::replaceLast('/', '', $uri) : $uri;
return "{$uri}/*";
})->all();

return Str::is($stateful, $referer);
}
}
10 changes: 10 additions & 0 deletions tests/EnsureFrontendRequestsAreStatefulTest.php
Expand Up @@ -25,6 +25,16 @@ public function test_request_referer_is_parsed_against_configuration()
$request->headers->set('referer', 'https://wrong.com');

$this->assertFalse(EnsureFrontendRequestsAreStateful::fromFrontend($request));

$request = Request::create('/');
$request->headers->set('referer', 'https://test.com.x');

$this->assertFalse(EnsureFrontendRequestsAreStateful::fromFrontend($request));

$request = Request::create('/');
$request->headers->set('referer', 'https://foobar.test.com/xxx');

$this->assertTrue(EnsureFrontendRequestsAreStateful::fromFrontend($request));
}

public function test_wildcard_matching()
Expand Down

0 comments on commit 2f8f0a9

Please sign in to comment.