Skip to content

Commit

Permalink
MDL-78072 core: Skip mobile encryption if Sodium is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 16, 2023
1 parent d838917 commit a1b3784
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
14 changes: 8 additions & 6 deletions message/output/airnotifier/message_output_airnotifier.php
Expand Up @@ -24,7 +24,6 @@
* @since Moodle 2.7
*/


require_once($CFG->dirroot . '/message/output/lib.php');

/**
Expand Down Expand Up @@ -109,17 +108,20 @@ public function send_message($eventdata) {
// We are sending to message to all devices.
$airnotifiermanager = new message_airnotifier_manager();
$devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $eventdata->userto->id);
$skipsend = $encryptnotifications && $encryptprocessing == message_airnotifier_manager::ENCRYPT_UNSUPPORTED_NOT_SEND;
$encryptionavailable = \core\encryption::is_sodium_installed();

foreach ($devicetokens as $devicetoken) {
if (!$devicetoken->enable) {
continue;
}

// Check if we should skip sending the notification.
if ($encryptnotifications && empty($devicetoken->publickey) &&
$encryptprocessing == message_airnotifier_manager::ENCRYPT_UNSUPPORTED_NOT_SEND) {

continue; // Avoid sending notifications to devices not supporting encryption.
if ($skipsend) {
// If encryption is not available, do not send notifications.
if (!$encryptionavailable || ($encryptnotifications && empty($devicetoken->publickey))) {
continue;
}
}

// Sending the message to the device.
Expand Down Expand Up @@ -173,7 +175,7 @@ protected function encrypt_payload(stdClass $payload, stdClass $devicetoken): st
return $payload;
}

if (empty($devicetoken->publickey)) {
if (empty($devicetoken->publickey)|| !\core\encryption::is_sodium_installed()) {
$payload->encrypted = false;
return $payload;
}
Expand Down
2 changes: 2 additions & 0 deletions user/tests/devicekey_test.php
Expand Up @@ -23,8 +23,10 @@
*
* @package core_user
* @covers \core_user\devicekey
* @requires extension sodium
*/
class devicekey_test extends \advanced_testcase {

/**
* Helper to create a device record.
*
Expand Down
1 change: 1 addition & 0 deletions user/tests/external/update_user_device_public_key_test.php
Expand Up @@ -29,6 +29,7 @@
*
* @package core_user
* @covers \core_user\external\update_user_device_public_key
* @requires extension sodium
*/
class update_user_device_public_key_test extends \advanced_testcase {
/**
Expand Down

0 comments on commit a1b3784

Please sign in to comment.