Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only parse APP_URL for default stateful domains when it's set #279

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions config/sanctum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
|
*/

'stateful' => explode(',', env(
'SANCTUM_STATEFUL_DOMAINS',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,'.parse_url(env('APP_URL'), PHP_URL_HOST)
)),
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
env('APP_URL') ? ','.parse_url(env('APP_URL'), PHP_URL_HOST) : ''
dshoreman marked this conversation as resolved.
Show resolved Hide resolved
))),

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
processIsolation="false"
stopOnFailure="false"
>
<php>
<env name="APP_URL" value="https://www.test.com"/>
</php>
<testsuites>
<testsuite name="Sanctum Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
19 changes: 14 additions & 5 deletions tests/DefaultConfigContainsAppUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

class DefaultConfigContainsAppUrlTest extends TestCase
{
protected function useDefaultStatefulConfiguration($app)
protected function getEnvironmentSetUp($app)
{
putenv('APP_URL=https://www.example.com');
$config = require __DIR__.'/../config/sanctum.php';

$app->config->set('sanctum.stateful', $config['stateful']);
$app['config']->set('sanctum.stateful', $config['stateful']);
}

public function test_default_config_contains_app_url()
Expand All @@ -24,12 +25,20 @@ public function test_default_config_contains_app_url()
$this->assertContains($app_host, $config['stateful']);
}

/**
* @environment-setup useDefaultStatefulConfiguration
*/
public function test_app_url_is_not_parsed_when_missing_from_env()
{
putenv('APP_URL');

$config = require __DIR__.'/../config/sanctum.php';

$this->assertNull(env('APP_URL'));
$this->assertNotContains('', $config['stateful']);
}

public function test_request_from_app_url_is_stateful_with_default_config()
{
$request = Request::create('/');

$request->headers->set('referer', env('APP_URL'));

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