Skip to content

Commit

Permalink
Merge pull request #123 from paragonie/psalm
Browse files Browse the repository at this point in the history
Integrate with Psalm
  • Loading branch information
paragonie-scott authored Feb 14, 2017
2 parents 736f6b0 + 1e71c3e commit 1925b3f
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 48 deletions.
36 changes: 23 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
sudo: false

matrix:
fast_finish: true
include:
- php: "5.3"
env: USE_PSALM=0
- php: "5.4"
env: USE_PSALM=0
- php: "5.5"
env: USE_PSALM=0
- php: "5.6"
env: USE_PSALM=1
- php: "7.0"
env: USE_PSALM=1
- php: "7.1"
env: USE_PSALM=1
- php: "hhvm"
env: USE_PSALM=1
allow_failures:
- php: hhvm

sudo: false
- php: "hhvm"

install:
- composer install
- composer self-update
- composer install
- if [[ $USE_PSALM -eq 1 ]]; then composer require --dev "vimeo/psalm:dev-master"; fi

script:
- ./phpunit.sh
- vendor/bin/phpunit
- php -dmbstring.func_overload=7 vendor/bin/phpunit
- if [[ $USE_PSALM -eq 1 ]]; then vendor/bin/psalm; fi
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### Version 2.0.5 - 2017-??-??

* Run random_compat through the static analysis tool, [psalm](https://github.com/vimeo/psalm),
as part of our continuous integration process.

### Version 2.0.4 - 2016-11-07

* Don't unnecessarily prevent `mcrypt_create_iv()` from being used.
Expand Down
14 changes: 7 additions & 7 deletions lib/byte_safe_strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function RandomCompat_strlen($binary_string)
);
}

return mb_strlen($binary_string, '8bit');
return (int) mb_strlen($binary_string, '8bit');
}

} else {
Expand All @@ -73,7 +73,7 @@ function RandomCompat_strlen($binary_string)
'RandomCompat_strlen() expects a string'
);
}
return strlen($binary_string);
return (int) strlen($binary_string);
}
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ function RandomCompat_substr($binary_string, $start, $length = null)
* mb_substr($str, 0, NULL, '8bit') returns an empty string on
* PHP 5.3, so we have to find the length ourselves.
*/
$length = RandomCompat_strlen($length) - $start;
$length = RandomCompat_strlen($binary_string) - $start;
} elseif (!is_int($length)) {
throw new TypeError(
'RandomCompat_substr(): Third argument should be an integer, or omitted'
Expand All @@ -130,10 +130,10 @@ function RandomCompat_substr($binary_string, $start, $length = null)
return '';
}
if ($start > RandomCompat_strlen($binary_string)) {
return false;
return '';
}

return mb_substr($binary_string, $start, $length, '8bit');
return (string) mb_substr($binary_string, $start, $length, '8bit');
}

} else {
Expand Down Expand Up @@ -172,10 +172,10 @@ function RandomCompat_substr($binary_string, $start, $length = null)
);
}

return substr($binary_string, $start, $length);
return (string) substr($binary_string, $start, $length);
}

return substr($binary_string, $start);
return (string) substr($binary_string, $start);
}
}
}
8 changes: 5 additions & 3 deletions lib/cast_to_int.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @param int|float $number The number we want to convert to an int
* @param boolean $fail_open Set to true to not throw an exception
*
* @return int (or float if $fail_open)
* @return int|float
*
* @throws TypeError
*/
Expand All @@ -60,8 +60,10 @@ function RandomCompat_intval($number, $fail_open = false)
$number = (int) $number;
}

