Skip to content

Commit 4dbf0d5

Browse files
SystemKeeperbackportbot[bot]
authored andcommitted
fix: Psalm for encrypt methods
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
1 parent a7a0a82 commit 4dbf0d5

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

lib/Push.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public function proxyPushToDevice(int $id, IUser $user, array $devices, INotific
461461
$this->printInfo('Device token: ' . $device['token']);
462462

463463
try {
464-
$payload = json_encode($this->encryptAndSign($userKey, $device, $id, $notification, $isTalkNotification), JSON_THROW_ON_ERROR);
464+
$payload = json_encode($this->encryptAndSign($userKey->getPrivate(), $device, $id, $notification, $isTalkNotification), JSON_THROW_ON_ERROR);
465465

466466
$proxyServer = rtrim($device['proxyserver'], '/');
467467
if (!isset($this->payloadsToSend[$proxyServer])) {
@@ -629,7 +629,7 @@ public function proxyPushDeleteToDevice(string $userId, IUser $user, array $devi
629629
}
630630

631631
if ($deleteAll) {
632-
$data = $this->encryptAndSignDelete($userKey, $device, null);
632+
$data = $this->encryptAndSignDelete($userKey->getPrivate(), $device, null);
633633
try {
634634
$this->payloadsToSend[$proxyServer][] = json_encode($data['payload'], JSON_THROW_ON_ERROR);
635635
} catch (\JsonException $e) {
@@ -640,7 +640,7 @@ public function proxyPushDeleteToDevice(string $userId, IUser $user, array $devi
640640
// use to not support `delete-multiple`
641641
if (!\in_array($app, ['spreed', 'talk', 'admin_notification_talk'], true)) {
642642
foreach ($notificationIds ?? [] as $notificationId) {
643-
$data = $this->encryptAndSignDelete($userKey, $device, [$notificationId]);
643+
$data = $this->encryptAndSignDelete($userKey->getPrivate(), $device, [$notificationId]);
644644
try {
645645
$this->payloadsToSend[$proxyServer][] = json_encode($data['payload'], JSON_THROW_ON_ERROR);
646646
} catch (\JsonException $e) {
@@ -650,7 +650,7 @@ public function proxyPushDeleteToDevice(string $userId, IUser $user, array $devi
650650
} else {
651651
$temp = $notificationIds;
652652
while (!empty($temp)) {
653-
$data = $this->encryptAndSignDelete($userKey, $device, $temp);
653+
$data = $this->encryptAndSignDelete($userKey->getPrivate(), $device, $temp);
654654
$temp = $data['remaining'];
655655
try {
656656
$this->payloadsToSend[$proxyServer][] = json_encode($data['payload'], JSON_THROW_ON_ERROR);
@@ -948,7 +948,7 @@ protected function getNotifTopicAndUrgency(string $app, string $type): array {
948948
}
949949

950950
/**
951-
* @param Key $userKey
951+
* @param string $userPrivateKey
952952
* @param array $device
953953
* @param int $id
954954
* @param INotification $notification
@@ -958,14 +958,16 @@ protected function getNotifTopicAndUrgency(string $app, string $type): array {
958958
* @throws InvalidTokenException
959959
* @throws \InvalidArgumentException
960960
*/
961-
protected function encryptAndSign(Key $userKey, array $device, int $id, INotification $notification, bool $isTalkNotification): array {
961+
protected function encryptAndSign(string $userPrivateKey, array $device, int $id, INotification $notification, bool $isTalkNotification): array {
962962
$data = $this->encodeNotif($id, $notification, 200);
963963
$ret = $this->getNotifTopicAndUrgency($data['app'], $data['type']);
964964
$priority = $ret['urgency'];
965965
$type = $ret['type'];
966966

967+
$jsonData = json_encode($data, JSON_THROW_ON_ERROR);
968+
967969
$this->printInfo('Device public key size: ' . strlen((string)$device['devicepublickey']));
968-
$this->printInfo('Data to encrypt is: ' . json_encode($data));
970+
$this->printInfo('Data to encrypt is: ' . $jsonData);
969971

970972
$padding = $this->appConfig->getAppValueString('push_encryption_padding', 'PKCS1') === 'OAEP' ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING;
971973
if (!openssl_public_encrypt(json_encode($data), $encryptedSubject, $device['devicepublickey'], $padding)) {
@@ -975,13 +977,13 @@ protected function encryptAndSign(Key $userKey, array $device, int $id, INotific
975977
throw new \InvalidArgumentException('Failed to encrypt message for device');
976978
}
977979

978-
if (openssl_sign($encryptedSubject, $signature, $userKey->getPrivate(), OPENSSL_ALGO_SHA512)) {
980+
if (openssl_sign($encryptedSubject, $signature, $userPrivateKey, OPENSSL_ALGO_SHA512)) {
979981
$this->printInfo('Signed encrypted push subject');
980982
} else {
981983
$this->printInfo('<error>Failed to signed encrypted push subject</error>');
982984
}
983-
$base64EncryptedSubject = base64_encode((string)$encryptedSubject);
984-
$base64Signature = base64_encode((string)$signature);
985+
$base64EncryptedSubject = base64_encode($encryptedSubject);
986+
$base64Signature = base64_encode($signature);
985987

986988
return [
987989
'deviceIdentifier' => $device['deviceidentifier'],
@@ -994,15 +996,15 @@ protected function encryptAndSign(Key $userKey, array $device, int $id, INotific
994996
}
995997

996998
/**
997-
* @param Key $userKey
999+
* @param string $userPrivateKey
9981000
* @param array $device
9991001
* @param ?int[] $ids
10001002
* @return array
1001-
* @psalm-return array{remaining: list<int>, payload: array{deviceIdentifier: string, pushTokenHash: string, subject: string, signature: string, priority: string, type: string}}
1003+
* @psalm-return array{remaining: array<array-key, int>, payload: array{deviceIdentifier: string, pushTokenHash: string, subject: string, signature: string, priority: string, type: string}}
10021004
* @throws InvalidTokenException
10031005
* @throws \InvalidArgumentException
10041006
*/
1005-
protected function encryptAndSignDelete(Key $userKey, array $device, ?array $ids): array {
1007+
protected function encryptAndSignDelete(string $userPrivateKey, array $device, ?array $ids): array {
10061008
$ret = $this->encodeDeleteNotifs($ids);
10071009
$remainingIds = $ret['remaining'];
10081010
$data = $ret['data'];
@@ -1013,9 +1015,9 @@ protected function encryptAndSignDelete(Key $userKey, array $device, ?array $ids
10131015
throw new \InvalidArgumentException('Failed to encrypt message for device');
10141016
}
10151017

1016-
openssl_sign($encryptedSubject, $signature, $userKey->getPrivate(), OPENSSL_ALGO_SHA512);
1017-
$base64EncryptedSubject = base64_encode((string)$encryptedSubject);
1018-
$base64Signature = base64_encode((string)$signature);
1018+
openssl_sign($encryptedSubject, $signature, $userPrivateKey, OPENSSL_ALGO_SHA512);
1019+
$base64EncryptedSubject = base64_encode($encryptedSubject);
1020+
$base64Signature = base64_encode($signature);
10191021

10201022
return [
10211023
'remaining' => $remainingIds,

tests/Unit/PushTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public function testPushToDeviceSending(bool $isDebug, string $padding): void {
553553
->method('getAppValueString')
554554
->willReturnMap([
555555
['subscription_aware_server', 'https://push-notifications.nextcloud.com', 'https://push-notifications.nextcloud.com'],
556-
['push_encryption_padding', 'PKCS1', $padding],
556+
['push_encryption_padding', 'OAEP', $padding],
557557
]);
558558

559559
$this->globalAppConfig

tests/psalm-baseline.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
<code><![CDATA[$this->keyManager]]></code>
3939
<code><![CDATA[$this->tokenProvider]]></code>
4040
<code><![CDATA[ClientException]]></code>
41-
<code><![CDATA[Key]]></code>
42-
<code><![CDATA[Key]]></code>
4341
<code><![CDATA[ServerException]]></code>
4442
<code><![CDATA[protected]]></code>
4543
<code><![CDATA[protected]]></code>

0 commit comments

Comments
 (0)