CVE-2023-48220: Insufficient Information (NVD-CWE-noinfo)
value: Decidim is a participatory democracy framework. Starting in version 0.4.rc3 and prior to version 2.0.9 of the `devise_invitable` gem, the invites feature allows users to accept the invitation for an unlimited amount of time through the password reset functionality. This issue creates vulnerable dependencies starting in version 0.0.1.alpha3 and prior to versions 0.26.9, 0.27.5, and 0.28.0 of the `decidim,` `decidim-admin`, and `decidim-system` gems. When using the password reset functionality, the `devise_invitable` gem always accepts the pending invitation if the user has been invited. The only check done is if the user has been invited but the code does not ensure that the pending invitation is still valid as defined by the `invite_for` expiry period. Decidim sets this configuration to `2.weeks` so this configuration should be respected. The bug is in the `devise_invitable` gem and should be fixed there and the dependency should be upgraded in Decidim once the fix becomes available. `devise_invitable` to version `2.0.9` and above fix this issue. Versions 0.26.9, 0.27.5, and 0.28.0 of the `decidim,` `decidim-admin`, and `decidim-system` gems contain this fix. As a workaround, invitations can be cancelled directly from the database.
CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
Published: 2024-02-20T18:15Z
Repository: https://github.com/scambra/devise_invitable
Language: Ruby
Files changed: lib/devise_invitable/model.rb, test/models/invitable_test.rb, test/models_test.rb, test/rails_app/app/active_record/user.rb, test/rails_app/app/mongoid/user.rb, test/rails_app/config/initializers/devise.rb, test/rails_app/db/migrate/20100401102949_create_tables.rb
Commit date: 2011-02-21 22:24:36 +0100
Commit message: clear invitation token when password is reset

Diff: diff --git a/lib/devise_invitable/model.rb b/lib/devise_invitable/model.rb
index 21a9cb8..2ff5d96 100644
--- a/lib/devise_invitable/model.rb
+++ b/lib/devise_invitable/model.rb
@@ -75,10 +75,16 @@ module Devise
         super unless invited?
       end
 
       protected
 
+        # Clear invitation token when reset password token is cleared too
+        def clear_reset_password_token
+          self.invitation_token = nil if invited?
+          super
+        end
+
         # Checks if the invitation for the user is within the limit time.
         # We do this by calculating if the difference between today and the
         # invitation sent date does not exceed the invite for time configured.
         # Invite_for is a model configuration, must always be an integer value.
         #
diff --git a/test/models/invitable_test.rb b/test/models/invitable_test.rb
index f524db7..26ccc4b 100644
--- a/test/models/invitable_test.rb
+++ b/test/models/invitable_test.rb
@@ -116,10 +116,19 @@ class InvitableTest < ActiveSupport::TestCase
     User.accept_invitation!(:invitation_token => user.invitation_token, :password => '123456789', :password_confirmation => '987654321')
     user.reload
     assert_present user.invitation_token
   end
 
+  test 'should clear invitation token while resetting the password' do
+    user = User.invite!(:email => "valid@email.com")
+    user.send(:generate_reset_password_token!)
+    assert_present user.reset_password_token
+    assert_present user.invitation_token
+    User.reset_password_by_token(:reset_password_token => user.reset_password_token, :password => '123456789', :password_confirmation => '123456789')
+    assert_nil user.reload.invitation_token
+  end
+
   test 'should reset invitation token and send invitation by email' do
     user = new_user
     assert_difference('ActionMailer::Base.deliveries.size') do
       token = user.invitation_token
       user.invite!
diff --git a/test/models_test.rb b/test/models_test.rb
index 9645807..4dc858d 100644
--- a/test/models_test.rb
+++ b/test/models_test.rb
@@ -15,11 +15,11 @@ class ModelsTest < ActiveSupport::TestCase
       assert !include_module?(klass, mod), "#{klass} include #{mod}"
     end
   end
 
   test 'should include Devise modules' do
-    assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
+    assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
   end
 
   test 'should have a default value for invite_for' do
     assert_equal 0, User.invite_for
   end
diff --git a/test/rails_app/app/active_record/user.rb b/test/rails_app/app/active_record/user.rb
index e039895..3ceb7e3 100644
--- a/test/rails_app/app/active_record/user.rb
+++ b/test/rails_app/app/active_record/user.rb
@@ -1,7 +1,7 @@
 class User < ActiveRecord::Base
-  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
+  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
   
   attr_accessible :email, :username, :password, :password_confirmation
   
   validates :username, :length => { :maximum => 20 }
 end
diff --git a/test/rails_app/app/mongoid/user.rb b/test/rails_app/app/mongoid/user.rb
index f432328..bd8ebb0 100644
--- a/test/rails_app/app/mongoid/user.rb
+++ b/test/rails_app/app/mongoid/user.rb
@@ -3,9 +3,9 @@ class User
   include Shim
   
   field :created_at, :type => DateTime
   field :username,   :type => String
   
-  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
+  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
   
   validates :username, :length => { :maximum => 20 }
 end
diff --git a/test/rails_app/config/initializers/devise.rb b/test/rails_app/config/initializers/devise.rb
index 766a4b7..21caec1 100644
--- a/test/rails_app/config/initializers/devise.rb
+++ b/test/rails_app/config/initializers/devise.rb
@@ -80,11 +80,11 @@ Devise.setup do |config|
   # If true, extends the user's remember period when remembered via cookie.
   # config.extend_remember_period = false
 
   # If true, uses the password salt as remember token. This should be turned
   # to false if you are not using database authenticatable.
-  config.use_salt_as_remember_token = true
+  config.use_salt_as_remember_token = false
 
   # ==> Configuration for :validatable
   # Range for password length. Default is 6..20.
   # config.password_length = 6..20
 
diff --git a/test/rails_app/db/migrate/20100401102949_create_tables.rb b/test/rails_app/db/migrate/20100401102949_create_tables.rb
index 3e357b5..9c5843b 100644
--- a/test/rails_app/db/migrate/20100401102949_create_tables.rb
+++ b/test/rails_app/db/migrate/20100401102949_create_tables.rb
@@ -3,11 +3,11 @@ class CreateTables < ActiveRecord::Migration
     create_table :users do |t|
       t.database_authenticatable :null => true
       t.string :username
       t.confirmable
       t.invitable
-      t.encryptable
+      t.recoverable
       
       t.timestamps
     end
   end
 

Vulnerable code:     assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
  config.use_salt_as_remember_token = true
      t.encryptable
Fixed code:         # Clear invitation token when reset password token is cleared too
        def clear_reset_password_token
          self.invitation_token = nil if invited?
          super
        end

  test 'should clear invitation token while resetting the password' do
    user = User.invite!(:email => "valid@email.com")
    user.send(:generate_reset_password_token!)
    assert_present user.reset_password_token
    assert_present user.invitation_token
    User.reset_password_by_token(:reset_password_token => user.reset_password_token, :password => '123456789', :password_confirmation => '123456789')
    assert_nil user.reload.invitation_token
  end

    assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
  devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
  config.use_salt_as_remember_token = false
      t.recoverable