Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/Ravada/Auth/SQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,9 @@ Arguments: password

=cut

sub change_password {
my $self = shift;
my $password = shift or die "ERROR: password required\n";
my ($force_change_password) = @_;
sub change_password($self, $password, $force_change_password=0) {
die "ERROR: password required\n"
if !defined $password || !length($password);

_init_connector();

Expand All @@ -570,14 +569,18 @@ sub change_password {
}

my $sth;
if (defined($force_change_password)) {
$sth= $$CON->dbh->prepare("UPDATE users set password=?, change_password=?"
." WHERE name=?");
$sth->execute(sha1_hex($password), $force_change_password ? 1 : 0, $self->name);
} else {
my $sth= $$CON->dbh->prepare("UPDATE users set password=?"
." WHERE name=?");
$sth->execute(sha1_hex($password), $self->name);
$sth= $$CON->dbh->prepare("UPDATE users "
." set password=?, change_password=?, password_expiration_date=?"
." WHERE id=?");
$sth->execute(sha1_hex($password), ($force_change_password or 0) ,0, $self->id);
Comment on lines +572 to +575
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change_password implementation is still storing user passwords using a plain SHA1 hash via sha1_hex($password) before writing to the users table. SHA1 without a dedicated password hashing scheme (e.g., bcrypt, scrypt, Argon2) is fast and unsalted, making it practical for attackers to crack large numbers of passwords offline if the database is ever compromised. Replace this with a modern, slow, and salted password hashing function from a well-vetted library and migrate existing hashes where possible.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback


# Keep the in-memory user data in sync with the database after a password change.
if (exists $self->{_data} && ref($self->{_data}) eq 'HASH') {
# Safely unlock and relock the hash if it is locked.
eval { unlock_hash(%{ $self->{_data} }) };
$self->{_data}->{change_password} = ($force_change_password or 0);
$self->{_data}->{password_expiration_date} = 0;
eval { lock_hash(%{ $self->{_data} }) };
}
}

Expand Down
3 changes: 2 additions & 1 deletion t/mojo/40_security_policy.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ my $SECONDS_TIMEOUT = 15;
my $t;

my $URL_LOGOUT = '/logout';
my ($USERNAME, $PASSWORD) = (user_admin->name, "$$ $$");
my $SCRIPT = path(__FILE__)->dirname->sibling('../script/rvd_front');

$ENV{MOJO_MODE} = 'devel';
Expand All @@ -31,6 +30,8 @@ like($connector->{driver} , qr/mysql/i) or BAIL_OUT;

$Test::Ravada::BACKGROUND=1;

my ($USERNAME, $PASSWORD) = (user_admin->name, "$$ $$");

$t = Test::Mojo->new($SCRIPT);
$t->ua->inactivity_timeout(900);
$t->ua->connect_timeout(60);
Expand Down
13 changes: 13 additions & 0 deletions t/user/50_admin.t
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ sub test_default_admin {
eval { $user2= Ravada::Auth::login('admin','admin');};
like($@, qr/Password expired/, "Expected error message");
ok(!$user2);

$user->password_expiration_date(time+300);
eval { $user2= Ravada::Auth::login('admin','admin');};
ok($user2);

my $p = "newnew";
$user2->change_password($p);

eval { $user2= Ravada::Auth::login('admin',$p);};
ok($user2);
is($user2->password_expiration_date(),0);
is($user2->password_will_be_changed(),0);

}

################################################################
Expand Down