Skip to content

Commit

Permalink
Merge pull request #776 from yannickl88/fix/perm-key-check
Browse files Browse the repository at this point in the history
Removed chmod from CryptKey and add toggle to disable checking
  • Loading branch information
alexbilbie authored Aug 3, 2017
2 parents b264821 + 2aca909 commit e184691
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/CryptKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class CryptKey
/**
* @param string $keyPath
* @param null|string $passPhrase
* @param bool $keyPermissionsCheck
*/
public function __construct($keyPath, $passPhrase = null)
public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true)
{
if (preg_match(self::RSA_KEY_PATTERN, $keyPath)) {
$keyPath = $this->saveKeyToFile($keyPath);
Expand All @@ -44,21 +45,15 @@ public function __construct($keyPath, $passPhrase = null)
throw new \LogicException(sprintf('Key path "%s" does not exist or is not readable', $keyPath));
}

// Verify the permissions of the key
$keyPathPerms = decoct(fileperms($keyPath) & 0777);
if ($keyPathPerms !== '600') {
// Attempt to correct the permissions
if (chmod($keyPath, 0600) === false) {
// @codeCoverageIgnoreStart
trigger_error(
sprintf(
'Key file "%s" permissions are not correct, should be 600 instead of %s, unable to automatically resolve the issue',
$keyPath,
$keyPathPerms
),
E_USER_NOTICE
);
// @codeCoverageIgnoreEnd
if ($keyPermissionsCheck === true) {
// Verify the permissions of the key
$keyPathPerms = decoct(fileperms($keyPath) & 0777);
if (in_array($keyPathPerms, ['600', '660'], true) === false) {
trigger_error(sprintf(
'Key file "%s" permissions are not correct, should be 600 or 660 instead of %s',
$keyPath,
$keyPathPerms
), E_USER_NOTICE);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/AuthorizationServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@

class AuthorizationServerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
// Make sure the keys have the correct permissions.
chmod(__DIR__ . '/Stubs/private.key', 0600);
chmod(__DIR__ . '/Stubs/public.key', 0600);
}

public function testRespondToRequestInvalidGrantType()
{
$server = new AuthorizationServer(
Expand Down

0 comments on commit e184691

Please sign in to comment.