Skip to content

fix(comment-automation): validate flowId exists before saving a reply - #1235

Open
rawdaymx wants to merge 2 commits into
ChatbotXIO:mainfrom
rawdaymx:fix/comment-automation-flow-validation
Open

rawdaymx wants to merge 2 commits into
ChatbotXIO:mainfrom
rawdaymx:fix/comment-automation-flow-validation

Conversation

@rawdaymx

Copy link
Copy Markdown
Contributor

Fixes #1228.

None of CommentAutomation's 8 write methods (Messenger, Instagram, Threads, TikTok — create/update) validated that privateReply.value or publicReply.value pointed at a real flow when the reply type is "flow". AutomatedResponse (Keywords) already does this via flowService.exists before saving (automated-response/service.ts); this table never did, for either reply field or any of its write methods.

A stale or mistyped flowId saves silently and the automation looks active in the UI, but every trigger fails at delivery time with a bare FlowVersion not found (worker's detectFlowVersion, apps/worker/src/lib/db.ts) — that failure is never surfaced back to the person who configured the automation.

Not a tenant-isolation issue

We checked this specifically before treating it as a normal bug: detectFlowVersion scopes its lookup to the triggering conversation's own workspaceId in every case, for both privateReply (private-reply.ts) and publicReply (public-reply.ts). A foreign flowId can never execute cross-tenant — it just fails closed with "not found." That's why this is a validation gap, not a security report.

Change

Adds a private assertFlowReplyExists(workspaceId, field, reply, tx?) helper, mirroring the check AutomatedResponse already runs, and wires it into all 8 write methods:

  • createMessenger / updateMessengerprivateReply + publicReply
  • createInstagram / updateInstagramprivateReply + publicReply
  • createThreadsAutomation / updateThreadsAutomationpublicReply only
  • createTiktokAutomation / updateTiktokAutomationpublicReply only

(Threads and TikTok fix privateReply to {type: "none"} and don't let the caller set it, so only their publicReply needs covering.)

Verification

packages/business/__tests__/comment-automation-write-methods.test.ts, 7 new cases added to the existing suite:

Test Files  1 passed (1)
     Tests  17 passed (17)

ultracite check and check-types are clean.

None of CommentAutomation's 8 write methods (Messenger, Instagram,
Threads, TikTok create/update) validated that privateReply.value or
publicReply.value pointed at a real flow when their reply type is
"flow" — AutomatedResponse (Keywords) already does this via
flowService.exists before saving. A stale or mistyped flowId saves
silently and the automation looks active in the UI, but every trigger
fails at delivery time with a bare "FlowVersion not found" (worker's
detectFlowVersion, apps/worker/src/lib/db.ts) — never surfaced back to
the user who configured it.

detectFlowVersion scopes its lookup to the triggering conversation's
own workspaceId in all cases, so a foreign flowId can never execute
cross-tenant — this is a validation gap, not a tenant-isolation
defect.

Adds assertFlowReplyExists, mirroring the check Keywords already runs,
wired into all 8 write methods for privateReply and (where the
channel lets the caller set it) publicReply.
@github-actions github-actions Bot added the bug Something isn't working as expected label Sep 19, 2026
service.ts now calls flowService.exists() before saving a flow reply
(previous commit). Importing the real flowService transitively pulls
in botFieldService -> the full contact-filter query builder, which
these two suites fully replace @chatbotx.io/database/partials and
@chatbotx.io/database/schema without, so the real chain broke both
with "No X export is defined on the mock".

Neither suite is testing flow validation, so flowService.exists is
stubbed to always resolve true instead of chasing the transitive mock
surface deeper.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment automations never validate a reply's flow id — Keywords does, comment triggers don't, so a bad reference silently dead-ends at delivery time

1 participant