Skip to content

Commit 4e4ced5

Browse files
Feature to treat same domain requests to be from frontend and make stateful (#564)
* Introducing the ability to dynamically include the request http host as a domain to be included in the stateful domain list. * fixying styling issues * fixying styling issues * Refactoring to introduce a fixed token for Sanctum::currentRequestHost * Fixing styling issue * Update sanctum.php * Update Sanctum.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 092da51 commit 4e4ced5

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

config/sanctum.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
1919
'%s%s',
2020
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
21-
Sanctum::currentApplicationUrlWithPort()
21+
Sanctum::currentApplicationUrlWithPort(),
22+
// Sanctum::currentRequestHost(),
2223
))),
2324

2425
/*

src/Http/Middleware/EnsureFrontendRequestsAreStateful.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Routing\Pipeline;
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Str;
8+
use Laravel\Sanctum\Sanctum;
89

910
class EnsureFrontendRequestsAreStateful
1011
{
@@ -83,7 +84,9 @@ public static function fromFrontend($request)
8384

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

86-
return Str::is(Collection::make($stateful)->map(function ($uri) {
87+
return Str::is(Collection::make($stateful)->map(function ($uri) use ($request) {
88+
$uri = $uri === Sanctum::currentRequestHost() ? $request->getHttpHost() : $uri;
89+
8790
return trim($uri).'/*';
8891
})->all(), $domain);
8992
}

src/Sanctum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public static function currentApplicationUrlWithPort()
4242
return $appUrl ? ','.parse_url($appUrl, PHP_URL_HOST).(parse_url($appUrl, PHP_URL_PORT) ? ':'.parse_url($appUrl, PHP_URL_PORT) : '') : '';
4343
}
4444

45+
/**
46+
* Get a fixed token instructing Sanctum to include the current request host in the list of stateful domains.
47+
*
48+
* @return string
49+
*/
50+
public static function currentRequestHost()
51+
{
52+
return '__SANCTUM_CURRENT_REQUEST_HOST__';
53+
}
54+
4555
/**
4656
* Set the current user for the application with the given abilities.
4757
*

tests/Feature/EnsureFrontendRequestsAreStatefulTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
7+
use Laravel\Sanctum\Sanctum;
78
use Orchestra\Testbench\Concerns\WithWorkbench;
89
use Orchestra\Testbench\TestCase;
910

@@ -59,6 +60,18 @@ public function test_request_origin_fallback()
5960
$this->assertTrue(EnsureFrontendRequestsAreStateful::fromFrontend($request));
6061
}
6162

63+
public function test_same_domain_stateful()
64+
{
65+
$request = Request::create('https://app-domain.com/');
66+
$request->headers->set('origin', 'app-domain.com');
67+
68+
config(['sanctum.stateful' => []]);
69+
$this->assertFalse(EnsureFrontendRequestsAreStateful::fromFrontend($request));
70+
71+
config(['sanctum.stateful' => [Sanctum::currentRequestHost()]]);
72+
$this->assertTrue(EnsureFrontendRequestsAreStateful::fromFrontend($request));
73+
}
74+
6275
public function test_wildcard_matching()
6376
{
6477
$request = Request::create('/');

0 commit comments

Comments
 (0)