Skip to content

Commit

Permalink
Merge branch 'master_MDL-79134' of https://github.com/mattporritt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Aug 29, 2023
2 parents b327210 + 4b556a6 commit 8f9b3e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backup/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ information provided here is intended especially for developers.

* The function get_async_backup_links_backup has a new param of $backupid and is part of a fix to
async backups (See MDL-69983).
* During restore the function create_included_users has been updated to convert backups containing
legacy MD5 hashed passwords to the new password hashing scheme (See MDL-79134).

=== 4.1 ===

Expand Down
15 changes: 15 additions & 0 deletions backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,10 @@ public static function create_included_users($basepath, $restoreid, $userid,
} else if ($userauth->isinternal and $userauth->canresetpwd) {
$user->password = 'restored';
}
} else if (self::password_should_be_discarded($user->password)) {
// Password is not empty and it is MD5 hashed. Generate a new random password for the user.
// We don't want MD5 hashes in the database and users won't be able to log in with the associated password anyway.
$user->password = hash_internal_user_password(base64_encode(random_bytes(24)));
}

// Creating new user, we must reset the policyagreed always
Expand Down Expand Up @@ -1904,6 +1908,17 @@ public static function create_new_course($fullname, $shortname, $categoryid) {
public static function delete_course_content($courseid, array $options = null) {
return remove_course_contents($courseid, false, $options);
}

/**
* Checks if password stored in backup is a MD5 hash.
* Returns true if it is, false otherwise.
*
* @param string $password The password to check.
* @return bool
*/
private static function password_should_be_discarded(#[\SensitiveParameter] string $password): bool {
return (bool) preg_match('/^[0-9a-f]{32}$/', $password);
}
}

/*
Expand Down

0 comments on commit 8f9b3e1

Please sign in to comment.