Skip to content

Commit

Permalink
Merge pull request #107 from paragonie/psalm-redundant
Browse files Browse the repository at this point in the history
Psalm redundant
  • Loading branch information
paragonie-scott authored Mar 27, 2018
2 parents 653bbf0 + c7c16a0 commit cbda59b
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 53 deletions.
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<directory name="./src" />
</projectFiles>
<issueHandlers>
<RedundantConditionGivenDocblockType errorLevel="info" /><!-- We can clean this up later -->
<TooFewArguments errorLevel="suppress" /> <!-- \sodium_memzero() -->
<PropertyNotSetInConstructor errorLevel="suppress" />
</issueHandlers>
Expand Down
10 changes: 0 additions & 10 deletions src/Asymmetric/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ public static function encryptWithAd(
$ourPrivateKey,
$theirPublicKey
);
// @codeCoverageIgnoreStart
if (!($ss instanceof HiddenString)) {
throw new \TypeError();
}
// @codeCoverageIgnoreEnd
$sharedSecretKey = new EncryptionKey($ss);
$ciphertext = SymmetricCrypto::encryptWithAd(
$plaintext,
Expand Down Expand Up @@ -188,11 +183,6 @@ public static function decryptWithAd(
$ourPrivateKey,
$theirPublicKey
);
// @codeCoverageIgnoreStart
if (!($ss instanceof HiddenString)) {
throw new \TypeError();
}
// @codeCoverageIgnoreEnd
$sharedSecretKey = new EncryptionKey($ss);
$plaintext = SymmetricCrypto::decryptWithAd(
$ciphertext,
Expand Down
8 changes: 2 additions & 6 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public function fetch(string $name)
return null;
}
try {
/** @var string $stored */
/** @var string|array|int|float|bool $stored */
$stored = $_COOKIE[$name];
if (!\is_string($stored)) {
throw new InvalidType('Cookie value is not a string');
}
$config = self::getConfig((string) $stored);
$config = self::getConfig($stored);
$decrypted = Crypto::decrypt(
$stored,
$this->key,
Expand Down Expand Up @@ -120,10 +120,6 @@ protected static function getConfig(string $stored): SymmetricConfig
if (\hash_equals(Binary::safeSubstr($stored, 0, 5), Halite::VERSION_PREFIX)) {
/** @var string $decoded */
$decoded = Base64UrlSafe::decode($stored);
if (!\is_string($decoded)) {
\sodium_memzero($stored);
throw new InvalidMessage('Incorrect encoding');
}
return SymmetricConfig::getConfig(
$decoded,
'encrypt'
Expand Down
7 changes: 2 additions & 5 deletions src/KeyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ public static function loadSignatureKeyPair(string $filePath): SignatureKeyPair
/**
* Export a cryptography key to a string (with a checksum)
*
* @param Key|KeyPair $key
* @param object $key
* @return HiddenString
*
* @throws CannotPerformOperation
Expand All @@ -745,8 +745,7 @@ public static function export($key): HiddenString
return self::export(
$key->getSecretKey()
);
}
if ($key instanceof Key) {
} elseif ($key instanceof Key) {
return new HiddenString(
Hex::encode(
Halite::HALITE_VERSION_KEYS . $key->getRawKeyMaterial() .
Expand All @@ -758,9 +757,7 @@ public static function export($key): HiddenString
)
);
}
// @codeCoverageIgnoreStart
throw new \TypeError('Expected a Key.');
// @codeCoverageIgnoreEnd
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ protected static function getConfig(string $stored): SymmetricConfig
) {
/** @var string $decoded */
$decoded = Base64UrlSafe::decode($stored);
if (!\is_string($decoded)) {
// @codeCoverageIgnoreStart
\sodium_memzero($stored);
throw new InvalidMessage('Invalid encoding');
// @codeCoverageIgnoreEnd
}
return SymmetricConfig::getConfig(
$decoded,
'encrypt'
Expand Down
3 changes: 2 additions & 1 deletion src/Stream/MutableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class MutableFile implements StreamInterface
* @param string|resource $file
* @throws InvalidType
* @throws FileAccessDenied
* @psalm-suppress RedundantConditionGivenDocblockType
*/
public function __construct($file)
{
Expand Down Expand Up @@ -182,7 +183,7 @@ public function readBytes(int $num, bool $skipTests = false): string
}
/** @var int $bufSize */
$bufSize = \min($remaining, self::CHUNK);
/** @var string $read */
/** @var string|bool $read */
$read = \fread($this->fp, $bufSize);
if (!\is_string($read)) {
// @codeCoverageIgnoreStart
Expand Down
4 changes: 3 additions & 1 deletion src/Stream/ReadOnlyFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ReadOnlyFile implements StreamInterface
* @throws FileError
* @throws InvalidType
* @throws \TypeError
* @psalm-suppress RedundantConditionGivenDocblockType
*/
public function __construct($file, Key $key = null)
{
Expand All @@ -81,6 +82,7 @@ public function __construct($file, Key $key = null)
'Could not open file for reading'
);
}
/** @var resource|bool $fp */
$fp = \fopen($file, 'rb');
// @codeCoverageIgnoreStart
if (!\is_resource($fp)) {
Expand Down Expand Up @@ -241,7 +243,7 @@ public function readBytes(int $num, bool $skipTests = false): string
break;
}
// @codeCoverageIgnoreEnd
/** @var string $read */
/** @var string|bool $read */
$read = \fread($this->fp, $remaining);
if (!\is_string($read)) {
// @codeCoverageIgnoreStart
Expand Down
15 changes: 0 additions & 15 deletions src/Symmetric/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ public static function decryptWithAd(
/** @var string $auth */
$auth = $pieces[5];

// @codeCoverageIgnoreStart
if (!($config instanceof Config)) {
throw new CannotPerformOperation(
'Config is not an instance of Config. This should not happen.'
);
}
// @codeCoverageIgnoreEnd

/* Split our key into two keys: One for encryption, the other for
authentication. By using separate keys, we can reasonably dismiss
likely cross-protocol attacks.
Expand Down Expand Up @@ -210,13 +202,6 @@ public static function decryptWithAd(
(string) $nonce,
(string) $encKey
);
if (!\is_string($plaintext)) {
// @codeCoverageIgnoreStart
throw new InvalidMessage(
'Invalid message'
);
// @codeCoverageIgnoreEnd
}
\sodium_memzero($encrypted);
\sodium_memzero($nonce);
\sodium_memzero($encKey);
Expand Down
8 changes: 0 additions & 8 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ public static function hkdfBlake2b(
// ORM = first L octets of T
/** @var string $orm */
$orm = Binary::safeSubstr($t, 0, $length);

// @codeCoverageIgnoreStart
if (!\is_string($orm)) {
throw new CannotPerformOperation(
'An unknown error has occurred'
);
}
// @codeCoverageIgnoreEnd
return $orm;
}

Expand Down
6 changes: 6 additions & 0 deletions test/unit/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ public function testImport()
bin2hex($encKeypair->getPublicKey()->getRawKeyMaterial()),
bin2hex($import->getRawKeyMaterial())
);

try {
KeyFactory::export(new stdClass());
$this->fail('Expected a TypeError to be raised');
} catch (TypeError $ex) {
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/unit/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public function testUnreadableFile()
$this->assertSame('Could not open file for writing', $ex->getMessage());
}
unlink($filename);

try {
new ReadOnlyFile('/etc/shadow');
$this->fail('File should not be readable');
} catch (CryptoException\FileAccessDenied $ex) {
$this->assertSame('Could not open file for reading', $ex->getMessage());
}
}

/**
Expand Down

0 comments on commit cbda59b

Please sign in to comment.