Skip to content

bug: GET /api/v1/accounts/{id}/labels returns 500 (NoMethodError api_and_webhooks_enabled?) #1

Description

@CrisAlva1414

Descripción

El endpoint GET /api/v1/accounts/{account_id}/labels retorna 500 Internal Server Error al usar autenticación por api_access_token (token de usuario API). El mismo error ocurre en POST /api/v1/accounts/{account_id}/labels.

Error en logs

NoMethodError (undefined method 'api_and_webhooks_enabled?' for nil):
app/controllers/api/v1/accounts/base_controller.rb:11
in 'Api::V1::Accounts::BaseController#validate_token_api_access'

Causa raíz

  1. validate_token_api_access llama a Current.account.api_and_webhooks_enabled?
  2. Current.account es nil porque current_user (Devise) no se setea cuando la autenticación es vía api_access_token
  3. El helper EnsureCurrentAccountHelper#ensure_current_account verifica if current_user pero current_user (Devise) es nil aunque Current.user fue seteado por AccessTokenAuthHelper#authenticate_access_token!
  4. Al no encontrar current_user ni @resource.is_a?(AgentBot), cae al else que llama render_unauthorized y nunca setea Current.account

Flujo afectado

Api::BaseController
  → before_action :authenticate_access_token!  (setea Current.user, OK)
  → before_action :validate_bot_access_token!  (OK, Current.user es User)

Api::V1::Accounts::BaseController
  → before_action :current_account
    → ensure_current_account
      → if current_user  ← Devise current_user es nil!
        → else: render_unauthorized
        → Current.account NUNCA se setea
  → before_action :validate_token_api_access
    → Current.account.api_and_webhooks_enabled?  ← NoMethodError for nil

Entorno

  • Chatwoot versión: self-hosted, aprox post-commit 9c44431 (feature flag api_and_webhooks)
  • Base URL: chatwoot.ruki-bot.com
  • Account ID: 3
  • Auth: api_access_token de usuario (rol administrator)
  • Feature flag api_and_webhooks: habilitado vía Account.find(3).enable_features!("'api_and_webhooks')

Pasos para reproducir

curl -H "api_access_token: <token>" \
  "https://chatwoot.ejemplo.com/api/v1/accounts/3/labels"

Posible solución

El método ensure_current_account en EnsureCurrentAccountHelper debería reconocer a Current.user (seteado por token API) como equivalente a current_user (Devise). Actualmente el helper usa current_user que Devise no setea cuando la autenticación es por token.

def ensure_current_account
  account = Account.find(params[:account_id])
  # ...
  # Fix: usar Current.user si current_user (Devise) es nil
  user = current_user || Current.user
  if user
    account_accessible_for_user?(account)
  elsif @resource.is_a?(AgentBot)
    account_accessible_for_bot?(account)
  else
    render_unauthorized(I18n.t('errors.account.not_authorized'))
  end
  account
end

Workaround en nuestra app

Mientras se resuelve, nuestra app saltea create_label (que dispara el 500) y asigna labels directo a contactos vía POST /contacts/{id}/labels, que sí funciona.

Referencias

  • Commit 9c44431: feat: add api_and_webhooks feature flag
  • Archivo: app/controllers/concerns/ensure_current_account_helper.rb
  • Archivo: app/controllers/api/v1/accounts/base_controller.rb

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions