Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
fix: harden account-scoped user responses (#16327)
Sony Mathewb69fccf1e61d
diff --git a/.github/workflows/run_mfa_spec.yml b/.github/workflows/run_mfa_spec.ymlindex eb45656987b..ea305fbaef3 100644--- a/.github/workflows/run_mfa_spec.yml+++ b/.github/workflows/run_mfa_spec.yml@@ -74,6 +74,7 @@ jobs:spec/controllers/api/v1/notification_subscriptions_controller_spec.rb \spec/requests/api/v1/profile/sessions_controller_spec.rb \spec/controllers/devise_overrides/sessions_controller_spec.rb \+ spec/requests/devise_overrides/sessions_controller_spec.rb \spec/requests/devise_overrides/passwords_controller_spec.rb \spec/requests/devise_overrides/confirmations_controller_spec.rb \spec/controllers/api/v1/accounts_controller_spec.rb \diff --git a/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.spec.js b/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.spec.jsnew file mode 100644index 00000000000..63fd5859d46--- /dev/null+++ b/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.spec.js@@ -0,0 +1,30 @@+import { shallowMount } from '@vue/test-utils';+import AgentCapacityPolicyCard from './AgentCapacityPolicyCard.vue';+import CardPopover from '../components/CardPopover.vue';++describe('AgentCapacityPolicyCard', () => {+ it('shows avatars from the agent response in the assigned users popover', () => {+ const wrapper = shallowMount(AgentCapacityPolicyCard, {+ global: {+ stubs: {+ CardLayout: { template: '<div><slot /></div>' },+ },+ },+ props: {+ id: 1,+ users: [+ {+ id: 2,+ name: 'Agent',+ email: '[email protected]',+ thumbnail: 'https://example.com/avatar.png',+ },+ ],+ },+ });++ expect(wrapper.findComponent(CardPopover).props('items')[0].avatarUrl).toBe(+ 'https://example.com/avatar.png'+ );+ });+});diff --git a/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.vue b/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.vueindex aad1c2153d2..066ab9e5283 100644--- a/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.vue+++ b/app/javascript/dashboard/components-next/AssignmentPolicy/AgentCapacityPolicyCard/AgentCapacityPolicyCard.vue@@ -25,7 +25,7 @@ const users = computed(() => {name: user.name,key: user.id,email: user.email,- avatarUrl: user.avatarUrl,+ avatarUrl: user.avatarUrl || user.thumbnail,};});});diff --git a/app/views/api/v1/models/_user.json.jbuilder b/app/views/api/v1/models/_user.json.jbuilderindex e0e44d262a3..5e5acd86474 100644--- a/app/views/api/v1/models/_user.json.jbuilder+++ b/app/views/api/v1/models/_user.json.jbuilder@@ -4,7 +4,8 @@ account_users = account_users.includes(:custom_role) if ChatwootApp.enterprise?# full-precision timestamps, nils last, matching ORDER BY active_at DESC NULLS LASTactive_account_user = account_users.max_by { |account_user| [account_user.active_at ? 1 : 0, account_user.active_at || Time.at(0).utc] }-json.access_token resource.accounts.any?(&:api_and_webhooks_enabled?) ? resource.access_token.token : ''+authenticated_resource = resource == local_assigns.fetch(:authenticated_user, Current.user)+json.access_token resource.accounts.any?(&:api_and_webhooks_enabled?) ? resource.access_token.token : '' if authenticated_resourcejson.account_id active_account_user&.account_idjson.available_name resource.available_namejson.avatar_url resource.avatar_url@@ -17,7 +18,7 @@ json.id resource.idjson.inviter_id active_account_user&.inviter_idjson.name resource.namejson.provider resource.provider-json.pubsub_token resource.pubsub_token+json.pubsub_token resource.pubsub_token if authenticated_resourcejson.custom_attributes resource.custom_attributes if resource.custom_attributes.present?json.role active_account_user&.rolejson.ui_settings resource.ui_settingsdiff --git a/app/views/devise/_auth.json.jbuilder b/app/views/devise/_auth.json.jbuilderindex 31fb0abb734..90af053fc0a 100644--- a/app/views/devise/_auth.json.jbuilder+++ b/app/views/devise/_auth.json.jbuilder@@ -1,3 +1,3 @@json.data do- json.partial! 'api/v1/models/user', formats: [:json], resource: resource+ json.partial! 'api/v1/models/user', formats: [:json], resource: resource, authenticated_user: resourceenddiff --git a/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/create.json.jbuilder b/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/create.json.jbuilderindex ca84a01f704..adad85132f6 100644--- a/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/create.json.jbuilder+++ b/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/create.json.jbuilder@@ -1 +1 @@-json.partial! 'api/v1/models/user', resource: @user+json.partial! 'api/v1/models/agent', formats: [:json], resource: @userdiff --git a/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/index.json.jbuilder b/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/index.json.jbuilderindex bfbc7787425..85377ed5822 100644--- a/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/index.json.jbuilder+++ b/enterprise/app/views/api/v1/accounts/agent_capacity_policies/users/index.json.jbuilder@@ -1,3 +1,3 @@json.array! @users do |user|- json.partial! 'api/v1/models/user', resource: user+ json.partial! 'api/v1/models/agent', formats: [:json], resource: userenddiff --git a/spec/controllers/devise/token_validations_controller_spec.rb b/spec/controllers/devise/token_validations_controller_spec.rbindex 873f1c70552..a69239d1dfb 100644--- a/spec/controllers/devise/token_validations_controller_spec.rb+++ b/spec/controllers/devise/token_validations_controller_spec.rb@@ -14,12 +14,18 @@ RSpec.describe 'Token Validation API', type: :request docontext 'when it is a valid token' dolet(:agent) { create(:user, account: account, role: :agent) }- it 'returns all the labels for the conversation' do+ it 'returns the authenticated user with self credentials' do+ account.enable_features!(:api_and_webhooks)+get '/auth/validate_token',headers: agent.create_new_auth_tokenexpect(response).to have_http_status(:success)- expect(response.body).to include('payload')+ expect(response.parsed_body.dig('payload', 'data')).to include(+ 'access_token' => agent.access_token.token,+ 'pubsub_token' => agent.pubsub_token+ )+ expect(Current.user).to be_nilendenddiff --git a/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rbindex 9ed83710734..c84041c7286 100644--- a/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb+++ b/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb@@ -6,6 +6,8 @@ RSpec.describe 'Agent Capacity Policy Users API', type: :request dolet!(:user) { create(:user, account: account, role: :agent) }let(:administrator) { create(:user, account: account, role: :administrator) }let(:agent) { create(:user, account: account, role: :agent) }+ let(:other_account) { create(:account) }+ let(:external_user) { create(:user, account: other_account, role: :administrator) }describe 'GET /api/v1/accounts/{account.id}/agent_capacity_policies/{policy_id}/users' docontext 'when admin' do@@ -39,6 +41,20 @@ RSpec.describe 'Agent Capacity Policy Users API', type: :request doexpect(user_ids).to contain_exactly(user.id, agent.id)expect(user_ids.uniq).to eq(user_ids) # No duplicatesend++ it 'does not expose credentials for a user from another account' do+ external_account_user = create(:account_user, account: account, user: external_user)+ external_account_user.update!(agent_capacity_policy: agent_capacity_policy)++ get "/api/v1/accounts/#{account.id}/agent_capacity_policies/#{agent_capacity_policy.id}/users",+ headers: administrator.create_new_auth_token,+ as: :json++ expect(response).to have_http_status(:success)+ external_user_response = response.parsed_body.find { |item| item['id'] == external_user.id }+ expect(external_user_response).not_to have_key('access_token')+ expect(external_user_response).not_to have_key('pubsub_token')+ endendend@@ -64,6 +80,19 @@ RSpec.describe 'Agent Capacity Policy Users API', type: :request doexpect(response).to have_http_status(:success)expect(user.account_users.first.reload.agent_capacity_policy).to eq(agent_capacity_policy)end++ it 'does not expose credentials when assigning a user from another account' do+ create(:account_user, account: account, user: external_user)++ post "/api/v1/accounts/#{account.id}/agent_capacity_policies/#{agent_capacity_policy.id}/users",+ params: { user_id: external_user.id },+ headers: administrator.create_new_auth_token,+ as: :json++ expect(response).to have_http_status(:success)+ expect(response.parsed_body).not_to have_key('access_token')+ expect(response.parsed_body).not_to have_key('pubsub_token')+ endendenddiff --git a/spec/requests/devise_overrides/confirmations_controller_spec.rb b/spec/requests/devise_overrides/confirmations_controller_spec.rbindex a2fe53139ce..4c1dbc73afb 100644--- a/spec/requests/devise_overrides/confirmations_controller_spec.rb+++ b/spec/requests/devise_overrides/confirmations_controller_spec.rb@@ -12,11 +12,17 @@ RSpec.describe 'Confirmations API', type: :request doendit 'issues session tokens on successful confirmation' do+ account.enable_features!(:api_and_webhooks)confirm_user(user)expect(response).to have_http_status(:success)expect(response.headers['access-token']).to be_presentexpect(user.reload.confirmed?).to be true+ expect(response.parsed_body.fetch('data')).to include(+ 'access_token' => user.access_token.token,+ 'pubsub_token' => user.pubsub_token+ )+ expect(Current.user).to be_nilendcontext 'when account enforces MFA' dodiff --git a/spec/requests/devise_overrides/passwords_controller_spec.rb b/spec/requests/devise_overrides/passwords_controller_spec.rbindex f6dc3f15316..616bd3fa17f 100644--- a/spec/requests/devise_overrides/passwords_controller_spec.rb+++ b/spec/requests/devise_overrides/passwords_controller_spec.rb@@ -18,10 +18,16 @@ RSpec.describe 'Passwords API', type: :request doendit 'issues session tokens on successful reset' do+ account.enable_features!(:api_and_webhooks)reset_password(reset_token_for(user))expect(response).to have_http_status(:success)expect(response.headers['access-token']).to be_present+ expect(response.parsed_body.fetch('data')).to include(+ 'access_token' => user.access_token.token,+ 'pubsub_token' => user.reload.pubsub_token+ )+ expect(Current.user).to be_nilendcontext 'when account enforces MFA' dodiff --git a/spec/requests/devise_overrides/sessions_controller_spec.rb b/spec/requests/devise_overrides/sessions_controller_spec.rbnew file mode 100644index 00000000000..03a1ef374ae--- /dev/null+++ b/spec/requests/devise_overrides/sessions_controller_spec.rb@@ -0,0 +1,77 @@+require 'rails_helper'++RSpec.describe 'Session authentication responses', type: :request do+ let(:account) { create(:account) }+ let(:user) { create(:user, account: account, password: 'Test@123456') }+ let(:response_data) { response.parsed_body.fetch('data') }++ before { account.enable_features!(:api_and_webhooks) }++ after { Current.reset }++ it 'returns self credentials after password sign-in' do+ post '/auth/sign_in', params: { email: user.email, password: 'Test@123456' }, as: :json++ expect(response).to have_http_status(:success)+ expect(response_data).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ expect(Current.user).to be_nil+ end++ it 'returns self credentials after SSO sign-in' do+ post '/auth/sign_in', params: { email: user.email, sso_auth_token: user.generate_sso_auth_token }, as: :json++ expect(response).to have_http_status(:success)+ expect(response_data).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ expect(Current.user).to be_nil+ end++ it 'does not return self credentials for an invalid password' do+ post '/auth/sign_in', params: { email: user.email, password: 'invalid' }, as: :json++ expect(response).to have_http_status(:unauthorized)+ expect(response.parsed_body).not_to have_key('data')+ expect(Current.user).to be_nil+ end++ context 'with MFA' do+ before do+ skip('Skipping since MFA is not configured in this environment') unless Chatwoot.encryption_configured?+ user.enable_two_factor!+ end++ it 'returns self credentials after OTP verification' do+ user.update!(otp_required_for_login: true)+ token = Mfa::TokenService.new(user: user).generate_token++ post '/auth/sign_in', params: { mfa_token: token, otp_code: user.current_otp }, as: :json++ expect(response).to have_http_status(:success)+ expect(response_data).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ expect(Current.user).to be_nil+ end++ it 'returns self credentials after backup code verification' do+ user.update!(otp_required_for_login: true)+ backup_codes = user.generate_backup_codes!+ token = Mfa::TokenService.new(user: user).generate_token++ post '/auth/sign_in', params: { mfa_token: token, backup_code: backup_codes.first }, as: :json++ expect(response).to have_http_status(:success)+ expect(response_data).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ expect(Current.user).to be_nil+ end++ it 'returns self credentials and backup codes after MFA enrollment' do+ account.update!(enforce_mfa: true)+ token = Mfa::SetupTokenService.new(user: user).generate_token++ post '/auth/sign_in', params: { mfa_setup_token: token, otp_code: user.current_otp }, as: :json++ expect(response).to have_http_status(:success)+ expect(response_data).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ expect(response.parsed_body['backup_codes'].size).to eq(10)+ expect(Current.user).to be_nil+ end+ end+enddiff --git a/spec/views/api/v1/models/_user.json.jbuilder_spec.rb b/spec/views/api/v1/models/_user.json.jbuilder_spec.rbnew file mode 100644index 00000000000..09d1f4be633--- /dev/null+++ b/spec/views/api/v1/models/_user.json.jbuilder_spec.rb@@ -0,0 +1,53 @@+require 'rails_helper'++RSpec.describe 'api/v1/models/_user', type: :view do+ let(:account) { create(:account) }+ let(:user) { create(:user, account: account, role: :administrator) }++ before { account.enable_features!(:api_and_webhooks) }++ after { Current.reset }++ it 'returns credentials for the authenticated administrator themselves' do+ Current.user = user++ render partial: 'api/v1/models/user', formats: [:json], locals: { resource: user }++ expect(JSON.parse(rendered)).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ end++ it 'omits another user\'s credentials for an account administrator' do+ Current.user = create(:user, account: account, role: :administrator)++ render partial: 'api/v1/models/user', formats: [:json], locals: { resource: user }++ expect(JSON.parse(rendered)).not_to have_key('access_token')+ expect(JSON.parse(rendered)).not_to have_key('pubsub_token')+ end++ it 'omits credentials without an authenticated user' do+ Current.user = nil++ render partial: 'api/v1/models/user', formats: [:json], locals: { resource: user }++ expect(JSON.parse(rendered)).not_to have_key('access_token')+ expect(JSON.parse(rendered)).not_to have_key('pubsub_token')+ end++ it 'returns credentials for the user authenticated by the auth response' do+ Current.user = nil++ render partial: 'api/v1/models/user', formats: [:json], locals: { resource: user, authenticated_user: user }++ expect(JSON.parse(rendered)).to include('access_token' => user.access_token.token, 'pubsub_token' => user.pubsub_token)+ end++ it 'omits credentials when the supplied authenticated user is someone else' do+ authenticated_user = create(:user)++ render partial: 'api/v1/models/user', formats: [:json], locals: { resource: user, authenticated_user: authenticated_user }++ expect(JSON.parse(rendered)).not_to have_key('access_token')+ expect(JSON.parse(rendered)).not_to have_key('pubsub_token')+ end+end