if (is_int($number) || $fail_open) {
return $number;
if (is_int($number)) {
return (int) $number;
} elseif ($fail_open) {
return (float) $number;
}

throw new TypeError(
Expand Down
4 changes: 4 additions & 0 deletions lib/random.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ class_exists('COM')
/**
* We don't have any more options, so let's throw an exception right now
* and hope the developer won't let it fail silently.
*
* @param mixed $length
* @return void
* @throws Exception
*/
function random_bytes($length)
{
Expand Down
5 changes: 5 additions & 0 deletions lib/random_bytes_com_dotnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ function random_bytes($bytes)
}

$buf = '';
if (!class_exists('COM')) {
throw new Error(
'COM does not exist'
);
}
$util = new COM('CAPICOM.Utilities.1');
$execCount = 0;

Expand Down
35 changes: 26 additions & 9 deletions lib/random_bytes_dev_urandom.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,50 @@ function random_bytes($bytes)
* page load.
*/
if (!empty($fp)) {
/**
* @var int
*/
$remaining = $bytes;

/**
* @var string|bool
*/
$buf = '';

/**
* We use fread() in a loop to protect against partial reads
*/
do {
/**
* @var string|bool
*/
$read = fread($fp, $remaining);
if ($read === false) {
/**
* We cannot safely read from the file. Exit the
* do-while loop and trigger the exception condition
*/
$buf = false;
break;
if (!is_string($read)) {
if ($read === false) {
/**
* We cannot safely read from the file. Exit the
* do-while loop and trigger the exception condition
*
* @var string|bool
*/
$buf = false;
break;
}
}
/**
* Decrease the number of bytes returned from remaining
*/
$remaining -= RandomCompat_strlen($read);
$buf .= $read;
/**
* @var string|bool
*/
$buf = $buf . $read;
} while ($remaining > 0);

/**
* Is our result valid?
*/
if ($buf !== false) {
if (is_string($buf)) {
if (RandomCompat_strlen($buf) === $bytes) {
/**
* Return our random entropy buffer here:
Expand Down
2 changes: 1 addition & 1 deletion lib/random_bytes_libsodium_legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function random_bytes($bytes)
$buf = Sodium::randombytes_buf($bytes);
}

if ($buf !== false) {
if (is_string($buf)) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
Expand Down
5 changes: 0 additions & 5 deletions lib/random_int.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ function random_int($min, $max)
* Let's grab the necessary number of random bytes
*/
$randomByteString = random_bytes($bytes);
if ($randomByteString === false) {
throw new Exception(
'Random number generator failure'
);
}

/**
* Let's turn $randomByteString into an integer
Expand Down
8 changes: 1 addition & 7 deletions other/ide_stubs/COM.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ class COM
*/
public function GetRandom($bytes, $dummy)
{
static $fp = null;
if (!$fp) {
$fp = fopen('/dev/urandom', 'rb');
}
return fread($fp, $bytes);
return '';
}
}

throw new Exception('Attempting to include IDE stub files in a project.');
2 changes: 2 additions & 0 deletions other/ide_stubs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

These exist to reduce false positive errors on PHPStorm and other IDEs.

They also exist so Psalm has some idea what's going on.

Don't use them in your project.
2 changes: 0 additions & 2 deletions other/ide_stubs/com_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ class com_exception extends Exception
{

}

throw new Exception('Attempting to include IDE stub files in a project.');
90 changes: 90 additions & 0 deletions other/ide_stubs/libsodium.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* This does nothing if the libsodium extension is loaded, so it's harmless.
*
* This file alone is released under CC0 and WTFPL dual licensing.
*/
namespace Sodium {
if (!extension_loaded('libsodium')) {

/**
* Generate a string of random bytes
* /dev/urandom
*
* @param int $length
* @return string
*/
function randombytes_buf(
$length
)
{
return '';
}

/**
* Generate a 16-bit integer
* /dev/urandom
*
* @return int
*/
function randombytes_random16()
{
return '';
}

/**
* Generate an unbiased random integer between 0 and a specified value
* /dev/urandom
*
* @param int $upperBoundNonInclusive
* @return int
*/
function randombytes_uniform(
$upperBoundNonInclusive
)
{
return 0;
}
}
}
namespace {
class Sodium
{

/**
* Generate a string of random bytes
* /dev/urandom
*
* @param int $length
* @return string
*/
public static function randombytes_buf($length)
{
return '';
}

/**
* Generate a 16-bit integer
* /dev/urandom
*
* @return int
*/
public static function randombytes_random16()
{
return '';
}

/**
* Generate an unbiased random integer between 0 and a specified value
* /dev/urandom
*
* @param int $upperBoundNonInclusive
* @return int
*/
public static function randombytes_uniform($upperBoundNonInclusive = 0)
{
return 0;
}
}
}
1 change: 1 addition & 0 deletions psalm-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once 'lib/byte_safe_strings.php';
require_once 'lib/cast_to_int.php';
require_once 'lib/error_polyfill.php';
require_once 'other/ide_stubs/libsodium.php';
require_once 'lib/random.php';

$int = random_int(0, 65536);
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</projectFiles>
<issueHandlers>
<DuplicateClass errorLevel="info" />
<UndefinedClass errorLevel="info" />
<FailedTypeResolution errorLevel="info" />
<InvalidArgument errorLevel="info" />
<InvalidOperand errorLevel="info" />
Expand Down

0 comments on commit 1925b3f

Please sign in to comment.