|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace yrc\web\ncryptf; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use ncryptf\Request; |
|
7
|
|
|
use ncryptf\Response; |
|
8
|
|
|
use ncryptf\exceptions\DecryptionFailedException; |
|
9
|
|
|
use ncryptf\exceptions\InvalidSignatureException; |
|
10
|
|
|
use ncryptf\exceptions\InvalidChecksumException; |
|
11
|
|
|
use ncryptf\middleware\EncryptionKeyInterface; |
|
|
|
|
|
|
12
|
|
|
use yrc\models\redis\EncryptionKey; |
|
13
|
|
|
use yrc\web\Request as YiiRequest; |
|
14
|
|
|
use yii\base\Exception; |
|
15
|
|
|
use yii\base\InvalidParamException; |
|
16
|
|
|
use yii\helpers\Json; |
|
17
|
|
|
use yii\web\BadRequestHttpException; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Parses vnd.ncryptf+json |
|
22
|
|
|
* @class Ncryptf JsonParser |
|
23
|
|
|
*/ |
|
24
|
|
|
class JsonParser extends \yii\web\JsonParser |
|
25
|
|
|
{ |
|
26
|
|
|
private $decryptedBody; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the decrypted response |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getDecryptedBody() :? string |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->decryptedBody; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Parses vnd.ncryptf+json |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $rawBody |
|
42
|
|
|
* @param string $contentType |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function parse($rawBody, $contentType) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($contentType === 'application/vnd.25519+json') { |
|
48
|
|
|
Yii::warning([ |
|
49
|
|
|
'message' => '`application/vnd.25519+json` content type is deprecated. Migrate to `application/vnd.ncryptf+json' |
|
50
|
|
|
]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($rawBody === '') { |
|
54
|
|
|
$this->decryptedBody = ''; |
|
55
|
|
|
return []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$request = Yii::$app->request; |
|
|
|
|
|
|
59
|
|
|
$version = Response::getVersion(\base64_decode($rawBody)); |
|
60
|
|
|
$key = $this->getEncryptionKey($request); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$this->decryptedBody = $this->decryptRequest($key, $request, $rawBody, $version); |
|
64
|
|
|
} catch (DecryptionFailedException | InvalidArgumentException | InvalidSignatureException | InvalidChecksumException $e) { |
|
65
|
|
|
throw new BadRequestHttpException(Yii::t('yrc', 'Unable to decrypt response.')); |
|
66
|
|
|
} catch (\Exception $e) { |
|
67
|
|
|
Yii::warning([ |
|
68
|
|
|
'message' => 'An unexpected error occured when decryption the response. See attached exception', |
|
69
|
|
|
'exception' => $e |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
throw new BadRequestHttpException(Yii::t('yrc', 'Unable to decrypt response.')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
$parameters = Json::decode($this->decryptedBody, $this->asArray); |
|
77
|
|
|
return $parameters ?? []; |
|
78
|
|
|
} catch (InvalidParamException $e) { |
|
79
|
|
|
if ($this->throwException) { |
|
80
|
|
|
throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage()); |
|
81
|
|
|
} |
|
82
|
|
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Decrypts the request using a given encryption key and request parameters |
|
88
|
|
|
* |
|
89
|
|
|
* @param EncryptionKeyInterface $key |
|
90
|
|
|
* @param \yrc\web\Request $request |
|
91
|
|
|
* @param string $rawBody |
|
92
|
|
|
* @param string $version |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
private function decryptRequest(EncryptionKeyInterface $key, \yrc\web\Request $request, string $rawBody, int $version) |
|
96
|
|
|
{ |
|
97
|
|
|
static $response = null; |
|
98
|
|
|
static $nonce = null; |
|
99
|
|
|
static $publicKey = null; |
|
100
|
|
|
|
|
101
|
|
|
$response = new Response( |
|
102
|
|
|
$key->getBoxSecretKey() |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
if ($version === 1) { |
|
106
|
|
|
$publicKey = $request->headers->get('x-pubkey', null); |
|
107
|
|
|
$nonce = $request->headers->get('x-nonce', null); |
|
108
|
|
|
|
|
109
|
|
|
if ($publicKey === null || $nonce === null) { |
|
|
|
|
|
|
110
|
|
|
throw new Exception(Yii::t('yrc', 'Missing nonce or public key header. Unable to decrypt request.')); |
|
111
|
|
|
} |
|
112
|
|
|
$nonce = \base64_decode($nonce); |
|
|
|
|
|
|
113
|
|
|
$publicKey = \base64_decode($publicKey); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$decryptedRequest = $response->decrypt( |
|
117
|
|
|
\base64_decode($rawBody), |
|
118
|
|
|
$publicKey, |
|
119
|
|
|
$nonce |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
if ($key->isEphemeral()) { |
|
123
|
|
|
$key->delete(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $decryptedRequest; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Fetches the local encryption key from the data provided in the request |
|
131
|
|
|
* |
|
132
|
|
|
* @param \yrc\web\Request $request |
|
133
|
|
|
* @param string $rawBody |
|
134
|
|
|
* @param integer $version |
|
135
|
|
|
* @return EncryptionKey |
|
136
|
|
|
*/ |
|
137
|
|
|
private function getEncryptionKey(\yrc\web\Request $request) : EncryptionKey |
|
138
|
|
|
{ |
|
139
|
|
|
$lookup = $request->headers->get('x-hashid', null); |
|
140
|
|
|
if ($lookup === null) { |
|
|
|
|
|
|
141
|
|
|
Yii::warning([ |
|
142
|
|
|
'message' => 'X-HashId missing on request. Unable to decrypt response.' |
|
143
|
|
|
]); |
|
144
|
|
|
throw new Exception(Yii::t('yrc', 'Unable to decrypt response.')); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$key = EncryptionKey::find()->where([ |
|
148
|
|
|
'hash' => $lookup |
|
149
|
|
|
])->one(); |
|
150
|
|
|
|
|
151
|
|
|
if ($key === null) { |
|
152
|
|
|
throw new Exception(Yii::t('yrc', 'Unable to decrypt response.')); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $key; |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths