ChatbotX icon indicating copy to clipboard operation
ChatbotX copied to clipboard

feat(audit): opt-in AUDIT log line for every audit record (community keeps no trail)

Open demismeneghetti opened this issue 3 weeks ago • 1 comments

Have you checked for existing requests?

I searched the existing issues and pull requests for "audit". #1033 added the audit service, #1528 the platform audit log, and #1695 a persisted log of workspace and channel deletions. None of them covers this.

Is your feature request related to a problem?

On the community edition, auditService.record() still builds and enqueues a sendAuditLog job, but the worker handler (apps/worker/src/default/handlers/send-audit-log.ts) returns right away when NEXT_PUBLIC_EDITION === "community". Nothing is written anywhere: no table row, no log line. A self-hoster on the community edition therefore has no record of who logged in, who removed a member, who changed a flow, and so on.

The audit_log table is the right place for enterprise/cloud, and #1695 gives community a durable record of deletions only. What is missing is a lightweight way to get the same events into the process log, where an operator can already ship, filter and retain them with whatever log pipeline they use (docker compose logs, Loki, CloudWatch, ...).

Describe the solution you'd like

A new environment variable AUDIT_LOG_STDOUT. Default off, so nothing changes for anyone who does not set it. When it is exactly true, record() writes one structured line through the existing pino logger, after the actor and workspace are resolved and before the job is enqueued:

logger.info(
  { audit: { auditLogId, action, userId, workspaceId, ipAddress, userAgent, source } },
  `AUDIT ${action}`,
)
  • It runs before the community early return in the worker, so it works on every edition and does not depend on Redis being reachable.
  • auditLogId is the same id as the enqueued job, so a log line can be correlated with a table row where both exist.
  • detail is not logged. At the ~240 call sites it is a free-form string that can hold names, e-mail addresses or message text, so there is no generic allowlist. The line carries only identifiers, the action and request metadata. userAgent is cut at 256 characters because it is client-supplied.
  • userId, ipAddress and userAgent are personal data. .env.example says so and tells the operator to set a retention period and restrict access to the log stream.
  • Dropped records (missing actor or workspace) keep the existing warn and emit no AUDIT line.

The diff is small: one function and one block in packages/business/src/audit/service.ts, an .env.example entry, and unit tests in packages/business/__tests__/audit.service.test.ts (off by default, only the exact value true enables it, no detail in the line, same id as the job, emitted when the enqueue fails, dropped when there is no actor, user agent bounded). Nothing under enterprise/ is touched and there is no schema change.

Describe alternatives you've considered

  • Writing the audit_log table on community too: a larger change (the table requires workspaceId, so events such as a login do not fit) and a product decision about the edition boundary. I am not proposing it.
  • Extending the #1695 table to more events: that gives a durable record per event type but needs a schema and migration for each, which is heavier than a log line for operators who only need a trail.
  • Logging detail after redaction: too risky given how free-form it is. It can be added later for specific call sites if the maintainers want it.

Additional context

We run ChatbotX self-hosted and need this trail now, so we carry the same diff in a fork (beelabstudio/chatbotx). We would prefer to drop the fork and use upstream, so I am happy to adjust the variable name, the log shape or the field set to fit the project's conventions. A PR with this diff is linked right after this issue is opened.

demismeneghetti avatar Nov 11 '26 16:11 demismeneghetti

Closing: the Community Edition does not support the audit log. Related PR #1703 is closed for the same reason.

realcodesiman avatar Nov 26 '26 02:11 realcodesiman