Skip to content

Commit c97dbf0

Browse files
axlontaylorotwell
andauthored
[12.x] Use more secure key permissions (#1721)
* Use more secure key permissions * Update Passport.php * Update PassportServiceProvider.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b612476 commit c97dbf0

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/Console/KeysCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function handle()
5555
file_put_contents($privateKey, (string) $key);
5656
}
5757

58+
if (! windows_os()) {
59+
chmod($publicKey, 0660);
60+
chmod($privateKey, 0600);
61+
}
62+
5863
$this->info('Encryption keys generated successfully.');
5964
}
6065

src/Passport.php

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

1515
class Passport
1616
{
17+
/**
18+
* Indicates if Passport should validate the permissions of its encryption keys.
19+
*
20+
* @var bool
21+
*/
22+
public static $validateKeyPermissions = true;
23+
1724
/**
1825
* Indicates if the implicit grant type is enabled.
1926
*

src/PassportServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ protected function registerResourceServer()
309309
}
310310

311311
/**
312-
* Create a CryptKey instance without permissions check.
312+
* Create a CryptKey instance.
313313
*
314314
* @param string $type
315315
* @return \League\OAuth2\Server\CryptKey
@@ -322,7 +322,7 @@ protected function makeCryptKey($type)
322322
$key = 'file://'.Passport::keyPath('oauth-'.$type.'.key');
323323
}
324324

325-
return new CryptKey($key, null, false);
325+
return new CryptKey($key, null, Passport::$validateKeyPermissions && ! windows_os());
326326
}
327327

328328
/**

tests/Unit/PassportServiceProviderTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
class PassportServiceProviderTest extends TestCase
1313
{
14+
protected function tearDown(): void
15+
{
16+
parent::tearDown();
17+
18+
@unlink(__DIR__.'/../keys/oauth-private.key');
19+
}
20+
1421
public function test_can_use_crypto_keys_from_config()
1522
{
1623
$privateKey = openssl_pkey_new();
@@ -46,6 +53,7 @@ public function test_can_use_crypto_keys_from_local_disk()
4653

4754
openssl_pkey_export_to_file($privateKey, __DIR__.'/../keys/oauth-private.key');
4855
openssl_pkey_export($privateKey, $privateKeyString);
56+
chmod(__DIR__.'/../keys/oauth-private.key', 0600);
4957

5058
$config = m::mock(Config::class, function ($config) {
5159
$config->shouldReceive('get')->with('passport.private_key')->andReturn(null);
@@ -64,7 +72,5 @@ public function test_can_use_crypto_keys_from_local_disk()
6472
$privateKeyString,
6573
file_get_contents($cryptKey->getKeyPath())
6674
);
67-
68-
@unlink(__DIR__.'/../keys/oauth-private.key');
6975
}
7076
}

0 commit comments

Comments
 (0)