plvhx /
jwt
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Gandung\JWT; |
||
| 4 | |||
| 5 | use Gandung\JWT\Manager\KeyManagerInterface; |
||
| 6 | use Gandung\JWT\Token\JoseBuilderInterface; |
||
| 7 | use Gandung\JWT\Token\PayloadBuilderInterface; |
||
| 8 | use Gandung\JWT\Proxy\HMAC; |
||
| 9 | use Gandung\JWT\Proxy\RSA; |
||
| 10 | use Gandung\JWT\Proxy\ECDSA; |
||
| 11 | use Gandung\JWT\Parser\Parser; |
||
| 12 | use Gandung\JWT\Parser\ParserInterface; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @author Paulus Gandung Prakosa <[email protected]> |
||
| 16 | */ |
||
| 17 | class JWT |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $algo = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $proxy; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ParserInterface |
||
| 31 | */ |
||
| 32 | private $parser; |
||
| 33 | |||
| 34 | public function __construct(ParserInterface $parser) |
||
| 35 | { |
||
| 36 | $this->proxy = [ |
||
| 37 | 'hmac' => HMAC::create(), |
||
| 38 | 'rsa' => RSA::create(), |
||
| 39 | 'ecdsa' => ECDSA::create() |
||
| 40 | ]; |
||
| 41 | $this->parser = $parser; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * For chainability purpose. |
||
| 46 | */ |
||
| 47 | public static function create() |
||
| 48 | { |
||
| 49 | return new static(new Parser()); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function createToken( |
||
| 56 | JoseBuilderInterface $jose, |
||
| 57 | PayloadBuilderInterface $payload, |
||
| 58 | KeyManagerInterface $key |
||
| 59 | ) { |
||
| 60 | $portion = []; |
||
| 61 | |||
| 62 | $portion[] = $jose->getValue(); |
||
| 63 | $portion[] = $payload->getValue(); |
||
| 64 | $currentAlgo = $portion[0]['alg']; |
||
| 65 | |||
| 66 | $this->determineSigningAlgorithm($currentAlgo); |
||
| 67 | $this->validateKey($key, $currentAlgo); |
||
| 68 | |||
| 69 | $portion = join('.', \array_map(function ($q) { |
||
| 70 | return \Gandung\JWT\__jwt_base64UrlEncode( |
||
| 71 | \json_encode( |
||
| 72 | $q, |
||
| 73 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | }, $portion)); |
||
| 77 | |||
| 78 | $signature = $this->algo[$currentAlgo]->sign($portion, $key); |
||
| 79 | |||
| 80 | return $portion . '.' . \Gandung\JWT\__jwt_base64UrlEncode($signature); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function verifyToken( |
||
| 87 | $expected, |
||
| 88 | JoseBuilderInterface $jose, |
||
| 89 | PayloadBuilderInterface $payload, |
||
| 90 | KeyManagerInterface $key |
||
| 91 | ) { |
||
| 92 | $portion = []; |
||
| 93 | $portion[] = $jose->getValue(); |
||
| 94 | $portion[] = $payload->getValue(); |
||
| 95 | |||
| 96 | $this->parser->parse($expected); |
||
| 97 | |||
| 98 | $signature = $this->parser->getSignature(); |
||
| 99 | $currentAlgo = $portion[0]['alg']; |
||
| 100 | |||
| 101 | $this->determineSigningAlgorithm($currentAlgo); |
||
| 102 | |||
| 103 | $portion = join('.', \array_map(function ($q) { |
||
| 104 | return \Gandung\JWT\__jwt_base64UrlEncode( |
||
| 105 | \json_encode( |
||
| 106 | $q, |
||
| 107 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
||
| 108 | ) |
||
| 109 | ); |
||
| 110 | }, $portion)); |
||
| 111 | |||
| 112 | return $this->algo[$currentAlgo]->verify( |
||
| 113 | $signature, |
||
| 114 | $portion, |
||
| 115 | $key |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | private function determineSigningAlgorithm($algorithm) |
||
| 120 | { |
||
| 121 | if (!isset($this->algo[$algorithm])) { |
||
| 122 | if ($algorithm[0] === 'H') { |
||
| 123 | $this->algo[$algorithm] = \call_user_func([$this->proxy['hmac'], 'get' . $algorithm]); |
||
| 124 | } elseif ($algorithm[0] === 'R') { |
||
| 125 | $this->algo[$algorithm] = \call_user_func([$this->proxy['rsa'], 'get' . $algorithm]); |
||
| 126 | } elseif ($algorithm[0] === 'E') { |
||
| 127 | $this->algo[$algorithm] = \call_user_func([$this->proxy['ecdsa'], 'get' . $algorithm]); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | private function validateKey(KeyManagerInterface $key, $algorithm) |
||
| 133 | { |
||
| 134 | $header = [ |
||
| 135 | 'R' => ['BEGIN PRIVATE KEY', 'BEGIN ENCRYPTED PRIVATE KEY'], |
||
| 136 | 'E' => 'BEGIN EC PRIVATE KEY' |
||
| 137 | ]; |
||
| 138 | |||
| 139 | if ($algorithm[0] === 'H') { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | $content = $key->getContent(); |
||
| 144 | |||
| 145 | if (is_array($header[$algorithm[0]])) { |
||
| 146 | foreach ($header[$algorithm[0]] as $h) { |
||
|
0 ignored issues
–
show
|
|||
| 147 | $isHeaderMatched = strpos($content, $h) === false |
||
| 148 | ? false |
||
| 149 | : true; |
||
| 150 | |||
| 151 | if ($isHeaderMatched) { |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } else { |
||
| 156 | $isHeaderMatched = strpos($content, $header[$algorithm[0]]) === false |
||
| 157 | ? false |
||
| 158 | : true; |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($algorithm[0] === 'R' || $algorithm[0] === 'E') { |
||
| 162 | if (false === $isHeaderMatched) { |
||
|
0 ignored issues
–
show
The variable
$isHeaderMatched does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 163 | throw new \InvalidArgumentException( |
||
| 164 | "PEM certificate must provided in RSA or ECDSA signing mode." |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.